vpc_uuid parameter added to droplet creation

This commit is contained in:
Wayne Gemmell 2023-02-16 16:48:11 +02:00 committed by Megan Wilhite
parent c2d2522f1f
commit fc89d6fe8b

View file

@ -180,6 +180,15 @@ def list_nodes(call=None):
)
return _list_nodes()
def list_vpcs(call=None):
"""
Return a list of the VMs that are on the provider
"""
if call == "action":
raise SaltCloudSystemExit(
"The list_nodes function must be called with -f or --function."
)
return _list_vpcs()
def list_nodes_full(call=None, for_output=True):
"""
@ -355,27 +364,44 @@ def create(vm_):
" or 'private'."
)
private_networking = config.get_cloud_config_value(
"private_networking",
vpc_name = config.get_cloud_config_value(
"vpc_name",
vm_,
__opts__,
search_global=False,
default=None,
)
if private_networking is not None:
if not isinstance(private_networking, bool):
raise SaltCloudConfigError(
"'private_networking' should be a boolean value."
)
kwargs["private_networking"] = private_networking
if not private_networking and ssh_interface == "private":
if vpc_name is not None:
vpc = _get_vpc_by_name(vpc_name)
if vpc is None:
raise SaltCloudConfigError(
"The DigitalOcean driver requires ssh_interface if defined as 'private' "
"then private_networking should be set as 'True'."
"Invalid VPC name provided"
)
else:
kwargs["vpc_uuid"] = vpc[vpc_name]['id']
else:
private_networking = config.get_cloud_config_value(
"private_networking",
vm_,
__opts__,
search_global=False,
default=None,
)
if private_networking is not None:
if not isinstance(private_networking, bool):
raise SaltCloudConfigError(
"'private_networking' should be a boolean value."
)
kwargs["private_networking"] = private_networking
if not private_networking and ssh_interface == "private":
raise SaltCloudConfigError(
"The DigitalOcean driver requires ssh_interface if defined as 'private' "
"then private_networking should be set as 'True'."
)
backups_enabled = config.get_cloud_config_value(
"backups_enabled",
vm_,
@ -610,8 +636,11 @@ def query(
default="https://api.digitalocean.com/v2",
)
)
path = "{}/{}/".format(base_path, method)
# vpcs method doesn't like the / at the end.
if method == 'vpcs':
path = "{}/{}".format(base_path, method)
else:
path = "{}/{}/".format(base_path, method)
if droplet_id:
path += "{}/".format(droplet_id)
@ -1251,6 +1280,40 @@ def unassign_floating_ip(kwargs=None, call=None):
return result
def _get_vpc_by_name(name):
"""
Helper function to format and parse vpc data.
"""
fetch = True
page = 1
ret = {}
log.debug("Matching vpc name with: %s",name)
while fetch:
items = query(method="vpcs", command="?page=" + str(page) + "&per_page=200")
for node in items["vpcs"]:
log.debug("Node returned : %s", node["name"])
if(name == node["name"]):
log.debug("Matched VPC node")
ret[name] = {
"id": node["id"],
"urn": node["urn"],
"name": name,
"description": node["description"],
"region": node["region"],
"ip_range": node["ip_range"],
"default": node["default"],
}
return ret
page += 1
try:
fetch = "next" in items["links"]["pages"]
except KeyError:
fetch = False
ret[name] = None
return ret
def _list_nodes(full=False, for_output=False):
"""