|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# (C) Copyright IBM Corp. 2021. |
| 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 ResourceManagerV2 |
| 18 | +""" |
| 19 | + |
| 20 | +import os |
| 21 | +import pytest |
| 22 | +from ibm_cloud_sdk_core import ApiException, read_external_sources |
| 23 | +from ibm_platform_services.resource_manager_v2 import * |
| 24 | + |
| 25 | +# |
| 26 | +# This file provides an example of how to use the Resource Manager service. |
| 27 | +# |
| 28 | +# The following configuration properties are assumed to be defined: |
| 29 | +# RESOURCE_MANAGER_URL=<service base url> |
| 30 | +# RESOURCE_MANAGER_AUTH_TYPE=iam |
| 31 | +# RESOURCE_MANAGER_APIKEY=<IAM apikey> |
| 32 | +# RESOURCE_MANAGER_AUTH_URL=<IAM token service base URL - omit this if using the production environment> |
| 33 | +# |
| 34 | +# These configuration properties can be exported as environment variables, or stored |
| 35 | +# in a configuration file and then: |
| 36 | +# export IBM_CREDENTIALS_FILE=<name of configuration file> |
| 37 | +# |
| 38 | +config_file = 'resource_manager.env' |
| 39 | + |
| 40 | +resource_manager_service = None |
| 41 | +delete_resource_manager_service = None |
| 42 | + |
| 43 | +config = None |
| 44 | + |
| 45 | +example_quota_id = None |
| 46 | +example_user_account_id = None |
| 47 | + |
| 48 | +resource_group_id = None |
| 49 | + |
| 50 | +############################################################################## |
| 51 | +# Start of Examples for Service: ResourceManagerV2 |
| 52 | +############################################################################## |
| 53 | +# region |
| 54 | +class TestResourceManagerV2Examples(): |
| 55 | + """ |
| 56 | + Example Test Class for ResourceManagerV2 |
| 57 | + """ |
| 58 | + |
| 59 | + @classmethod |
| 60 | + def setup_class(cls): |
| 61 | + global resource_manager_service |
| 62 | + global delete_resource_manager_service |
| 63 | + |
| 64 | + if os.path.exists(config_file): |
| 65 | + os.environ['IBM_CREDENTIALS_FILE'] = config_file |
| 66 | + |
| 67 | + # begin-common |
| 68 | + |
| 69 | + resource_manager_service = ResourceManagerV2.new_instance( |
| 70 | + service_name=ResourceManagerV2.DEFAULT_SERVICE_NAME, |
| 71 | + ) |
| 72 | + |
| 73 | + delete_resource_manager_service = ResourceManagerV2.new_instance( |
| 74 | + service_name='ALT_RESOURCE_MANAGER', |
| 75 | + ) |
| 76 | + |
| 77 | + # end-common |
| 78 | + assert resource_manager_service is not None |
| 79 | + assert delete_resource_manager_service is not None |
| 80 | + |
| 81 | + # Load the configuration |
| 82 | + global config |
| 83 | + config = read_external_sources(ResourceManagerV2.DEFAULT_SERVICE_NAME) |
| 84 | + |
| 85 | + global example_quota_id |
| 86 | + example_quota_id = config['QUOTA_ID'] |
| 87 | + |
| 88 | + global example_user_account_id |
| 89 | + example_user_account_id = config['USER_ACCOUNT_ID'] |
| 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_create_resource_group_example(self): |
| 99 | + """ |
| 100 | + create_resource_group request example |
| 101 | + """ |
| 102 | + assert example_user_account_id is not None |
| 103 | + |
| 104 | + try: |
| 105 | + # begin-create_resource_group |
| 106 | + |
| 107 | + res_create_resource_group = resource_manager_service.create_resource_group( |
| 108 | + account_id=example_user_account_id, |
| 109 | + name='ExampleGroup', |
| 110 | + ).get_result() |
| 111 | + |
| 112 | + print(json.dumps(res_create_resource_group, indent=2)) |
| 113 | + |
| 114 | + # end-create_resource_group |
| 115 | + |
| 116 | + global resource_group_id |
| 117 | + resource_group_id = res_create_resource_group.get('id') |
| 118 | + |
| 119 | + except ApiException as e: |
| 120 | + pytest.fail(str(e)) |
| 121 | + |
| 122 | + @needscredentials |
| 123 | + def test_get_resource_group_example(self): |
| 124 | + """ |
| 125 | + get_resource_group request example |
| 126 | + """ |
| 127 | + assert resource_group_id is not None |
| 128 | + |
| 129 | + try: |
| 130 | + # begin-get_resource_group |
| 131 | + |
| 132 | + resource_group = resource_manager_service.get_resource_group( |
| 133 | + id=resource_group_id, |
| 134 | + ).get_result() |
| 135 | + |
| 136 | + print(json.dumps(resource_group, indent=2)) |
| 137 | + |
| 138 | + # end-get_resource_group |
| 139 | + |
| 140 | + except ApiException as e: |
| 141 | + pytest.fail(str(e)) |
| 142 | + |
| 143 | + @needscredentials |
| 144 | + def test_update_resource_group_example(self): |
| 145 | + """ |
| 146 | + update_resource_group request example |
| 147 | + """ |
| 148 | + assert resource_group_id is not None |
| 149 | + |
| 150 | + try: |
| 151 | + # begin-update_resource_group |
| 152 | + |
| 153 | + resource_group = resource_manager_service.update_resource_group( |
| 154 | + id=resource_group_id, |
| 155 | + name='RenamedExampleGroup', |
| 156 | + state='ACTIVE', |
| 157 | + ).get_result() |
| 158 | + |
| 159 | + print(json.dumps(resource_group, indent=2)) |
| 160 | + |
| 161 | + # end-update_resource_group |
| 162 | + |
| 163 | + except ApiException as e: |
| 164 | + pytest.fail(str(e)) |
| 165 | + |
| 166 | + @needscredentials |
| 167 | + def test_list_resource_groups_example(self): |
| 168 | + """ |
| 169 | + list_resource_groups request example |
| 170 | + """ |
| 171 | + assert example_user_account_id is not None |
| 172 | + |
| 173 | + try: |
| 174 | + # begin-list_resource_groups |
| 175 | + |
| 176 | + resource_group_list = resource_manager_service.list_resource_groups( |
| 177 | + account_id=example_user_account_id, |
| 178 | + include_deleted=True, |
| 179 | + ).get_result() |
| 180 | + |
| 181 | + print(json.dumps(resource_group_list, indent=2)) |
| 182 | + |
| 183 | + # end-list_resource_groups |
| 184 | + |
| 185 | + except ApiException as e: |
| 186 | + pytest.fail(str(e)) |
| 187 | + |
| 188 | + @needscredentials |
| 189 | + def test_delete_resource_group_example(self): |
| 190 | + """ |
| 191 | + delete_resource_group request example |
| 192 | + """ |
| 193 | + assert resource_group_id is not None |
| 194 | + |
| 195 | + try: |
| 196 | + # begin-delete_resource_group |
| 197 | + |
| 198 | + response = delete_resource_manager_service.delete_resource_group( |
| 199 | + id=resource_group_id, |
| 200 | + ).get_result() |
| 201 | + |
| 202 | + print(json.dumps(response, indent=2)) |
| 203 | + |
| 204 | + # end-delete_resource_group |
| 205 | + |
| 206 | + except ApiException as e: |
| 207 | + pytest.fail(str(e)) |
| 208 | + |
| 209 | + @needscredentials |
| 210 | + def test_get_quota_definition_example(self): |
| 211 | + """ |
| 212 | + get_quota_definition request example |
| 213 | + """ |
| 214 | + assert example_quota_id is not None |
| 215 | + |
| 216 | + try: |
| 217 | + # begin-get_quota_definition |
| 218 | + |
| 219 | + quota_definition = resource_manager_service.get_quota_definition( |
| 220 | + id=example_quota_id, |
| 221 | + ).get_result() |
| 222 | + |
| 223 | + print(json.dumps(quota_definition, indent=2)) |
| 224 | + |
| 225 | + # end-get_quota_definition |
| 226 | + |
| 227 | + except ApiException as e: |
| 228 | + pytest.fail(str(e)) |
| 229 | + |
| 230 | + @needscredentials |
| 231 | + def test_list_quota_definitions_example(self): |
| 232 | + """ |
| 233 | + list_quota_definitions request example |
| 234 | + """ |
| 235 | + try: |
| 236 | + # begin-list_quota_definitions |
| 237 | + |
| 238 | + quota_definition_list = resource_manager_service.list_quota_definitions().get_result() |
| 239 | + |
| 240 | + print(json.dumps(quota_definition_list, indent=2)) |
| 241 | + |
| 242 | + # end-list_quota_definitions |
| 243 | + |
| 244 | + except ApiException as e: |
| 245 | + pytest.fail(str(e)) |
| 246 | + |
| 247 | + |
| 248 | +# endregion |
| 249 | +############################################################################## |
| 250 | +# End of Examples for Service: ResourceManagerV2 |
| 251 | +############################################################################## |
0 commit comments