Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



Resource Views

Limited Preview

This is a Limited Preview release of Resource Views. As a result, it might be changed in backward-incompatible ways and is not recommended for production use. It is not subject to any SLA or deprecation policy. Request to be whitelisted to use this feature .


Google Compute Engine Resource Views allows you to specify collections of Compute Engine resources, such as virtual machine instances, so that they can be managed collectively. You can then use these resource views for other Google Cloud services, such as autoscaling, which uses resource views to identify which resources to autoscale.

Some Google Cloud services also automatically create resource views for you, while other services require that you create resource views separately. For example, the Replica Pool service creates resource views for each replica pool that you deploy and passes that information to the autoscaling module.

Currently, you can only group virtual machine instances into resources views.

You will need to use the Resource Views API

if you are using a specific service that requires creating a resource view. You can also use the Resource Views API to manage resource views created by other services.

Contents

Sign up

Before you can sign up for the Resource View service, you must first sign up and enable the correct APIs. Follow the instructions below to sign up for the service.

Enable the Compute Engine API

The Replica Pool service requires access to Compute Engine resources. You must first enable the Compute Engine API before enabling Replica Pools.

To sign up and enable Compute Engine, see the Compute Engine signup instructions .

Request access to the service

The Resource View API is currently in Limited Preview and requires that you request access to the service before you can use it.

Request access

Enable the Resource View API

Once you have given access to the API, you must enable it for your Google Developers Console project.

Enable the Resource View API in the same project as Compute Engine.

Quotas

The Resource View API enforces the following API rate limits:

The Resource View API rate limits are independent of Compute Engine API rate limits . All requests to the Resource View API are subject to the rate limits above.

Concepts

The Resource Views service uses the following key concepts.

Zones and regions

Resource views can live in a zone or a region . Zones and regions are the geographical location where the resource view and the resources within the resource view must live. For example, only resources that live in a specific zone can be a member of a zone resource view.

Region resource views

It is possible to group virtual machine instances into either region or zone resource views. A region resource view must contain only resources that live in that region. These resources can live in different zones within the region but must all be from the same region.

For example, a resource view in the us-central1 region can contain resources from any zones in the us-central1 region, such as the us-central1-a and us-central1-b zones, depicted in the following diagram.


Click to enlarge

Zone resource view

A zone resource view is scoped to a specific zone and can only contain resources that live in the same zone as the resource view. For example, a resource view in us-central1-a zone cannot contain virtual machine instances from the us-central1-b zone.

Setup

In order to use resource views either in the API or the command-line tool, you must set up your environment. This document provides examples using the command-line tool and the Google APIS Python client library .

Set up the Command-line tool


The Resource Views service uses the gcloud command-line tool . To prepare the command-line tool to use Resource Views service, follow these steps.

  1. Install the gcloud tool.
  2. Once you have installed the tool, enable preview features in the gcloud tool by running the following command. If you have already enabled preview features for another service, you do not need to run this command again.

    $ gcloud components update preview
    
  3. Invoke the command-line tool for Resource Views like so:

    $ gcloud [--project=<project-id>] preview resource-views COMMAND
    

    See the sections below that describe how to work with resource views. You will replace COMMAND in the example above with the subcommand that you need to use.

Set up the Python client library


To use the Python client library, you must authorize and build the service.

Authorize access

The Resource View API uses OAuth 2.0 authorization. You will need to create a client ID and client secret, and use both with the oauth2client library. By default, the oauth2 library is included in the Python client library.

To find your project's client ID and client secret, do the following:

  1. Go to the Google Developers Console .
  2. Select a project, or create a new one.
  3. In the sidebar on the left, expand APIs & auth . Next, click APIs . In the list of APIs, make sure the status is ON for the Resource View API.
  4. In the sidebar on the left, select Credentials .
  5. If you haven't done so already, create your project's OAuth 2.0 credentials by clicking Create new Client ID , and providing the information needed to create the credentials.
  6. Look for the Client ID and Client secret in the table associated with each of your credentials.

Note that not all types of credentials use both a client ID and client secret and won't be listed in the table if they are not used.

When prompted:

  1. Select Installed application .
  2. Select Other under the Installed application type .
  3. Once you have created your client ID and secret, save it locally by clicking Download JSON and naming the file client_secrets.json in the same directory as code.

In your code, you can authorize to the Resource View service using your client_secrets.json file:

#!/usr/bin/python

import logging
import sys
import argparse
import httplib2
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client import tools
from oauth2client.tools import run_flow
from apiclient.discovery import build
import json

CLIENT_SECRETS = "client_secrets.json"
MANAGE_SCOPE = "https://www.googleapis.com/auth/ndev.cloudman"
OAUTH2_STORAGE = "oauth.dat"

def main(argv):
  logging.basicConfig(level=logging.INFO)

  parser = argparse.ArgumentParser(
    description=__doc__,
    formatter_class=argparse.RawDescriptionHelpFormatter,
    parents=[tools.argparser])

  # Parse the command-line flags.
  flags = parser.parse_args(argv[1:])

  # Perform OAuth 2.0 authorization.
  flow = flow_from_clientsecrets(CLIENT_SECRETS, scope=MANAGE_SCOPE)
  storage = Storage(OAUTH2_STORAGE)
  credentials = storage.get()

  if credentials is None or credentials.invalid:
    credentials = run_flow(flow, storage, flags)
  http = httplib2.Http()
  auth_http = credentials.authorize(http=http)

if __name__ == "__main__":
   main(sys.argv)

Build the service

To build the service, include the following in your code:

# Build the service
resourceview_service = build("resourceviews", "v1beta1", http=auth_http)

Creating a resource view

To create a resource view, you can use the REST API , or the command-line tool.This section describes how use the command-tool line or the Google APIs Python Client Library .

Creating a new resource view

Command-line tool


In the command-line tool, you can create a new resource view by running the following command:

$ gcloud [--project=<project-id>] preview resource-views --zone || --region <zone-or-region-name> create <resource-view-name>

Important flags and parameters

--project=<project-id>
[Optional] If you did not save your project ID when you ran gcloud auth login , provide your project ID here.
--zone <zone-name> or --region <region-name>
[Required] Provide a zone or region for this resource view.
<resource-view-name>
[Required] The name of this resource view. The name must be unique in this project, from 1-63 characters long and match the regular expression: [a-z]([-a-z0-9]*[a-z0-9])? , which means the first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash.

Confirm that your resource view was successfully create by using the following command:

$ gcloud [--project=<project-id>] preview resource-views --zone || --region <zone-or-region-name> list

You should receive a response that looks like the following example:

[
   {
    "creationTime": "2014-04-21T19:25:58.768Z",
     "name": "zone-rv",
      "selfLink": "https://www.googleapis.com/resourceviews/v1beta1/projects/myproject/zones/us-central1-a/resourceViews/zone-rv"
    }
]

Python client library


To add a resource view using the client library, use the insert method, either for a region resource view or a zone resource view . The follow method creates a new zone resource view:

def addZoneResourceView(resourceview_service):
  body = {
    "name": "my-resource-view"
    "members" : [
       "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/instance-1",
       "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/instance-1"
     ]
  }

  request = resourceview_service.zoneViews().insert(zone="us-central1-a", projectName=PROJECT_ID, body=body)
  response = request.execute()

  print response

It is optional to include a list of members during the creation of the resource view; you can also choose to add members after the resource view has been created.

Adding resources to your resource view

Once you have created your resource view, you can start adding resources.

Command-line tool


Use the following command to add instances by name to a region or zone view:

$ gcloud [--project=<project-id>] preview resource-views --zone || --region <zone-or-region-name> resources --resourceview <resource-view-name> addinstance <instance-1> <instance-2> ... <instance-n>

Important flags and parameters

--project=<project-id>
[Optional] If you did not save your project ID when you ran gcloud auth login , provide your project ID here.
--zone || --region <zone-or-region-name>
[Required] The zone or region for this resource view.
<resource-view-name>
[Required] The name of this resource view. The name must be unique in this project, from 1-63 characters long and match the regular expression: [a-z]([-a-z0-9]*[a-z0-9])? , which means the first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash.
<instance-1> <instance-2> ... <instance-n>
[Required] The names of the virtual machine instances to add to this resource view.

If you wanted to add virtual machine instances using the fully-qualified URL of the virtual machine resources, you can use the add subcommand instead of the addinstances subcommand:

$ gcloud [--project=<project-id>] preview resource-views --zone || --region <zone-or-region-name> resources --resourceview <resource-view-name> add <url-to-instance-1> <url-to-instance-2> ... <url-to-instance-n>

For example, a fully-qualified URL to instance-1 would look like:

https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/instance-1

There are no distinct differences between the two commands, and using one or the other is a matter of preference.

Python client library


In the Python client library, use the zoneViews.addresources method or the regionViews.addresources . The example below adds a zone resource view:

def addZoneResources(resourceview_service):
  body = {
    "resources": [
      "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/instance-1"
    ]
  }

  request = resourceview_service.zoneViews().addresources(zone="us-central1-a", projectName=PROJECT_ID, body=body, resourceViewName="my-resource-view")
  response = request.execute()

  print response

Removing resources

Use the following methods to remove resources from your zone or region view.

Command-line tool


In the command-line tool, use the removeinstances command or the remove command to remove resources from your zone or region view. The removeinstances command allows you to remove instances using the instance names. Alternatively, the remove command allows you to remove instances using the fully-qualified URL to the instance:

$ gcloud [--project=<project-id>] preview resource-views --zone || --region <zone-or-region-name> resources --resourceview <resource-view-name> removeinstances <instance-1> <instance-2> ... <instance-n>

Important flags and parameters

--project=<project-id>
[Optional] If you did not save your project ID when you ran gcloud auth login , provide your project ID here.
--zone || --region <zone-or-region-name>
[Required] The zone or region for this resource view.
<resource-view-name>
[Required] The name of this resource view. The name must be unique in this project, from 1-63 characters long and match the regular expression: [a-z]([-a-z0-9]*[a-z0-9])? , which means the first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash.
<instance-1> <instance-2> ... <instance-n>
[Required] The names of the virtual machine instances to remove from this resource view.

Similar to the addinstances and add command, you can also use the remove command, providing the fully-qualified URL to your resources, instead of the resource name:

    $ gcloud [--project=<project-id>] preview resource-views --zone || --region <zone-or-region-name> resources --resourceview <resource-view-name> remove <url-to-instance-1> <url-to-instance-2> ... <url-to-instance-n>

For example, a fully-qualified URL to instance-1 would look like:

https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/instance-1

Python client library


In the Python client library, use the zoneViews.removeresources method or the regionViews.removeresources . The example below removes a resources from a zone resource view.

def removeZoneResources(resourceview_service):
  body = {
    "resources": [
      "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances/instance-1"
    ]
  }

  request = resourceview_service.zoneViews().removeresources(zone="us-central1-a", projectName=PROJECT_ID, body=body, resourceViewName="my-resource-view")
  response = request.execute()

  print response

Deleting a resource view

Command-line tool


To delete a resource view, use the delete command with the desired zone or region:

$ gcloud [--project=<project-id] preview resource-views --zone || --region <zone-or-region-name> delete <resource-view-name>

Important flags and parameters

--project=<project-id>
[Optional] If you did not save your project ID when you ran gcloud auth login , provide your project ID here.
--zone || --region <zone-or-region-name>
[Required] The zone or region for this resource view.
<resource-view-name>
[Required] The name of the resource view to delete.

Deleting a resource view does not delete any of the resources within the view. You must delete these resources separately if you would like to remove them.

Python client library


In the client library, use the zoneViews.delete method or the regionViews.delete method to delete a zone or region view. The following delets a zone resource view.

def deleteZoneView(resourceview_service):

  request = resourceview_service.zoneViews().delete(zone="us-central1-a", projectName=PROJECT_ID, resourceViewName="my-resource-view")
  response = request.execute()

  print response

Getting help and providing feedback

For feedback and questions, please email the Limited Preview discussion group .

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.