def create_compartment(identity, tenancy_id, key_name):
    cd_name = 'SectionOne_DevOps_' + key_name
    cd_description = " This compartment is used for monitor %s shift operations " % (
        key_name)
    compartment_details = CreateCompartmentDetails()
    compartment_details.compartment_id = tenancy_id
    compartment_details.name = cd_name
    compartment_details.description = cd_description
    new_c = identity.create_compartment(compartment_details).data
    return new_c.id
def create_compartment(identity_client, module):
    create_compartment_details = CreateCompartmentDetails()
    for attribute in create_compartment_details.attribute_map.keys():
        if attribute in module.params:
            setattr(create_compartment_details, attribute, module.params[attribute])

    # If parent_compartment_id is specified in module option, use it to set compartment_id attribute of
    # CreateCompartmentDetails model.
    if module.params["parent_compartment_id"]:
        create_compartment_details.compartment_id = module.params[
            "parent_compartment_id"
        ]

    result = oci_utils.create_and_wait(
        resource_type="compartment",
        create_fn=identity_client.create_compartment,
        kwargs_create={"create_compartment_details": create_compartment_details},
        client=identity_client,
        get_fn=identity_client.get_compartment,
        get_param="compartment_id",
        module=module,
    )
    return result
Example #3
0
def create_compartment(identity_client, module):
    create_compartment_details = CreateCompartmentDetails()
    for attribute in create_compartment_details.attribute_map.keys():
        if attribute in module.params:
            setattr(create_compartment_details, attribute, module.params[attribute])

    result = oci_utils.create_and_wait(resource_type="compartment",
                                       create_fn=identity_client.create_compartment,
                                       kwargs_create={"create_compartment_details": create_compartment_details},
                                       client=identity_client,
                                       get_fn=identity_client.get_compartment,
                                       get_param="compartment_id",
                                       module=module
                                       )
    return result
Example #4
0
def main():
    module_args = oci_utils.get_taggable_arg_spec(supports_wait=True)
    module_args.update(
        dict(compartment_id=dict(type='str', required=True),
             name=dict(type='str', required=False),
             description=dict(type='str', required=False),
             state=dict(type='str',
                        required=False,
                        default='present',
                        choices=['present'])))

    module = AnsibleModule(argument_spec=module_args,
                           supports_check_mode=False)

    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)

    compartment_id = module.params['compartment_id']

    tenancy = None
    try:
        tenancy = oci_utils.call_with_backoff(identity_client.get_tenancy,
                                              tenancy_id=compartment_id).data
    except ServiceError:
        pass

    # Check if the provided compartment_id is OCID of a tenancy(root compartment). If tenancy OCID is provided, it's
    # supposed to be a call to create compartment in the tenancy else it's a call to update compartment.
    if tenancy is not None:
        result = oci_utils.check_and_create_resource(
            resource_type='compartment',
            create_fn=create_compartment,
            kwargs_create={
                'identity_client': identity_client,
                'module': module
            },
            list_fn=identity_client.list_compartments,
            kwargs_list={"compartment_id": module.params['compartment_id']},
            module=module,
            model=CreateCompartmentDetails())
    else:
        result = update_compartment(identity_client, module)
    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, aliases=["id"]),
            name=dict(type="str", required=False),
            description=dict(type="str", required=False),
            state=dict(
                type="str",
                required=False,
                default="present",
                choices=["present", "absent"],
            ),
            parent_compartment_id=dict(type="str", required=False),
        )
    )

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=False,
        # Name of the compartment `name` is required when `parent_compartment_id` is provided to create a compartment.
        required_if=[["parent_compartment_id", not None, ["name"]]],
        required_one_of=[["parent_compartment_id", "compartment_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"]

    if state == "present":
        # For backward compatibility if tenancy OCID is specified in module option `compartment_id` along with option
        # `name`, then process the task as a request to create a sub-compartment under root compartment. If option
        # `name` is not specified or non-root compartment id is provided, process the task as a request to update
        # root/non-root compartment details.
        if module.params["compartment_id"]:
            if (
                is_compartment_root(identity_client, module.params["compartment_id"])
                and module.params["name"]
            ):
                # Create a sub-compartment under root.
                result = oci_utils.check_and_create_resource(
                    resource_type="compartment",
                    create_fn=create_compartment,
                    kwargs_create={
                        "identity_client": identity_client,
                        "module": module,
                    },
                    list_fn=identity_client.list_compartments,
                    kwargs_list={"compartment_id": module.params["compartment_id"]},
                    module=module,
                    model=CreateCompartmentDetails(),
                )
            else:
                # Update compartment details.
                result = update_compartment(identity_client, module)

        else:
            # Create a sub-compartment under compartment specified by module.params['parent_compartment_id'].
            result = oci_utils.check_and_create_resource(
                resource_type="compartment",
                create_fn=create_compartment,
                kwargs_create={"identity_client": identity_client, "module": module},
                list_fn=identity_client.list_compartments,
                kwargs_list={"compartment_id": module.params["parent_compartment_id"]},
                module=module,
                model=CreateCompartmentDetails(),
            )
    else:
        result = delete_compartment(identity_client, module)

    module.exit_json(**result)
Example #6
0
def main():
    module_args = oci_utils.get_taggable_arg_spec(supports_wait=True)
    module_args.update(
        dict(compartment_id=dict(type='str', required=True),
             name=dict(type='str', required=False),
             description=dict(type='str', required=False),
             state=dict(type='str',
                        required=False,
                        default='present',
                        choices=['present'])))

    module = AnsibleModule(argument_spec=module_args,
                           supports_check_mode=False)

    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)

    compartment_id = module.params['compartment_id']

    # Check if the provided compartment_id is OCID of a tenancy(root compartment). If tenancy OCID is provided, it's
    # supposed to be a call to create compartment in the tenancy else it's a call to update compartment.
    tenancy = None
    try:
        tenancy = oci_utils.call_with_backoff(identity_client.get_tenancy,
                                              tenancy_id=compartment_id).data
    except ServiceError as se:
        if se.status == 404 and se.code == "TenantNotFound":
            # the user has provided a compartment ocid, try to find a compartment with the provided value
            try:
                oci_utils.call_with_backoff(identity_client.get_compartment,
                                            compartment_id=compartment_id).data
            except ServiceError as serr:
                if serr.status == 404 and serr.code == "CompartmentNotFound":
                    # If there exists no compartment, then it is likely a user error (an invalid compartment or tenancy
                    # id)
                    module.fail_json(
                        msg=
                        "The provided compartment_id is an invalid tenancy or compartment ocid. Please"
                        "check the provided compartment_id")
                else:
                    # some other ServiceError trying to get compartment information
                    module.fail_json(msg=se.message)
        else:
            # Some other ServiceError trying to get tenancy information
            module.fail_json(msg=se.message)

    if tenancy is not None:
        result = oci_utils.check_and_create_resource(
            resource_type='compartment',
            create_fn=create_compartment,
            kwargs_create={
                'identity_client': identity_client,
                'module': module
            },
            list_fn=identity_client.list_compartments,
            kwargs_list={"compartment_id": module.params['compartment_id']},
            module=module,
            model=CreateCompartmentDetails())
    else:
        result = update_compartment(identity_client, module)
    module.exit_json(**result)