Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Update or Create Project Labels using Python Resource Manager API

Hello,

 

 I have tried several times how to create a label or update a one in a project but I cannot reach the proper way to do it. Could you please help me to identify what is wrong ?  Thanks in advance!!

 
from google.cloud import resourcemanager_v3
from google.protobuf import field_mask_pb2
from google.cloud.resourcemanager_v3.types import Project

def update_project_labels(project_id, application_id, application_cluster):
    # Create a client
    client = resourcemanager_v3.ProjectsClient()

    # Initialize request argument(s)
    update_mask = field_mask_pb2.FieldMask(paths=["labels.application_id:{}".format(application_id), "labels.application_cluster:{}".format(application_cluster)])
 
    request = resourcemanager_v3.UpdateProjectRequest(project=Project(name=f"projects/{project_id}"),  update_mask=update_mask)
     

    # Make the request
    operation = client.update_project(request=request)

    print("Waiting for operation to complete...")

    response = operation.result()

    # Handle the response
    print(response)

 

1 5 3,178
5 REPLIES 5

Hi @friveros:

Welcome to Google Cloud Community!

You can have the option to either create or update labels using the Resource Manager API and the Google Cloud console.

Create labels:

  1. Open the Labels page in the Google Cloud console.
  2. Select your project from the Select a project drop-down.
  3. To add a new label entry, click + Add label and enter a label key and value for each label you want to add.
  4. When you're finished adding labels, click Save.

To add labels for more than one project at the same time:

  1. Open the Manage resources page in the Google Cloud console.
  2. On the Manage resources page, select the projects for which you want to add labels.
  3. In the info panel, in the Labels tab, click + Add label and enter a label key and value for each label you want to add.
  4. When you're finished adding labels, click Save.

Update labels:

  1. Open the Labels page in the Google Cloud console.
  2. On the Manage resources page, select the projects for which you want to update labels.
  3. In the info panel, click the Labels tab and update labels for the selected projects:
    1. To edit a label, click the value that you want to edit, then make your desired changes.
    2. To delete a label, hold the pointer over the key or value, then click the trash icon.
  4. When you're finished updating labels, click Save

You can check this documentation on creating and managing labels for additional information. REST API is also available if you prefer that option.

Hope this helps.

Thanks @robertcarlos but it seems you didn't answer my question. I am building a code with python where I need to use the python API of resource manager to update labels on specified resources, so how can I do that using the python according my code snippet ? since I have searched and I cannot find information which is the right way. If you could figure out internally it would be great !

Thanks in advance!

 

Apologies @friveros. You could use this link on Python Client for Resource Manager. Supported Python version should be at least 3.7. You could also check this Python package index for Google Cloud Resource Manager for additional details and guides in using it.

Additional resource can also be found through this link on Exploring Google Cloud Resource Manager with Python — V1.0.

Hope this helps.

Thanks @robertcarlos but the problem is with the 

 update_mask = field_mask_pb2.FieldMask(paths=["labels.application_id:{}".format(application_id), "labels.application_cluster:{}".format(application_cluster)])
 

there is nowhere an example how to build that Field Mask. Could you show that please?

Thanks

Hi @friveros,

To update your labels do the following:

client = resourcemanager_v3.ProjectsClient()

get_project = client.get_project(name=f"projects/{project_id}")
## field you want to update
update_mask = field_mask_pb2.FieldMask(paths=["labels"])

## get current project labels
updated_labels = get_project.labels

## append your new labels to current labels
updated_labels.update(labels)
get_project.labels = updated_labels

updated_project = client.update_project(
project=get_project,
update_mask=update_mask)
return updated_project



Top Labels in this Space
Top Solution Authors