|
| 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 CaseManagementV1 |
| 18 | +""" |
| 19 | + |
| 20 | +import io |
| 21 | +import os |
| 22 | +import pytest |
| 23 | +from ibm_cloud_sdk_core import ApiException, read_external_sources |
| 24 | +from ibm_platform_services.case_management_v1 import * |
| 25 | + |
| 26 | +# |
| 27 | +# This file provides an example of how to use the Case Management service. |
| 28 | +# |
| 29 | +# CASE_MANAGEMENT_URL=<service url> |
| 30 | +# CASE_MANAGEMENT_AUTHTYPE=iam |
| 31 | +# CASE_MANAGEMENT_APIKEY=<IAM apikey> |
| 32 | +# CASE_MANAGEMENT_AUTH_URL=<IAM token service URL - omit this if using the production environment> |
| 33 | +# CASE_MANAGEMENT_RESOURCE_CRN=<cloud resource name> |
| 34 | +# |
| 35 | + |
| 36 | +# Config file name |
| 37 | +config_file = 'case_management.env' |
| 38 | + |
| 39 | +case_management_service = None |
| 40 | + |
| 41 | +config = None |
| 42 | + |
| 43 | +case_number = None |
| 44 | +attachment_id = None |
| 45 | +resource_crn = None |
| 46 | + |
| 47 | + |
| 48 | +############################################################################## |
| 49 | +# Start of Examples for Service: CaseManagementV1 |
| 50 | +############################################################################## |
| 51 | +# region |
| 52 | +class TestCaseManagementV1Examples(): |
| 53 | + """ |
| 54 | + Example Test Class for CaseManagementV1 |
| 55 | + """ |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def setup_class(cls): |
| 59 | + global case_management_service |
| 60 | + if os.path.exists(config_file): |
| 61 | + os.environ['IBM_CREDENTIALS_FILE'] = config_file |
| 62 | + |
| 63 | + # begin-common |
| 64 | + |
| 65 | + case_management_service = CaseManagementV1.new_instance( |
| 66 | + ) |
| 67 | + |
| 68 | + # end-common |
| 69 | + assert case_management_service is not None |
| 70 | + |
| 71 | + # Load the configuration |
| 72 | + global config |
| 73 | + config = read_external_sources( |
| 74 | + CaseManagementV1.DEFAULT_SERVICE_NAME) |
| 75 | + |
| 76 | + global resource_crn |
| 77 | + resource_crn = config['RESOURCE_CRN'] |
| 78 | + |
| 79 | + print('Setup complete.') |
| 80 | + |
| 81 | + needscredentials = pytest.mark.skipif( |
| 82 | + not os.path.exists(config_file), reason='External configuration not available, skipping...' |
| 83 | + ) |
| 84 | + |
| 85 | + @needscredentials |
| 86 | + def test_create_case_example(self): |
| 87 | + """ |
| 88 | + create_case request example |
| 89 | + """ |
| 90 | + try: |
| 91 | + # begin-createCase |
| 92 | + |
| 93 | + offering_type = OfferingType( |
| 94 | + group='crn_service_name', |
| 95 | + key='cloud-object-storage' |
| 96 | + ) |
| 97 | + offering_payload = Offering( |
| 98 | + name='Cloud Object Storage', |
| 99 | + type=offering_type |
| 100 | + ) |
| 101 | + |
| 102 | + case = case_management_service.create_case( |
| 103 | + type='technical', |
| 104 | + subject='Example technical case', |
| 105 | + description='This is an example case description. This is where the problem would be described.', |
| 106 | + offering=offering_payload, |
| 107 | + severity=4, |
| 108 | + ).get_result() |
| 109 | + |
| 110 | + print(json.dumps(case, indent=2)) |
| 111 | + |
| 112 | + # end-createCase |
| 113 | + |
| 114 | + assert case is not None |
| 115 | + assert case['number'] is not None |
| 116 | + |
| 117 | + global case_number |
| 118 | + case_number = case['number'] |
| 119 | + |
| 120 | + except ApiException as e: |
| 121 | + pytest.fail(str(e)) |
| 122 | + |
| 123 | + @needscredentials |
| 124 | + def test_get_case_example(self): |
| 125 | + """ |
| 126 | + get_case request example |
| 127 | + """ |
| 128 | + assert case_number is not None |
| 129 | + |
| 130 | + try: |
| 131 | + # begin-getCase |
| 132 | + |
| 133 | + fields_to_return = [ |
| 134 | + GetCaseEnums.Fields.DESCRIPTION, |
| 135 | + GetCaseEnums.Fields.STATUS, |
| 136 | + GetCaseEnums.Fields.SEVERITY, |
| 137 | + GetCaseEnums.Fields.CREATED_BY, |
| 138 | + ] |
| 139 | + |
| 140 | + case = case_management_service.get_case( |
| 141 | + case_number=case_number, |
| 142 | + fields=fields_to_return |
| 143 | + ).get_result() |
| 144 | + |
| 145 | + print(json.dumps(case, indent=2)) |
| 146 | + |
| 147 | + # end-getCase |
| 148 | + |
| 149 | + except ApiException as e: |
| 150 | + pytest.fail(str(e)) |
| 151 | + |
| 152 | + @needscredentials |
| 153 | + def test_get_cases_example(self): |
| 154 | + """ |
| 155 | + get_cases request example |
| 156 | + """ |
| 157 | + try: |
| 158 | + # begin-getCases |
| 159 | + |
| 160 | + case_list = case_management_service.get_cases( |
| 161 | + offset=0, |
| 162 | + limit=100, |
| 163 | + search='blocker', |
| 164 | + sort=GetCasesEnums.Fields.UPDATED_AT, |
| 165 | + ).get_result() |
| 166 | + |
| 167 | + print(json.dumps(case_list, indent=2)) |
| 168 | + |
| 169 | + # end-getCases |
| 170 | + |
| 171 | + except ApiException as e: |
| 172 | + pytest.fail(str(e)) |
| 173 | + |
| 174 | + @needscredentials |
| 175 | + def test_add_comment_example(self): |
| 176 | + """ |
| 177 | + add_comment request example |
| 178 | + """ |
| 179 | + assert case_number is not None |
| 180 | + |
| 181 | + try: |
| 182 | + # begin-addComment |
| 183 | + |
| 184 | + comment = case_management_service.add_comment( |
| 185 | + case_number=case_number, |
| 186 | + comment='This is an example comment.' |
| 187 | + ).get_result() |
| 188 | + |
| 189 | + print(json.dumps(comment, indent=2)) |
| 190 | + |
| 191 | + # end-addComment |
| 192 | + |
| 193 | + except ApiException as e: |
| 194 | + pytest.fail(str(e)) |
| 195 | + |
| 196 | + @needscredentials |
| 197 | + def test_add_watchlist_example(self): |
| 198 | + """ |
| 199 | + add_watchlist request example |
| 200 | + """ |
| 201 | + assert case_number is not None |
| 202 | + |
| 203 | + try: |
| 204 | + # begin-addWatchlist |
| 205 | + |
| 206 | + watchlist_users = [ |
| 207 | + User( realm='IBMid', user_id='[email protected]') |
| 208 | + ] |
| 209 | + |
| 210 | + watchlist_add_response = case_management_service.add_watchlist( |
| 211 | + case_number=case_number, |
| 212 | + watchlist=watchlist_users, |
| 213 | + ).get_result() |
| 214 | + |
| 215 | + print(json.dumps(watchlist_add_response, indent=2)) |
| 216 | + |
| 217 | + # end-addWatchlist |
| 218 | + |
| 219 | + except ApiException as e: |
| 220 | + pytest.fail(str(e)) |
| 221 | + |
| 222 | + @needscredentials |
| 223 | + def test_remove_watchlist_example(self): |
| 224 | + """ |
| 225 | + remove_watchlist request example |
| 226 | + """ |
| 227 | + assert case_number is not None |
| 228 | + |
| 229 | + try: |
| 230 | + # begin-removeWatchlist |
| 231 | + |
| 232 | + watchlist_users = [ |
| 233 | + User( realm='IBMid', user_id='[email protected]') |
| 234 | + ] |
| 235 | + |
| 236 | + watchlist = case_management_service.remove_watchlist( |
| 237 | + case_number=case_number, |
| 238 | + watchlist=watchlist_users, |
| 239 | + ).get_result() |
| 240 | + |
| 241 | + print(json.dumps(watchlist, indent=2)) |
| 242 | + |
| 243 | + # end-removeWatchlist |
| 244 | + |
| 245 | + except ApiException as e: |
| 246 | + pytest.fail(str(e)) |
| 247 | + |
| 248 | + @needscredentials |
| 249 | + def test_add_resource_example(self): |
| 250 | + """ |
| 251 | + add_resource request example |
| 252 | + """ |
| 253 | + assert case_number is not None |
| 254 | + assert resource_crn is not None |
| 255 | + |
| 256 | + try: |
| 257 | + # begin-addResource |
| 258 | + |
| 259 | + resource = case_management_service.add_resource( |
| 260 | + case_number=case_number, |
| 261 | + crn=resource_crn, |
| 262 | + note='This resource is the service that is having the problem.', |
| 263 | + ).get_result() |
| 264 | + |
| 265 | + print(json.dumps(resource, indent=2)) |
| 266 | + |
| 267 | + # end-addResource |
| 268 | + |
| 269 | + except ApiException as e: |
| 270 | + pytest.fail(str(e)) |
| 271 | + |
| 272 | + @needscredentials |
| 273 | + def test_upload_file_example(self): |
| 274 | + """ |
| 275 | + upload_file request example |
| 276 | + """ |
| 277 | + assert case_number is not None |
| 278 | + |
| 279 | + try: |
| 280 | + # begin-uploadFile |
| 281 | + |
| 282 | + example_file_content = b'This is the content of the file to upload.' |
| 283 | + |
| 284 | + file_with_metadata_model = { |
| 285 | + 'data': io.BytesIO(example_file_content).getvalue(), |
| 286 | + 'filename': 'example.log', |
| 287 | + 'content_type': 'application/octet-stream', |
| 288 | + } |
| 289 | + |
| 290 | + files_to_upload = [file_with_metadata_model] |
| 291 | + |
| 292 | + attachment = case_management_service.upload_file( |
| 293 | + case_number=case_number, |
| 294 | + file=files_to_upload, |
| 295 | + ).get_result() |
| 296 | + |
| 297 | + print(json.dumps(attachment, indent=2)) |
| 298 | + |
| 299 | + # end-uploadFile |
| 300 | + |
| 301 | + assert attachment is not None |
| 302 | + assert attachment['id'] is not None |
| 303 | + |
| 304 | + global attachment_id |
| 305 | + attachment_id = attachment['id'] |
| 306 | + |
| 307 | + except ApiException as e: |
| 308 | + pytest.fail(str(e)) |
| 309 | + |
| 310 | + @needscredentials |
| 311 | + def test_download_file_example(self): |
| 312 | + """ |
| 313 | + download_file request example |
| 314 | + """ |
| 315 | + assert case_number is not None |
| 316 | + assert attachment_id is not None |
| 317 | + |
| 318 | + try: |
| 319 | + # begin-downloadFile |
| 320 | + |
| 321 | + response = case_management_service.download_file( |
| 322 | + case_number=case_number, |
| 323 | + file_id=attachment_id, |
| 324 | + ) |
| 325 | + |
| 326 | + file = response.get_result() |
| 327 | + |
| 328 | + print('Attachment content-type:', |
| 329 | + response.get_headers()['content-type']) |
| 330 | + print('Attachment contents:', file.content) |
| 331 | + |
| 332 | + # end-downloadFile |
| 333 | + |
| 334 | + except ApiException as e: |
| 335 | + pytest.fail(str(e)) |
| 336 | + |
| 337 | + @needscredentials |
| 338 | + def test_delete_file_example(self): |
| 339 | + """ |
| 340 | + delete_file request example |
| 341 | + """ |
| 342 | + assert case_number is not None |
| 343 | + assert attachment_id is not None |
| 344 | + |
| 345 | + try: |
| 346 | + # begin-deleteFile |
| 347 | + |
| 348 | + attachment_list = case_management_service.delete_file( |
| 349 | + case_number=case_number, |
| 350 | + file_id=attachment_id |
| 351 | + ).get_result() |
| 352 | + |
| 353 | + print(json.dumps(attachment_list, indent=2)) |
| 354 | + |
| 355 | + # end-deleteFile |
| 356 | + |
| 357 | + except ApiException as e: |
| 358 | + pytest.fail(str(e)) |
| 359 | + |
| 360 | + @needscredentials |
| 361 | + def test_update_case_status_example(self): |
| 362 | + """ |
| 363 | + update_case_status request example |
| 364 | + """ |
| 365 | + assert case_number is not None |
| 366 | + |
| 367 | + try: |
| 368 | + # begin-updateCaseStatus |
| 369 | + |
| 370 | + status_payload_model = { |
| 371 | + 'action': 'resolve', |
| 372 | + 'comment': 'The problem has been resolved.', |
| 373 | + 'resolution_code': 1, |
| 374 | + } |
| 375 | + |
| 376 | + case = case_management_service.update_case_status( |
| 377 | + case_number=case_number, |
| 378 | + status_payload=status_payload_model |
| 379 | + ).get_result() |
| 380 | + |
| 381 | + print(json.dumps(case, indent=2)) |
| 382 | + |
| 383 | + # end-updateCaseStatus |
| 384 | + |
| 385 | + except ApiException as e: |
| 386 | + pytest.fail(str(e)) |
| 387 | + |
| 388 | +# endregion |
| 389 | +############################################################################## |
| 390 | +# End of Examples for Service: CaseManagementV1 |
| 391 | +############################################################################## |
0 commit comments