Skip to content

Commit

Permalink
Add functionality for tagging resource by its crn
Browse files Browse the repository at this point in the history
nikromen committed Dec 17, 2024

Verified

This commit was signed with the committer’s verified signature.
skitt Stephen Kitt
1 parent 20e5884 commit c838866
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions resalloc_ibm_cloud/argparsers.py
Original file line number Diff line number Diff line change
@@ -68,6 +68,13 @@ def vm_arg_parser():
)
parser_create.add_argument(
"--resource-group-id", help="Resource group id, get it from `$ ibmcloud resources`"
)
parser_create.add_argument(
"--tags",
type=str,
nargs="+",
help="Space separated list of key:value tags, e.g. app:copr",
)
parser_delete = subparsers.add_parser(
"delete", help="Delete instance by it's name from IBM Cloud"
)
22 changes: 22 additions & 0 deletions resalloc_ibm_cloud/ibm_cloud_vm.py
Original file line number Diff line number Diff line change
@@ -210,6 +210,9 @@ def create_instance(service, instance_name, opts):
instance_id = opts.instance_created["id"]
log.info("Instance ID: %s", instance_id)

if opts.tags:
assign_user_tags(opts.instance_created["crn"], service, opts)

if opts.no_floating_ip:
# assuming you have access through to private IP address
ip_address = _get_private_ip_of_instance(instance_id, service)
@@ -227,6 +230,25 @@ def create_instance(service, instance_name, opts):
raise


def assign_user_tags(crn, service, opts):
"""
Assign tags to the resource according to the CRN within the region.
"""
service_url = "https://tags.global-search-tagging.cloud.ibm.com/v3"
url = service_url + "/tags/attach?tag_type=user"
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {service.authenticator.token_manager.get_token()}",
"Content-Type": "application/json",
}
data = {
"resources": [{"resource_id": crn}],
"tag_names": list(opts.tags),
}
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()


def delete_all_ips(service):
"""
Go through all reserved IPs, and remove all which are not assigned

0 comments on commit c838866

Please sign in to comment.