I have namespace already created and defined tags to resources. When I try adding new tags to the resources, the old tags are getting deleted. As I would like to use the old data and return the value along with the new tags. Please help me with how I can achieve this.
get volume details from a specific compartment
import oci config = oci.config.from_file("~/.oci/config") core_client = oci.core.BlockstorageClient(config) get_volume_response = core_client.get_volume( volume_id="ocid1.test.oc1..<unique_ID>EXAMPLE-volumeId-Value") # Get the data from response print(get_volume_response.data)
output
{ "availability_domain": "eto:PHX-AD-1", "compartment_id": "ocid1.compartment.oc1..aaaaaaaapmj", "defined_tags": { "OMCS": { "CREATOR": "xyz@gmail.com" }, "Oracle-Tags": { "CreatedBy": "xyz@gmail.com", "CreatedOn": "2022-07-5T08:29:24.865Z" } }, "display_name": "test_VG", "freeform_tags": {}, "id": "ocid1.volumegroup.oc1.phx.abced", "is_hydrated": null, "lifecycle_state": "AVAILABLE", "size_in_gbs": 100, "size_in_mbs": 102400, "source_details": { "type": "volumeIds", "volume_ids": [ "ocid1.volume.oc1.phx.xyz" ] }
I want the API below to update the tag along with the old data.
old tag
"defined_tags": { "OMCS": { "CREATOR": "xyz@gmail.com" }, "Oracle-Tags": { "CreatedBy": "xyz@gmail.com", "CreatedOn": "2022-07-5T08:29:24.865Z"
import oci config = oci.config.from_file("~/.oci/config") core_client = oci.core.BlockstorageClient(config) update_volume_response = core_client.update_volume( volume_id="ocid1.test.oc1..<unique_ID>EXAMPLE-volumeId-Value", update_volume_details=oci.core.models.UpdateVolumeDetails( defined_tags={ 'OMCS':{ 'INSTANCE': 'TEST', 'COMPONENT': 'temp1.mt.exy.vcn.com' } }, display_name = "TEMPMT01")) print(update_volume_response.data)
I also tried but got an attribute error.
for tag in get_volume_response.data: def_tag.appened(tag.defined_tags) return (def_tag)
Please help on how can I append the defined_tags?
Advertisement
Answer
tags
are defined as dict
in OCI
. Append works the same way as in appending dict
.
Below I have pasted the code for updating the defined_tags
for Block Volumes
in OCI
import oci from oci.config import from_file configAPI = from_file() # Config file is read from user's home location i.e., ~/.oci/config core_client = oci.core.BlockstorageClient(configAPI) get_volume_response = core_client.get_volume( volume_id="ocid1.volume.oc1.ap-hyderabad-1.ameen") # Get the data from response volume_details = get_volume_response.data defined_tags = getattr(volume_details, "defined_tags") freeform_tags = getattr(volume_details, "freeform_tags") # Add new tags as required. As defined_tags is a dict, addition of new key/value pair works like below. # In case there are multiple tags to be added then use update() method of dict. defined_tags["OMCS"]["INSTANCE"] = "TEST" defined_tags["OMCS"]["COMPONENT"] = "temp1.mt.exy.vcn.com" myJson={"freeform_tags":freeform_tags,"defined_tags": defined_tags} update_volume_response = core_client.update_volume( volume_id="ocid1.volume.oc1.ap-hyderabad-1.ameen", update_volume_details=oci.core.models.UpdateVolumeDetails( defined_tags=defined_tags, freeform_tags=freeform_tags)) print(update_volume_response.data)