oracle/oci-python-sdk

Tags - using usage_api call

alvenkatesh opened this issue · 11 comments

Hi all,

i am trying to get all the tags associated for a resource weather it comes under defined tags or free form tags. and i am using usage_api.usageapiclient using the usage_api.models.requestsummarizedusagedetails and in the group_by filed i am giving 4 values which are resourceId, tagKey, tagNamespace, tagValue. and i am getting the response.

in this response we are seeing the tag column but we are getting only a single record or set of tagKey, tagNamespace, tagValue instead of all the key and values for that associated resource and also missing some key value pairs.

is this an expected behaviour or am i missing some thing here?

below i am attaching the code and response

identity = oci.identity.IdentityClient(config)
user = identity.get_user(config["user"]).data
identity_client = oci.identity.IdentityClient(config)
tenancy_id = config["tenancy"]

start_date = datetime.utcnow().replace(second=0, microsecond=0, minute=0,hour=0).replace(tzinfo=None) - timedelta(days = 30)
end_date = datetime.utcnow().replace(second=0, microsecond=0, minute=0,hour=0).replace(tzinfo=None)

usage_client = oci.usage_api.UsageapiClient(config)

requestSummarizedUsagesDetails = oci.usage_api.models.RequestSummarizedUsagesDetails(
tenant_id=tenancy_id,
granularity='DAILY',
query_type='COST',
group_by=['resourceId', 'tagKey', 'tagValue', 'skuName'],
time_usage_started=start_date,
time_usage_ended=end_date
)

usageClient.request_summarized_usages

request_summarized_usages = usage_client.request_summarized_usages(
requestSummarizedUsagesDetails,
retry_strategy=oci.retry.DEFAULT_RETRY_STRATEGY
)

file = '/home/ubuntu/Desktop/example2.txt'
temp = str(request_summarized_usages.data.items)
with open (file, 'w') as f:
f.write(temp)

example2.txt

Hi, usageapi summaries the data using grouping, and what you got above is expected behavour
If you would like the full tag information, you can use the usage report files or search or query each service individually.

"Hi @adizohar, I understand that we need to perform individual searches or queries for each service.

However, what I am looking for is to retrieve all the tags related to the services within a specific compartment or under that tenancy.

Is there an API call available to obtain the requested information?"

You can use the usage api below, you can modify to add compartment to the grouping if needed:

import oci
config = oci.config.from_file("~/.oci/config", "DEFAULT")
tenant_id = config['tenancy']
time_usage_started = "2023-12-01T00:00:00Z"
time_usage_ended = "2023-12-05T00:00:00Z"

print("Tenant Name  : " + str(tenant_id))

usage_client = oci.usage_api.UsageapiClient(config)

requestSummarizedUsagesDetails = oci.usage_api.models.RequestSummarizedUsagesDetails(
    tenant_id=tenant_id,
    granularity='DAILY',
    query_type='COST',
    group_by=['service', 'tagNamespace', 'tagKey', 'tagValue'],
    time_usage_started=time_usage_started,
    time_usage_ended=time_usage_ended
)

# usageClient.request_summarized_usages
request_summarized_usages = usage_client.request_summarized_usages(
    requestSummarizedUsagesDetails,
    retry_strategy=oci.retry.DEFAULT_RETRY_STRATEGY
)

print("##############################################################################")
print("#                            Cost per Tag                                    #")
print("##############################################################################")
for item in request_summarized_usages.data.items:
    print(
        str(item.time_usage_started) +
        ", " + str(item.time_usage_ended) +
        ", " + str(item.tags) +
        ", " + str(item.service) +
        ", " + str(item.computed_amount)
    )