Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/saltext/vmware/modules/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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):
"""
Expand Down
56 changes: 56 additions & 0 deletions src/saltext/vmware/utils/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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