|
| 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 PostureManagementV1 |
| 18 | +""" |
| 19 | + |
| 20 | +import os |
| 21 | +import pytest |
| 22 | +from ibm_cloud_sdk_core import ApiException, read_external_sources |
| 23 | +from ibm_platform_services.posture_management_v1 import * |
| 24 | + |
| 25 | +# |
| 26 | +# This file provides an example of how to use the Posture Management service. |
| 27 | +# |
| 28 | +# The following configuration properties are assumed to be defined: |
| 29 | +# POSTURE_MANAGEMENT_URL=<service base url> |
| 30 | +# POSTURE_MANAGEMENT_AUTH_TYPE=iam |
| 31 | +# POSTURE_MANAGEMENT_APIKEY=<IAM apikey> |
| 32 | +# POSTURE_MANAGEMENT_AUTH_URL=<IAM token service base URL - omit this if using the production environment> |
| 33 | +# POSTURE_MANAGEMENT_ACCOUNT_ID=<IBM CLOUD ACCOUNT ID> |
| 34 | +# POSTURE_MANAGEMENT_SCOPES_NAME=<The name of the scope> |
| 35 | +# POSTURE_MANAGEMENT_PROFILE_NAME=<The name of profile - CIS IBM Foundations Benchmark 1.0.0> |
| 36 | +# |
| 37 | +# These configuration properties can be exported as environment variables, or stored |
| 38 | +# in a configuration file and then: |
| 39 | +# export IBM_CREDENTIALS_FILE=<name of configuration file> |
| 40 | +# |
| 41 | +config_file = 'posture_management.env' |
| 42 | + |
| 43 | +posture_management_service = None |
| 44 | + |
| 45 | +config = None |
| 46 | + |
| 47 | +account_id = None |
| 48 | +profile_name = None |
| 49 | +scopes_name = None |
| 50 | + |
| 51 | +profile_id = None |
| 52 | +scope_id = None |
| 53 | +group_profile_id = 0 |
| 54 | + |
| 55 | + |
| 56 | +############################################################################## |
| 57 | +# Start of Examples for Service: PostureManagementV1 |
| 58 | +############################################################################## |
| 59 | +# region |
| 60 | +class TestPostureManagementV1Examples(): |
| 61 | + """ |
| 62 | + Example Test Class for PostureManagementV1 |
| 63 | + """ |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def setup_class(cls): |
| 67 | + global posture_management_service |
| 68 | + if os.path.exists(config_file): |
| 69 | + os.environ['IBM_CREDENTIALS_FILE'] = config_file |
| 70 | + |
| 71 | + # begin-common |
| 72 | + |
| 73 | + posture_management_service = PostureManagementV1.new_instance() |
| 74 | + |
| 75 | + # end-common |
| 76 | + assert posture_management_service is not None |
| 77 | + |
| 78 | + # Load the configuration |
| 79 | + global config |
| 80 | + config = read_external_sources(PostureManagementV1.DEFAULT_SERVICE_NAME) |
| 81 | + |
| 82 | + global account_id |
| 83 | + account_id = config['ACCOUNT_ID'] |
| 84 | + |
| 85 | + global profile_name |
| 86 | + profile_name = config['PROFILE_NAME'] |
| 87 | + |
| 88 | + global scopes_name |
| 89 | + scopes_name = config['SCOPES_NAME'] |
| 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 | + |
| 98 | + @needscredentials |
| 99 | + def test_list_profiles_example(self): |
| 100 | + """ |
| 101 | + list_profiles request example |
| 102 | + """ |
| 103 | + try: |
| 104 | + # begin-list_profiles |
| 105 | + |
| 106 | + profiles_list = posture_management_service.list_profiles( |
| 107 | + account_id=account_id, |
| 108 | + name=profile_name, |
| 109 | + ).get_result() |
| 110 | + |
| 111 | + print('\nlist_profiles() result:\n' + json.dumps(profiles_list, indent=2)) |
| 112 | + |
| 113 | + # end-list_profile |
| 114 | + |
| 115 | + global profile_id |
| 116 | + profile_id = profiles_list['profiles'][0]['profile_id'] |
| 117 | + |
| 118 | + except ApiException as e: |
| 119 | + pytest.fail(str(e)) |
| 120 | + |
| 121 | + @needscredentials |
| 122 | + def test_list_scopes_example(self): |
| 123 | + """ |
| 124 | + list_scopes request example |
| 125 | + """ |
| 126 | + try: |
| 127 | + # begin-list_scopes |
| 128 | + |
| 129 | + scopes_list = posture_management_service.list_scopes( |
| 130 | + account_id=account_id, |
| 131 | + name=scopes_name, |
| 132 | + ).get_result() |
| 133 | + |
| 134 | + print('\nlist_scopes() result:\n' + json.dumps(scopes_list, indent=2)) |
| 135 | + |
| 136 | + # end-list_scopes |
| 137 | + |
| 138 | + global scope_id |
| 139 | + scope_id = scopes_list['scopes'][0]['scope_id'] |
| 140 | + |
| 141 | + except ApiException as e: |
| 142 | + pytest.fail(str(e)) |
| 143 | + |
| 144 | + @needscredentials |
| 145 | + def test_create_validation_example(self): |
| 146 | + """ |
| 147 | + create_validation request example |
| 148 | + """ |
| 149 | + try: |
| 150 | + # begin-create_validation |
| 151 | + |
| 152 | + result = posture_management_service.create_validation( |
| 153 | + account_id=account_id, |
| 154 | + scope_id=scope_id, |
| 155 | + profile_id=profile_id, |
| 156 | + group_profile_id=group_profile_id, |
| 157 | + ).get_result() |
| 158 | + |
| 159 | + print('\ncreate_validation() result:\n' + json.dumps(result, indent=2)) |
| 160 | + |
| 161 | + # end-create_validation |
| 162 | + |
| 163 | + except ApiException as e: |
| 164 | + pytest.fail(str(e)) |
| 165 | +# endregion |
| 166 | +############################################################################## |
| 167 | +# End of Examples for Service: PostureManagementV1 |
| 168 | +############################################################################## |
0 commit comments