|
| 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 | +Examples for UsageMeteringV4 |
| 17 | +""" |
| 18 | + |
| 19 | +import os |
| 20 | +import pytest |
| 21 | +import time |
| 22 | +from ibm_cloud_sdk_core import ApiException |
| 23 | +from ibm_platform_services.usage_metering_v4 import * |
| 24 | + |
| 25 | +# |
| 26 | +# This file provides an example of how to use the Usage Metering service. |
| 27 | +# |
| 28 | +# The following configuration properties are assumed to be defined in the external configuration file: |
| 29 | +# USAGE_METERING_URL=<service url> |
| 30 | +# USAGE_METERING_AUTHTYPE=iam |
| 31 | +# USAGE_METERING_APIKEY=<your iam apikey> |
| 32 | +# USAGE_METERING_AUTH_URL=<IAM token service URL - omit this if using the production environment> |
| 33 | +# |
| 34 | + |
| 35 | +# Config file name |
| 36 | +config_file = 'usage_metering.env' |
| 37 | + |
| 38 | +usage_metering_service = None |
| 39 | + |
| 40 | +config = None |
| 41 | + |
| 42 | + |
| 43 | +############################################################################## |
| 44 | +# Start of Examples for Service: UsageMeteringV4 |
| 45 | +############################################################################## |
| 46 | +# region |
| 47 | +class TestUsageMeteringV4Examples(): |
| 48 | + """ |
| 49 | + Example Test Class for UsageMeteringV4 |
| 50 | + """ |
| 51 | + @classmethod |
| 52 | + def setup_class(cls): |
| 53 | + global usage_metering_service |
| 54 | + if os.path.exists(config_file): |
| 55 | + os.environ['IBM_CREDENTIALS_FILE'] = config_file |
| 56 | + |
| 57 | + # begin-common |
| 58 | + |
| 59 | + usage_metering_service = UsageMeteringV4.new_instance() |
| 60 | + |
| 61 | + # end-common |
| 62 | + assert usage_metering_service is not None |
| 63 | + |
| 64 | + print('Setup complete.') |
| 65 | + |
| 66 | + needscredentials = pytest.mark.skipif( |
| 67 | + not os.path.exists(config_file), |
| 68 | + reason="External configuration not available, skipping...") |
| 69 | + |
| 70 | + @needscredentials |
| 71 | + def test_report_resource_usage_example(self): |
| 72 | + """ |
| 73 | + report_resource_usage request example |
| 74 | + """ |
| 75 | + try: |
| 76 | + # Get current time in ms since epoch. |
| 77 | + start_time = int(time.time() * 1000) |
| 78 | + end_time = start_time |
| 79 | + |
| 80 | + resource_id = 'cloudant' |
| 81 | + resource_instance_id = 'crn:v1:staging:public:cloudantnosqldb:us-south:a/f5086e3df886495991303628d21da513:3aafbbee-88e2-4d29-b144-9d267d97064c::' |
| 82 | + plan_id = 'cloudant-standard' |
| 83 | + region = 'us-south' |
| 84 | + |
| 85 | + # begin-report_resource_usage |
| 86 | + |
| 87 | + # Report usage for a mythical resource. |
| 88 | + # Use zero for quantities since this is only an example. |
| 89 | + measures = [ |
| 90 | + { |
| 91 | + 'measure': 'LOOKUP', |
| 92 | + 'quantity': 0, |
| 93 | + }, |
| 94 | + { |
| 95 | + 'measure': 'WRITE', |
| 96 | + 'quantity': 0, |
| 97 | + }, |
| 98 | + { |
| 99 | + 'measure': 'QUERY', |
| 100 | + 'quantity': 0, |
| 101 | + }, |
| 102 | + { |
| 103 | + 'measure': 'GIGABYTE', |
| 104 | + 'quantity': 0, |
| 105 | + }, |
| 106 | + ] |
| 107 | + |
| 108 | + resource_instance_usage_model = { |
| 109 | + 'resource_instance_id': resource_instance_id, |
| 110 | + 'plan_id': plan_id, |
| 111 | + 'region': region, |
| 112 | + 'start': start_time, |
| 113 | + 'end': end_time, |
| 114 | + 'measured_usage': measures, |
| 115 | + } |
| 116 | + |
| 117 | + response_accepted = usage_metering_service.report_resource_usage( |
| 118 | + resource_id=resource_id, |
| 119 | + resource_usage=[resource_instance_usage_model]).get_result() |
| 120 | + |
| 121 | + print(json.dumps(response_accepted, indent=2)) |
| 122 | + |
| 123 | + # end-report_resource_usage |
| 124 | + |
| 125 | + except ApiException as e: |
| 126 | + pytest.fail(str(e)) |
| 127 | + |
| 128 | + |
| 129 | +# endregion |
| 130 | +############################################################################## |
| 131 | +# End of Examples for Service: UsageMeteringV4 |
| 132 | +############################################################################## |
0 commit comments