From cb863e72b2fb0986ac586839138ba919ae0352e4 Mon Sep 17 00:00:00 2001 From: Natalie Lee Date: Mon, 10 Jul 2023 14:08:52 +0000 Subject: [PATCH 1/8] changes made last week --- .../clouds/saltext.azurerm.clouds.azurerm.rst | 1 + src/saltext/azurerm/clouds/azurerm.py | 82 ++++++++----------- 2 files changed, 37 insertions(+), 46 deletions(-) diff --git a/docs/ref/clouds/saltext.azurerm.clouds.azurerm.rst b/docs/ref/clouds/saltext.azurerm.clouds.azurerm.rst index a89efe7..4ccd6de 100644 --- a/docs/ref/clouds/saltext.azurerm.clouds.azurerm.rst +++ b/docs/ref/clouds/saltext.azurerm.clouds.azurerm.rst @@ -26,6 +26,7 @@ get_api_versions get_configured_provider get_conn + get_conn_dict get_dependencies get_location get_resource_by_id diff --git a/src/saltext/azurerm/clouds/azurerm.py b/src/saltext/azurerm/clouds/azurerm.py index 2593fd0..194c9d9 100644 --- a/src/saltext/azurerm/clouds/azurerm.py +++ b/src/saltext/azurerm/clouds/azurerm.py @@ -106,6 +106,9 @@ import salt.utils.stringutils # pylint: disable=import-error import salt.utils.yaml # pylint: disable=import-error import salt.version # pylint: disable=import-error +import saltext.azurerm.modules.azurerm_compute +import saltext.azurerm.modules.azurerm_network +import saltext.azurerm.modules.azurerm_resource import saltext.azurerm.utils.azurerm from salt.exceptions import SaltCloudConfigError # pylint: disable=import-error from salt.exceptions import SaltCloudExecutionFailure # pylint: disable=import-error @@ -247,6 +250,16 @@ def get_conn(client_type): """ Return a connection object for a client type. """ + conn_kwargs = get_conn_dict() + client = saltext.azurerm.utils.azurerm.get_client(client_type=client_type, **conn_kwargs) + + return client + + +def get_conn_dict(): + """ + Return a connection auth dictionary. + """ conn_kwargs = {} conn_kwargs["subscription_id"] = salt.utils.stringutils.to_str( @@ -285,9 +298,7 @@ def get_conn(client_type): ) conn_kwargs.update({"username": username, "password": password}) - client = saltext.azurerm.utils.azurerm.get_client(client_type=client_type, **conn_kwargs) - - return client + return conn_kwargs def get_location(call=None, kwargs=None): # pylint: disable=unused-argument @@ -543,19 +554,8 @@ def list_resource_groups(call=None): raise SaltCloudSystemExit( "The list_hosted_services function must be called with -f or --function" ) - - resconn = get_conn(client_type="resource") - ret = {} - try: - groups = resconn.resource_groups.list() - - for group_obj in groups: - group = group_obj.as_dict() - ret[group["name"]] = group - except HttpResponseError as exc: - saltext.azurerm.utils.azurerm.log_cloud_error("resource", exc.message) - ret = {"Error": exc.message} - + conn_kwargs = get_conn_dict() + ret = saltext.azurerm.modules.azurerm_resource.resource_groups_list(**conn_kwargs) return ret @@ -583,31 +583,27 @@ def delete_interface(call=None, kwargs=None): # pylint: disable=unused-argument if kwargs is None: kwargs = {} - netconn = get_conn(client_type="network") - if kwargs.get("resource_group") is None: kwargs["resource_group"] = config.get_cloud_config_value( "resource_group", {}, __opts__, search_global=True ) - + conn_kwargs = get_conn_dict() ips = [] - iface = netconn.network_interfaces.get( - kwargs["resource_group"], - kwargs["iface_name"], + iface = saltext.azurerm.modules.azurerm_network.network_interface_get( + resource_group=kwargs["resource_group"], name=kwargs["iface_name"], **conn_kwargs ) - iface_name = iface.name - for ip_ in iface.ip_configurations: - ips.append(ip_.name) + iface_name = iface["name"] + for ip_ in iface["ip_configurations"]: + ips.append(ip_["name"]) - poller = netconn.network_interfaces.begin_delete( - kwargs["resource_group"], - kwargs["iface_name"], + saltext.azurerm.modules.azurerm_network.network_interface_delete( + resource_group=kwargs["resource_group"], name=kwargs["iface_name"], **conn_kwargs ) - poller.wait() for ip_ in ips: - poller = netconn.public_ip_addresses.begin_delete(kwargs["resource_group"], ip_) - poller.wait() + saltext.azurerm.modules.azurerm_network.public_ip_address_delete( + resource_group=kwargs["resource_group"], name=ip_, **conn_kwargs + ) return {iface_name: ips} @@ -616,17 +612,11 @@ def _get_public_ip(name, resource_group): """ Get the public ip address details by name. """ - netconn = get_conn(client_type="network") - try: - pubip_query = netconn.public_ip_addresses.get( - resource_group_name=resource_group, public_ip_address_name=name - ) - pubip = pubip_query.as_dict() - except HttpResponseError as exc: - saltext.azurerm.utils.azurerm.log_cloud_error("network", exc.message) - pubip = {"error": exc.message} - - return pubip + conn_kwargs = get_conn_dict() + ret = saltext.azurerm.modules.azurerm_network.public_ip_address_get( + name=name, resource_group=resource_group, **conn_kwargs + ) + return ret def _get_network_interface(name, resource_group): @@ -642,12 +632,12 @@ def _get_network_interface(name, resource_group): } ) netapi_version = netapi_versions[0] - netconn = get_conn(client_type="network") - netiface_query = netconn.network_interfaces.get( - resource_group_name=resource_group, network_interface_name=name + + conn_kwargs = get_conn_dict() + netiface = saltext.azurerm.modules.azurerm_network.network_interface_get( + name=name, resource_group=resource_group, **conn_kwargs ) - netiface = netiface_query.as_dict() for index, ip_config in enumerate(netiface["ip_configurations"]): if ip_config.get("private_ip_address") is not None: private_ips.append(ip_config["private_ip_address"]) From c27900a6fdd57fa7d5314b5e09360acdf7e8376c Mon Sep 17 00:00:00 2001 From: Natalie Lee Date: Wed, 12 Jul 2023 19:33:37 +0000 Subject: [PATCH 2/8] seemingly working clouds module create_network_interface function --- src/saltext/azurerm/clouds/azurerm.py | 135 +++--------------- .../azurerm/modules/azurerm_network.py | 21 +++ 2 files changed, 44 insertions(+), 112 deletions(-) diff --git a/src/saltext/azurerm/clouds/azurerm.py b/src/saltext/azurerm/clouds/azurerm.py index 194c9d9..78addf3 100644 --- a/src/saltext/azurerm/clouds/azurerm.py +++ b/src/saltext/azurerm/clouds/azurerm.py @@ -94,7 +94,6 @@ import os.path import pprint import string -import time from multiprocessing import cpu_count from multiprocessing.pool import ThreadPool @@ -664,18 +663,15 @@ def create_network_interface(call=None, kwargs=None): # pylint: disable=invalid-name IPAllocationMethod = getattr(network_models, "IPAllocationMethod") # pylint: disable=invalid-name - NetworkInterface = getattr(network_models, "NetworkInterface") - # pylint: disable=invalid-name - NetworkInterfaceIPConfiguration = getattr(network_models, "NetworkInterfaceIPConfiguration") - # pylint: disable=invalid-name PublicIPAddress = getattr(network_models, "PublicIPAddress") if not isinstance(kwargs, dict): kwargs = {} vm_ = kwargs - netconn = get_conn(client_type="network") + conn_kwargs = get_conn_dict() + # Set values for various parameters if they are not provided if kwargs.get("location") is None: kwargs["location"] = get_location() @@ -697,47 +693,14 @@ def create_network_interface(call=None, kwargs=None): if kwargs.get("iface_name") is None: kwargs["iface_name"] = "{}-iface0".format(vm_["name"]) - try: - subnet_obj = netconn.subnets.get( - resource_group_name=kwargs["network_resource_group"], - virtual_network_name=kwargs["network"], - subnet_name=kwargs["subnet"], - ) - except HttpResponseError as exc: - raise SaltCloudSystemExit( # pylint: disable=raise-missing-from - '{} (Resource Group: "{}", VNET: "{}", Subnet: "{}")'.format( - exc.message, - kwargs["network_resource_group"], - kwargs["network"], - kwargs["subnet"], - ) - ) - + # Handle IP configuration based on provided parameters ip_kwargs = {} ip_configurations = None if "load_balancer_backend_address_pools" in kwargs: - pool_dicts = kwargs["load_balancer_backend_address_pools"] - if isinstance(pool_dicts, dict): - pool_ids = [] - for load_bal, be_pools in pool_dicts.items(): - for pool in be_pools: - try: - lbbep_data = netconn.load_balancer_backend_address_pools.get( - kwargs["resource_group"], - load_bal, - pool, - ) - pool_ids.append({"id": lbbep_data.as_dict()["id"]}) - except HttpResponseError as exc: - log.error("There was a cloud error: %s", str(exc)) - except KeyError as exc: - log.error( - "There was an error getting the Backend Pool ID: %s", - str(exc), - ) - ip_kwargs["load_balancer_backend_address_pools"] = pool_ids - + ip_kwargs["load_balancer_backend_address_pools"] = kwargs[ + "load_balancer_backend_address_pools" + ] if "private_ip_address" in kwargs.keys(): ip_kwargs["private_ip_address"] = kwargs["private_ip_address"] ip_kwargs["private_ip_allocation_method"] = IPAllocationMethod.static @@ -746,82 +709,30 @@ def create_network_interface(call=None, kwargs=None): if kwargs.get("allocate_public_ip") is True: pub_ip_name = "{}-ip".format(kwargs["iface_name"]) - poller = netconn.public_ip_addresses.create_or_update( - resource_group_name=kwargs["resource_group"], - public_ip_address_name=pub_ip_name, - parameters=PublicIPAddress( - location=kwargs["location"], - public_ip_allocation_method=IPAllocationMethod.static, - ), + pub_ip_data = saltext.azurerm.modules.azurerm_network.public_ip_address_create_or_update( + name=pub_ip_name, resource_group=kwargs["resource_group"], **conn_kwargs ) - count = 0 - poller.wait() - while True: - try: - pub_ip_data = netconn.public_ip_addresses.get( - kwargs["resource_group"], - pub_ip_name, - ) - if pub_ip_data.ip_address: # pylint: disable=no-member - ip_kwargs["public_ip_address"] = PublicIPAddress( - id=str(pub_ip_data.id), # pylint: disable=no-member - ) - ip_configurations = [ - NetworkInterfaceIPConfiguration( - name="{}-ip".format(kwargs["iface_name"]), - subnet=subnet_obj, - **ip_kwargs - ) - ] - break - except HttpResponseError as exc: - log.error("There was a cloud error: %s", exc) - count += 1 - if count > 120: - raise ValueError("Timed out waiting for public IP Address.") - time.sleep(5) - else: - priv_ip_name = "{}-ip".format(kwargs["iface_name"]) - ip_configurations = [ - NetworkInterfaceIPConfiguration(name=priv_ip_name, subnet=subnet_obj, **ip_kwargs) - ] - network_security_group = None - if kwargs.get("security_group") is not None: - network_security_group = netconn.network_security_groups.get( - resource_group_name=kwargs["resource_group"], - network_security_group_name=kwargs["security_group"], + ip_kwargs["public_ip_address"] = PublicIPAddress( + id=str(pub_ip_data["id"]), ) + ip_kwargs["name"] = pub_ip_name + ip_configurations = [ip_kwargs] + else: + priv_ip_name = "{}-ip".format(kwargs["iface_name"]) + ip_kwargs["name"] = priv_ip_name + ip_configurations = [ip_kwargs] - iface_params = NetworkInterface( - location=kwargs["location"], - network_security_group=network_security_group, + netiface = saltext.azurerm.modules.azurerm_network.network_interface_create_or_update( # pylint: disable=unused-variable + name=kwargs["iface_name"], ip_configurations=ip_configurations, + subnet=kwargs["subnet"], + virtual_network=kwargs["network"], + resource_group=kwargs["resource_group"], + **conn_kwargs ) - poller = netconn.network_interfaces.begin_create_or_update( - kwargs["resource_group"], kwargs["iface_name"], iface_params, polling=True - ) - try: - poller.wait() - except Exception as exc: # pylint: disable=broad-except - log.warning( - "Network interface creation could not be polled. " - "It is likely that we are reusing an existing interface. (%s)", - exc, - ) - - count = 0 - while True: - try: - return _get_network_interface(kwargs["iface_name"], kwargs["resource_group"]) - except HttpResponseError: - count += 1 - if count > 120: - raise ValueError( # pylint: disable=raise-missing-from - "Timed out waiting for operation to complete." - ) # pylint: disable=raise-missing-from - time.sleep(5) + return _get_network_interface(kwargs["iface_name"], kwargs["resource_group"]) def request_instance(vm_, kwargs=None): diff --git a/src/saltext/azurerm/modules/azurerm_network.py b/src/saltext/azurerm/modules/azurerm_network.py index c196704..4b30a7f 100644 --- a/src/saltext/azurerm/modules/azurerm_network.py +++ b/src/saltext/azurerm/modules/azurerm_network.py @@ -36,6 +36,20 @@ # Python libs import logging +import salt.config # pylint: disable=import-error +import salt.loader # pylint: disable=import-error + + +try: + __opts__ # pylint: disable=used-before-assignment +except NameError: + __opts__ = salt.config.minion_config("/etc/salt/minion") + +try: + __salt__ # pylint: disable=used-before-assignment +except NameError: + __salt__ = salt.loader.minion_mods(__opts__) + import saltext.azurerm.utils.azurerm # Azure libs @@ -1474,6 +1488,7 @@ def network_interface_create_or_update( ) if "error" not in subnet: subnet = {"id": str(subnet["id"])} + ip_configurations_objs = [] for ipconfig in ip_configurations: if "name" in ipconfig: ipconfig["subnet"] = subnet @@ -1495,6 +1510,12 @@ def network_interface_create_or_update( if "error" not in pub_ip: ipconfig["public_ip_address"] = {"id": str(pub_ip["id"])} + ipconfig_model = saltext.azurerm.utils.azurerm.create_object_model( + "network", "NetworkInterfaceIPConfiguration", **ipconfig + ) + ip_configurations_objs.append(ipconfig_model) + + # Make the Network Interface try: nicmodel = saltext.azurerm.utils.azurerm.create_object_model( "network", "NetworkInterface", ip_configurations=ip_configurations, **kwargs From 389155e827a00ed7a915ba9363f705d23d7b7795 Mon Sep 17 00:00:00 2001 From: Natalie Lee Date: Thu, 13 Jul 2023 19:18:53 +0000 Subject: [PATCH 3/8] WIP network_interface_create_or_update function... ill be back --- .../azurerm/modules/azurerm_network.py | 56 ++++++++++++++++--- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/src/saltext/azurerm/modules/azurerm_network.py b/src/saltext/azurerm/modules/azurerm_network.py index 4b30a7f..d3d7ae9 100644 --- a/src/saltext/azurerm/modules/azurerm_network.py +++ b/src/saltext/azurerm/modules/azurerm_network.py @@ -57,6 +57,7 @@ try: import azure.mgmt.network.models # pylint: disable=unused-import from azure.core.exceptions import ResourceNotFoundError, HttpResponseError, SerializationError + from azure.mgmt.core.tools import is_valid_resource_id HAS_LIBS = True except ImportError: @@ -1488,7 +1489,6 @@ def network_interface_create_or_update( ) if "error" not in subnet: subnet = {"id": str(subnet["id"])} - ip_configurations_objs = [] for ipconfig in ip_configurations: if "name" in ipconfig: ipconfig["subnet"] = subnet @@ -1496,8 +1496,53 @@ def network_interface_create_or_update( # TODO: Add ID lookup for referenced object names pass if isinstance(ipconfig.get("load_balancer_backend_address_pools"), list): - # TODO: Add ID lookup for referenced object names - pass + pool_ids = [] + for idx in range(0, len(ipconfig["load_balancer_backend_address_pools"])): + pool_dict = ipconfig["load_balancer_backend_address_pools"][idx] + if isinstance(pool_dict, dict): + if "id" in ipconfig["load_balancer_backend_address_pools"][idx]: + pool_id = ipconfig["load_balancer_backend_address_pools"][idx][ + "id" + ] + if is_valid_resource_id(pool_id): + # If the pool ID is valid, append it to the list of pool IDs + pool_ids.append({"id": pool_id}) + + else: + errmsg = "The provided Backend Pool ID is not a valid resource ID string." + log.error(errmsg) + + elif ( + "load_balancer_name" + and "backend_address_pool_name" + in ipconfig["load_balancer_backend_address_pools"][idx] + ): + try: + lbbep_data = ( + netconn.load_balancer_backend_address_pools.get( + resource_group_name=resource_group, + load_balancer_name=ipconfig[ + "load_balancer_backend_address_pools" + ][idx]["load_balancer_name"], + backend_address_pool_name=ipconfig[ + "load_balancer_backend_address_pools" + ][idx]["backend_address_pool_name"], + ) + ) + pool_ids.append({"id": lbbep_data.as_dict()["id"]}) + except HttpResponseError as exc: + log.error("There was a cloud error: %s", str(exc)) + except KeyError as exc: + log.error( + "There was an error getting the Backend Pool ID: %s", + str(exc), + ) + else: + errmsg = "Backend Pool must be provided as a dictionay of \ + a valid resource id or backend_address_pool_name and load_balancer_name." + + log.error(errmsg) + ipconfig["load_balancer_backend_address_pools"] = pool_ids if isinstance(ipconfig.get("load_balancer_inbound_nat_rules"), list): # TODO: Add ID lookup for referenced object names pass @@ -1510,11 +1555,6 @@ def network_interface_create_or_update( if "error" not in pub_ip: ipconfig["public_ip_address"] = {"id": str(pub_ip["id"])} - ipconfig_model = saltext.azurerm.utils.azurerm.create_object_model( - "network", "NetworkInterfaceIPConfiguration", **ipconfig - ) - ip_configurations_objs.append(ipconfig_model) - # Make the Network Interface try: nicmodel = saltext.azurerm.utils.azurerm.create_object_model( From 03f6399a3b16ca90a08610d0d6d3199834abe595 Mon Sep 17 00:00:00 2001 From: Natalie Lee Date: Mon, 24 Jul 2023 15:36:19 +0000 Subject: [PATCH 4/8] some working updated functions --- src/saltext/azurerm/clouds/azurerm.py | 161 ++++++------------ .../azurerm_compute_virtual_machine.py | 14 +- 2 files changed, 62 insertions(+), 113 deletions(-) diff --git a/src/saltext/azurerm/clouds/azurerm.py b/src/saltext/azurerm/clouds/azurerm.py index 78addf3..e8b9a06 100644 --- a/src/saltext/azurerm/clouds/azurerm.py +++ b/src/saltext/azurerm/clouds/azurerm.py @@ -114,6 +114,17 @@ from salt.exceptions import SaltCloudExecutionTimeout # pylint: disable=import-error from salt.exceptions import SaltCloudSystemExit # pylint: disable=import-error +try: + __opts__ # pylint: disable=used-before-assignment +except NameError: + __opts__ = salt.config.minion_config("/etc/salt/minion") + +try: + __salt__ # pylint: disable=used-before-assignment +except NameError: + __salt__ = salt.loader.minion_mods(__opts__) + + HAS_LIBS = False try: import azure.mgmt.compute.models as compute_models @@ -722,7 +733,7 @@ def create_network_interface(call=None, kwargs=None): priv_ip_name = "{}-ip".format(kwargs["iface_name"]) ip_kwargs["name"] = priv_ip_name ip_configurations = [ip_kwargs] - + # pylint: disable=unused-variable netiface = saltext.azurerm.modules.azurerm_network.network_interface_create_or_update( # pylint: disable=unused-variable name=kwargs["iface_name"], ip_configurations=ip_configurations, @@ -1562,49 +1573,27 @@ def delete_managed_disk(call=None, kwargs=None): # pylint: disable=unused-argum """ Delete a managed disk from a resource group. """ + conn_kwargs = get_conn_dict() - compconn = get_conn(client_type="compute") - - try: - compconn.disks.begin_delete(kwargs["resource_group"], kwargs["blob"]) - except Exception as exc: # pylint: disable=broad-except - log.error( - "Error deleting managed disk %s - %s", - kwargs.get("blob"), - str(exc), - ) - return False + ret = __salt__["azurerm_compute_disk.delete"]( + name=kwargs["blob"], resource_group=kwargs["resource_group"], **conn_kwargs + ) - return True + return ret -def list_virtual_networks(call=None, kwargs=None): +def list_virtual_networks(call=None): """ List virtual networks. """ - if kwargs is None: - kwargs = {} - if call == "action": raise SaltCloudSystemExit("The avail_sizes function must be called with -f or --function") - netconn = get_conn(client_type="network") - resource_groups = list_resource_groups() + conn_kwargs = get_conn_dict() - ret = {} - for group in resource_groups: - try: - networks = netconn.virtual_networks.list(resource_group_name=group) - except HttpResponseError: - networks = {} - for network_obj in networks: - network = network_obj.as_dict() - ret[network["name"]] = network - ret[network["name"]]["subnets"] = list_subnets( - kwargs={"resource_group": group, "network": network["name"]} - ) + networks = __salt__["azurerm_network.virtual_networks_list_all"](**conn_kwargs) - return ret + return networks def list_subnets(call=None, kwargs=None): @@ -1617,8 +1606,6 @@ def list_subnets(call=None, kwargs=None): if call == "action": raise SaltCloudSystemExit("The avail_sizes function must be called with -f or --function") - netconn = get_conn(client_type="network") - resource_group = kwargs.get("resource_group") or config.get_cloud_config_value( "resource_group", get_configured_provider(), __opts__, search_global=False ) @@ -1635,20 +1622,15 @@ def list_subnets(call=None, kwargs=None): ) if "network" not in kwargs or kwargs["network"] is None: - raise SaltCloudSystemExit('A "network" must be specified') + raise SaltCloudSystemExit('A virtual network name must be specified as "network"') - ret = {} - subnets = netconn.subnets.list(resource_group, kwargs["network"]) - for subnet in subnets: - ret[subnet.name] = subnet.as_dict() - ret[subnet.name]["ip_configurations"] = {} - for ip_ in subnet.ip_configurations: - comps = ip_.id.split("/") - name = comps[-1] - ret[subnet.name]["ip_configurations"][name] = ip_.as_dict() - ret[subnet.name]["ip_configurations"][name]["subnet"] = subnet.name - ret[subnet.name]["resource_group"] = resource_group - return ret + conn_kwargs = get_conn_dict() + + subnets = __salt__["azurerm_network.subnets_list"]( + resource_group=resource_group, virtual_network=kwargs["network"], **conn_kwargs + ) + + return subnets def create_or_update_vmextension(call=None, kwargs=None): # pylint: disable=unused-argument @@ -1763,47 +1745,30 @@ def stop(name, call=None): if call == "function": raise SaltCloudSystemExit("The stop action must be called with -a or --action.") - compconn = get_conn(client_type="compute") + conn_kwargs = get_conn_dict() resource_group = config.get_cloud_config_value( "resource_group", get_configured_provider(), __opts__, search_global=False ) - ret = {} + ret = False if not resource_group: groups = list_resource_groups() for group in groups: - try: - instance = compconn.virtual_machines.deallocate( - vm_name=name, resource_group_name=group - ) - instance.wait() - vm_result = instance.result() - ret = vm_result.as_dict() - break - except HttpResponseError as exc: - if "was not found" in exc.message: - continue - else: - ret = {"error": exc.message} - if not ret: - saltext.azurerm.utils.azurerm.log_cloud_error( - "compute", "Unable to find virtual machine with name: {}".format(name) + ret = __salt__["azurerm_compute_virtual_machine.deallocate"]( + name=name, resource_group=group, **conn_kwargs ) + if ret: + break + if "error" in ret and "was not found" in ret["error"]: + continue + + if not ret or "error" in ret: ret = {"error": "Unable to find virtual machine with name: {}".format(name)} else: - try: - instance = compconn.virtual_machines.deallocate( - vm_name=name, resource_group_name=resource_group - ) - instance.wait() - vm_result = instance.result() - ret = vm_result.as_dict() - except HttpResponseError as exc: - saltext.azurerm.utils.azurerm.log_cloud_error( - "compute", "Error attempting to stop {}: {}".format(name, exc.message) - ) - ret = {"error": exc.message} + ret = __salt__["azurerm_compute_virtual_machine.deallocate"]( + name=name, resource_group=resource_group, **conn_kwargs + ) return ret @@ -1823,45 +1788,29 @@ def start(name, call=None): if call == "function": raise SaltCloudSystemExit("The start action must be called with -a or --action.") - compconn = get_conn(client_type="compute") + conn_kwargs = get_conn_dict() resource_group = config.get_cloud_config_value( "resource_group", get_configured_provider(), __opts__, search_global=False ) - ret = {} + ret = False if not resource_group: groups = list_resource_groups() for group in groups: - try: - instance = compconn.virtual_machines.start(vm_name=name, resource_group_name=group) - instance.wait() - vm_result = instance.result() - ret = vm_result.as_dict() - break - except HttpResponseError as exc: - if "was not found" in exc.message: - continue - else: - ret = {"error": exc.message} - if not ret: - saltext.azurerm.utils.azurerm.log_cloud_error( - "compute", "Unable to find virtual machine with name: {}".format(name) + ret = __salt__["azurerm_compute_virtual_machine.start"]( + name=name, resource_group=group, **conn_kwargs ) + if ret: + break + if "error" in ret and "was not found" in ret["error"]: + continue + + if not ret or "error" in ret: ret = {"error": "Unable to find virtual machine with name: {}".format(name)} else: - try: - instance = compconn.virtual_machines.start( - vm_name=name, resource_group_name=resource_group - ) - instance.wait() - vm_result = instance.result() - ret = vm_result.as_dict() - except HttpResponseError as exc: - saltext.azurerm.utils.azurerm.log_cloud_error( - "compute", - "Error attempting to start {}: {}".format(name, exc.message), - ) - ret = {"error": exc.message} + ret = __salt__["azurerm_compute_virtual_machine.start"]( + name=name, resource_group=resource_group, **conn_kwargs + ) return ret diff --git a/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py b/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py index 9a98615..5375a40 100644 --- a/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py +++ b/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py @@ -1048,6 +1048,7 @@ def deallocate(name, resource_group, **kwargs): salt-call azurerm_compute_virtual_machine.deallocate testvm testgroup """ + result = False compconn = saltext.azurerm.utils.azurerm.get_client("compute", **kwargs) try: @@ -1056,9 +1057,8 @@ def deallocate(name, resource_group, **kwargs): resource_group_name=resource_group, vm_name=name ) vm.wait() - vm_result = vm.result() - result = vm_result.as_dict() - except HttpResponseError as exc: + result = True + except (HttpResponseError, ResourceNotFoundError) as exc: saltext.azurerm.utils.azurerm.log_cloud_error("compute", str(exc), **kwargs) result = {"error": str(exc)} @@ -1451,16 +1451,16 @@ def start(name, resource_group, **kwargs): salt-call azurerm_compute_virtual_machine.start testvm testgroup """ - result = {} + result = False compconn = saltext.azurerm.utils.azurerm.get_client("compute", **kwargs) + try: # pylint: disable=invalid-name vm = compconn.virtual_machines.begin_start(resource_group_name=resource_group, vm_name=name) vm.wait() - vm_result = vm.result() - result = vm_result.as_dict() - except HttpResponseError as exc: + result = True + except (HttpResponseError, ResourceNotFoundError) as exc: saltext.azurerm.utils.azurerm.log_cloud_error("compute", str(exc), **kwargs) result = {"error": str(exc)} From c6ef3d621bbc143947320b82c7be31974988a1e2 Mon Sep 17 00:00:00 2001 From: Natalie Lee Date: Tue, 25 Jul 2023 13:59:26 +0000 Subject: [PATCH 5/8] other updated functions --- src/saltext/azurerm/clouds/azurerm.py | 61 ++++++++----------- .../azurerm_compute_virtual_machine.py | 2 +- ...urerm_compute_virtual_machine_extension.py | 2 +- 3 files changed, 26 insertions(+), 39 deletions(-) diff --git a/src/saltext/azurerm/clouds/azurerm.py b/src/saltext/azurerm/clouds/azurerm.py index e8b9a06..45c572a 100644 --- a/src/saltext/azurerm/clouds/azurerm.py +++ b/src/saltext/azurerm/clouds/azurerm.py @@ -733,7 +733,7 @@ def create_network_interface(call=None, kwargs=None): priv_ip_name = "{}-ip".format(kwargs["iface_name"]) ip_kwargs["name"] = priv_ip_name ip_configurations = [ip_kwargs] - # pylint: disable=unused-variable + netiface = saltext.azurerm.modules.azurerm_network.network_interface_create_or_update( # pylint: disable=unused-variable name=kwargs["iface_name"], ip_configurations=ip_configurations, @@ -1256,7 +1256,7 @@ def _query_node_data(name, bootstrap_interface): return ret -def destroy(name, call=None, kwargs=None): # pylint: disable=unused-argument +def destroy(name, call=None, kwargs=None): """ Destroy a VM. @@ -1275,7 +1275,7 @@ def destroy(name, call=None, kwargs=None): # pylint: disable=unused-argument "The destroy action must be called with -d, --destroy, -a or --action." ) - compconn = get_conn(client_type="compute") + conn_kwargs = get_conn_dict() node_data = show_instance(name, call="action") if node_data["storage_profile"]["os_disk"].get("managed_disk"): @@ -1285,8 +1285,12 @@ def destroy(name, call=None, kwargs=None): # pylint: disable=unused-argument ret = {name: {}} log.debug("Deleting VM") - result = compconn.virtual_machines.begin_delete(node_data["resource_group"], name) - result.wait() + result = __salt__["azurerm_compute_virtual_machine.delete"]( # pylint: disable=unused-variable + name=name, resource_group=node_data["resource_group"], **conn_kwargs + ) + if not result: + log.error("VM deletion failed. Stopping further execution.") + return ret if __opts__.get("update_cachedir", False) is True: __utils__["cloud.delete_minion_cachedir"]( @@ -1666,11 +1670,6 @@ def create_or_update_vmextension(call=None, kwargs=None): # pylint: disable=unu if "virtual_machine_name" not in kwargs: raise SaltCloudSystemExit("A virtual machine name must be specified") - compconn = get_conn(client_type="compute") - - # pylint: disable=invalid-name - VirtualMachineExtension = getattr(compute_models, "VirtualMachineExtension") - resource_group = kwargs.get("resource_group") or config.get_cloud_config_value( "resource_group", get_configured_provider(), __opts__, search_global=False ) @@ -1698,34 +1697,22 @@ def create_or_update_vmextension(call=None, kwargs=None): # pylint: disable=unu " must be specified." ) - log.info("Creating VM extension %s", kwargs["extension_name"]) - - ret = {} - try: - params = VirtualMachineExtension( - location=location, - publisher=publisher, - virtual_machine_extension_type=virtual_machine_extension_type, - type_handler_version=type_handler_version, - auto_upgrade_minor_version=auto_upgrade_minor_version, - settings=settings, - protected_settings=protected_settings, - ) - poller = compconn.virtual_machine_extensions.create_or_update( - resource_group, - kwargs["virtual_machine_name"], - kwargs["extension_name"], - params, - ) - ret = poller.result() - ret = ret.as_dict() + conn_kwargs = get_conn_dict() - except HttpResponseError as exc: - saltext.azurerm.utils.azurerm.log_cloud_error( - "compute", - "Error attempting to create the VM extension: {}".format(exc.message), - ) - ret = {"error": exc.message} + log.info("Creating VM extension %s", kwargs["extension_name"]) + ret = __salt__["azurerm_compute_virtual_machine_extension.create_or_update"]( + name=kwargs["extension_name"], + vm_name=kwargs["virtual_machine_name"], + resource_group=resource_group, + location=location, + publisher=publisher, + extension_type=virtual_machine_extension_type, + version=type_handler_version, + settings=settings, + auto_upgrade_minor_version=auto_upgrade_minor_version, + protected_settings=protected_settings, + **conn_kwargs + ) return ret diff --git a/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py b/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py index 5375a40..1ec0dc5 100644 --- a/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py +++ b/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py @@ -1140,7 +1140,7 @@ def virtual_machines_list_all(**kwargs): .. code-block:: bash - salt-call azurerm_compute_virtual_machines_list_all + salt-call azurerm_compute_virtual_machine.virtual_machines_list_all """ result = {} diff --git a/src/saltext/azurerm/modules/azurerm_compute_virtual_machine_extension.py b/src/saltext/azurerm/modules/azurerm_compute_virtual_machine_extension.py index 97ff10e..28d672f 100644 --- a/src/saltext/azurerm/modules/azurerm_compute_virtual_machine_extension.py +++ b/src/saltext/azurerm/modules/azurerm_compute_virtual_machine_extension.py @@ -113,7 +113,7 @@ def create_or_update( location=location, settings=settings, publisher=publisher, - virtual_machine_extension_type=extension_type, + type_properties_type=extension_type, type_handler_version=version, auto_upgrade_minor_version=auto_upgrade_minor_version, **kwargs, From 4382f2e9a2a9ebc449135ebea787cadbefd28aa8 Mon Sep 17 00:00:00 2001 From: Natalie Lee Date: Tue, 25 Jul 2023 14:05:04 +0000 Subject: [PATCH 6/8] use salt loader for execution mod calls --- src/saltext/azurerm/clouds/azurerm.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/saltext/azurerm/clouds/azurerm.py b/src/saltext/azurerm/clouds/azurerm.py index 45c572a..1333b59 100644 --- a/src/saltext/azurerm/clouds/azurerm.py +++ b/src/saltext/azurerm/clouds/azurerm.py @@ -105,9 +105,6 @@ import salt.utils.stringutils # pylint: disable=import-error import salt.utils.yaml # pylint: disable=import-error import salt.version # pylint: disable=import-error -import saltext.azurerm.modules.azurerm_compute -import saltext.azurerm.modules.azurerm_network -import saltext.azurerm.modules.azurerm_resource import saltext.azurerm.utils.azurerm from salt.exceptions import SaltCloudConfigError # pylint: disable=import-error from salt.exceptions import SaltCloudExecutionFailure # pylint: disable=import-error @@ -565,7 +562,7 @@ def list_resource_groups(call=None): "The list_hosted_services function must be called with -f or --function" ) conn_kwargs = get_conn_dict() - ret = saltext.azurerm.modules.azurerm_resource.resource_groups_list(**conn_kwargs) + ret = __salt__["azurerm_resource.resource_groups_list"](**conn_kwargs) return ret @@ -599,19 +596,19 @@ def delete_interface(call=None, kwargs=None): # pylint: disable=unused-argument ) conn_kwargs = get_conn_dict() ips = [] - iface = saltext.azurerm.modules.azurerm_network.network_interface_get( + iface = __salt__["azurerm_network.network_interface_get"]( resource_group=kwargs["resource_group"], name=kwargs["iface_name"], **conn_kwargs ) iface_name = iface["name"] for ip_ in iface["ip_configurations"]: ips.append(ip_["name"]) - saltext.azurerm.modules.azurerm_network.network_interface_delete( + __salt__["azurerm_network.network_interface_delete"]( resource_group=kwargs["resource_group"], name=kwargs["iface_name"], **conn_kwargs ) for ip_ in ips: - saltext.azurerm.modules.azurerm_network.public_ip_address_delete( + __salt__["azurerm_network.public_ip_address_delete"]( resource_group=kwargs["resource_group"], name=ip_, **conn_kwargs ) @@ -623,7 +620,7 @@ def _get_public_ip(name, resource_group): Get the public ip address details by name. """ conn_kwargs = get_conn_dict() - ret = saltext.azurerm.modules.azurerm_network.public_ip_address_get( + ret = __salt__["azurerm_network.public_ip_address_get"]( name=name, resource_group=resource_group, **conn_kwargs ) return ret @@ -644,7 +641,7 @@ def _get_network_interface(name, resource_group): netapi_version = netapi_versions[0] conn_kwargs = get_conn_dict() - netiface = saltext.azurerm.modules.azurerm_network.network_interface_get( + netiface = __salt__["azurerm_network.network_interface_get"]( name=name, resource_group=resource_group, **conn_kwargs ) @@ -720,7 +717,7 @@ def create_network_interface(call=None, kwargs=None): if kwargs.get("allocate_public_ip") is True: pub_ip_name = "{}-ip".format(kwargs["iface_name"]) - pub_ip_data = saltext.azurerm.modules.azurerm_network.public_ip_address_create_or_update( + pub_ip_data = __salt__["azurerm_network.public_ip_address_create_or_update"]( name=pub_ip_name, resource_group=kwargs["resource_group"], **conn_kwargs ) @@ -733,8 +730,8 @@ def create_network_interface(call=None, kwargs=None): priv_ip_name = "{}-ip".format(kwargs["iface_name"]) ip_kwargs["name"] = priv_ip_name ip_configurations = [ip_kwargs] - - netiface = saltext.azurerm.modules.azurerm_network.network_interface_create_or_update( # pylint: disable=unused-variable + # pylint: disable=unused-variable + netiface = __salt__["azurerm_network.network_interface_create_or_update"]( name=kwargs["iface_name"], ip_configurations=ip_configurations, subnet=kwargs["subnet"], From 57ecd10f54ff4d452eeed12f8088a2d3a5641cec Mon Sep 17 00:00:00 2001 From: Natalie Lee Date: Tue, 25 Jul 2023 14:23:57 +0000 Subject: [PATCH 7/8] removing these got rid of the salt loader warning vomit and everything is still working as expected --- src/saltext/azurerm/clouds/azurerm.py | 10 ---------- src/saltext/azurerm/modules/azurerm_network.py | 16 ++-------------- 2 files changed, 2 insertions(+), 24 deletions(-) diff --git a/src/saltext/azurerm/clouds/azurerm.py b/src/saltext/azurerm/clouds/azurerm.py index 1333b59..503eb4f 100644 --- a/src/saltext/azurerm/clouds/azurerm.py +++ b/src/saltext/azurerm/clouds/azurerm.py @@ -111,16 +111,6 @@ from salt.exceptions import SaltCloudExecutionTimeout # pylint: disable=import-error from salt.exceptions import SaltCloudSystemExit # pylint: disable=import-error -try: - __opts__ # pylint: disable=used-before-assignment -except NameError: - __opts__ = salt.config.minion_config("/etc/salt/minion") - -try: - __salt__ # pylint: disable=used-before-assignment -except NameError: - __salt__ = salt.loader.minion_mods(__opts__) - HAS_LIBS = False try: diff --git a/src/saltext/azurerm/modules/azurerm_network.py b/src/saltext/azurerm/modules/azurerm_network.py index d3d7ae9..010c14e 100644 --- a/src/saltext/azurerm/modules/azurerm_network.py +++ b/src/saltext/azurerm/modules/azurerm_network.py @@ -36,20 +36,8 @@ # Python libs import logging -import salt.config # pylint: disable=import-error -import salt.loader # pylint: disable=import-error - - -try: - __opts__ # pylint: disable=used-before-assignment -except NameError: - __opts__ = salt.config.minion_config("/etc/salt/minion") - -try: - __salt__ # pylint: disable=used-before-assignment -except NameError: - __salt__ = salt.loader.minion_mods(__opts__) - +import salt.config # pylint: disable=import-error, unused-import +import salt.loader # pylint: disable=import-error, unused-import import saltext.azurerm.utils.azurerm # Azure libs From 7c05ba5bcaebdde4cd01071f8d4694bf03eeefda Mon Sep 17 00:00:00 2001 From: Natalie Lee Date: Tue, 25 Jul 2023 14:47:12 +0000 Subject: [PATCH 8/8] fixing random idem naming that i forgot to change --- src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py | 2 +- src/saltext/azurerm/states/azurerm_compute_virtual_machine.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py b/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py index 1ec0dc5..d90ae69 100644 --- a/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py +++ b/src/saltext/azurerm/modules/azurerm_compute_virtual_machine.py @@ -63,7 +63,7 @@ def create_or_update( name, resource_group, vm_size, - admin_username="idem", + admin_username="salt", os_disk_create_option="FromImage", os_disk_size_gb=30, ssh_public_keys=None, diff --git a/src/saltext/azurerm/states/azurerm_compute_virtual_machine.py b/src/saltext/azurerm/states/azurerm_compute_virtual_machine.py index 7c4a4a7..b09ba3a 100644 --- a/src/saltext/azurerm/states/azurerm_compute_virtual_machine.py +++ b/src/saltext/azurerm/states/azurerm_compute_virtual_machine.py @@ -347,7 +347,7 @@ def present( Ensure virtual machine exists: azurerm_compute_virtual_machine.present: - name: salt-vm01 - - resource_group: idem + - resource_group: salt-rg01 - vm_size: Standard_B1s - virtual_network: vnet1 - subnet: default