def main(): module_args = oci_utils.get_common_arg_spec(supports_wait=True) module_args.update( dict(instance_id=dict(type='str', required=False), is_read_only=dict(type='bool', required=False, default=False), state=dict(type='str', required=False, default='present', choices=['absent', 'present']), type=dict(type='str', required=False, choices=['iscsi', 'paravirtualized']), use_chap=dict(type='bool', required=False, default=False), volume_id=dict(type='str', required=False), volume_attachment_id=dict(type='str', required=False, aliases=['id']), display_name=dict(type='str', required=False))) module = AnsibleModule( argument_spec=module_args, supports_check_mode=False, required_if=[['state', 'absent', ['volume_attachment_id']], ['state', 'present', ['instance_id', 'volume_id']]]) if not HAS_OCI_PY_SDK: module.fail_json(msg='oci python sdk required for this module.') config = oci_utils.get_oci_config(module) compute_client = ComputeClient(config) state = module.params['state'] exclude_attributes = {'display_name': True} if state == 'absent': result = oci_utils.delete_and_wait( resource_type=RESOURCE_NAME, client=compute_client, get_fn=compute_client.get_volume_attachment, kwargs_get={ "volume_attachment_id": module.params["volume_attachment_id"] }, delete_fn=compute_client.detach_volume, kwargs_delete={ "volume_attachment_id": module.params["volume_attachment_id"] }, module=module) else: compartment_id = compute_client.get_instance( module.params["instance_id"]).data.compartment_id result = oci_utils.check_and_create_resource( resource_type=RESOURCE_NAME, create_fn=attach_volume, kwargs_create={ "compute_client": compute_client, "module": module }, list_fn=compute_client.list_volume_attachments, kwargs_list={ "compartment_id": compartment_id, "instance_id": module.params['instance_id'], "volume_id": module.params['volume_id'] }, module=module, model=AttachVolumeDetails(), exclude_attributes=exclude_attributes, default_attribute_values={ "is_read_only": False, "use_chap": False }) module.exit_json(**result)
def main(): module_args = oci_utils.get_common_arg_spec(supports_wait=True) module_args.update( dict(instance_id=dict(type='str', required=False), state=dict(type='str', required=False, default='present', choices=['absent', 'present']), boot_volume_id=dict(type='str', required=False), boot_volume_attachment_id=dict(type='str', required=False, aliases=['id']), display_name=dict(type='str', required=False, aliases=['name']))) module = AnsibleModule( argument_spec=module_args, supports_check_mode=False, required_if=[['state', 'absent', ['boot_volume_attachment_id']], ['state', 'present', ['instance_id', 'boot_volume_id']]]) if not HAS_OCI_PY_SDK: module.fail_json(msg='oci python sdk required for this module.') config = oci_utils.get_oci_config(module) compute_client = ComputeClient(config) exclude_attributes = {'display_name': True} state = module.params['state'] if state == 'absent': result = oci_utils.delete_and_wait( resource_type=RESOURCE_NAME, client=compute_client, get_fn=compute_client.get_boot_volume_attachment, kwargs_get={ "boot_volume_attachment_id": module.params["boot_volume_attachment_id"] }, delete_fn=compute_client.detach_boot_volume, kwargs_delete={ "boot_volume_attachment_id": module.params["boot_volume_attachment_id"] }, module=module) else: instance = compute_client.get_instance( module.params["instance_id"]).data result = oci_utils.check_and_create_resource( resource_type=RESOURCE_NAME, create_fn=attach_boot_volume, kwargs_create={ "compute_client": compute_client, "module": module }, list_fn=compute_client.list_boot_volume_attachments, kwargs_list={ "availability_domain": instance.availability_domain, "compartment_id": instance.compartment_id, "instance_id": module.params['instance_id'], "boot_volume_id": module.params['boot_volume_id'] }, exclude_attributes=exclude_attributes, module=module, model=AttachBootVolumeDetails()) module.exit_json(**result)
def main(): my_logger = oci_utils.get_logger("oci_instance") set_logger(my_logger) module_args = oci_utils.get_common_arg_spec(supports_wait=True) module_args.update( dict(display_name=dict(type='str', required=False, aliases=['name']), instance_id=dict(type='str', required=False), vnic_attachment_id=dict(type='str', required=False, aliases=['id']), nic_index=dict(type='int', required=False, default=0), create_vnic_details=dict(type='dict', required=False, aliases=['vnic']), state=dict(type='str', required=False, default='present', choices=['present', 'absent']))) module = AnsibleModule( argument_spec=module_args, supports_check_mode=False, mutually_exclusive=['id', 'create_vnic_details'], required_if=[('state', 'absent', ['vnic_attachment_id'])], ) if not HAS_OCI_PY_SDK: module.fail_json(msg='oci python sdk required for this module.') config = oci_utils.get_oci_config(module) compute_client = ComputeClient(config) state = module.params['state'] result = dict(changed=False) vna_id = module.params['vnic_attachment_id'] debug("VNIC attachment Id provided by user is " + str(vna_id)) try: if vna_id is not None: vna = oci_utils.get_existing_resource( compute_client.get_vnic_attachment, module, vnic_attachment_id=vna_id) if state == 'absent': if vna is not None: debug("Deleting " + vna.id) result = oci_utils.delete_and_wait( resource_type=RESOURCE_NAME, client=compute_client, get_fn=compute_client.get_vnic_attachment, kwargs_get={"vnic_attachment_id": vna_id}, delete_fn=compute_client.detach_vnic, kwargs_delete={"vnic_attachment_id": vna_id}, module=module, wait_applicable=True) else: debug("VNIC attachment " + vna_id + " already detached. So returning changed=False.") else: module.fail_json( "To delete a VNIC attachment, set state=absent") else: # Create a secondary VNIC and attach it to an instance instance_id = module.params.get("instance_id") exclude_attributes = {'display_name': True} default_attribute_values = {"nic_index": 0} compartment_id = compute_client.get_instance( instance_id=instance_id).data.compartment_id result = oci_utils.check_and_create_resource( resource_type=RESOURCE_NAME, create_fn=attach_vnic, kwargs_create={ "compute_client": compute_client, "module": module }, list_fn=compute_client.list_vnic_attachments, kwargs_list={ "compartment_id": compartment_id, "instance_id": instance_id }, module=module, model=AttachVnicDetails(), exclude_attributes=exclude_attributes, default_attribute_values=default_attribute_values) except ServiceError as ex: module.fail_json(msg=ex.message) except MaximumWaitTimeExceeded as mwte: module.fail_json(msg=str(mwte)) module.exit_json(**result)