|
| 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 UsageReportsV4 |
| 18 | +""" |
| 19 | + |
| 20 | +import os |
| 21 | +import pytest |
| 22 | +from ibm_cloud_sdk_core import ApiException, read_external_sources |
| 23 | +from ibm_platform_services.usage_reports_v4 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 | +# USAGE_REPORTS_URL=<service url> |
| 30 | +# USAGE_REPORTS_AUTHTYPE=iam |
| 31 | +# USAGE_REPORTS_APIKEY=<IAM api key of user with authority to create rules> |
| 32 | +# USAGE_REPORTS_AUTH_URL=<IAM token service URL - omit this if using the production environment> |
| 33 | +# USAGE_REPORTS_ACCOUNT_ID=<the id of the account whose usage info will be retrieved> |
| 34 | +# USAGE_REPORTS_RESOURCE_GROUP_ID=<the id of the resource group whose usage info will be retrieved> |
| 35 | +# USAGE_REPORTS_ORG_ID=<the id of the organization whose usage info will be retrieved> |
| 36 | +# 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 = 'usage_reports.env' |
| 45 | + |
| 46 | +usage_reports_service = None |
| 47 | + |
| 48 | +config = None |
| 49 | + |
| 50 | +account_id = None |
| 51 | +resource_group_id = None |
| 52 | +org_id = None |
| 53 | +billing_month = None |
| 54 | + |
| 55 | +############################################################################## |
| 56 | +# Start of Examples for Service: UsageReportsV4 |
| 57 | +############################################################################## |
| 58 | +# region |
| 59 | + |
| 60 | + |
| 61 | +class TestUsageReportsV4Examples(): |
| 62 | + """ |
| 63 | + Example Test Class for UsageReportsV4 |
| 64 | + """ |
| 65 | + |
| 66 | + @classmethod |
| 67 | + def setup_class(cls): |
| 68 | + global usage_reports_service |
| 69 | + if os.path.exists(config_file): |
| 70 | + os.environ['IBM_CREDENTIALS_FILE'] = config_file |
| 71 | + |
| 72 | + # begin-common |
| 73 | + |
| 74 | + usage_reports_service = UsageReportsV4.new_instance( |
| 75 | + ) |
| 76 | + |
| 77 | + # end-common |
| 78 | + assert usage_reports_service is not None |
| 79 | + |
| 80 | + # Load the configuration |
| 81 | + global config |
| 82 | + config = read_external_sources(UsageReportsV4.DEFAULT_SERVICE_NAME) |
| 83 | + |
| 84 | + # Retrieve and verify some additional test-related config properties. |
| 85 | + global account_id |
| 86 | + account_id = config.get("ACCOUNT_ID") |
| 87 | + |
| 88 | + global resource_group_id |
| 89 | + resource_group_id = config.get("RESOURCE_GROUP_ID") |
| 90 | + |
| 91 | + global org_id |
| 92 | + org_id = config.get("ORG_ID") |
| 93 | + |
| 94 | + global billing_month |
| 95 | + billing_month = config.get("BILLING_MONTH") |
| 96 | + |
| 97 | + assert account_id is not None |
| 98 | + assert resource_group_id is not None |
| 99 | + assert org_id is not None |
| 100 | + assert billing_month is not None |
| 101 | + |
| 102 | + print('Setup complete.') |
| 103 | + |
| 104 | + needscredentials = pytest.mark.skipif( |
| 105 | + not os.path.exists(config_file), reason="External configuration not available, skipping..." |
| 106 | + ) |
| 107 | + |
| 108 | + @needscredentials |
| 109 | + def test_get_account_summary_example(self): |
| 110 | + """ |
| 111 | + get_account_summary request example |
| 112 | + """ |
| 113 | + try: |
| 114 | + global account_id, billing_month |
| 115 | + |
| 116 | + # begin-get_account_summary |
| 117 | + |
| 118 | + account_summary = usage_reports_service.get_account_summary( |
| 119 | + account_id=account_id, |
| 120 | + billingmonth=billing_month |
| 121 | + ).get_result() |
| 122 | + |
| 123 | + print(json.dumps(account_summary, indent=2)) |
| 124 | + |
| 125 | + # end-get_account_summary |
| 126 | + |
| 127 | + except ApiException as e: |
| 128 | + pytest.fail(str(e)) |
| 129 | + |
| 130 | + @needscredentials |
| 131 | + def test_get_account_usage_example(self): |
| 132 | + """ |
| 133 | + get_account_usage request example |
| 134 | + """ |
| 135 | + try: |
| 136 | + global account_id, billing_month |
| 137 | + |
| 138 | + # begin-get_account_usage |
| 139 | + |
| 140 | + account_usage = usage_reports_service.get_account_usage( |
| 141 | + account_id=account_id, |
| 142 | + billingmonth=billing_month |
| 143 | + ).get_result() |
| 144 | + |
| 145 | + print(json.dumps(account_usage, indent=2)) |
| 146 | + |
| 147 | + # end-get_account_usage |
| 148 | + |
| 149 | + except ApiException as e: |
| 150 | + pytest.fail(str(e)) |
| 151 | + |
| 152 | + @needscredentials |
| 153 | + def test_get_resource_group_usage_example(self): |
| 154 | + """ |
| 155 | + get_resource_group_usage request example |
| 156 | + """ |
| 157 | + try: |
| 158 | + global account_id, resource_group_id, billing_month |
| 159 | + |
| 160 | + # begin-get_resource_group_usage |
| 161 | + |
| 162 | + resource_group_usage = usage_reports_service.get_resource_group_usage( |
| 163 | + account_id=account_id, |
| 164 | + resource_group_id=resource_group_id, |
| 165 | + billingmonth=billing_month |
| 166 | + ).get_result() |
| 167 | + |
| 168 | + print(json.dumps(resource_group_usage, indent=2)) |
| 169 | + |
| 170 | + # end-get_resource_group_usage |
| 171 | + |
| 172 | + except ApiException as e: |
| 173 | + pytest.fail(str(e)) |
| 174 | + |
| 175 | + @needscredentials |
| 176 | + def test_get_org_usage_example(self): |
| 177 | + """ |
| 178 | + get_org_usage request example |
| 179 | + """ |
| 180 | + try: |
| 181 | + global account_id, org_id, billing_month |
| 182 | + |
| 183 | + # begin-get_org_usage |
| 184 | + |
| 185 | + org_usage = usage_reports_service.get_org_usage( |
| 186 | + account_id=account_id, |
| 187 | + organization_id=org_id, |
| 188 | + billingmonth=billing_month |
| 189 | + ).get_result() |
| 190 | + |
| 191 | + print(json.dumps(org_usage, indent=2)) |
| 192 | + |
| 193 | + # end-get_org_usage |
| 194 | + |
| 195 | + except ApiException as e: |
| 196 | + pytest.fail(str(e)) |
| 197 | + |
| 198 | + @needscredentials |
| 199 | + def test_get_resource_usage_account_example(self): |
| 200 | + """ |
| 201 | + get_resource_usage_account request example |
| 202 | + """ |
| 203 | + try: |
| 204 | + global account_id, billing_month |
| 205 | + |
| 206 | + # begin-get_resource_usage_account |
| 207 | + |
| 208 | + instances_usage = usage_reports_service.get_resource_usage_account( |
| 209 | + account_id=account_id, |
| 210 | + billingmonth=billing_month |
| 211 | + ).get_result() |
| 212 | + |
| 213 | + print(json.dumps(instances_usage, indent=2)) |
| 214 | + |
| 215 | + # end-get_resource_usage_account |
| 216 | + |
| 217 | + except ApiException as e: |
| 218 | + pytest.fail(str(e)) |
| 219 | + |
| 220 | + @needscredentials |
| 221 | + def test_get_resource_usage_resource_group_example(self): |
| 222 | + """ |
| 223 | + get_resource_usage_resource_group request example |
| 224 | + """ |
| 225 | + try: |
| 226 | + global account_id, resource_group_id, billing_month |
| 227 | + |
| 228 | + # begin-get_resource_usage_resource_group |
| 229 | + |
| 230 | + instances_usage = usage_reports_service.get_resource_usage_resource_group( |
| 231 | + account_id=account_id, |
| 232 | + resource_group_id=resource_group_id, |
| 233 | + billingmonth=billing_month |
| 234 | + ).get_result() |
| 235 | + |
| 236 | + print(json.dumps(instances_usage, indent=2)) |
| 237 | + |
| 238 | + # end-get_resource_usage_resource_group |
| 239 | + |
| 240 | + except ApiException as e: |
| 241 | + pytest.fail(str(e)) |
| 242 | + |
| 243 | + @needscredentials |
| 244 | + def test_get_resource_usage_org_example(self): |
| 245 | + """ |
| 246 | + get_resource_usage_org request example |
| 247 | + """ |
| 248 | + try: |
| 249 | + global account_id, org_id, billing_month |
| 250 | + |
| 251 | + # begin-get_resource_usage_org |
| 252 | + |
| 253 | + instances_usage = usage_reports_service.get_resource_usage_org( |
| 254 | + account_id=account_id, |
| 255 | + organization_id=org_id, |
| 256 | + billingmonth=billing_month |
| 257 | + ).get_result() |
| 258 | + |
| 259 | + print(json.dumps(instances_usage, indent=2)) |
| 260 | + |
| 261 | + # end-get_resource_usage_org |
| 262 | + |
| 263 | + except ApiException as e: |
| 264 | + pytest.fail(str(e)) |
| 265 | + |
| 266 | +# endregion |
| 267 | +############################################################################## |
| 268 | +# End of Examples for Service: UsageReportsV4 |
| 269 | +############################################################################## |
0 commit comments