|
| 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 GlobalTaggingV1 |
| 18 | +""" |
| 19 | + |
| 20 | +import os |
| 21 | +import pytest |
| 22 | +from ibm_cloud_sdk_core import ApiException, read_external_sources |
| 23 | +from ibm_platform_services.global_tagging_v1 import * |
| 24 | + |
| 25 | +# This file provides an example of how to use the Global Tagging service. |
| 26 | +# |
| 27 | +# The following configuration properties are assumed to be defined in the external configuration file: |
| 28 | +# GLOBAL_TAGGING_URL=<service url> |
| 29 | +# GLOBAL_TAGGING_AUTHTYPE=iam |
| 30 | +# GLOBAL_TAGGING_APIKEY=<IAM api key> |
| 31 | +# GLOBAL_TAGGING_AUTH_URL=<IAM token service URL - omit this if using the production environment> |
| 32 | +# GLOBAL_TAGGING_RESOURCE_CRN=<the crn of the resource to be used in the examples> |
| 33 | + |
| 34 | +# Config file name |
| 35 | +config_file = 'global_tagging.env' |
| 36 | + |
| 37 | +global_tagging_service = None |
| 38 | + |
| 39 | +config = None |
| 40 | +resource_crn = None |
| 41 | + |
| 42 | + |
| 43 | +############################################################################## |
| 44 | +# Start of Examples for Service: GlobalTaggingV1 |
| 45 | +############################################################################## |
| 46 | +# region |
| 47 | +class TestGlobalTaggingV1Examples(): |
| 48 | + """ |
| 49 | + Example Test Class for GlobalTaggingV1 |
| 50 | + """ |
| 51 | + |
| 52 | + @classmethod |
| 53 | + def setup_class(cls): |
| 54 | + global global_tagging_service |
| 55 | + if os.path.exists(config_file): |
| 56 | + os.environ['IBM_CREDENTIALS_FILE'] = config_file |
| 57 | + |
| 58 | + # begin-common |
| 59 | + |
| 60 | + global_tagging_service = GlobalTaggingV1.new_instance() |
| 61 | + |
| 62 | + # end-common |
| 63 | + assert global_tagging_service is not None |
| 64 | + |
| 65 | + # Load the configuration |
| 66 | + global config |
| 67 | + config = read_external_sources( |
| 68 | + GlobalTaggingV1.DEFAULT_SERVICE_NAME) |
| 69 | + |
| 70 | + global resource_crn |
| 71 | + resource_crn = config.get("RESOURCE_CRN") |
| 72 | + |
| 73 | + print('Setup complete.') |
| 74 | + |
| 75 | + needscredentials = pytest.mark.skipif( |
| 76 | + not os.path.exists(config_file), reason="External configuration not available, skipping..." |
| 77 | + ) |
| 78 | + |
| 79 | + @needscredentials |
| 80 | + def test_list_tags_example(self): |
| 81 | + """ |
| 82 | + list_tags request example |
| 83 | + """ |
| 84 | + try: |
| 85 | + # begin-list_tags |
| 86 | + |
| 87 | + tag_list = global_tagging_service.list_tags( |
| 88 | + attached_only=True).get_result() |
| 89 | + |
| 90 | + print(json.dumps(tag_list, indent=2)) |
| 91 | + |
| 92 | + # end-list_tags |
| 93 | + |
| 94 | + except ApiException as e: |
| 95 | + pytest.fail(str(e)) |
| 96 | + |
| 97 | + @needscredentials |
| 98 | + def test_attach_tag_example(self): |
| 99 | + """ |
| 100 | + attach_tag request example |
| 101 | + """ |
| 102 | + try: |
| 103 | + # begin-attach_tag |
| 104 | + |
| 105 | + resource_model = { |
| 106 | + 'resource_id': resource_crn |
| 107 | + } |
| 108 | + |
| 109 | + tag_results = global_tagging_service.attach_tag( |
| 110 | + resources=[resource_model], |
| 111 | + tag_names=['tag_test_1', 'tag_test_2'] |
| 112 | + ).get_result() |
| 113 | + |
| 114 | + print(json.dumps(tag_results, indent=2)) |
| 115 | + |
| 116 | + # end-attach_tag |
| 117 | + |
| 118 | + except ApiException as e: |
| 119 | + pytest.fail(str(e)) |
| 120 | + |
| 121 | + @needscredentials |
| 122 | + def test_detach_tag_example(self): |
| 123 | + """ |
| 124 | + detach_tag request example |
| 125 | + """ |
| 126 | + try: |
| 127 | + # begin-detach_tag |
| 128 | + |
| 129 | + resource_model = { |
| 130 | + 'resource_id': resource_crn |
| 131 | + } |
| 132 | + |
| 133 | + tag_results = global_tagging_service.detach_tag( |
| 134 | + resources=[resource_model], |
| 135 | + tag_names=['tag_test_1', 'tag_test_2'] |
| 136 | + ).get_result() |
| 137 | + |
| 138 | + print(json.dumps(tag_results, indent=2)) |
| 139 | + |
| 140 | + # end-detach_tag |
| 141 | + |
| 142 | + except ApiException as e: |
| 143 | + pytest.fail(str(e)) |
| 144 | + |
| 145 | + @ needscredentials |
| 146 | + def test_delete_tag_example(self): |
| 147 | + """ |
| 148 | + delete_tag request example |
| 149 | + """ |
| 150 | + try: |
| 151 | + # begin-delete_tag |
| 152 | + |
| 153 | + delete_tag_results = global_tagging_service.delete_tag( |
| 154 | + tag_name='tag_test_1' |
| 155 | + ).get_result() |
| 156 | + |
| 157 | + print(json.dumps(delete_tag_results, indent=2)) |
| 158 | + |
| 159 | + # end-delete_tag |
| 160 | + |
| 161 | + except ApiException as e: |
| 162 | + pytest.fail(str(e)) |
| 163 | + |
| 164 | + @ needscredentials |
| 165 | + def test_delete_tag_all_example(self): |
| 166 | + """ |
| 167 | + delete_tag_all request example |
| 168 | + """ |
| 169 | + try: |
| 170 | + # begin-delete_tag_all |
| 171 | + |
| 172 | + delete_tags_result = global_tagging_service.delete_tag_all().get_result() |
| 173 | + |
| 174 | + print(json.dumps(delete_tags_result, indent=2)) |
| 175 | + |
| 176 | + # end-delete_tag_all |
| 177 | + |
| 178 | + except ApiException as e: |
| 179 | + pytest.fail(str(e)) |
| 180 | + |
| 181 | +# endregion |
| 182 | +############################################################################## |
| 183 | +# End of Examples for Service: GlobalTaggingV1 |
| 184 | +############################################################################## |
0 commit comments