Skip to content

How can we retrieve the cost of child tenancies using the OCI Python SDK and the Usage API? #763

Description

@Gopichand-Heeddata

How can we retrieve the costs of child tenancies using the OCI Python SDK and the Usage API.
Suppose I have four child tenancies, each associated with a single parent tenancy. I need to fetch the costs of those child tenancies with parent tenancies using Usage API calls.
@adizohar, can you guide me on how to achieve this?.

import oci

config = oci.config.from_file()
tenant_id = config['tenancy']
time_usage_started = "2024-04-01T00:00:00Z"
time_usage_ended = "2024-04-21T00:00:00Z"
lis=[]
usage_client = oci.usage_api.UsageapiClient(config,timeout=(10,3000))

requestSummarizedUsagesDetails = oci.usage_api.models.RequestSummarizedUsagesDetails(
    tenant_id=tenant_id,
    granularity='DAILY',
    query_type='USAGE',
    group_by=[ 'resourceId','service','compartmentName'],
    time_usage_started=time_usage_started,
    time_usage_ended=time_usage_ended,
    compartment_depth=2
)
# CUSTOM_RETRY_STRATEGY = RetryStrategyBuilder().add_max_attempts(max_attempts=8) \
#     .add_total_elapsed_time(total_elapsed_time_seconds=25000) \
#     .add_service_error_check(service_error_retry_config=retry_checkers.RETRYABLE_STATUSES_AND_CODES,
#                              service_error_retry_on_any_5xx=True) \
#     .get_retry_strategy()

# Retrieve summarized usage data
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(len(item.resource_id))

Activity

  1. adizohar commented on May 2, 2025

    @adizohar
    Member
  2. Gopichand-Heeddata commented on May 3, 2025

    @Gopichand-Heeddata
    Author

    Thankyou @adizohar , i ll check

  3. Gopichand-Heeddata commented on May 3, 2025

    @Gopichand-Heeddata
    Author

    I need to retrieve the root tenancy instance details in OCI. I have created three instances and want to fetch their metadata, along with details of child tenancies that I’ve also created.
    Ideally, I want to use the same API to retrieve information from both the root tenancy and its child tenancies—not just instance data, but also any associated resources and their metrics.
    If it's not possible with a single API, please suggest the best approach or combination of APIs to achieve this.
    @adizohar

    import oci
    
    config=oci.config.from_file()
    compute_client = oci.core.ComputeClient(config)
    
    # Get the list of compartments
    identity_client = oci.identity.IdentityClient(config)
    compartment_response = identity_client.list_compartments(compartment_id=config["tenancy"])
    
    detached_count = 0
    # Iterate over each compartment and retrieve the volume details for each region
    for compartment in compartment_response.data:
        compartment_id = compartment.id
    
         # Get the list of regions
            region_response = identity_client.list_region_subscriptions(tenancy_id=config['tenancy'])
         # Iterate over each region and retrieve the volume details
        for region in region_response.data:
            region_name = region.region_name
            
            #List instances in the current compartment
            instance_response = compute_client.list_instances(compartment_id=compartment_id).data
        
                for instance in instance_response:
                    print(instance)
    
  4. adizohar commented on May 3, 2025

    @adizohar
    Member

    Hi Gopi, Please open new issue for new question, and don't forget to close the issue once answered.

    You cannot query different tenants with same API, you will need to authenticate to each separately

    You can see how I extract all the information (per tenant) with my showoci script - https://github.com/oracle/oci-python-sdk/tree/master/examples/showoci

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions