Example #1
0
def create_or_update_user(identity_client, module):
    existing_user = None
    user = None
    result = dict(changed=False, user='')
    user_id = module.params.get('user_id')
    try:
        if user_id:
            existing_user = oci_utils.get_existing_resource(
                identity_client.get_user, module, user_id=user_id)
            changed, user = update_user(identity_client, existing_user, module)
            result['changed'] = changed
            result['user'] = user
        else:
            result = oci_utils.check_and_create_resource(
                resource_type='user',
                create_fn=create_user,
                kwargs_create={
                    'identity_client': identity_client,
                    'module': module
                },
                list_fn=identity_client.list_users,
                kwargs_list={
                    'compartment_id': module.params.get('compartment_id')
                },
                module=module,
                model=CreateUserDetails())
    except ServiceError as ex:
        module.fail_json(msg=ex.message)
    except MaximumWaitTimeExceeded as ex:
        module.fail_json(msg=ex.args)
    return result
Example #2
0
def create_or_update_virtual_circuit(virtual_network_client, module):
    result = dict(changed=False, virtual_circuit="")
    virtual_circuit_id = module.params.get("virtual_circuit_id")
    exclude_attributes = {"display_name": True, "public_prefixes": True}
    try:
        if virtual_circuit_id:
            existing_virtual_circuit = oci_utils.get_existing_resource(
                virtual_network_client.get_virtual_circuit,
                module,
                virtual_circuit_id=virtual_circuit_id,
            )
            result = update_virtual_circuit(
                virtual_network_client, existing_virtual_circuit, module
            )
        else:
            result = oci_utils.check_and_create_resource(
                resource_type="virtual_circuit",
                create_fn=create_virtual_circuit,
                kwargs_create={
                    "virtual_network_client": virtual_network_client,
                    "module": module,
                },
                list_fn=virtual_network_client.list_virtual_circuits,
                kwargs_list={"compartment_id": module.params.get("compartment_id")},
                module=module,
                exclude_attributes=exclude_attributes,
                model=CreateVirtualCircuitDetails(),
            )
    except ServiceError as ex:
        module.fail_json(msg=ex.message)
    except MaximumWaitTimeExceeded as ex:
        module.fail_json(msg=str(ex))

    return result
Example #3
0
def create_or_update_autonomous_data_warehouse(db_client, module):
    result = dict(changed=False, autonomous_data_warehouse="")
    autonomous_data_warehouse_id = module.params.get(
        "autonomous_data_warehouse_id")
    try:
        if autonomous_data_warehouse_id:
            result = update_autonomous_data_warehouse(db_client, module)
        else:
            result = oci_utils.check_and_create_resource(
                resource_type="autonomous_data_warehouse",
                create_fn=create_autonomous_data_warehouse,
                kwargs_create={
                    "db_client": db_client,
                    "module": module
                },
                list_fn=db_client.list_autonomous_data_warehouses,
                kwargs_list={
                    "compartment_id": module.params.get("compartment_id")
                },
                module=module,
                model=CreateAutonomousDataWarehouseDetails(),
            )
    except ServiceError as ex:
        get_logger().error(
            "Unable to create/update autonomous data warehouse due to: %s",
            ex.message)
        module.fail_json(msg=ex.message)
    except ClientError as ex:
        get_logger().error(
            "Unable to create/update autonomous data warehouse due to: %s",
            str(ex))
        module.fail_json(msg=str(ex))

    return result
def create_or_update_autonomous_database(db_client, module):
    result = dict(changed=False, autonomous_database='')
    autonomous_database_id = module.params.get('autonomous_database_id')
    try:
        if autonomous_database_id:
            result = update_autonomous_database(db_client, module)
        else:
            result = oci_utils.check_and_create_resource(
                resource_type='autonomous_database',
                create_fn=create_autonomous_database,
                kwargs_create={
                    'db_client': db_client,
                    'module': module
                },
                list_fn=db_client.list_autonomous_databases,
                kwargs_list={
                    'compartment_id': module.params.get('compartment_id')
                },
                module=module,
                model=CreateAutonomousDatabaseDetails())
    except ServiceError as ex:
        get_logger().error(
            "Unable to create/update autonomous database due to: %s",
            ex.message)
        module.fail_json(msg=ex.message)
    except ClientError as ex:
        get_logger().error(
            "Unable to launch/update autonomous database due to: %s", str(ex))
        module.fail_json(msg=str(ex))

    return result
def create_or_update_mount_target(file_storage_client, module):
    result = dict(changed=False, mount_target="")
    mount_target_id = module.params.get("mount_target_id")
    try:
        if mount_target_id:
            result = update_mount_target(file_storage_client, module)
        else:
            result = oci_utils.check_and_create_resource(
                resource_type="mount_target",
                create_fn=create_mount_target,
                kwargs_create={
                    "file_storage_client": file_storage_client,
                    "module": module,
                },
                list_fn=file_storage_client.list_mount_targets,
                kwargs_list={
                    "compartment_id": module.params.get("compartment_id"),
                    "availability_domain": module.params.get("availability_domain"),
                },
                module=module,
                model=CreateMountTargetDetails(),
            )
    except ServiceError as ex:
        get_logger().error(
            "Unable to create/update Mount Target due to: %s", ex.message
        )
        module.fail_json(msg=ex.message)
    except ClientError as ex:
        get_logger().error("Unable to launch/update Mount Target due to: %s", str(ex))
        module.fail_json(msg=str(ex))

    return result
def create_or_update_path_route_set(lb_client, module):
    path_route_set = None
    result = dict(
        changed=False,
        path_route_set=''
    )
    load_balancer_id = module.params.get('load_balancer_id')
    name = module.params.get('name')
    path_route_set = oci_utils.get_existing_resource(
        lb_client.get_path_route_set, module, load_balancer_id=load_balancer_id, path_route_set_name=name)
    try:
        if path_route_set:
            result = update_path_route_set(lb_client, module, load_balancer_id, path_route_set, name)
        else:
            result = oci_utils.check_and_create_resource(resource_type='path_route_set',
                                                         create_fn=create_path_route_set,
                                                         kwargs_create={'lb_client': lb_client,
                                                                        'module': module},
                                                         list_fn=lb_client.list_path_route_sets,
                                                         kwargs_list={
                                                             'load_balancer_id': module.params.get('load_balancer_id')},
                                                         module=module,
                                                         model=CreatePathRouteSetDetails())
    except ServiceError as ex:
        get_logger().error("Unable to create/update path route set due to: %s", ex.message)
        module.fail_json(msg=ex.message)
    except ClientError as ex:
        get_logger().error("Unable to create/update path route set due to: %s", str(ex))
        module.fail_json(msg=str(ex))

    return result
Example #7
0
def create_or_update_export(file_storage_client, module):
    result = dict(changed=False, export="")
    export_id = module.params.get("export_id")
    try:
        if export_id:
            export = oci_utils.get_existing_resource(
                file_storage_client.get_export, module, export_id=export_id)
            result = update_export(file_storage_client, module, export)
        else:
            result = oci_utils.check_and_create_resource(
                resource_type="export",
                create_fn=create_export,
                kwargs_create={
                    "file_storage_client": file_storage_client,
                    "module": module,
                },
                list_fn=file_storage_client.list_exports,
                kwargs_list={
                    "file_system_id": module.params.get("file_system_id")
                },
                module=module,
                model=CreateExportDetails(),
            )
    except ServiceError as ex:
        get_logger().error("Unable to create/update Export due to: %s",
                           ex.message)
        module.fail_json(msg=ex.message)
    except ClientError as ex:
        get_logger().error("Unable to launch/update Export due to: %s",
                           str(ex))
        module.fail_json(msg=str(ex))

    return result
def create_or_update_hostname(lb_client, module):
    hostname = None
    result = dict(changed=False, hostname='')
    load_balancer_id = module.params.get('load_balancer_id')
    name = module.params.get('name')
    hostname = oci_utils.get_existing_resource(
        lb_client.get_hostname,
        module,
        load_balancer_id=load_balancer_id,
        name=name)
    if hostname:
        result = update_hostname(lb_client, module, load_balancer_id, hostname,
                                 name)
    else:
        result = oci_utils.check_and_create_resource(
            resource_type='hostname',
            create_fn=create_hostname,
            kwargs_create={
                'lb_client': lb_client,
                'module': module
            },
            list_fn=lb_client.list_hostnames,
            kwargs_list={
                'load_balancer_id': module.params.get('load_balancer_id')
            },
            module=module,
            model=CreateHostnameDetails())

    return result
Example #9
0
def create_or_update_group(identity_client, module):
    group = None
    existing_group = None
    result = dict(changed=False, group='')
    group_id = module.params.get('group_id')
    exclude_attributes = {'compartment_id': True}
    try:
        if group_id:
            existing_group = oci_utils.get_existing_resource(
                identity_client.get_group, module, group_id=group_id)
            changed, group = update_group(identity_client, existing_group,
                                          module)
            result['changed'] = changed
            result['group'] = group
        else:
            result = oci_utils.check_and_create_resource(
                resource_type='group',
                create_fn=create_group,
                kwargs_create={
                    'identity_client': identity_client,
                    'module': module
                },
                list_fn=identity_client.list_groups,
                kwargs_list={
                    'compartment_id': module.params.get('compartment_id')
                },
                module=module,
                exclude_attributes=exclude_attributes,
                model=CreateGroupDetails())
    except ServiceError as ex:
        module.fail_json(msg=ex.message)
    except MaximumWaitTimeExceeded as ex:
        module.fail_json(msg=ex.args)
    return result
Example #10
0
def create_or_update_snapshot(file_storage_client, module):
    result = dict(changed=False, snapshot='')
    snapshot_id = module.params.get('snapshot_id')
    try:
        if snapshot_id:
            result = update_snapshot(file_storage_client, module)
        else:
            result = oci_utils.check_and_create_resource(
                resource_type='snapshot',
                create_fn=create_snapshot,
                kwargs_create={
                    'file_storage_client': file_storage_client,
                    'module': module
                },
                list_fn=file_storage_client.list_snapshots,
                kwargs_list={
                    'file_system_id': module.params.get('file_system_id')
                },
                module=module,
                model=CreateSnapshotDetails())
    except ServiceError as ex:
        get_logger().error("Unable to create/update Snapshot due to: %s",
                           ex.message)
        module.fail_json(msg=ex.message)
    except ClientError as ex:
        get_logger().error("Unable to create/update Snapshot due to: %s",
                           str(ex))
        module.fail_json(msg=str(ex))

    return result
Example #11
0
def create_or_update_route_table(virtual_network_client, module):
    result = dict(changed=False, route_table="")
    rt_id = module.params.get("rt_id")
    exclude_attributes = {"display_name": True}
    try:
        if rt_id:
            existing_route_table = oci_utils.get_existing_resource(
                virtual_network_client.get_route_table, module, rt_id=rt_id
            )
            result = update_route_table(
                virtual_network_client, existing_route_table, module
            )
        else:
            result = oci_utils.check_and_create_resource(
                resource_type="route_table",
                create_fn=create_route_table,
                kwargs_create={
                    "virtual_network_client": virtual_network_client,
                    "module": module,
                },
                list_fn=virtual_network_client.list_route_tables,
                kwargs_list={
                    "compartment_id": module.params.get("compartment_id"),
                    "vcn_id": module.params.get("vcn_id"),
                },
                module=module,
                exclude_attributes=exclude_attributes,
                model=CreateRouteTableDetails(),
            )
    except ServiceError as ex:
        module.fail_json(msg=ex.message)
    except MaximumWaitTimeExceeded as ex:
        module.fail_json(msg=str(ex))

    return result
def create_or_update_smtp_credential(identity_client, module):
    result = dict(changed=False, export="")
    smtp_credential_id = module.params.get("smtp_credential_id")
    try:
        if smtp_credential_id:
            result = update_smtp_credential(identity_client, module)
        else:
            result = oci_utils.check_and_create_resource(
                resource_type=RESOURCE_NAME,
                create_fn=create_smtp_credential,
                kwargs_create={
                    "identity_client": identity_client,
                    "module": module
                },
                list_fn=identity_client.list_smtp_credentials,
                kwargs_list={"user_id": module.params.get("user_id")},
                module=module,
                model=CreateSmtpCredentialDetails(),
            )
    except ServiceError as ex:
        get_logger().error(
            "Unable to create/update SMTP credential due to: %s", ex.message)
        module.fail_json(msg=ex.message)
    except ClientError as ex:
        get_logger().error(
            "Unable to create/update SMTP credential due to: %s", str(ex))
        module.fail_json(msg=str(ex))

    return result
def create_or_update_backend_set(lb_client, module):
    backend_set = None
    result = dict(changed=False, backend_set="")
    lb_id = module.params.get("load_balancer_id")
    name = module.params.get("name")
    backend_set = oci_utils.get_existing_resource(
        lb_client.get_backend_set, module, load_balancer_id=lb_id, backend_set_name=name
    )
    try:
        if backend_set:
            result = update_backend_set(lb_client, module, lb_id, backend_set, name)
        else:
            result = oci_utils.check_and_create_resource(
                resource_type="backend_set",
                create_fn=create_backend_set,
                kwargs_create={"lb_client": lb_client, "module": module},
                list_fn=lb_client.list_backend_sets,
                kwargs_list={"load_balancer_id": lb_id},
                module=module,
                model=CreateBackendSetDetails(),
            )
    except ServiceError as ex:
        get_logger().error("Unable to create/update backend set due to: %s", ex.message)
        module.fail_json(msg=ex.message)
    except ClientError as ex:
        get_logger().error("Unable to create/update backend set due to: %s", str(ex))
        module.fail_json(msg=str(ex))

    return result
def create_or_update_internet_gateway(virtual_network_client, module):
    result = dict(changed=False, internet_gateway="")
    ig_id = module.params.get("ig_id")
    exclude_attributes = {"display_name": True}
    try:
        if ig_id:
            result = update_internet_gateway(virtual_network_client, module)
        else:
            result = oci_utils.check_and_create_resource(
                resource_type="internet_gateway",
                create_fn=create_internet_gateway,
                kwargs_create={
                    "virtual_network_client": virtual_network_client,
                    "module": module,
                },
                list_fn=virtual_network_client.list_internet_gateways,
                kwargs_list={
                    "compartment_id": module.params.get("compartment_id"),
                    "vcn_id": module.params.get("vcn_id"),
                },
                module=module,
                exclude_attributes=exclude_attributes,
                model=CreateInternetGatewayDetails(),
            )
    except ServiceError as ex:
        module.fail_json(msg=ex.message)
    except MaximumWaitTimeExceeded as ex:
        module.fail_json(msg=str(ex))

    return result
Example #15
0
def create_or_update_internet_gateway(virtual_network_client, module):
    result = dict(changed=False, internet_gateway='')
    ig_id = module.params.get('ig_id')
    exclude_attributes = {'display_name': True}
    try:
        if ig_id:
            result = update_internet_gateway(virtual_network_client, module)
        else:
            result = oci_utils.check_and_create_resource(
                resource_type='internet_gateway',
                create_fn=create_internet_gateway,
                kwargs_create={
                    'virtual_network_client': virtual_network_client,
                    'module': module
                },
                list_fn=virtual_network_client.list_internet_gateways,
                kwargs_list={
                    'compartment_id': module.params.get('compartment_id'),
                    'vcn_id': module.params.get('vcn_id')
                },
                module=module,
                exclude_attributes=exclude_attributes,
                model=CreateInternetGatewayDetails())
    except ServiceError as ex:
        module.fail_json(msg=ex.message)
    except MaximumWaitTimeExceeded as ex:
        module.fail_json(msg=str(ex))

    return result
Example #16
0
def main():
    set_logger(oci_utils.get_logger("oci_customer_secret_key"))

    module_args = oci_utils.get_common_arg_spec(supports_create=True)
    module_args.update(dict(
        user_id=dict(type='str', required=True),
        customer_secret_key_id=dict(type='str', required=False, aliases=['id']),
        name=dict(type='str', required=False, aliases=['display_name']),
        state=dict(type='str', required=False, default='present', choices=['present', 'absent'])
    ))

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=False,
        required_if=[('state', 'absent', ['customer_secret_key_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)
    identity_client = IdentityClient(config)
    state = module.params['state']

    result = dict(changed=False)

    user_id = module.params.get("user_id", None)
    secret_key_id = module.params.get("customer_secret_key_id", None)
    name = module.params.get('name', None)
    get_logger().debug("Id is " + str(secret_key_id))

    if secret_key_id is not None:
        secret_key = _get_customer_secret_key_from_id(identity_client, user_id, secret_key_id, module)

        if state == 'absent':
            get_logger().debug("Delete Customer Secret Key %s for user %s requested", secret_key_id, user_id)
            if secret_key is not None:
                get_logger().debug("Deleting %s", secret_key.id)
                result = delete_customer_secret_key(identity_client, user_id, secret_key_id, module)
            else:
                get_logger().debug("Customer secret key %s already deleted.", secret_key_id)
        elif state == 'present':
            if secret_key.display_name != name:
                result = update_customer_secret_key(identity_client, user_id, secret_key_id, name, module)
            else:
                # No change needed, return existing customer secret key details
                result[RESOURCE_NAME] = to_dict(secret_key)
    else:
        result = oci_utils.check_and_create_resource(resource_type=RESOURCE_NAME,
                                                     create_fn=create_customer_secret_key,
                                                     kwargs_create={"identity_client": identity_client,
                                                                    "user_id": user_id, "display_name": name,
                                                                    "module": module},
                                                     list_fn=identity_client.list_customer_secret_keys,
                                                     kwargs_list={"user_id": user_id},
                                                     module=module,
                                                     model=CreateCustomerSecretKeyDetails())

    module.exit_json(**result)
Example #17
0
def main():
    logger = oci_utils.get_logger("oci_volume_backup")
    set_logger(logger)
    module_args = oci_utils.get_taggable_arg_spec(supports_create=True,
                                                  supports_wait=True)
    module_args.update(
        dict(display_name=dict(type='str', required=False, aliases=['name']),
             state=dict(type='str',
                        required=False,
                        default='present',
                        choices=['absent', 'present']),
             type=dict(type='str',
                       required=False,
                       default='INCREMENTAL',
                       choices=['FULL', 'INCREMENTAL']),
             volume_backup_id=dict(type='str', required=False, aliases=['id']),
             volume_id=dict(type='str', required=False)))

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=False,
        required_if=[['state', 'absent', ['volume_backup_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)
    block_storage_client = BlockstorageClient(config)

    state = module.params['state']
    volume_backup_id = module.params['volume_backup_id']

    if state == 'absent':
        result = delete_volume_backup(block_storage_client, module)

    else:
        if volume_backup_id is None:
            # Get compartment_id of volume to list all the volume backups of the volume in that compartment.
            compartment_id = block_storage_client.get_volume(
                module.params['volume_id']).data.compartment_id
            result = oci_utils.check_and_create_resource(
                resource_type='volume_backup',
                create_fn=create_volume_backup,
                kwargs_create={
                    'block_storage_client': block_storage_client,
                    'module': module
                },
                list_fn=block_storage_client.list_volume_backups,
                kwargs_list={
                    'compartment_id': compartment_id,
                    'volume_id': module.params['volume_id']
                },
                module=module,
                model=CreateVolumeBackupDetails(),
                exclude_attributes={'display_name': True})
        else:
            result = update_volume_backup(block_storage_client, module)

    module.exit_json(**result)
Example #18
0
def main():
    module_args = oci_utils.get_common_arg_spec(supports_create=True, supports_wait=True)
    module_args.update(dict(
        compartment_id=dict(type='str', required=False),
        display_name=dict(type='str', required=False, aliases=['name']),
        state=dict(type='str', required=False, default='present', choices=['absent', 'present']),
        remote_peering_connection_id=dict(type='str', required=False, aliases=['id']),
        peer_region_name=dict(type='str', required=False),
        peer_id=dict(type='str', required=False),
        drg_id=dict(type='str', required=False)
    ))

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=False,
        required_if=[
            ('state', 'absent', ['remote_peering_connection_id'])
        ],
        required_together=[
            ['peer_id', 'peer_region_name']
        ]
    )

    if not HAS_OCI_PY_SDK:
        module.fail_json(msg='oci python sdk required for this module.')

    virtual_network_client = oci_utils.create_service_client(module, VirtualNetworkClient)

    exclude_attributes = {'display_name': True}
    state = module.params['state']

    if state == 'absent':
        result = delete_remote_peering_connection(virtual_network_client, module)

    else:
        remote_peering_connection_id = module.params['remote_peering_connection_id']
        if remote_peering_connection_id is not None:
            result = update_remote_peering_connection(virtual_network_client, module)
            # A RPC can be connected to another RPC. Perform this operation when peer_id is specified along with
            # remote_peering_connection_id.
            if module.params['peer_id'] is not None:
                result_of_connect_rpc = connect_rpc(virtual_network_client, module)
                result['changed'] = result['changed'] or result_of_connect_rpc['changed']
                result['remote_peering_connection'] = result_of_connect_rpc['remote_peering_connection']
        else:
            result = oci_utils.check_and_create_resource(resource_type='remote_peering_connection',
                                                         create_fn=create_remote_peering_connection,
                                                         kwargs_create={
                                                             'virtual_network_client': virtual_network_client,
                                                             'module': module},
                                                         list_fn=virtual_network_client.list_remote_peering_connections,
                                                         kwargs_list={'compartment_id': module.params['compartment_id'],
                                                                      'drg_id': module.params['drg_id']
                                                                      },
                                                         module=module,
                                                         model=CreateRemotePeeringConnectionDetails(),
                                                         exclude_attributes=exclude_attributes)

    module.exit_json(**result)
Example #19
0
def main():
    logger = oci_utils.get_logger("oci_data_guard_association")
    set_logger(logger)
    module_args = oci_utils.get_common_arg_spec(supports_create=True,
                                                supports_wait=True)
    module_args.update(
        dict(database_id=dict(type='str', required=True),
             data_guard_association_id=dict(type='str',
                                            required=False,
                                            aliases=['id']),
             creation_type=dict(type='str',
                                required=False,
                                default='ExistingDbSystem',
                                choices=['ExistingDbSystem']),
             database_admin_password=dict(type='str',
                                          required=False,
                                          no_log=True),
             protection_mode=dict(type='str',
                                  required=False,
                                  choices=[
                                      'MAXIMUM_AVAILABILITY',
                                      'MAXIMUM_PERFORMANCE',
                                      'MAXIMUM_PROTECTION'
                                  ]),
             transport_type=dict(type='str',
                                 required=False,
                                 choices=['SYNC', 'ASYNC', 'FASTSYNC']),
             peer_db_system_id=dict(type='str', required=False),
             state=dict(
                 type='str',
                 required=False,
                 default='present',
                 choices=['present', 'switchover', 'failover', 'reinstate'])))

    module = AnsibleModule(argument_spec=module_args)

    if not HAS_OCI_PY_SDK:
        module.fail_json(msg='oci python sdk required for this module')

    db_client = oci_utils.create_service_client(module, DatabaseClient)
    state = module.params['state']
    if state == 'present':
        result = oci_utils.check_and_create_resource(
            resource_type='data_guard_association',
            create_fn=create_data_guard_association,
            kwargs_create={
                'db_client': db_client,
                'module': module
            },
            list_fn=db_client.list_data_guard_associations,
            kwargs_list={'database_id': module.params['database_id']},
            module=module,
            model=get_creation_type_instance(module))

    else:
        result = perform_data_guard_operations(db_client, module)

    module.exit_json(**result)
def main():
    module_args = oci_utils.get_common_arg_spec(supports_create=True,
                                                supports_wait=True)
    module_args.update(
        dict(compartment_id=dict(type='str', required=False),
             name=dict(type='str', required=False, aliases=['zone_name']),
             zone_id=dict(type='str', required=False, aliases=['id']),
             zone_type=dict(type='str',
                            required=False,
                            choices=['PRIMARY', 'SECONDARY']),
             external_masters=dict(type='list', required=False),
             state=dict(type='str',
                        required=False,
                        default='present',
                        choices=['absent', 'present'])))

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=False,
        mutually_exclusive=['zone_id', 'name'],
    )

    if not HAS_OCI_PY_SDK:
        module.fail_json(msg='oci python sdk required for this module.')

    config = oci_utils.get_oci_config(module)
    dns_client = DnsClient(config)

    state = module.params['state']
    zone_id = module.params['zone_id']

    if state == 'absent':
        result = delete_zone(dns_client, module)

    else:
        if zone_id is not None:
            module.params['zone_name_or_id'] = get_zone_name_or_id(module)
            result = update_zone(dns_client, module)
            # XXX: also handle case where zone name is specified
        else:
            if module.params['zone_type'] is None:
                module.fail_json(
                    msg='Zone_type must be specified while creating a Zone')
            kwargs_list = {'compartment_id': module.params['compartment_id']}

            result = oci_utils.check_and_create_resource(
                resource_type='zone',
                create_fn=create_zone,
                kwargs_create={
                    'dns_client': dns_client,
                    'module': module
                },
                list_fn=dns_client.list_zones,
                kwargs_list=kwargs_list,
                module=module,
                model=CreateZoneDetails(),
                exclude_attributes=None)
    module.exit_json(**result)
def main():
    module_args = oci_utils.get_taggable_arg_spec(supports_create=True,
                                                  supports_wait=True)
    module_args.update(
        dict(instance_id=dict(type='str', required=False),
             name=dict(type='str', required=False, aliases=['display_name']),
             instance_console_history_id=dict(type='str',
                                              required=False,
                                              aliases=['id']),
             state=dict(type='str',
                        required=False,
                        default='present',
                        choices=['absent', 'present'])))

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=False,
        mutually_exclusive=['console_history_id', 'instance_id'],
        required_if=[('state', 'absent', ['instance_console_history_id'])])

    if not HAS_OCI_PY_SDK:
        module.fail_json(msg='oci python sdk required for this module.')

    compute_client = oci_utils.create_service_client(module, ComputeClient)

    state = module.params['state']

    if state == 'absent':
        result = delete_console_history(compute_client, module)

    else:
        console_history_id = module.params['instance_console_history_id']
        if console_history_id is not None:
            # Update service gateway details.
            result = update_console_history(compute_client, module)
        else:
            instance_id = module.params['instance_id']

            kwargs_list = {
                'instance_id':
                instance_id,
                'compartment_id':
                _get_compartment_of_instance(compute_client, instance_id)
            }

            result = oci_utils.check_and_create_resource(
                resource_type='console_history',
                create_fn=capture_console_history,
                kwargs_create={
                    'compute_client': compute_client,
                    'module': module
                },
                list_fn=compute_client.list_console_histories,
                kwargs_list=kwargs_list,
                module=module,
                model=CaptureConsoleHistoryDetails())
    module.exit_json(**result)
Example #22
0
def main():
    my_logger = oci_utils.get_logger(RESOURCE_NAME)
    set_logger(my_logger)

    module_args = oci_utils.get_taggable_arg_spec(supports_create=True, supports_wait=True)
    module_args.update(dict(
        compartment_id=dict(type='str', required=False),
        display_name=dict(type='str', required=False, aliases=['name']),
        instance_configuration_id=dict(type='str', required=False),
        placement_configurations=dict(type='list', required=False),
        instance_pool_id=dict(type='str', required=False, aliases=['id']),
        size=dict(type='int', required=False),
        state=dict(type='str', required=False, default='present', choices=['absent', 'present', 'running', 'reset',
                                                                           'softreset', 'stopped']))
    )

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=False,
        mutually_exclusive=['instance_pool_id', 'compartment_id'],
        required_if=[('state', 'absent', ['instance_pool_id'])]
    )

    if not HAS_OCI_PY_SDK:
        module.fail_json(msg='oci python sdk required for this module.')

    compute_management_client = oci_utils.create_service_client(module, ComputeManagementClient)

    state = module.params['state']

    if state == 'absent':
        result = delete_instance_pool(compute_management_client, module)

    elif state == 'present':
        instance_pool_id = module.params['instance_pool_id']

        if instance_pool_id is None:
            kwargs_list = {'compartment_id': module.params['compartment_id']}
            result = oci_utils.check_and_create_resource(resource_type=RESOURCE_NAME,
                                                         create_fn=create_instance_pool,
                                                         kwargs_create={
                                                             'compute_management_client': compute_management_client,
                                                             'module': module},
                                                         list_fn=compute_management_client.list_instance_pools,
                                                         kwargs_list=kwargs_list,
                                                         module=module,
                                                         model=CreateInstancePoolDetails(),
                                                         exclude_attributes=None,
                                                         supports_sort_by_time_created=False
                                                         )
        else:
            result = update_instance_pool(compute_management_client, module)
    else:
        # one of the power actions
        result = power_action_on_instance_pool(compute_management_client, module)

    module.exit_json(**result)
Example #23
0
def main():
    module_args = oci_utils.get_taggable_arg_spec(supports_create=True,
                                                  supports_wait=True)
    module_args.update(
        dict(instance_id=dict(type='str', required=False),
             public_key=dict(type='str', required=False),
             instance_console_connection_id=dict(type='str',
                                                 required=False,
                                                 aliases=['id']),
             state=dict(type='str',
                        required=False,
                        default='present',
                        choices=['absent', 'present'])))

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=False,
        mutually_exclusive=['instance_console_connection_id', 'instance_id'],
        required_if=[('state', 'absent', ['instance_console_connection_id']),
                     ('state', 'present', ['instance_id', 'public_key'])])

    if not HAS_OCI_PY_SDK:
        module.fail_json(msg='oci python sdk required for this module.')

    compute_client = oci_utils.create_service_client(module, ComputeClient)

    state = module.params['state']

    if state == 'absent':
        result = delete_instance_console_connection(compute_client, module)

    else:
        # Replace value of `public_key` key by content of file pointed to by the `public_key` param
        module.params["public_key"] = get_file_content(
            module.params['public_key'])

        instance_id = module.params['instance_id']
        kwargs_list = {
            'instance_id':
            instance_id,
            'compartment_id':
            _get_compartment_of_instance(compute_client, instance_id)
        }

        result = oci_utils.check_and_create_resource(
            resource_type='instance_console_connection',
            create_fn=create_instance_console_connection,
            kwargs_create={
                'compute_client': compute_client,
                'module': module
            },
            list_fn=compute_client.list_instance_console_connections,
            kwargs_list=kwargs_list,
            module=module,
            model=CreateInstanceConsoleConnectionDetails(),
            exclude_attributes=None)
    module.exit_json(**result)
Example #24
0
def main():
    module_args = oci_utils.get_common_arg_spec(supports_create=True,
                                                supports_wait=True)
    module_args.update(
        dict(compartment_id=dict(type='str', required=False),
             node_pool_id=dict(type='str', required=False, aliases=['id']),
             cluster_id=dict(type='str', required=False),
             kubernetes_version=dict(type='str', required=False),
             name=dict(type='str', required=False),
             initial_node_labels=dict(type=list, required=False),
             subnet_ids=dict(type=list, required=False),
             state=dict(type='str',
                        required=False,
                        default='present',
                        choices=['absent', 'present']),
             node_image_name=dict(type='str', required=False),
             node_shape=dict(type='str', required=False),
             quantity_per_subnet=dict(type='int', required=False),
             ssh_public_key=dict(type='str', required=False),
             count_of_nodes_to_wait=dict(type=int, required=False, default=1),
             wait_until=dict(type='str', required=False, default="ACTIVE")))

    module = AnsibleModule(argument_spec=module_args,
                           supports_check_mode=False,
                           required_if=[('state', 'absent', ['node_pool_id'])])

    if not HAS_OCI_PY_SDK:
        module.fail_json(msg='oci python sdk required for this module.')

    container_engine_client = oci_utils.create_service_client(
        module, ContainerEngineClient)

    state = module.params['state']
    node_pool_id = module.params['node_pool_id']

    if state == 'absent':
        result = delete_node_pool(container_engine_client, module)

    else:
        if node_pool_id is not None:
            result = update_node_pool(container_engine_client, module)
        else:
            kwargs_list = {'compartment_id': module.params['compartment_id']}
            exclude_attributes = {'name': True}
            result = oci_utils.check_and_create_resource(
                resource_type='node_pool',
                create_fn=create_node_pool,
                kwargs_create={
                    'container_engine_client': container_engine_client,
                    'module': module
                },
                list_fn=container_engine_client.list_node_pools,
                kwargs_list=kwargs_list,
                module=module,
                model=CreateNodePoolDetails(),
                exclude_attributes=exclude_attributes)
    module.exit_json(**result)
Example #25
0
def main():
    module_args = oci_utils.get_taggable_arg_spec(supports_create=True,
                                                  supports_wait=True)
    module_args.update(
        dict(
            block_traffic=dict(type="bool", required=False, default=False),
            vcn_id=dict(type="str", required=False),
            compartment_id=dict(type="str", required=False),
            display_name=dict(type="str", required=False, aliases=["name"]),
            state=dict(
                type="str",
                required=False,
                default="present",
                choices=["absent", "present"],
            ),
            nat_gateway_id=dict(type="str", required=False, aliases=["id"]),
        ))

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=False,
        required_if=[("state", "absent", ["nat_gateway_id"])],
    )

    if not HAS_OCI_PY_SDK:
        module.fail_json(msg="oci python sdk required for this module.")

    virtual_network_client = oci_utils.create_service_client(
        module, VirtualNetworkClient)

    exclude_attributes = {"display_name": True}
    state = module.params["state"]

    if state == "absent":
        result = delete_nat_gateway(virtual_network_client, module)

    else:
        if module.params["nat_gateway_id"] is not None:
            result = update_nat_gateway(virtual_network_client, module)
        else:
            result = oci_utils.check_and_create_resource(
                resource_type="nat_gateway",
                create_fn=create_nat_gateway,
                kwargs_create={
                    "virtual_network_client": virtual_network_client,
                    "module": module,
                },
                list_fn=virtual_network_client.list_nat_gateways,
                kwargs_list={
                    "compartment_id": module.params["compartment_id"],
                    "vcn_id": module.params["vcn_id"],
                },
                module=module,
                model=CreateNatGatewayDetails(),
                exclude_attributes=exclude_attributes,
            )
    module.exit_json(**result)
def main():
    module_args = oci_utils.get_taggable_arg_spec(supports_wait=True)
    module_args.update(
        dict(
            compartment_id=dict(type="str", required=False),
            description=dict(type="str", required=False),
            name=dict(type="str", required=False),
            policy_document=dict(type="str", required=False),
            policy_id=dict(type="str", required=False, aliases=["id"]),
            state=dict(
                type="str",
                required=False,
                default="present",
                choices=["absent", "present"],
            ),
            statements=dict(type="list", required=False),
            version_date=dict(type="datetime", required=False),
        ))

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=False,
        mutually_exclusive=[["policy_document", "statements"]],
        required_if=[["state", "absent", ["policy_id"]]],
    )

    if not HAS_OCI_PY_SDK:
        module.fail_json(msg="oci python sdk required for this module.")

    identity_client = oci_utils.create_service_client(module, IdentityClient)
    state = module.params["state"]
    policy_id = module.params["policy_id"]
    exclude_attributes = {"version_date": True}
    if state == "absent":
        result = delete_policy(identity_client, module)

    else:
        if policy_id is not None:
            result = update_policy(identity_client, module)
        else:
            result = oci_utils.check_and_create_resource(
                resource_type="policy",
                create_fn=create_policy,
                kwargs_create={
                    "identity_client": identity_client,
                    "module": module
                },
                list_fn=identity_client.list_policies,
                kwargs_list={
                    "compartment_id": module.params["compartment_id"]
                },
                module=module,
                model=CreatePolicyDetails(),
                exclude_attributes=exclude_attributes,
            )

    module.exit_json(**result)
Example #27
0
def main():
    module_args = oci_utils.get_common_arg_spec(supports_create=True,
                                                supports_wait=True)
    module_args.update(
        dict(
            compartment_id=dict(type="str", required=False),
            cluster_id=dict(type="str", required=False, aliases=["id"]),
            kubernetes_version=dict(type="str", required=False),
            name=dict(type="str", required=False),
            options=dict(type=dict, required=False),
            vcn_id=dict(type="str", required=False),
            state=dict(
                type="str",
                required=False,
                default="present",
                choices=["absent", "present"],
            ),
        ))

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=False,
        required_if=[("state", "absent", ["cluster_id"])],
    )

    if not HAS_OCI_PY_SDK:
        module.fail_json(msg="oci python sdk required for this module.")

    container_engine_client = oci_utils.create_service_client(
        module, ContainerEngineClient)

    state = module.params["state"]
    cluster_id = module.params["cluster_id"]

    if state == "absent":
        result = delete_cluster(container_engine_client, module)

    else:
        if cluster_id is not None:
            result = update_cluster(container_engine_client, module)
        else:
            kwargs_list = {"compartment_id": module.params["compartment_id"]}
            exclude_attributes = {"name": True}
            result = oci_utils.check_and_create_resource(
                resource_type="cluster",
                create_fn=create_cluster,
                kwargs_create={
                    "container_engine_client": container_engine_client,
                    "module": module,
                },
                list_fn=container_engine_client.list_clusters,
                kwargs_list=kwargs_list,
                module=module,
                model=CreateClusterDetails(),
                exclude_attributes=exclude_attributes,
            )
    module.exit_json(**result)
def main():
    module_args = oci_utils.get_taggable_arg_spec()
    module_args.update(
        dict(hostname_label=dict(type='str', required=False),
             ip_address=dict(type='str', required=False),
             display_name=dict(type='str', required=False, aliases=['name']),
             vnic_id=dict(type='str', required=False),
             state=dict(type='str',
                        required=False,
                        default='present',
                        choices=['absent', 'present']),
             private_ip_id=dict(type='str', required=False, aliases=['id'])))

    module = AnsibleModule(argument_spec=module_args,
                           supports_check_mode=False,
                           required_if=[('state', 'absent', ['private_ip_id'])
                                        ])

    if not HAS_OCI_PY_SDK:
        module.fail_json(msg='oci python sdk required for this module.')

    virtual_network_client = oci_utils.create_service_client(
        module, VirtualNetworkClient)

    state = module.params['state']
    private_ip_id = module.params['private_ip_id']

    if state == 'absent':
        result = delete_private_ip(virtual_network_client, module)

    else:
        if private_ip_id is not None:
            result = update_private_ip(virtual_network_client, module)
        else:
            # Exclude ip_address & display_name when matching private_ips if they are not explicitly specified by user.
            exclude_attributes = {'display_name': True, 'ip_address': True}
            subnet_id = oci_utils.call_with_backoff(
                virtual_network_client.get_vnic,
                vnic_id=module.params['vnic_id']).data.subnet_id
            result = oci_utils.check_and_create_resource(
                resource_type='private_ip',
                create_fn=create_private_ip,
                kwargs_create={
                    'virtual_network_client': virtual_network_client,
                    'module': module
                },
                list_fn=virtual_network_client.list_private_ips,
                kwargs_list={
                    "vnic_id": module.params['vnic_id'],
                    "subnet_id": subnet_id
                },
                module=module,
                model=CreatePrivateIpDetails(),
                exclude_attributes=exclude_attributes)

    module.exit_json(**result)
Example #29
0
def main():
    module_args = oci_utils.get_common_arg_spec(supports_create=True, supports_wait=True)
    module_args.update(dict(
        drg_attachment_id=dict(type='str', required=False, aliases=['id']),
        display_name=dict(type='str', required=False, aliases=['name']),
        state=dict(type='str', required=False, default='present', choices=['absent', 'present']),
        drg_id=dict(type='str', required=False),
        vcn_id=dict(type='str', required=False)
    ))

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=False,
        required_if=[
            ('state', 'absent', ['drg_attachment_id'])
        ]
    )

    if not HAS_OCI_PY_SDK:
        module.fail_json(msg='oci python sdk required for this module.')

    virtual_network_client = oci_utils.create_service_client(module, VirtualNetworkClient)

    exclude_attributes = {'display_name': True}
    state = module.params['state']

    if state == 'absent':
        result = delete_drg_attachment(virtual_network_client, module)

    else:
        drg_attachment_id = module.params['drg_attachment_id']
        if drg_attachment_id is not None:
            result = update_drg_attachment(virtual_network_client, module)
        else:
            # To list existing DRG attachments, compartment_id is required.
            # DRG attachment is created in same compartment as the VCN. Retrieve VCN details to get compartment_id.
            compartment_id = oci_utils.call_with_backoff(virtual_network_client.get_vcn,
                                                         vcn_id=module.params['vcn_id']
                                                         ).data.compartment_id

            result = oci_utils.check_and_create_resource(resource_type='drg_attachment',
                                                         create_fn=create_drg_attachment,
                                                         kwargs_create={
                                                             'virtual_network_client': virtual_network_client,
                                                             'module': module},
                                                         list_fn=virtual_network_client.list_drg_attachments,
                                                         kwargs_list={'compartment_id': compartment_id,
                                                                      'vcn_id': module.params['vcn_id'],
                                                                      'drg_id': module.params['drg_id']
                                                                      },
                                                         module=module,
                                                         model=CreateDrgAttachmentDetails(),
                                                         exclude_attributes=exclude_attributes)

    module.exit_json(**result)
def main():
    logger = oci_utils.get_logger("oci_sender")
    set_logger(logger)

    module_args = oci_utils.get_common_arg_spec(supports_create=True,
                                                supports_wait=True)
    module_args.update(
        dict(
            compartment_id=dict(type="str", required=False),
            sender_id=dict(type="str", required=False, aliases=["id"]),
            email_address=dict(type="str", required=False),
            state=dict(
                type="str",
                required=False,
                default="present",
                choices=["present", "absent"],
            ),
        ))

    module = AnsibleModule(argument_spec=module_args)

    if not HAS_OCI_PY_SDK:
        module.fail_json(msg="oci python sdk required for this module")

    email_client = oci_utils.create_service_client(module, EmailClient)
    state = module.params["state"]

    if state == "present":
        try:
            result = oci_utils.check_and_create_resource(
                resource_type="sender",
                create_fn=create_sender,
                kwargs_create={
                    "email_client": email_client,
                    "module": module
                },
                list_fn=email_client.list_senders,
                kwargs_list={
                    "compartment_id": module.params.get("compartment_id")
                },
                module=module,
                model=CreateSenderDetails(),
            )
        except ServiceError as ex:
            get_logger().error("Unable to create Sender due to: %s",
                               ex.message)
            module.fail_json(msg=ex.message)
        except ClientError as ex:
            get_logger().error("Unable to create Sender due to: %s", str(ex))
            module.fail_json(msg=str(ex))
    elif state == "absent":
        result = delete_sender(email_client, module)

    module.exit_json(**result)