Setting common instance metadata with Metadata class

52 views
Skip to first unread message

Electrosis Music

unread,
Oct 1, 2017, 1:53:14 PM10/1/17
to Google Cloud Developers
I'm trying to set common instance metadata on my google cloud platform account by manipulating the fields of a metadata object and then sending a http request, using the Java API. Like so:

Metadata meta = new Metadata();

ArrayList<Metadata.Items> list = new ArrayList();

list.add(new Metadata.Items().set("test1", "one"));

list.add(new Metadata.Items().set("test2", "two"));

meta.setItems(list);

meta.set("test3", "three");

meta.set("uri", "urn:ogf:network:google.com:google-cloud:example-uri");

HttpRequest request = computeClient.projects().setCommonInstanceMetadata(projectID, meta).buildHttpRequest().execute();


After I run the above code, the metadata object ends up like so:

{items=[{test1=one}, {test2=two}], test3=three, uri=urn:ogf:network:google.com:google-cloud:example-uri}


Yet, after running the code actually clears out all metadata fields in common instance metadata, even if they had been set previously via a web browser. So, I know I'm calling the correct function but it's not working properly. The only thing I can think is that I must not be configuring the metadata object properly. If anyone has any experience with setting common instance metadata from API this way, your input would be very helpful.

Thanks.

Kenworth (Google Cloud Platform)

unread,
Oct 1, 2017, 4:13:00 PM10/1/17
to Google Cloud Developers
1- Java 7 or 8?
2- Try to remove 'buildHttpRequest()'
3- Provide the result of the 'response' object.

Per this docs, here is the boilerplate code for setting common instance metadata in Java:

/ Project ID for this request.
    String project = "my-project"; // TODO: Update placeholder value.

    // TODO: Assign values to desired fields of `requestBody`:
    Metadata requestBody = new Metadata();

    Compute computeService = createComputeService();
    Compute.Projects.SetCommonInstanceMetadata request =
        computeService.projects().setCommonInstanceMetadata(project, requestBody);

    Operation response = request.execute();

    // TODO: Change code below to process the `response` object:
    System.out.println(response);


Electrosis Music

unread,
Oct 2, 2017, 11:04:41 AM10/2/17
to Google Cloud Developers
1. I'm running Java 8

2. I tried removing .buildHttpRequest, but it doesn't change anything.

3. Here's the result of result.parseAsString(), with the service account email address removed.

{

 "kind": "compute#operation",

 "id": "6942523982935485097",

 "name": "operation-1506956358444-55a919e6fe3e0-0f8df5ed-02b14207",

 "operationType": "setMetadata",

 "targetLink": "https://www.googleapis.com/compute/v1/projects/elegant-whales-176421",

 "targetId": "271958204962",

 "status": "PENDING",

 "user": <email address>,

 "progress": 0,

 "insertTime": "2017-10-02T07:59:18.656-07:00",

 "selfLink": "https://www.googleapis.com/compute/v1/projects/elegant-whales-176421/global/operations/operation-1506956358444-55a919e6fe3e0-0f8df5ed-02b14207"

Kenworth (Google Cloud Platform)

unread,
Oct 2, 2017, 6:39:40 PM10/2/17
to Google Cloud Developers
Thank you for the additional information. It seems your case may potentially qualify as a defect report. As such, I recommend you file a public issue tracker so that proper attention will be given to it.

Electrosis Music

unread,
Oct 3, 2017, 2:20:13 PM10/3/17
to Google Cloud Developers
I managed to get it to work, by setting the key and value for each item separately. It does sound like a bug however, so I'll file it with the public issue tracker as you suggested.
Reply all
Reply to author
Forward
0 new messages