Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Here we use a composite operation to create a VCN and wait for it to enter the given state. Note that the
# states are passed as an array so it is possible to wait on multiple states. The waiter will complete
# (and the method will return) once the resource enters ANY of the provided states.
get_vcn_response = virtual_network_client_composite_ops.create_vcn_and_wait_for_state(
oci.core.models.CreateVcnDetails(
cidr_block='10.0.0.0/16',
display_name='PySdkCompositeOpExample',
compartment_id=compartment_id
),
[oci.core.models.Vcn.LIFECYCLE_STATE_AVAILABLE]
)
vcn = get_vcn_response.data
print('Created VCN')
get_subnet_response = virtual_network_client_composite_ops.create_subnet_and_wait_for_state(
oci.core.models.CreateSubnetDetails(
compartment_id=compartment_id,
availability_domain=first_ad,
display_name='PySdkCompositeOpsExampleSubnet1',
vcn_id=vcn.id,
cidr_block='10.0.0.0/24'
),
[oci.core.models.Subnet.LIFECYCLE_STATE_AVAILABLE]
)
subnet_one = get_subnet_response.data
print('Created Subnet 1')
get_subnet_response = virtual_network_client_composite_ops.create_subnet_and_wait_for_state(
oci.core.models.CreateSubnetDetails(
compartment_id=compartment_id,
availability_domain=second_ad,
display_name='PySdkCompositeOpsExampleSubnet2',
def create_nsg(virtual_network_client, vcn_id, display_name, compartment_id):
create_nsg_response = virtual_network_client.create_network_security_group(
oci.core.models.CreateNetworkSecurityGroupDetails(
display_name=display_name,
compartment_id=compartment_id,
vcn_id=vcn_id
)
).data
return create_nsg_response
def delete_vcn_and_subnet(virtual_network, vcn_and_subnet):
composite_virtual_network = oci.core.VirtualNetworkClientCompositeOperations(virtual_network)
vcn = vcn_and_subnet['vcn']
subnet = vcn_and_subnet['subnet']
composite_virtual_network.delete_subnet_and_wait_for_state(
subnet.id,
[oci.core.models.Subnet.LIFECYCLE_STATE_TERMINATED]
)
composite_virtual_network.delete_vcn_and_wait_for_state(
vcn.id,
[oci.core.models.Vcn.LIFECYCLE_STATE_TERMINATED]
)
def launch_instance_in_nsg(virtual_network_client,
compute_client,
availability_domain,
subnet_id,
image_id):
launch_instance_details = oci.core.models.LaunchInstanceDetails(
display_name="NSG Example Instance",
compartment_id=compartment_id,
availability_domain=availability_domain,
shape='VM.Standard2.1',
source_details=oci.core.models.InstanceSourceViaImageDetails(image_id=image_id),
create_vnic_details=oci.core.models.CreateVnicDetails(
subnet_id=subnet_id,
nsg_ids=[nsg_id, nsg_id_2]
)
)
launch_instance_response = compute_client.launch_instance(launch_instance_details)
instance = launch_instance_response.data
oci.wait_until(
compute_client,
compute_client.get_instance(instance.id),
'lifecycle_state',
self.subnet_id = subnet.id
instance_metadata = {
'ssh_authorized_keys': self._get_ssh_public_key()
}
launch_instance_details = oci.core.models.LaunchInstanceDetails(
display_name=self.display_name,
compartment_id=self.compartment_id,
availability_domain=self.availability_domain,
shape=self.instance_type or OCI_DEFAULT_TYPE,
metadata=instance_metadata,
source_details=oci.core.models.InstanceSourceViaImageDetails(
image_id=self.image_id
),
create_vnic_details=oci.core.models.CreateVnicDetails(
subnet_id=self.subnet_id
)
)
response = self.compute_client.launch_instance(
launch_instance_details
)
instance = response.data
except Exception as error:
try:
self._terminate_instance()
except Exception:
pass
if hasattr(error, 'message'):
raise OCICloudException(error.message)
vcn = vcn_drg_and_drg_attachment['vcn']
drg = vcn_drg_and_drg_attachment['drg']
drg_attachment = vcn_drg_and_drg_attachment['drg_attachment']
composite_virtual_network_client = oci.core.VirtualNetworkClientCompositeOperations(
virtual_network_client)
print('Deleting DRG Attachment')
composite_virtual_network_client.delete_drg_attachment_and_wait_for_state(
drg_attachment.id,
oci.core.models.DrgAttachment.LIFECYCLE_STATE_DETACHED
)
print('Deleting DRG')
composite_virtual_network_client.delete_drg_and_wait_for_state(
drg.id,
oci.core.models.Drg.LIFECYCLE_STATE_TERMINATED
)
print('Deleting VCN')
composite_virtual_network_client.delete_vcn_and_wait_for_state(
vcn.id,
oci.core.models.Vcn.LIFECYCLE_STATE_TERMINATED
)
The name.
wait: bool
Wait for completion if set.
# --GT-- kept in place to avoid breaking method calls.
Returns
-------
str
The private IP address if successfully added.
Raises
------
OCISDKError
On failure to add.
"""
cpid = oci_sdk.core.models.CreatePrivateIpDetails(
display_name=display_name,
ip_address=private_ip,
vnic_id=self.get_ocid())
nc = self._oci_session.get_network_client()
try:
private_ip = self._oci_session.sdk_call(
nc.create_private_ip,
cpid)
return OCIPrivateIP(session=self._oci_session,
private_ip_data=private_ip.data)
except oci_sdk.exceptions.ServiceError as e:
OCIVNIC._logger.debug('Failed to add private IP', exc_info=True)
raise OCISDKError("Failed to add private IP: %s" % e.message)
# FIXME: wait not implemented!
def create_vcn(virtual_network_composite_operations, compartment_id, cidr_block):
vcn_name = 'py_sdk_example_vcn'
create_vcn_details = oci.core.models.CreateVcnDetails(
cidr_block=cidr_block,
display_name=vcn_name,
compartment_id=compartment_id
)
create_vcn_response = virtual_network_composite_operations.create_vcn_and_wait_for_state(
create_vcn_details,
wait_for_states=[oci.core.models.Vcn.LIFECYCLE_STATE_AVAILABLE]
)
vcn = create_vcn_response.data
print('Created VCN: {}'.format(vcn.id))
print('{}'.format(vcn))
print()
return vcn
def delete_vcn_drg_and_drg_attachment(virtual_network_client, vcn_drg_and_drg_attachment):
vcn = vcn_drg_and_drg_attachment['vcn']
drg = vcn_drg_and_drg_attachment['drg']
drg_attachment = vcn_drg_and_drg_attachment['drg_attachment']
composite_virtual_network_client = oci.core.VirtualNetworkClientCompositeOperations(
virtual_network_client)
print('Deleting DRG Attachment')
composite_virtual_network_client.delete_drg_attachment_and_wait_for_state(
drg_attachment.id,
oci.core.models.DrgAttachment.LIFECYCLE_STATE_DETACHED
)
print('Deleting DRG')
composite_virtual_network_client.delete_drg_and_wait_for_state(
drg.id,
oci.core.models.Drg.LIFECYCLE_STATE_TERMINATED
)
print('Deleting VCN')
composite_virtual_network_client.delete_vcn_and_wait_for_state(
vcn.id,
oci.core.models.Vcn.LIFECYCLE_STATE_TERMINATED
)