|
| 1 | +# |
| 2 | +# oci-stop-untagged-instance-python version 1.0. |
| 3 | +# |
| 4 | +# Copyright (c) 2020 Oracle, Inc. |
| 5 | +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. |
| 6 | +# |
| 7 | + |
| 8 | +import io |
| 9 | +import json |
| 10 | +from fdk import response |
| 11 | + |
| 12 | +import oci |
| 13 | + |
| 14 | +def handler(ctx, data: io.BytesIO=None): |
| 15 | + |
| 16 | + resp=None |
| 17 | + |
| 18 | + try: |
| 19 | + body = json.loads(data.getvalue()) |
| 20 | + jsondata = body.get("data") |
| 21 | + |
| 22 | + |
| 23 | + print("event type : " + body["eventType"],flush=True) |
| 24 | + print("Instance Id : " + body["data"]["resourceId"],flush=True) |
| 25 | + print("Instance Name : " + body["data"]["resourceName"],flush=True) |
| 26 | + print("Availability Domain : " + body["data"]["availabilityDomain"],flush=True) |
| 27 | + |
| 28 | + print(jsondata.get("resourceId"),flush=True) |
| 29 | + print(jsondata.get("resourceName"),flush=True) |
| 30 | + print(jsondata.get("availabilityDomain"),flush=True) |
| 31 | + |
| 32 | + print(json.dumps(body, indent=4), flush=True) |
| 33 | + |
| 34 | + instanceId = body["data"]["resourceId"] |
| 35 | + signer = oci.auth.signers.get_resource_principals_signer() |
| 36 | + |
| 37 | + resp = do(signer,instanceId) |
| 38 | + |
| 39 | + return response.Response( |
| 40 | + ctx, response_data=json.dumps(resp), |
| 41 | + headers={"Content-Type": "application/json"} |
| 42 | + ) |
| 43 | + except ( Exception, ValueError) as e: |
| 44 | + print("Error " + str(e), flush=True) |
| 45 | + |
| 46 | +def do(signer,instanceId): |
| 47 | + |
| 48 | + print("Searching for untagged instance", flush=True) |
| 49 | + |
| 50 | + results = "" |
| 51 | + message = "" |
| 52 | + resp = "" |
| 53 | + |
| 54 | + try: |
| 55 | + search_client = oci.resource_search.ResourceSearchClient(config={}, signer=signer) |
| 56 | + print("Search client initialized",flush=True) |
| 57 | + |
| 58 | + key="costcenter" |
| 59 | + value="1234" |
| 60 | + |
| 61 | + structured_search = oci.resource_search.models.StructuredSearchDetails( |
| 62 | + query="query instance resources where ((freeformTags.key != '{}' && freeformTags.value != '{}') && (identifier='{}'))".format(key,value,instanceId), |
| 63 | + type='Structured', |
| 64 | + matching_context_type=oci.resource_search.models.SearchDetails.MATCHING_CONTEXT_TYPE_NONE) |
| 65 | + results = search_client.search_resources(structured_search) |
| 66 | + if len(results.data.items) == 1: |
| 67 | + message = "Instance " + instanceId + " was untagged " |
| 68 | + print(message, flush=True) |
| 69 | + resp = perform_action(signer,instanceId, 'STOP') |
| 70 | + else: |
| 71 | + message = "Instance " + instanceId + " properly tagged " |
| 72 | + print(message, flush=True) |
| 73 | + |
| 74 | + except oci.exceptions.ServiceError as e: |
| 75 | + print('RQS Search failed with Service Error: {0}'.format(e),flush=True) |
| 76 | + raise |
| 77 | + except oci.exceptions.RequestException as e: |
| 78 | + print('RQS Search failed w/ a Request exception. {0}'.format(e),flush=True) |
| 79 | + raise |
| 80 | + |
| 81 | + return resp |
| 82 | + |
| 83 | + |
| 84 | +def perform_action(signer,instanceId,action): |
| 85 | + |
| 86 | + compute_client = oci.core.ComputeClient(config={}, signer=signer) |
| 87 | + print("Performing action", flush=True) |
| 88 | + try: |
| 89 | + if compute_client.get_instance(instanceId).data.lifecycle_state in ('RUNNING'): |
| 90 | + try: |
| 91 | + |
| 92 | + resp = compute_client.instance_action(instanceId,action) |
| 93 | + print('response code: {0}'.format(resp.status),flush=True) |
| 94 | + except oci.exceptions.ServiceError as e: |
| 95 | + print('Action failed. {0}'.format(e),flush=True) |
| 96 | + raise |
| 97 | + else: |
| 98 | + print('The instance {0} was in the incorrect state to stop'.format(instanceId),flush=True) |
| 99 | + except oci.exceptions.ServiceError as e: |
| 100 | + print('Action failed. {0}'.format(e),flush=True) |
| 101 | + raise |
| 102 | + |
| 103 | + print('Action ' + action + ' performed on instance: {}'.format(instanceId),flush=True) |
| 104 | + |
| 105 | + return compute_client.get_instance(instanceId).data.lifecycle_state |
0 commit comments