|
| 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 IamPolicyManagementV1 |
| 18 | +""" |
| 19 | + |
| 20 | +import os |
| 21 | +import pytest |
| 22 | +from ibm_cloud_sdk_core import ApiException, read_external_sources |
| 23 | +from ibm_platform_services.iam_policy_management_v1 import * |
| 24 | + |
| 25 | +# |
| 26 | +# This file provides an example of how to use the IAM Policy Management service. |
| 27 | +# |
| 28 | +# The following configuration properties are assumed to be defined: |
| 29 | +# |
| 30 | +# IAM_POLICY_MANAGEMENT_URL=<service url> |
| 31 | +# IAM_POLICY_MANAGEMENT_AUTH_TYPE=iam |
| 32 | +# IAM_POLICY_MANAGEMENT_AUTH_URL=<IAM token service URL - omit this if using the production environment> |
| 33 | +# IAM_POLICY_MANAGEMENT_APIKEY=<YOUR_APIKEY> |
| 34 | +# IAM_POLICY_MANAGEMENT_TEST_ACCOUNT_ID=<YOUR_ACCOUNT_ID> |
| 35 | +# |
| 36 | +# These configuration properties can be exported as environment variables, or stored |
| 37 | +# in a configuration file and then: |
| 38 | +# export IBM_CREDENTIALS_FILE=<name of config file> |
| 39 | +# |
| 40 | +# Config file name |
| 41 | +config_file = 'iam_policy_management.env' |
| 42 | + |
| 43 | +iam_policy_management_service = None |
| 44 | + |
| 45 | +config = None |
| 46 | + |
| 47 | +example_account_id = None |
| 48 | +example_policy_id = None |
| 49 | +example_policy_etag = None |
| 50 | +example_custom_role_id = None |
| 51 | +example_custom_role_etag = None |
| 52 | +example_user_id = "IBMid-user1" |
| 53 | +example_service_name = "iam-groups" |
| 54 | + |
| 55 | +############################################################################## |
| 56 | +# Start of Examples for Service: IamPolicyManagementV1 |
| 57 | +############################################################################## |
| 58 | +# region |
| 59 | + |
| 60 | + |
| 61 | +class TestIamPolicyManagementV1Examples(): |
| 62 | + """ |
| 63 | + Example Test Class for IamPolicyManagementV1 |
| 64 | + """ |
| 65 | + |
| 66 | + @classmethod |
| 67 | + def setup_class(cls): |
| 68 | + global iam_policy_management_service |
| 69 | + if os.path.exists(config_file): |
| 70 | + os.environ['IBM_CREDENTIALS_FILE'] = config_file |
| 71 | + |
| 72 | + # begin-common |
| 73 | + |
| 74 | + iam_policy_management_service = IamPolicyManagementV1.new_instance( |
| 75 | + ) |
| 76 | + |
| 77 | + # end-common |
| 78 | + assert iam_policy_management_service is not None |
| 79 | + |
| 80 | + # Load the configuration |
| 81 | + global config, example_account_id |
| 82 | + config = read_external_sources( |
| 83 | + IamPolicyManagementV1.DEFAULT_SERVICE_NAME) |
| 84 | + example_account_id = config['TEST_ACCOUNT_ID'] |
| 85 | + |
| 86 | + print('Setup complete.') |
| 87 | + |
| 88 | + needscredentials = pytest.mark.skipif( |
| 89 | + not os.path.exists(config_file), reason="External configuration not available, skipping..." |
| 90 | + ) |
| 91 | + |
| 92 | + @needscredentials |
| 93 | + def test_create_policy_example(self): |
| 94 | + """ |
| 95 | + create_policy request example |
| 96 | + """ |
| 97 | + try: |
| 98 | + global example_policy_id |
| 99 | + # begin-create_policy |
| 100 | + |
| 101 | + policy_subjects = PolicySubject( |
| 102 | + attributes=[SubjectAttribute(name='iam_id', value=example_user_id)]) |
| 103 | + policy_roles = PolicyRole( |
| 104 | + role_id='crn:v1:bluemix:public:iam::::role:Viewer') |
| 105 | + account_id_resource_attribute = ResourceAttribute( |
| 106 | + name='accountId', value=example_account_id) |
| 107 | + service_name_resource_attribute = ResourceAttribute( |
| 108 | + name='serviceType', value='service') |
| 109 | + policy_resource_tag = ResourceTag( |
| 110 | + name='project', value='prototype') |
| 111 | + policy_resources = PolicyResource( |
| 112 | + attributes=[account_id_resource_attribute, |
| 113 | + service_name_resource_attribute], |
| 114 | + tags=[policy_resource_tag]) |
| 115 | + |
| 116 | + policy = iam_policy_management_service.create_policy( |
| 117 | + type='access', |
| 118 | + subjects=[policy_subjects], |
| 119 | + roles=[policy_roles], |
| 120 | + resources=[policy_resources] |
| 121 | + ).get_result() |
| 122 | + |
| 123 | + print(json.dumps(policy, indent=2)) |
| 124 | + |
| 125 | + # end-create_policy |
| 126 | + example_policy_id = policy['id'] |
| 127 | + |
| 128 | + except ApiException as e: |
| 129 | + pytest.fail(str(e)) |
| 130 | + |
| 131 | + @needscredentials |
| 132 | + def test_get_policy_example(self): |
| 133 | + """ |
| 134 | + get_policy request example |
| 135 | + """ |
| 136 | + try: |
| 137 | + global example_policy_etag |
| 138 | + # begin-get_policy |
| 139 | + |
| 140 | + response = iam_policy_management_service.get_policy( |
| 141 | + policy_id=example_policy_id |
| 142 | + ) |
| 143 | + policy = response.get_result() |
| 144 | + |
| 145 | + print(json.dumps(policy, indent=2)) |
| 146 | + |
| 147 | + # end-get_policy |
| 148 | + example_policy_etag = response.get_headers().get("Etag") |
| 149 | + |
| 150 | + except ApiException as e: |
| 151 | + pytest.fail(str(e)) |
| 152 | + |
| 153 | + @needscredentials |
| 154 | + def test_update_policy_example(self): |
| 155 | + """ |
| 156 | + update_policy request example |
| 157 | + """ |
| 158 | + try: |
| 159 | + # begin-update_policy |
| 160 | + |
| 161 | + policy_subjects = PolicySubject( |
| 162 | + attributes=[SubjectAttribute(name='iam_id', value=example_user_id)]) |
| 163 | + account_id_resource_attribute = ResourceAttribute( |
| 164 | + name='accountId', value=example_account_id) |
| 165 | + service_name_resource_attribute = ResourceAttribute( |
| 166 | + name='serviceType', value='service') |
| 167 | + policy_resource_tag = ResourceTag( |
| 168 | + name='project', value='prototype') |
| 169 | + policy_resources = PolicyResource( |
| 170 | + attributes=[account_id_resource_attribute, |
| 171 | + service_name_resource_attribute], |
| 172 | + tags=[policy_resource_tag]) |
| 173 | + updated_policy_roles = PolicyRole( |
| 174 | + role_id='crn:v1:bluemix:public:iam::::role:Editor') |
| 175 | + |
| 176 | + policy = iam_policy_management_service.update_policy( |
| 177 | + type='access', |
| 178 | + policy_id=example_policy_id, |
| 179 | + if_match=example_policy_etag, |
| 180 | + subjects=[policy_subjects], |
| 181 | + roles=[updated_policy_roles], |
| 182 | + resources=[policy_resources] |
| 183 | + ).get_result() |
| 184 | + |
| 185 | + print(json.dumps(policy, indent=2)) |
| 186 | + |
| 187 | + # end-update_policy |
| 188 | + |
| 189 | + except ApiException as e: |
| 190 | + pytest.fail(str(e)) |
| 191 | + |
| 192 | + @needscredentials |
| 193 | + def test_list_policies_example(self): |
| 194 | + """ |
| 195 | + list_policies request example |
| 196 | + """ |
| 197 | + try: |
| 198 | + # begin-list_policies |
| 199 | + |
| 200 | + policy_list = iam_policy_management_service.list_policies( |
| 201 | + account_id=example_account_id, iam_id=example_user_id, format='include_last_permit' |
| 202 | + ).get_result() |
| 203 | + |
| 204 | + print(json.dumps(policy_list, indent=2)) |
| 205 | + |
| 206 | + # end-list_policies |
| 207 | + |
| 208 | + except ApiException as e: |
| 209 | + pytest.fail(str(e)) |
| 210 | + |
| 211 | + @needscredentials |
| 212 | + def test_delete_policy_example(self): |
| 213 | + """ |
| 214 | + delete_policy request example |
| 215 | + """ |
| 216 | + try: |
| 217 | + # begin-delete_policy |
| 218 | + |
| 219 | + response = iam_policy_management_service.delete_policy( |
| 220 | + policy_id=example_policy_id |
| 221 | + ).get_result() |
| 222 | + |
| 223 | + print(json.dumps(response, indent=2)) |
| 224 | + |
| 225 | + # end-delete_policy |
| 226 | + |
| 227 | + except ApiException as e: |
| 228 | + pytest.fail(str(e)) |
| 229 | + |
| 230 | + @needscredentials |
| 231 | + def test_create_role_example(self): |
| 232 | + """ |
| 233 | + create_role request example |
| 234 | + """ |
| 235 | + try: |
| 236 | + global example_custom_role_id |
| 237 | + # begin-create_role |
| 238 | + |
| 239 | + custom_role = iam_policy_management_service.create_role( |
| 240 | + display_name='IAM Groups read access', |
| 241 | + actions=['iam-groups.groups.read'], |
| 242 | + name='ExampleRoleIAMGroups', |
| 243 | + account_id=example_account_id, |
| 244 | + service_name=example_service_name |
| 245 | + ).get_result() |
| 246 | + |
| 247 | + print(json.dumps(custom_role, indent=2)) |
| 248 | + |
| 249 | + # end-create_role |
| 250 | + example_custom_role_id = custom_role["id"] |
| 251 | + |
| 252 | + except ApiException as e: |
| 253 | + pytest.fail(str(e)) |
| 254 | + |
| 255 | + @needscredentials |
| 256 | + def test_get_role_example(self): |
| 257 | + """ |
| 258 | + get_role request example |
| 259 | + """ |
| 260 | + try: |
| 261 | + global example_custom_role_etag |
| 262 | + # begin-get_role |
| 263 | + |
| 264 | + response = iam_policy_management_service.get_role( |
| 265 | + role_id=example_custom_role_id |
| 266 | + ) |
| 267 | + custom_role = response.get_result() |
| 268 | + |
| 269 | + print(json.dumps(custom_role, indent=2)) |
| 270 | + |
| 271 | + # end-get_role |
| 272 | + example_custom_role_etag = response.get_headers().get("Etag") |
| 273 | + |
| 274 | + except ApiException as e: |
| 275 | + pytest.fail(str(e)) |
| 276 | + |
| 277 | + @needscredentials |
| 278 | + def test_update_role_example(self): |
| 279 | + """ |
| 280 | + update_role request example |
| 281 | + """ |
| 282 | + try: |
| 283 | + # begin-update_role |
| 284 | + |
| 285 | + updated_role_actions = [ |
| 286 | + 'iam-groups.groups.read', 'iam-groups.groups.list'] |
| 287 | + custom_role = iam_policy_management_service.update_role( |
| 288 | + role_id=example_custom_role_id, |
| 289 | + if_match=example_custom_role_etag, |
| 290 | + actions=updated_role_actions |
| 291 | + ).get_result() |
| 292 | + |
| 293 | + print(json.dumps(custom_role, indent=2)) |
| 294 | + |
| 295 | + # end-update_role |
| 296 | + |
| 297 | + except ApiException as e: |
| 298 | + pytest.fail(str(e)) |
| 299 | + |
| 300 | + @needscredentials |
| 301 | + def test_list_roles_example(self): |
| 302 | + """ |
| 303 | + list_roles request example |
| 304 | + """ |
| 305 | + try: |
| 306 | + # begin-list_roles |
| 307 | + |
| 308 | + role_list = iam_policy_management_service.list_roles( |
| 309 | + account_id=example_account_id |
| 310 | + ).get_result() |
| 311 | + |
| 312 | + print(json.dumps(role_list, indent=2)) |
| 313 | + |
| 314 | + # end-list_roles |
| 315 | + |
| 316 | + except ApiException as e: |
| 317 | + pytest.fail(str(e)) |
| 318 | + |
| 319 | + @needscredentials |
| 320 | + def test_delete_role_example(self): |
| 321 | + """ |
| 322 | + delete_role request example |
| 323 | + """ |
| 324 | + try: |
| 325 | + # begin-delete_role |
| 326 | + |
| 327 | + response = iam_policy_management_service.delete_role( |
| 328 | + role_id=example_custom_role_id |
| 329 | + ).get_result() |
| 330 | + |
| 331 | + print(json.dumps(response, indent=2)) |
| 332 | + |
| 333 | + # end-delete_role |
| 334 | + |
| 335 | + except ApiException as e: |
| 336 | + pytest.fail(str(e)) |
| 337 | + |
| 338 | +# endregion |
| 339 | +############################################################################## |
| 340 | +# End of Examples for Service: IamPolicyManagementV1 |
| 341 | +############################################################################## |
0 commit comments