|
| 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 ConfigurationGovernanceV1 |
| 18 | +""" |
| 19 | + |
| 20 | +import os |
| 21 | +import pytest |
| 22 | +from ibm_cloud_sdk_core import ApiException, read_external_sources |
| 23 | +from ibm_platform_services.configuration_governance_v1 import * |
| 24 | + |
| 25 | +# Config file name |
| 26 | +config_file = 'configuration_governance.env' |
| 27 | + |
| 28 | +configuration_governance_service = None |
| 29 | + |
| 30 | +config = None |
| 31 | + |
| 32 | +# Variables to hold link values |
| 33 | +attachment_etag_link = None |
| 34 | +attachment_id_link = None |
| 35 | +rule_etag_link = None |
| 36 | +rule_id_link = None |
| 37 | + |
| 38 | +# Additional configuration settings |
| 39 | +test_label = 'PythonSDKExamples' |
| 40 | +account_id = None |
| 41 | +service_name = None |
| 42 | +enterprise_scope_id = None |
| 43 | +subacct_scope_id = None |
| 44 | + |
| 45 | +############################################################################## |
| 46 | +# Start of Examples for Service: ConfigurationGovernanceV1 |
| 47 | +############################################################################## |
| 48 | +# region |
| 49 | +class TestConfigurationGovernanceV1Examples(): |
| 50 | + """ |
| 51 | + Example Test Class for ConfigurationGovernanceV1 |
| 52 | + """ |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def setup_class(cls): |
| 56 | + global configuration_governance_service |
| 57 | + if os.path.exists(config_file): |
| 58 | + os.environ['IBM_CREDENTIALS_FILE'] = config_file |
| 59 | + |
| 60 | + # begin-common |
| 61 | + |
| 62 | + configuration_governance_service = ConfigurationGovernanceV1.new_instance( |
| 63 | + ) |
| 64 | + |
| 65 | + # end-common |
| 66 | + assert configuration_governance_service is not None |
| 67 | + |
| 68 | + # Load the configuration |
| 69 | + global config, account_id, service_name, enterprise_scope_id, subacct_scope_id |
| 70 | + config = read_external_sources(ConfigurationGovernanceV1.DEFAULT_SERVICE_NAME) |
| 71 | + |
| 72 | + account_id = config['ACCOUNT_ID'] |
| 73 | + service_name = config['TEST_SERVICE_NAME'] |
| 74 | + enterprise_scope_id = config['ENTERPRISE_SCOPE_ID'] |
| 75 | + subacct_scope_id = config['SUBACCT_SCOPE_ID'] |
| 76 | + |
| 77 | + print('Setup complete.') |
| 78 | + |
| 79 | + needscredentials = pytest.mark.skipif( |
| 80 | + not os.path.exists(config_file), reason="External configuration not available, skipping..." |
| 81 | + ) |
| 82 | + |
| 83 | + @needscredentials |
| 84 | + def test_create_rules_example(self): |
| 85 | + """ |
| 86 | + create_rules request example |
| 87 | + """ |
| 88 | + try: |
| 89 | + # begin-create_rules |
| 90 | + |
| 91 | + target_resource_model = { |
| 92 | + 'service_name': service_name, |
| 93 | + 'resource_kind': 'service' |
| 94 | + } |
| 95 | + |
| 96 | + rule_required_config_model = { |
| 97 | + 'description': 'Public access check', |
| 98 | + 'property': 'public_access_enabled', |
| 99 | + 'operator': 'is_true' |
| 100 | + } |
| 101 | + |
| 102 | + enforcement_action_model = { |
| 103 | + 'action': 'disallow' |
| 104 | + } |
| 105 | + |
| 106 | + rule_request_model = { |
| 107 | + 'account_id': account_id, |
| 108 | + 'name': 'Disable public access', |
| 109 | + 'description': 'Ensure that public access to account resources is disabled.', |
| 110 | + 'target': {'service_name':service_name,'resource_kind':'service'}, |
| 111 | + 'required_config': {'description':'Public access check','and':[{'property':'public_access_enabled','operator':'is_false'}]}, |
| 112 | + 'enforcement_actions': [enforcement_action_model], |
| 113 | + 'labels': [test_label] |
| 114 | + } |
| 115 | + |
| 116 | + create_rule_request_model = { |
| 117 | + 'request_id': '3cebc877-58e7-44a5-a292-32114fa73558', |
| 118 | + 'rule': {'account_id':account_id,'name':'Disable public access','description':'Ensure that public access to account resources is disabled.','labels':[test_label],'target':{'service_name':service_name,'resource_kind':'service'},'required_config':{'description':'Public access check','and':[{'property':'public_access_enabled','operator':'is_false'}]},'enforcement_actions':[{'action':'disallow'},{'action':'audit_log'}]} |
| 119 | + } |
| 120 | + |
| 121 | + create_rules_response = configuration_governance_service.create_rules( |
| 122 | + rules=[create_rule_request_model] |
| 123 | + ).get_result() |
| 124 | + |
| 125 | + print(json.dumps(create_rules_response, indent=2)) |
| 126 | + |
| 127 | + # end-create_rules |
| 128 | + |
| 129 | + global rule_id_link |
| 130 | + rule_id_link = create_rules_response['rules'][0]['rule']['rule_id']; |
| 131 | + except ApiException as e: |
| 132 | + pytest.fail(str(e)) |
| 133 | + |
| 134 | + @needscredentials |
| 135 | + def test_create_attachments_example(self): |
| 136 | + """ |
| 137 | + create_attachments request example |
| 138 | + """ |
| 139 | + try: |
| 140 | + # begin-create_attachments |
| 141 | + |
| 142 | + rule_scope_model = { |
| 143 | + 'note': 'My enterprise', |
| 144 | + 'scope_id': enterprise_scope_id, |
| 145 | + 'scope_type': 'enterprise' |
| 146 | + } |
| 147 | + |
| 148 | + attachment_request_model = { |
| 149 | + 'account_id': account_id, |
| 150 | + 'included_scope': {'note':'My enterprise','scope_id':enterprise_scope_id,'scope_type':'enterprise'}, |
| 151 | + 'excluded_scopes': [rule_scope_model] |
| 152 | + } |
| 153 | + |
| 154 | + create_attachments_response = configuration_governance_service.create_attachments( |
| 155 | + rule_id=rule_id_link, |
| 156 | + attachments=[attachment_request_model] |
| 157 | + ).get_result() |
| 158 | + |
| 159 | + print(json.dumps(create_attachments_response, indent=2)) |
| 160 | + |
| 161 | + # end-create_attachments |
| 162 | + |
| 163 | + global attachment_id_link |
| 164 | + attachment_id_link = create_attachments_response['attachments'][0]['attachment_id']; |
| 165 | + except ApiException as e: |
| 166 | + pytest.fail(str(e)) |
| 167 | + |
| 168 | + @needscredentials |
| 169 | + def test_get_attachment_example(self): |
| 170 | + """ |
| 171 | + get_attachment request example |
| 172 | + """ |
| 173 | + try: |
| 174 | + # begin-get_attachment |
| 175 | + |
| 176 | + attachment = configuration_governance_service.get_attachment( |
| 177 | + rule_id=rule_id_link, |
| 178 | + attachment_id=attachment_id_link |
| 179 | + ).get_result() |
| 180 | + |
| 181 | + print(json.dumps(attachment, indent=2)) |
| 182 | + |
| 183 | + # end-get_attachment |
| 184 | + |
| 185 | + global attachment_etag_link |
| 186 | + attachment_etag_link = attachment['']; |
| 187 | + except ApiException as e: |
| 188 | + pytest.fail(str(e)) |
| 189 | + |
| 190 | + @needscredentials |
| 191 | + def test_get_rule_example(self): |
| 192 | + """ |
| 193 | + get_rule request example |
| 194 | + """ |
| 195 | + try: |
| 196 | + # begin-get_rule |
| 197 | + |
| 198 | + rule = configuration_governance_service.get_rule( |
| 199 | + rule_id=rule_id_link |
| 200 | + ).get_result() |
| 201 | + |
| 202 | + print(json.dumps(rule, indent=2)) |
| 203 | + |
| 204 | + # end-get_rule |
| 205 | + |
| 206 | + global rule_etag_link |
| 207 | + rule_etag_link = rule['']; |
| 208 | + except ApiException as e: |
| 209 | + pytest.fail(str(e)) |
| 210 | + |
| 211 | + @needscredentials |
| 212 | + def test_list_rules_example(self): |
| 213 | + """ |
| 214 | + list_rules request example |
| 215 | + """ |
| 216 | + try: |
| 217 | + # begin-list_rules |
| 218 | + |
| 219 | + rule_list = configuration_governance_service.list_rules( |
| 220 | + account_id=account_id |
| 221 | + ).get_result() |
| 222 | + |
| 223 | + print(json.dumps(rule_list, indent=2)) |
| 224 | + |
| 225 | + # end-list_rules |
| 226 | + |
| 227 | + except ApiException as e: |
| 228 | + pytest.fail(str(e)) |
| 229 | + |
| 230 | + @needscredentials |
| 231 | + def test_update_rule_example(self): |
| 232 | + """ |
| 233 | + update_rule request example |
| 234 | + """ |
| 235 | + try: |
| 236 | + # begin-update_rule |
| 237 | + |
| 238 | + rule_target_attribute_model = { |
| 239 | + 'name': 'testString', |
| 240 | + 'operator': 'string_equals' |
| 241 | + } |
| 242 | + |
| 243 | + target_resource_model = { |
| 244 | + 'service_name': service_name, |
| 245 | + 'resource_kind': 'service', |
| 246 | + 'additional_target_attributes': [rule_target_attribute_model] |
| 247 | + } |
| 248 | + |
| 249 | + rule_required_config_model = { |
| 250 | + 'property': 'public_access_enabled', |
| 251 | + 'operator': 'is_false' |
| 252 | + } |
| 253 | + |
| 254 | + enforcement_action_model = { |
| 255 | + 'action': 'audit_log' |
| 256 | + } |
| 257 | + |
| 258 | + rule = configuration_governance_service.update_rule( |
| 259 | + rule_id=rule_id_link, |
| 260 | + if_match='testString', |
| 261 | + name='Disable public access', |
| 262 | + description='Ensure that public access to account resources is disabled.', |
| 263 | + target={'service_name':service_name,'resource_kind':'service','additional_target_attributes':[]}, |
| 264 | + required_config={'property':'public_access_enabled','operator':'is_false'}, |
| 265 | + enforcement_actions=[enforcement_action_model], |
| 266 | + account_id=account_id, |
| 267 | + version='1.0.0', |
| 268 | + rule_type='user_defined', |
| 269 | + labels=['testString'] |
| 270 | + ).get_result() |
| 271 | + |
| 272 | + print(json.dumps(rule, indent=2)) |
| 273 | + |
| 274 | + # end-update_rule |
| 275 | + |
| 276 | + except ApiException as e: |
| 277 | + pytest.fail(str(e)) |
| 278 | + |
| 279 | + @needscredentials |
| 280 | + def test_list_attachments_example(self): |
| 281 | + """ |
| 282 | + list_attachments request example |
| 283 | + """ |
| 284 | + try: |
| 285 | + # begin-list_attachments |
| 286 | + |
| 287 | + attachment_list = configuration_governance_service.list_attachments( |
| 288 | + rule_id=rule_id_link |
| 289 | + ).get_result() |
| 290 | + |
| 291 | + print(json.dumps(attachment_list, indent=2)) |
| 292 | + |
| 293 | + # end-list_attachments |
| 294 | + |
| 295 | + except ApiException as e: |
| 296 | + pytest.fail(str(e)) |
| 297 | + |
| 298 | + @needscredentials |
| 299 | + def test_update_attachment_example(self): |
| 300 | + """ |
| 301 | + update_attachment request example |
| 302 | + """ |
| 303 | + try: |
| 304 | + # begin-update_attachment |
| 305 | + |
| 306 | + rule_scope_model = { |
| 307 | + 'note': 'My enterprise', |
| 308 | + 'scope_id': enterprise_scope_id, |
| 309 | + 'scope_type': 'enterprise' |
| 310 | + } |
| 311 | + |
| 312 | + attachment = configuration_governance_service.update_attachment( |
| 313 | + rule_id=rule_id_link, |
| 314 | + attachment_id=attachment_id_link, |
| 315 | + if_match='testString', |
| 316 | + account_id=account_id, |
| 317 | + included_scope={'note':'My enterprise','scope_id':enterprise_scope_id,'scope_type':'enterprise'}, |
| 318 | + excluded_scopes=[rule_scope_model] |
| 319 | + ).get_result() |
| 320 | + |
| 321 | + print(json.dumps(attachment, indent=2)) |
| 322 | + |
| 323 | + # end-update_attachment |
| 324 | + |
| 325 | + except ApiException as e: |
| 326 | + pytest.fail(str(e)) |
| 327 | + |
| 328 | + @needscredentials |
| 329 | + def test_delete_attachment_example(self): |
| 330 | + """ |
| 331 | + delete_attachment request example |
| 332 | + """ |
| 333 | + try: |
| 334 | + # begin-delete_attachment |
| 335 | + |
| 336 | + response = configuration_governance_service.delete_attachment( |
| 337 | + rule_id=rule_id_link, |
| 338 | + attachment_id=attachment_id_link |
| 339 | + ).get_result() |
| 340 | + |
| 341 | + print(json.dumps(response, indent=2)) |
| 342 | + |
| 343 | + # end-delete_attachment |
| 344 | + |
| 345 | + except ApiException as e: |
| 346 | + pytest.fail(str(e)) |
| 347 | + |
| 348 | + @needscredentials |
| 349 | + def test_delete_rule_example(self): |
| 350 | + """ |
| 351 | + delete_rule request example |
| 352 | + """ |
| 353 | + try: |
| 354 | + # begin-delete_rule |
| 355 | + |
| 356 | + response = configuration_governance_service.delete_rule( |
| 357 | + rule_id=rule_id_link |
| 358 | + ).get_result() |
| 359 | + |
| 360 | + print(json.dumps(response, indent=2)) |
| 361 | + |
| 362 | + # end-delete_rule |
| 363 | + |
| 364 | + except ApiException as e: |
| 365 | + pytest.fail(str(e)) |
| 366 | + |
| 367 | +# endregion |
| 368 | +############################################################################## |
| 369 | +# End of Examples for Service: ConfigurationGovernanceV1 |
| 370 | +############################################################################## |
0 commit comments