oracle/oci-python-sdk

Tags in the response object are always null in the method "RequestSummarizedUsageDetails" using the client UsageApiClient

AzmeenaNarsingani opened this issue · 13 comments

I have been using the latest version of the OCI Python SDK ( https://pypi.org/project/oci/) but have found that the response object's "tags" property is empty for all resources.

Github example for the same call : https://github.com/oracle/oci-python-sdk/blob/master/examples/usage_api_example.py

We would like to see all tags associated to a resource in the response other than the filtered tag

Specific call is the RequestSummarizedUsageDetails using the client UsageApiClient

usage_request = RequestSummarizedUsagesDetails(tenant_id=OCI_TENANT_ID, time_usage_started=startDate.replace( hour=0, minute=0, second=0, microsecond=0), time_usage_ended=endDate.replace( hour=0, minute=0, second=0, microsecond=0), granularity='MONTHLY', query_type='COST', is_aggregate_by_time=False, filter=oci.usage_api.models.Filter( operator="AND", tags=[ oci.usage_api.models.Tag( namespace="Example-Tags", key="DepartmentNumber", value=deptNumber)]), group_by=[ 'resourceId', 'compartmentName', 'skuName', 'skuPartNumber'], compartment_depth=6 )

Response is
{ "ad": null, "compartment_id": null, "compartment_name": "xxxxxxxxxxxxxxxxx", "compartment_path": null, "computed_amount": 18.432, "computed_quantity": 23040.0, "currency": "USD", "discount": null, "is_forecast": false, "list_rate": null, "overage": null, "overages_flag": null, "platform": null, "region": null, "resource_id": "xxxxxxxxxxxxxxxxxxxxxxxxx", "resource_name": null, "service": null, "shape": null, "sku_name": "Standard - E4 - Memory", "sku_part_number": "B93114", "subscription_id": null, "tags": [ { "key": null, "namespace": null, "value": null } ], "tenant_id": null, "tenant_name": null, "time_usage_ended": "2022-12-31T00:00:00+00:00", "time_usage_started": "2022-12-01T00:00:00+00:00", "unit": null, "unit_price": null, "weight": null },

Specifically, response tags": [ { "key": null, "namespace": null, "value": null } ],

Hi, the API is for summary usage not list of usage, with summary usage you can get summary of the cost per the aggregated function therefore you won't be able to get the associated tags because it is summary
In order to get list of cost, you can use the cost files from the console or read using the APIs
For your info

Below code how to report cost per tag and SkuName
You can check the documentation for additional info

import oci
config = oci.config.from_file("~/.oci/config", "DEFAULT")
tenant_id = config['tenancy']
time_usage_started = "2023-02-04T00:00:00Z"
time_usage_ended = "2023-02-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=['tagNamespace', 'tagKey', 'tagValue', 'skuName'],
    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
)

for item in request_summarized_usages.data.items:
    print(
        str(item.time_usage_started) +
        ", " + str(item.time_usage_ended) +
        ", " + str(item.tags) +
        ", " + str(item.sku_name) +
        ", " + str(item.computed_amount)
    )

You can groupby with below keys:
"tagNamespace", "tagKey", "tagValue", "service", "skuName", "skuPartNumber", "unit",
"compartmentName", "compartmentPath", "compartmentId", "platform", "region", "logicalAd",
"resourceId", "tenantId", "tenantName"

Public Doc = https://docs.oracle.com/en-us/iaas/Content/Billing/Concepts/costanalysisoverview.htm#cost_analysis_using_the_api

We dont need cost per tag. Cost per day is also available through RequestSummarizedUsagesDetails by adding in the filter. We are looking for all associated tags to the resources, I dont see any other API giving me that information

Azmeena, if you want all tags per resource, you should look at showoci https://github.com/oracle/oci-python-sdk/tree/master/examples/showoci
You can extract all compute resources including tag per column in csv format. (No cost associated)
If you would like more rebust tool , I developed also usage2adw which load the usage and cost files to ADW and you can view reports using APEX app - https://github.com/oracle/oci-python-sdk/tree/master/examples/usage_reports_to_adw
Please advise if this is what you are looking for.

Hello, I understand these market place tools are available but we are building a custom reporting for multiple clouds and we were looking to get the additional tags along with the resources through the API itself. We are able to get this information from the API for Azure and Aws clouds but not OCI.

I will advise the product management

Hi @AzmeenaNarsingani , thank you for bringing this issue up. We will let the concerned service team know and triage it as a feature request. They might be able to also suggest any workaround as well.

Thank you for the update. Please let us know what they suggest.

Hi @AzmeenaNarsingani
I think you can use group_by=[ 'resourceId', 'tagNamespace', 'tagKey', 'tagValue'] to get all tags for each resource.
If this is not the case, can you show me one example

Hi @Ruofan00 , did you get a chance to try the above workaround? Let us know if you have any questions.

Hello, group by only allows 4 values in the array. We need other information like compartment name , Id etc. Either way I will be missing certain information as it only gives values in the response if entered in the group by array.

We found a workaround for this by getting the tags from OCI cost and usage reports. we can close this request.

Thanks @AzmeenaNarsingani . I will be closing this issue.

@Ruofan00 please take note of the limitations mentioned - "group by only allows 4 values in the array". Thanks.