def create_tag( identity_client, tag_namespace_id, name, description, is_cost_tracking, module ): result = {} try: ctd = CreateTagDetails() ctd.name = name ctd.description = description ctd.is_cost_tracking = is_cost_tracking oci_utils.add_tags_to_model_from_module(ctd, module) create_response = oci_utils.call_with_backoff( identity_client.create_tag, tag_namespace_id=tag_namespace_id, create_tag_details=ctd, ) get_logger().info("Created tag definition %s", to_dict(create_response.data)) result["tag"] = to_dict(create_response.data) result["changed"] = True return result except ServiceError as ex: module.fail_json(msg=ex.message) except MaximumWaitTimeExceeded as mwte: module.fail_json(msg=str(mwte))
def handle_update_tag_definition(identity_client, tag, module): result = dict(changed=False, tag=to_dict(tag)) description = module.params.get("description", None) is_cost_tracking = module.params.get("is_cost_tracking", None) reactivate = module.params.get("reactivate", None) if description is None: # if description is not provided by the user, set it to the tag's current description # Update details requires a description and hence we have to do this. description = tag.description desc_change_needed = tag.description != description cost_tracking_change_needed = ( is_cost_tracking is not None and tag.is_cost_tracking != is_cost_tracking ) reactivate_needed = reactivate is not None and reactivate is True and tag.is_retired tag_update_needed = _is_tag_update_needed(tag, module) change_needed = any( [ desc_change_needed, cost_tracking_change_needed, reactivate_needed, tag_update_needed, ] ) if change_needed: utd = UpdateTagDetails() # The details model class requires a valid description always and so we # pass in the old description of the tag utd.description = description if cost_tracking_change_needed: utd.is_cost_tracking = is_cost_tracking if reactivate_needed: utd.is_retired = False if tag_update_needed: oci_utils.add_tags_to_model_from_module(utd, module) tag_namespace_id = module.params.get("tag_namespace_id", None) tag_name = module.params.get("tag_name", None) result["tag"] = to_dict( _update_tag_definition( identity_client, tag_namespace_id, tag_name, utd, module ) ) result["changed"] = True return result
def update_image(compute_client, id, display_name, module): result = dict() changed = False try: uid = UpdateImageDetails() uid.display_name = display_name oci_utils.add_tags_to_model_from_module(uid, module) debug("Instance " + id + " - updating with new name: " + display_name) response = oci_utils.call_with_backoff( compute_client.update_image, image_id=id, update_image_details=uid ) result["image"] = to_dict(response.data) changed = True except ServiceError as ex: module.fail_json(msg=ex.message) result["changed"] = changed return result
def _get_create_image_details(module): comp_id = module.params["compartment_id"] name = module.params["name"] instance_id = module.params["instance_id"] image_source_details = module.params["image_source_details"] launch_mode = module.params["launch_mode"] cid = CreateImageDetails() cid.compartment_id = comp_id cid.display_name = name cid.launch_mode = launch_mode oci_utils.add_tags_to_model_from_module(cid, module) # An image can be created from an instance, or imported from an exported image available in object storage if instance_id: cid.instance_id = instance_id elif image_source_details: source_type = image_source_details["source_type"] isd = None # The exported image can be provided as a tuple(namespace,bucket,object) or a URI if source_type == "objectStorageTuple": isd = ImageSourceViaObjectStorageTupleDetails() isd.source_type = source_type isd.namespace_name = image_source_details["namespace"] isd.bucket_name = image_source_details["bucket"] isd.object_name = image_source_details["object"] isd.source_image_type = image_source_details["source_image_type"] elif source_type == "objectStorageUri": isd = ImageSourceViaObjectStorageUriDetails() isd.source_type = source_type isd.source_uri = image_source_details["source_uri"] isd.source_image_type = image_source_details["source_image_type"] cid.image_source_details = isd else: module.fail_json( msg= "Specify instance_id or image_source_id while creating an instance." ) return cid
def create_tag_namespace(identity_client, compartment_id, name, description, module): result = {} try: ctnsd = CreateTagNamespaceDetails() ctnsd.name = name ctnsd.description = description ctnsd.compartment_id = compartment_id oci_utils.add_tags_to_model_from_module(ctnsd, module) create_response = oci_utils.call_with_backoff( identity_client.create_tag_namespace, create_tag_namespace_details=ctnsd ) get_logger().info("Created Tag Namespace %s", to_dict(create_response.data)) result["tag_namespace"] = to_dict(create_response.data) result["changed"] = True return result except ServiceError as ex: module.fail_json(msg=ex.message) except MaximumWaitTimeExceeded as mwte: module.fail_json(msg=str(mwte))
def handle_create_volume(block_storage_client, module): create_volume_details = CreateVolumeDetails() create_volume_details.availability_domain = module.params['availability_domain'] create_volume_details.compartment_id = module.params['compartment_id'] create_volume_details.display_name = module.params['display_name'] create_volume_details.size_in_gbs = module.params['size_in_gbs'] oci_utils.add_tags_to_model_from_module(create_volume_details, module) if module.params['source_details']: source_details = module.params['source_details'] volume_source = None if 'type' in source_details: if source_details['type'] == "volume": volume_source = VolumeSourceFromVolumeDetails() volume_source.id = source_details["id"] elif source_details['type'] == "volumeBackup": volume_source = VolumeSourceFromVolumeBackupDetails() volume_source.id = source_details["id"] else: module.fail_json(msg="value of state must be one of: volume, volumeBackup") else: module.fail_json(msg="missing required arguments: type") create_volume_details.source_details = volume_source result = oci_utils.create_and_wait(resource_type="volume", create_fn=block_storage_client.create_volume, kwargs_create={"create_volume_details": create_volume_details}, client=block_storage_client, get_fn=block_storage_client.get_volume, get_param="volume_id", module=module ) wait_for_copy = False copy_timeout = 1800 WAIT_FOR_INITIALIZATION = "wait_for_copy" INITIALIZATION_TIMEOUT = "copy_timeout" if create_volume_details.source_details is not None: if WAIT_FOR_INITIALIZATION in source_details: wait_for_copy = source_details[WAIT_FOR_INITIALIZATION] if INITIALIZATION_TIMEOUT in source_details: copy_timeout = source_details[INITIALIZATION_TIMEOUT] try: response = oci_utils.call_with_backoff(block_storage_client.get_volume, volume_id=result['volume']['id']) if wait_for_copy: result['volume'] = to_dict( oci.wait_until(block_storage_client, response, 'is_hydrated', True, max_wait_seconds=copy_timeout).data) except ServiceError as ex: module.fail_json(msg=ex.message) except MaximumWaitTimeExceeded as ex: module.fail_json(msg=str(ex)) return result