|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# (C) Copyright IBM Corp. 2024. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +""" |
| 17 | +Examples for PartnerUsageReportsV1 |
| 18 | +""" |
| 19 | + |
| 20 | +from ibm_cloud_sdk_core import ApiException, read_external_sources |
| 21 | +import os |
| 22 | +import pytest |
| 23 | +from ibm_platform_services.partner_usage_reports_v1 import * |
| 24 | + |
| 25 | +# |
| 26 | +# This file provides an example of how to use the Partner Usage Reports service. |
| 27 | +# |
| 28 | +# The following configuration properties are assumed to be defined: |
| 29 | +# PARTNER_USAGE_REPORTS_URL=<service base url> |
| 30 | +# PARTNER_USAGE_REPORTS_AUTH_TYPE=iam |
| 31 | +# PARTNER_USAGE_REPORTS_APIKEY=<IAM apikey> |
| 32 | +# PARTNER_USAGE_REPORTS_AUTH_URL=<IAM token service base URL - omit this if using the production environment> |
| 33 | +# PARTNER_USAGE_REPORTS_PARTNER_ID=<Enterprise ID of the distributor or reseller for which the report is requested> |
| 34 | +# PARTNER_USAGE_REPORTS_CUSTOMER_ID=<Enterprise ID of the child customer for which the report is requested> |
| 35 | +# PARTNER_USAGE_REPORTS_RESELLER_ID=<Enterprise ID of the reseller for which the report is requested> |
| 36 | +# PARTNER_USAGE_REPORTS_BILLING_MONTH=<The billing month for which the usage report is requested. Format is `yyyy-mm`> |
| 37 | +# PARTNER_USAGE_REPORTS_VIEWPOINT=<Enables partner to view the cost of provisioned services as applicable at each level of the hierarchy> |
| 38 | +# |
| 39 | +# These configuration properties can be exported as environment variables, or stored |
| 40 | +# in a configuration file and then: |
| 41 | +# export IBM_CREDENTIALS_FILE=<name of configuration file> |
| 42 | +# |
| 43 | +config_file = 'partner_usage_reports_v1.env' |
| 44 | + |
| 45 | +partner_usage_reports_service = None |
| 46 | + |
| 47 | +config = None |
| 48 | +partner_id = None |
| 49 | +billing_month = None |
| 50 | + |
| 51 | + |
| 52 | +############################################################################## |
| 53 | +# Start of Examples for Service: PartnerUsageReportsV1 |
| 54 | +############################################################################## |
| 55 | +# region |
| 56 | +class TestPartnerUsageReportsV1Examples: |
| 57 | + """ |
| 58 | + Example Test Class for PartnerUsageReportsV1 |
| 59 | + """ |
| 60 | + |
| 61 | + @classmethod |
| 62 | + def setup_class(cls): |
| 63 | + global partner_usage_reports_service |
| 64 | + if os.path.exists(config_file): |
| 65 | + os.environ['IBM_CREDENTIALS_FILE'] = config_file |
| 66 | + |
| 67 | + # begin-common |
| 68 | + |
| 69 | + partner_usage_reports_service = PartnerUsageReportsV1.new_instance() |
| 70 | + |
| 71 | + # end-common |
| 72 | + assert partner_usage_reports_service is not None |
| 73 | + |
| 74 | + # Load the configuration |
| 75 | + global config |
| 76 | + config = read_external_sources(PartnerUsageReportsV1.DEFAULT_SERVICE_NAME) |
| 77 | + |
| 78 | + global partner_id |
| 79 | + partner_id = config.get("PARTNER_ID") |
| 80 | + |
| 81 | + global billing_month |
| 82 | + billing_month = config.get("BILLING_MONTH") |
| 83 | + |
| 84 | + assert partner_id is not None |
| 85 | + assert billing_month is not None |
| 86 | + |
| 87 | + print('Setup complete.') |
| 88 | + |
| 89 | + needscredentials = pytest.mark.skipif( |
| 90 | + not os.path.exists(config_file), reason="External configuration not available, skipping..." |
| 91 | + ) |
| 92 | + |
| 93 | + @needscredentials |
| 94 | + def test_get_resource_usage_report_example(self): |
| 95 | + """ |
| 96 | + get_resource_usage_report request example |
| 97 | + """ |
| 98 | + try: |
| 99 | + print('\nget_resource_usage_report() result:') |
| 100 | + # begin-get_resource_usage_report |
| 101 | + |
| 102 | + all_results = [] |
| 103 | + pager = GetResourceUsageReportPager( |
| 104 | + client=partner_usage_reports_service, |
| 105 | + partner_id=partner_id, |
| 106 | + month=billing_month, |
| 107 | + limit=10, |
| 108 | + ) |
| 109 | + while pager.has_next(): |
| 110 | + next_page = pager.get_next() |
| 111 | + assert next_page is not None |
| 112 | + all_results.extend(next_page) |
| 113 | + |
| 114 | + print(json.dumps(all_results, indent=2)) |
| 115 | + |
| 116 | + # end-get_resource_usage_report |
| 117 | + except ApiException as e: |
| 118 | + pytest.fail(str(e)) |
| 119 | + |
| 120 | + |
| 121 | +# endregion |
| 122 | +############################################################################## |
| 123 | +# End of Examples for Service: PartnerUsageReportsV1 |
| 124 | +############################################################################## |
0 commit comments