diff --git a/src/saltext/vmware/modules/vm.py b/src/saltext/vmware/modules/vm.py index d441eb6d..e56ff231 100644 --- a/src/saltext/vmware/modules/vm.py +++ b/src/saltext/vmware/modules/vm.py @@ -9,6 +9,8 @@ import saltext.vmware.utils.datastore as utils_datastore import saltext.vmware.utils.vm as utils_vm +from com.vmware.vapi.std_client import DynamicID + log = logging.getLogger(__name__) try: @@ -318,9 +320,7 @@ def info(vm_name=None, service_instance=None, profile=None): datacenter_ref = utils_common.get_parent_type(vm, vim.Datacenter) mac_address = utils_vm.get_mac_address(vm) network = utils_vm.get_network(vm) - tags = [] - for tag in vm.tag: - tags.append(tag.name) + tags = get_tags(vm._moId) folder_path = utils_common.get_path(vm, service_instance) info[vm.summary.config.name] = { "guest_name": vm.summary.config.name, @@ -339,6 +339,23 @@ def info(vm_name=None, service_instance=None, profile=None): } return info +def get_tags(vm_mid): + """ + Return the tags associated with a virtual machine. + + vm_mid + The managed object ID of the virtual machine. + """ + tags = [] + client = connect.api_client(opts=__opts__, pillar=__pillar__) + dynamic_managed_object = DynamicID(type="VirtualMachine", id=vm_mid) + tag_association = client.tagging.TagAssociation + tags_list = tag_association.list_attached_tags(dynamic_managed_object) + + for tag in tags_list: + tag_name = client.tagging.Tag.get(tag).name + tags.append(tag_name) + return tags def power_state(vm_name, state, datacenter_name=None, service_instance=None, profile=None): """ diff --git a/src/saltext/vmware/utils/connect.py b/src/saltext/vmware/utils/connect.py index 44e28272..1a2c7bf2 100644 --- a/src/saltext/vmware/utils/connect.py +++ b/src/saltext/vmware/utils/connect.py @@ -5,10 +5,13 @@ import ssl import requests +from com.vmware.vapi.std.errors_client import Unauthorized, Unauthenticated from requests.auth import HTTPBasicAuth from requests.exceptions import HTTPError from requests.exceptions import RequestException from requests.exceptions import SSLError +from vmware.vapi.vsphere.client import create_vsphere_client + # pylint: disable=no-name-in-module try: @@ -276,3 +279,56 @@ def _get_session(host, user, password, cert): log.error(re) result = {"error": "Error occurred while calling vCenter API."} return result + +def api_client(opts=None, pillar=None): + """ + Return an API client object for VMware + + opts + (optional) Any additional options. + + pillar + (optional) If specified, allows for a dictionary of pillar data to be made + available to pillar and ext_pillar rendering. These pillar variables + will also override any variables of the same name in pillar or + ext_pillar. + """ + host = ( + os.environ.get("VMWARE_CONFIG_HOST") + or opts.get("vmware_config", {}).get("host") + or pillar.get("vmware_config", {}).get("host") + ) + user, password = get_username_password(esxi_host=host, opts=opts, pillar=pillar) + + cert = ( + os.environ.get("VMWARE_CONFIG_REST_API_CERT") + or opts.get("vmware_config", {}).get("rest_api_cert") + or pillar.get("vmware_config", {}).get("rest_api_cert") + ) + + session = requests.session() + if not cert: + session.verify = False + # Disable environment variable for proxy + session.trust_env = False + # Disable urllib3 warnings + requests.packages.urllib3.disable_warnings() + + config = { + "server": host, + "username": user, + "password": password, + "session": session, + } + + try: + client = create_vsphere_client(**config) + except Unauthorized as e: + log.error(e) + result = {"error": "Unauthorized, check credentials."} + return result + except Unauthenticated as e: + log.error(e) + result = {"error": "Unauthenticated, check credentials."} + return result + return client