|
| 1 | +# coding: utf-8 |
| 2 | +# Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
| 3 | + |
| 4 | +# This script provides a basic example of how to create an IPv6 VNIC attachment to a provided |
| 5 | +# instance using the Python SDK. NOTE: The provided instance must be within an IPv6-enabled VCN. |
| 6 | +# |
| 7 | +# USAGE: |
| 8 | +# `python examples/create_ipv6_example.py --compartment-id 'foo' --instance-id 'bar'` |
| 9 | +# |
| 10 | +# This script will create an IPv6 VNIC attachment to the provided instance and |
| 11 | +# accepts the following arguments: |
| 12 | +# |
| 13 | +# * The compartment ID in which the instance is located |
| 14 | +# * The instance ID on which the IPv6 will be created |
| 15 | +# * The display name for the IPv6 to be created |
| 16 | + |
| 17 | +import oci |
| 18 | +import argparse |
| 19 | + |
| 20 | +# parse arguments |
| 21 | +parser = argparse.ArgumentParser() |
| 22 | +parser.add_argument('--compartment-id', |
| 23 | + help='compartment ID in which to perform the operation', |
| 24 | + required=True |
| 25 | + ) |
| 26 | +parser.add_argument('--instance-id', |
| 27 | + help='instance ID on which the IPv6 attachment will be created', |
| 28 | + required=True |
| 29 | + ) |
| 30 | +parser.add_argument('--display-name', |
| 31 | + help='display name for the IPv6 to be created', |
| 32 | + required=False, |
| 33 | + default='python-sdk-create-ipv6-example' |
| 34 | + ) |
| 35 | +args = parser.parse_args() |
| 36 | + |
| 37 | + |
| 38 | +def get_vnic_attachments(compute_client, instance_id, compartment_id): |
| 39 | + vnic_attachments = compute_client.list_vnic_attachments( |
| 40 | + compartment_id, |
| 41 | + instance_id=instance_id |
| 42 | + ).data |
| 43 | + print('found VNIC attachments:\n{}'.format(vnic_attachments)) |
| 44 | + return vnic_attachments |
| 45 | + |
| 46 | + |
| 47 | +def create_ipv6(virtual_network_client, vnic_id, display_name): |
| 48 | + create_ipv6_response = virtual_network_client.create_ipv6( |
| 49 | + oci.core.models.CreateIpv6Details( |
| 50 | + display_name=display_name, |
| 51 | + vnic_id=vnic_id |
| 52 | + ) |
| 53 | + ).data |
| 54 | + return create_ipv6_response |
| 55 | + |
| 56 | + |
| 57 | +# ---------- assign provided arguments |
| 58 | +compartment_id = args.compartment_id |
| 59 | +instance_id = args.instance_id |
| 60 | +display_name = args.display_name |
| 61 | + |
| 62 | +# ---------- read config from file |
| 63 | +config = oci.config.from_file() |
| 64 | +compute_client = oci.core.ComputeClient(config) |
| 65 | +virtual_network_client = oci.core.VirtualNetworkClient(config) |
| 66 | + |
| 67 | +# ---------- get VNIC attachments for provided instance |
| 68 | +vnics = get_vnic_attachments( |
| 69 | + compute_client, |
| 70 | + args.instance_id, |
| 71 | + args.compartment_id |
| 72 | +) |
| 73 | + |
| 74 | +# ---------- create IPv6 object |
| 75 | +create_ipv6( |
| 76 | + virtual_network_client, |
| 77 | + vnics[0].vnic_id, # use first vnic ID returned from `get_vnic_attachments` |
| 78 | + display_name |
| 79 | +) |
0 commit comments