mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-26 12:21:26 -07:00
VMware: Modifying Rest Client to use vSphere-Api-Client instead of individual service(#55804)
* Same api client can be used for other service as well * Incorporated Review comments. Modified Category and Guest Fact modules which are also dependent on vmware_rest_client module util * Adding Integration Tests for vmware_rest_client changes * Changes to incroporate changes in vcsim testware * Change to get vm name to attach the tag
This commit is contained in:
parent
0bead3672f
commit
34a8594c91
11 changed files with 178 additions and 95 deletions
|
@ -25,21 +25,10 @@ except ImportError:
|
|||
PYVMOMI_IMP_ERR = traceback.format_exc()
|
||||
HAS_PYVMOMI = False
|
||||
|
||||
VCLOUD_IMP_ERR = None
|
||||
try:
|
||||
from vmware.vapi.lib.connect import get_requests_connector
|
||||
from vmware.vapi.security.session import create_session_security_context
|
||||
from vmware.vapi.security.user_password import create_user_password_security_context
|
||||
from com.vmware.cis_client import Session
|
||||
from com.vmware.vapi.std_client import DynamicID
|
||||
HAS_VCLOUD = True
|
||||
except ImportError:
|
||||
VCLOUD_IMP_ERR = traceback.format_exc()
|
||||
HAS_VCLOUD = False
|
||||
|
||||
VSPHERE_IMP_ERR = None
|
||||
try:
|
||||
from vmware.vapi.stdlib.client.factories import StubConfigurationFactory
|
||||
from com.vmware.vapi.std_client import DynamicID
|
||||
from vmware.vapi.vsphere.client import create_vsphere_client
|
||||
HAS_VSPHERE = True
|
||||
except ImportError:
|
||||
VSPHERE_IMP_ERR = traceback.format_exc()
|
||||
|
@ -58,7 +47,7 @@ class VmwareRestClient(object):
|
|||
self.module = module
|
||||
self.params = module.params
|
||||
self.check_required_library()
|
||||
self.connect = self.connect_to_rest()
|
||||
self.api_client = self.connect_to_vsphere_client()
|
||||
|
||||
def check_required_library(self):
|
||||
"""
|
||||
|
@ -76,59 +65,6 @@ class VmwareRestClient(object):
|
|||
msg=missing_required_lib('vSphere Automation SDK',
|
||||
url='https://code.vmware.com/web/sdk/65/vsphere-automation-python'),
|
||||
exception=VSPHERE_IMP_ERR)
|
||||
if not HAS_VCLOUD:
|
||||
self.module.fail_json(
|
||||
msg=missing_required_lib('vCloud Suite SDK',
|
||||
url='https://code.vmware.com/web/sdk/60/vcloudsuite-python'),
|
||||
exception=VCLOUD_IMP_ERR)
|
||||
|
||||
def connect_to_rest(self):
|
||||
"""
|
||||
Connect to server using username and password
|
||||
|
||||
"""
|
||||
session = requests.Session()
|
||||
session.verify = self.params.get('validate_certs')
|
||||
|
||||
username = self.params.get('username', None)
|
||||
password = self.params.get('password', None)
|
||||
protocol = self.params.get('protocol', 'https')
|
||||
hostname = self.params.get('hostname')
|
||||
|
||||
if not all([self.params.get('hostname', None), username, password]):
|
||||
self.module.fail_json(msg="Missing one of the following : hostname, username, password."
|
||||
" Please read the documentation for more information.")
|
||||
|
||||
vcenter_url = "%s://%s/api" % (protocol, hostname)
|
||||
|
||||
# Get request connector
|
||||
connector = get_requests_connector(session=session, url=vcenter_url)
|
||||
# Create standard Configuration
|
||||
stub_config = StubConfigurationFactory.new_std_configuration(connector)
|
||||
# Use username and password in the security context to authenticate
|
||||
security_context = create_user_password_security_context(username, password)
|
||||
# Login
|
||||
stub_config.connector.set_security_context(security_context)
|
||||
# Create the stub for the session service and login by creating a session.
|
||||
session_svc = Session(stub_config)
|
||||
session_id = None
|
||||
try:
|
||||
session_id = session_svc.create()
|
||||
except OSError as os_err:
|
||||
self.module.fail_json(msg="Failed to login to %s: %s" % (hostname,
|
||||
to_native(os_err)))
|
||||
|
||||
if session_id is None:
|
||||
self.module.fail_json(msg="Failed to create session using provided credentials."
|
||||
" Please check hostname, username and password.")
|
||||
# After successful authentication, store the session identifier in the security
|
||||
# context of the stub and use that for all subsequent remote requests
|
||||
session_security_context = create_session_security_context(session_id)
|
||||
stub_config.connector.set_security_context(session_security_context)
|
||||
|
||||
if stub_config is None:
|
||||
self.module.fail_json(msg="Failed to login to %s" % hostname)
|
||||
return stub_config
|
||||
|
||||
@staticmethod
|
||||
def vmware_client_argument_spec():
|
||||
|
@ -150,6 +86,31 @@ class VmwareRestClient(object):
|
|||
default=True),
|
||||
)
|
||||
|
||||
def connect_to_vsphere_client(self):
|
||||
"""
|
||||
Connect to vSphere API Client with Username and Password
|
||||
|
||||
"""
|
||||
username = self.params.get('username')
|
||||
password = self.params.get('password')
|
||||
hostname = self.params.get('hostname')
|
||||
session = requests.Session()
|
||||
session.verify = self.params.get('validate_certs')
|
||||
|
||||
if not all([hostname, username, password]):
|
||||
self.module.fail_json(msg="Missing one of the following : hostname, username, password."
|
||||
" Please read the documentation for more information.")
|
||||
|
||||
client = create_vsphere_client(
|
||||
server=hostname,
|
||||
username=username,
|
||||
password=password,
|
||||
session=session)
|
||||
if client is None:
|
||||
self.module.fail_json(msg="Failed to login to %s" % hostname)
|
||||
|
||||
return client
|
||||
|
||||
def get_tags_for_object(self, tag_service, tag_assoc_svc, dobj):
|
||||
"""
|
||||
Return list of tag objects associated with an object
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue