GCP Python – Delete Project Labels
Question:
I am trying to delete labels from projects, by using the UpdateProjectRequest.
It says that it needs an ‘update_mask’, but I do not know how to created this mask. It is a google.protobuf.field_mask_pb2.FieldMask
What i have tried is:
from google.cloud import resourcemanager_v3
from google.protobuf import field_mask_pb2
def sample_update_project(project_id):
client = resourcemanager_v3.ProjectsClient()
update_mask = field_mask_pb2.FieldMask(paths=["labels.newkey:None"])
resourcemanager_v3.Project=f"projects/{project_id}"
operation = client.update_project(update_mask=update_mask)
print("Waiting for operation to complete...")
response = operation.result()
print(response)
Thanks
Answers:
As you already mentioned, when you want to delete all labels, it requires an update mask for the labels field.
Reviewing this documentation, please note that if a request is provided, this should not be set. Also, you will find a code example that might help you.
Additionally, you should import filed_mask_pb2
if you will update data with a FieldMask use as follows:
from google.protobuf import field_mask_pb2
Having the main question in mind, one of two methods can be used to remove a label:
Use the read-modify-write pattern to completely remove the label, which removes both the key and the value. To do this, perform these steps:
- Use the resource’s get() function to read the current labels.
- Use a text editor or programmatically change the returned labels to
add or remove any necessary keys and their values.
- Call the patch() function on the resource to write the changed
labels.
To keep the key and remove the value, set the value to null
, but reviewing your code, you are currently using None
.
Also, consider the following documentation:Creating and managing labels and Method: projects.patch
UPDATE
Working again with your main question, here is a code that it was already tested to delete labels as you requested:
import googleapiclient.discovery
def sample_update_project_old(project_id):
manager = googleapiclient.discovery.build('cloudresourcemanager', 'v1')
request = manager.projects().get(projectId=project_id)
project = request.execute()
del project['labels']['key'] # replace 'key' with your actual key value
request = manager.projects().update(projectId=project_id, body=project)
project = request.execute()
sample_update_project_old("your-project-id")
I am trying to delete labels from projects, by using the UpdateProjectRequest.
It says that it needs an ‘update_mask’, but I do not know how to created this mask. It is a google.protobuf.field_mask_pb2.FieldMask
What i have tried is:
from google.cloud import resourcemanager_v3
from google.protobuf import field_mask_pb2
def sample_update_project(project_id):
client = resourcemanager_v3.ProjectsClient()
update_mask = field_mask_pb2.FieldMask(paths=["labels.newkey:None"])
resourcemanager_v3.Project=f"projects/{project_id}"
operation = client.update_project(update_mask=update_mask)
print("Waiting for operation to complete...")
response = operation.result()
print(response)
Thanks
As you already mentioned, when you want to delete all labels, it requires an update mask for the labels field.
Reviewing this documentation, please note that if a request is provided, this should not be set. Also, you will find a code example that might help you.
Additionally, you should import filed_mask_pb2
if you will update data with a FieldMask use as follows:
from google.protobuf import field_mask_pb2
Having the main question in mind, one of two methods can be used to remove a label:
Use the read-modify-write pattern to completely remove the label, which removes both the key and the value. To do this, perform these steps:
- Use the resource’s get() function to read the current labels.
- Use a text editor or programmatically change the returned labels to
add or remove any necessary keys and their values. - Call the patch() function on the resource to write the changed
labels.
To keep the key and remove the value, set the value to null
, but reviewing your code, you are currently using None
.
Also, consider the following documentation:Creating and managing labels and Method: projects.patch
UPDATE
Working again with your main question, here is a code that it was already tested to delete labels as you requested:
import googleapiclient.discovery
def sample_update_project_old(project_id):
manager = googleapiclient.discovery.build('cloudresourcemanager', 'v1')
request = manager.projects().get(projectId=project_id)
project = request.execute()
del project['labels']['key'] # replace 'key' with your actual key value
request = manager.projects().update(projectId=project_id, body=project)
project = request.execute()
sample_update_project_old("your-project-id")