|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# (C) Copyright IBM Corp. 2020. |
| 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 EnterpriseUsageReportsV1 |
| 18 | +""" |
| 19 | + |
| 20 | +import os |
| 21 | +import pytest |
| 22 | +from ibm_cloud_sdk_core import ApiException, read_external_sources |
| 23 | +from ibm_platform_services.enterprise_usage_reports_v1 import * |
| 24 | + |
| 25 | +# |
| 26 | +# This file provides an example of how to use the Usage Reports service. |
| 27 | +# |
| 28 | +# The following configuration properties are assumed to be defined: |
| 29 | +# ENTERPRISE_USAGE_REPORTS_URL=<service url> |
| 30 | +# ENTERPRISE_USAGE_REPORTS_AUTHTYPE=iam |
| 31 | +# ENTERPRISE_USAGE_REPORTS_APIKEY=<IAM api key of user with authority to create rules> |
| 32 | +# ENTERPRISE_USAGE_REPORTS_AUTH_URL=<IAM token service URL - omit this if using the production environment> |
| 33 | +# ENTERPRISE_USAGE_REPORTS_ENTERPRISE_ID=<the id of the enterprise whose usage info will be retrieved> |
| 34 | +# ENTERPRISE_USAGE_REPORTS_ACCOUNT_ID=<the id of the account whose usage info will be retrieved> |
| 35 | +# ENTERPRISE_USAGE_REPORTS_ACCOUNT_GROUP_ID=<the id of the account group whose usage info will be retrieved> |
| 36 | +# ENTERPRISE_USAGE_REPORTS_BILLING_MONTH=<the billing month (yyyy-mm) for which usage info will be retrieved> |
| 37 | +# |
| 38 | +# These configuration properties can be exported as environment variables, or stored |
| 39 | +# in a "credentials" file and then: |
| 40 | +# export IBM_CREDENTIALS_FILE=<name of credentials file> |
| 41 | +# |
| 42 | + |
| 43 | +# Config file name |
| 44 | +config_file = 'enterprise_usage_reports.env' |
| 45 | + |
| 46 | +enterprise_usage_reports_service = None |
| 47 | + |
| 48 | +config = None |
| 49 | + |
| 50 | +enterprise_id = None |
| 51 | +billing_month = None |
| 52 | + |
| 53 | +############################################################################## |
| 54 | +# Start of Examples for Service: EnterpriseUsageReportsV1 |
| 55 | +############################################################################## |
| 56 | +# region |
| 57 | + |
| 58 | + |
| 59 | +class TestEnterpriseUsageReportsV1Examples(): |
| 60 | + """ |
| 61 | + Example Test Class for EnterpriseUsageReportsV1 |
| 62 | + """ |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def setup_class(cls): |
| 66 | + global enterprise_usage_reports_service |
| 67 | + if os.path.exists(config_file): |
| 68 | + os.environ['IBM_CREDENTIALS_FILE'] = config_file |
| 69 | + |
| 70 | + # begin-common |
| 71 | + |
| 72 | + enterprise_usage_reports_service = EnterpriseUsageReportsV1.new_instance() |
| 73 | + |
| 74 | + # end-common |
| 75 | + assert enterprise_usage_reports_service is not None |
| 76 | + |
| 77 | + # Load the configuration |
| 78 | + global config |
| 79 | + config = read_external_sources( |
| 80 | + EnterpriseUsageReportsV1.DEFAULT_SERVICE_NAME) |
| 81 | + |
| 82 | + global enterprise_id |
| 83 | + enterprise_id = config.get("ENTERPRISE_ID") |
| 84 | + |
| 85 | + global billing_month |
| 86 | + billing_month = config.get("BILLING_MONTH") |
| 87 | + |
| 88 | + assert enterprise_id is not None |
| 89 | + assert billing_month is not None |
| 90 | + |
| 91 | + print('Setup complete.') |
| 92 | + |
| 93 | + needscredentials = pytest.mark.skipif( |
| 94 | + not os.path.exists(config_file), reason="External configuration not available, skipping..." |
| 95 | + ) |
| 96 | + |
| 97 | + @needscredentials |
| 98 | + def test_get_resource_usage_report_example(self): |
| 99 | + """ |
| 100 | + get_resource_usage_report request example |
| 101 | + """ |
| 102 | + try: |
| 103 | + global enterprise_id, billing_month |
| 104 | + # begin-get_resource_usage_report |
| 105 | + |
| 106 | + reports = enterprise_usage_reports_service.get_resource_usage_report( |
| 107 | + enterprise_id=enterprise_id, |
| 108 | + month=billing_month, |
| 109 | + limit=10 |
| 110 | + ).get_result() |
| 111 | + |
| 112 | + print(json.dumps(reports, indent=2)) |
| 113 | + |
| 114 | + # end-get_resource_usage_report |
| 115 | + |
| 116 | + except ApiException as e: |
| 117 | + pytest.fail(str(e)) |
| 118 | + |
| 119 | +# endregion |
| 120 | +############################################################################## |
| 121 | +# End of Examples for Service: EnterpriseUsageReportsV1 |
| 122 | +############################################################################## |
0 commit comments