Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: creating tags and attaching resource to resource group #30

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions resalloc_ibm_cloud/argparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ def vm_arg_parser():
help="Allocate additional volume of given size in GB",
default=160,
)
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"
)
Expand Down
25 changes: 25 additions & 0 deletions resalloc_ibm_cloud/ibm_cloud_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ def create_instance(service, instance_name, opts):
],
}

if opts.resource_group_id:
instance_prototype_model["resource_group"] = {"id": opts.resource_group_id}

for items in [
["name"],
["boot_volume_attachment", "volume", "name"],
Expand All @@ -207,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)
Expand All @@ -224,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
Expand Down
Loading