Skip to content

Commit 31940ab

Browse files
a-dubsaciba90
authored andcommitted
feat(oracle): ability to specify vcn by name for subnet selection
Oracle's pycloudlib implementation currently uses the most recently created VCN (Virtual Cloud Network) in the given compartment for new VM instances being created. Problems can therefore arise if a newly created VCN uses a subnet with different rules than the previously used VCN's subnet. Now, instead of relying on just blindly choosing the most recently created VCN, pycloudlib supports specifying a vcn_name value which it will use to find a VCN that has a name that exactly matches the given vcn_name.
1 parent 061657a commit 31940ab

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1!5.1.0
1+
1!5.2.0

pycloudlib/oci/cloud.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(
3333
compartment_id: Optional[str] = None,
3434
config_path: Optional[str] = None,
3535
config_dict: Optional[str] = None,
36+
vcn_name: Optional[str] = None,
3637
):
3738
"""
3839
Initialize the connection to OCI.
@@ -52,6 +53,8 @@ def __init__(
5253
config_path: Path of OCI config file
5354
config_dict: A dictionary containing the OCI config.
5455
Overrides the values from config_path
56+
vcn_name: Exact name of the VCN to use. If not provided, the newest
57+
VCN in the given compartment will be used.
5558
"""
5659
super().__init__(
5760
tag,
@@ -106,6 +109,8 @@ def __init__(
106109
)
107110
self.oci_config = oci.config.from_file(config_path) # type: ignore
108111

112+
self.vcn_name = vcn_name
113+
109114
self._log.debug("Logging into OCI")
110115
self.compute_client = oci.core.ComputeClient(self.oci_config) # type: ignore # noqa: E501
111116
self.network_client = oci.core.VirtualNetworkClient(self.oci_config) # type: ignore # noqa: E501
@@ -245,6 +250,8 @@ def launch(
245250
retry_strategy: a retry strategy from oci.retry module
246251
to apply for this operation
247252
username: username to use when connecting via SSH
253+
vcn_name: Name of the VCN to use. If not provided, the first VCN
254+
found will be used
248255
**kwargs: dictionary of other arguments to pass as
249256
LaunchInstanceDetails
250257
@@ -258,7 +265,10 @@ def launch(
258265
f" Found: {image_id}"
259266
)
260267
subnet_id = get_subnet_id(
261-
self.network_client, self.compartment_id, self.availability_domain
268+
self.network_client,
269+
self.compartment_id,
270+
self.availability_domain,
271+
vcn_name=self.vcn_name,
262272
)
263273
metadata = {
264274
"ssh_authorized_keys": self.key_pair.public_key_content,

pycloudlib/oci/utils.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This file is part of pycloudlib. See LICENSE file for license information.
22
"""Utilities for OCI images and instances."""
33
import time
4-
from typing import TYPE_CHECKING
4+
from typing import TYPE_CHECKING, Optional
55

66
from pycloudlib.errors import PycloudlibError
77

@@ -37,6 +37,7 @@ def get_subnet_id(
3737
network_client: "oci.core.VirtualNetworkClient", # type: ignore
3838
compartment_id: str,
3939
availability_domain: str,
40+
vcn_name: Optional[str] = None,
4041
) -> str:
4142
"""Get a subnet id linked to `availability_domain`.
4243
@@ -47,13 +48,26 @@ def get_subnet_id(
4748
network_client: Instance of VirtualNetworkClient.
4849
compartment_id: Compartment where the subnet has to belong
4950
availability_domain: Domain to look for subnet id in.
51+
vcn_name: Exact name of the VCN to use. If not provided, the newest
52+
VCN in the given compartment will be used.
5053
Returns:
51-
The updated version of the current_data
54+
id of the subnet selected
5255
Raises:
5356
`Exception` if unable to determine `subnet_id` for
5457
`availability_domain`
5558
"""
56-
vcn_id = network_client.list_vcns(compartment_id).data[0].id
59+
if vcn_name is not None: # if vcn_name specified, use that vcn
60+
vcns = network_client.list_vcns(
61+
compartment_id, display_name=vcn_name
62+
).data
63+
if len(vcns) == 0:
64+
raise PycloudlibError(f"Unable to determine vcn name: {vcn_name}")
65+
if len(vcns) > 1:
66+
raise PycloudlibError(f"Found multiple vcns with name: {vcn_name}")
67+
vcn_id = vcns[0].id
68+
else: # if no vcn_name specified, use most recently created vcn
69+
vcn_id = network_client.list_vcns(compartment_id).data[0].id
70+
5771
subnets = network_client.list_subnets(compartment_id, vcn_id=vcn_id).data
5872
subnet_id = None
5973
for subnet in subnets:

0 commit comments

Comments
 (0)