def create_vcn():
    cidr_block = getConfig("VCN", "cidr_block")
    vcn_client = oci.core.VirtualNetworkClient(config)
    vcn_details = CreateVcnDetails()
    vcn_details.cidr_block = cidr_block
    vcn_details.display_name = "".join(
        random.sample(string.ascii_letters + string.digits, 8))
    vcn_details.compartment_id = getConfig("VCN", "compartment_id")
    vcn_result = vcn_client.create_vcn(vcn_details)
    get_vcn_response = oci.wait_until(vcn_client,
                                      vcn_client.get_vcn(vcn_result.data.id),
                                      'lifecycle_state', 'AVAILABLE')
    print('Created VCN:{}'.format(get_vcn_response.data.id))
    return get_vcn_response
def create_vcn():
    global virtual_network_client_composite_ops
    global vcn_id

    # setup create vcn details
    create_vcn_details = CreateVcnDetails()
    create_vcn_details.cidr_block = '10.0.0.0/16'
    create_vcn_details.compartment_id = src_compartment_id
    create_vcn_details.display_name = 'test_change_service_gateway'

    # create vcn
    response = virtual_network_client_composite_ops.create_vcn_and_wait_for_state(create_vcn_details, wait_for_states=[Vcn.LIFECYCLE_STATE_AVAILABLE])
    vcn_id = response.data.id
    info("VCN ID : %s" % vcn_id)
def main():
    module_args = oci_utils.get_taggable_arg_spec(supports_create=True,
                                                  supports_wait=True)
    module_args.update(
        dict(cidr_block=dict(type='str', required=False),
             compartment_id=dict(type='str', required=False),
             display_name=dict(type='str', required=False, aliases=['name']),
             dns_label=dict(type='str', required=False),
             state=dict(type='str',
                        required=False,
                        default='present',
                        choices=['absent', 'present']),
             vcn_id=dict(type='str', required=False, aliases=['id'])))

    module = AnsibleModule(argument_spec=module_args,
                           supports_check_mode=False,
                           mutually_exclusive=[['compartment_id', 'vcn_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)
    virtual_network_client = VirtualNetworkClient(config)
    exclude_attributes = {'display_name': True, 'dns_label': True}
    state = module.params['state']
    vcn_id = module.params['vcn_id']

    if state == 'absent':
        if vcn_id is not None:
            result = delete_vcn(virtual_network_client, module)
        else:
            module.fail_json(
                msg="Specify vcn_id with state as 'absent' to delete a VCN.")

    else:
        if vcn_id is not None:
            result = update_vcn(virtual_network_client, module)
        else:
            result = oci_utils.check_and_create_resource(
                resource_type='vcn',
                create_fn=create_vcn,
                kwargs_create={
                    'virtual_network_client': virtual_network_client,
                    'module': module
                },
                list_fn=virtual_network_client.list_vcns,
                kwargs_list={
                    'compartment_id': module.params['compartment_id']
                },
                module=module,
                model=CreateVcnDetails(),
                exclude_attributes=exclude_attributes)

    module.exit_json(**result)
def create_vcn(virtual_network_client, module):
    create_vcn_details = CreateVcnDetails()
    for attribute in create_vcn_details.attribute_map.keys():
        if attribute in module.params:
            setattr(create_vcn_details, attribute, module.params[attribute])

    result = oci_utils.create_and_wait(
        resource_type="vcn",
        create_fn=virtual_network_client.create_vcn,
        kwargs_create={"create_vcn_details": create_vcn_details},
        client=virtual_network_client,
        get_fn=virtual_network_client.get_vcn,
        get_param="vcn_id",
        module=module)
    return result
def main():
    module_args = oci_utils.get_taggable_arg_spec(supports_create=True,
                                                  supports_wait=True)
    module_args.update(
        dict(
            cidr_block=dict(type="str", required=False),
            compartment_id=dict(type="str", required=False),
            display_name=dict(type="str", required=False, aliases=["name"]),
            dns_label=dict(type="str", required=False),
            state=dict(
                type="str",
                required=False,
                default="present",
                choices=["absent", "present"],
            ),
            vcn_id=dict(type="str", required=False, aliases=["id"]),
        ))

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=False,
        mutually_exclusive=[["compartment_id", "vcn_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, "dns_label": True}
    state = module.params["state"]
    vcn_id = module.params["vcn_id"]

    if state == "absent":
        if vcn_id is not None:
            result = delete_vcn(virtual_network_client, module)
        else:
            module.fail_json(
                msg="Specify vcn_id with state as 'absent' to delete a VCN.")

    else:
        if vcn_id is not None:
            result = update_vcn(virtual_network_client, module)
        else:
            result = oci_utils.check_and_create_resource(
                resource_type="vcn",
                create_fn=create_vcn,
                kwargs_create={
                    "virtual_network_client": virtual_network_client,
                    "module": module,
                },
                list_fn=virtual_network_client.list_vcns,
                kwargs_list={
                    "compartment_id": module.params["compartment_id"]
                },
                module=module,
                model=CreateVcnDetails(),
                exclude_attributes=exclude_attributes,
            )

    module.exit_json(**result)
Exemple #6
0
# Creating a VCN in Python demo script
import oci

config = oci.config.from_file()
identity_client = oci.identity.IdentityClient(config)
tenancy_id = config["tenancy"]
compartment = identity_client.list_compartments(tenancy_id)
print(compartment.data)

# change the assignment below to match your POCCOMP1 OCID
compartment_id = "ocid1.compartment.oc1..aaaaaaaal7yma7iwruue2altr2cmgns4svg57relimneoixu7t5hefneolxa"
virtual_network_client = oci.core.virtual_network_client.VirtualNetworkClient(
    config)

# prepare OCI request by assigning the desired VCN parameters
from oci.core.models import CreateVcnDetails

request = CreateVcnDetails()
request.compartment_id = compartment_id
request.display_name = "pythonvcn"
request.dns_label = "pythonvcn"
request.cidr_block = "172.0.0.0/16"

vcn = virtual_network_client.create_vcn(request)
Exemple #7
0
    def validate_options(cls, options):
        if not isinstance(options, dict):
            raise ValueError("options is not a dictionary")
        expected_profile_keys = [
            "compartment_id",
            "name",
        ]

        expected_instance_keys = [
            "availability_domain",
            "shape",
            "operating_system",
            "operating_system_version",
        ]
        optional_instance_metadata_keys = ["ssh_authorized_keys"]
        optional_instance_keys = ["display_name"]

        expected_vcn_keys = ["dns_label", "display_name"]
        optional_vcn_keys = [
            k for k, v in CreateVcnDetails().attribute_map.items()
            if k not in expected_vcn_keys
        ]
        optional_vcn_keys.append("id")

        expected_subnet_keys = ["dns_label", "display_name"]
        optional_subnet_keys = [
            k for k, v in CreateSubnetDetails().attribute_map.items()
            if k not in expected_subnet_keys
        ]
        optional_subnet_keys.append("id")

        expected_route_table_keys = ["routerules"]
        optional_route_table_keys = [
            k for k, v in CreateRouteTableDetails().attribute_map.items()
            if k not in expected_route_table_keys
        ]
        optional_route_table_keys.append("id")
        # optional_route_rule_keys = [k for k, v in RouteRule().attribute_map.items()]

        expected_gateway_keys = ["is_enabled"]
        optional_gateway_keys = [
            k for k, v in CreateInternetGatewayDetails().attribute_map.items()
            if k not in expected_gateway_keys
        ]
        optional_gateway_keys.append("id")

        expected_groups = {
            "profile": expected_profile_keys,
            "instance": expected_instance_keys +
            optional_instance_metadata_keys + optional_instance_keys,
            "vcn": expected_vcn_keys + optional_vcn_keys,
            "routetable":
            expected_route_table_keys + optional_route_table_keys,
            # "route_rule": optional_route_rule_keys,
            "internetgateway": expected_gateway_keys + optional_gateway_keys,
            "subnet": expected_subnet_keys + optional_subnet_keys,
        }

        # TODO, flatten the dict before the validation
        # -> avoid recursion for nested structures
        for group, keys in expected_groups.items():
            if group not in options:
                raise KeyError("Missing group: {}".format(group))

            if not isinstance(options[group], dict):
                raise TypeError("Group: {} must be a dictionary".format(group))

            for key, _ in options[group].items():
                if key not in keys:
                    raise KeyError("Incorrect key: {} is not in: {}".format(
                        key, keys))
 def get_create_model(self):
     create_vcn_details = CreateVcnDetails()
     for attr in create_vcn_details.attribute_map:
         if self.module.params.get(attr) is not None:
             setattr(create_vcn_details, attr, self.module.params[attr])
     return create_vcn_details