From 499355fc13f817edadc64eacb3fb9e67180c3e56 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Fri, 10 May 2019 01:25:03 +0530 Subject: [PATCH] VMware: Cleanup vCloud references (#56268) Signed-off-by: Abhijeet Kasurde --- .../modules/cloud/vmware/vmware_category.py | 1 - .../cloud/vmware/vmware_category_facts.py | 1 - .../modules/cloud/vmware/vmware_tag.py | 1 - .../modules/cloud/vmware/vmware_tag_facts.py | 1 - .../cloud/vmware/vmware_tag_manager.py | 1 - .../plugins/inventory/vmware_vm_inventory.py | 54 +++++-------------- test/integration/targets/vmware_tag/aliases | 2 +- .../targets/vmware_tag/tasks/main.yml | 2 +- .../targets/vmware_tag/tasks/tag_crud_ops.yml | 48 ++++++++++++++--- .../vmware_tag/tasks/tag_manager_ops.yml | 25 ++++++--- 10 files changed, 74 insertions(+), 62 deletions(-) diff --git a/lib/ansible/modules/cloud/vmware/vmware_category.py b/lib/ansible/modules/cloud/vmware/vmware_category.py index d5343ff5d9..591c622b76 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_category.py +++ b/lib/ansible/modules/cloud/vmware/vmware_category.py @@ -32,7 +32,6 @@ requirements: - python >= 2.6 - PyVmomi - vSphere Automation SDK -- vCloud Suite SDK options: category_name: description: diff --git a/lib/ansible/modules/cloud/vmware/vmware_category_facts.py b/lib/ansible/modules/cloud/vmware/vmware_category_facts.py index 60f5480a9f..7c5d7d3bc5 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_category_facts.py +++ b/lib/ansible/modules/cloud/vmware/vmware_category_facts.py @@ -32,7 +32,6 @@ requirements: - python >= 2.6 - PyVmomi - vSphere Automation SDK -- vCloud Suite SDK extends_documentation_fragment: vmware_rest_client.documentation ''' diff --git a/lib/ansible/modules/cloud/vmware/vmware_tag.py b/lib/ansible/modules/cloud/vmware/vmware_tag.py index ff9cc0c925..466bd989b7 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_tag.py +++ b/lib/ansible/modules/cloud/vmware/vmware_tag.py @@ -30,7 +30,6 @@ requirements: - python >= 2.6 - PyVmomi - vSphere Automation SDK -- vCloud Suite SDK options: tag_name: description: diff --git a/lib/ansible/modules/cloud/vmware/vmware_tag_facts.py b/lib/ansible/modules/cloud/vmware/vmware_tag_facts.py index ea5dca8d94..35e64ab568 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_tag_facts.py +++ b/lib/ansible/modules/cloud/vmware/vmware_tag_facts.py @@ -32,7 +32,6 @@ requirements: - python >= 2.6 - PyVmomi - vSphere Automation SDK -- vCloud Suite SDK extends_documentation_fragment: vmware_rest_client.documentation ''' diff --git a/lib/ansible/modules/cloud/vmware/vmware_tag_manager.py b/lib/ansible/modules/cloud/vmware/vmware_tag_manager.py index abe46ccfff..eeef793837 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_tag_manager.py +++ b/lib/ansible/modules/cloud/vmware/vmware_tag_manager.py @@ -31,7 +31,6 @@ requirements: - python >= 2.6 - PyVmomi - vSphere Automation SDK -- vCloud Suite SDK options: tag_names: description: diff --git a/lib/ansible/plugins/inventory/vmware_vm_inventory.py b/lib/ansible/plugins/inventory/vmware_vm_inventory.py index c791bde354..559c666faf 100644 --- a/lib/ansible/plugins/inventory/vmware_vm_inventory.py +++ b/lib/ansible/plugins/inventory/vmware_vm_inventory.py @@ -55,10 +55,9 @@ DOCUMENTATION = ''' with_tags: description: - Include tags and associated virtual machines. - - Requires 'vSphere Automation SDK' and 'vCloud Suite SDK' libraries to be installed on the given controller machine. + - Requires 'vSphere Automation SDK' library to be installed on the given controller machine. - Please refer following URLs for installation steps - 'https://code.vmware.com/web/sdk/65/vsphere-automation-python' - - 'https://code.vmware.com/web/sdk/60/vcloudsuite-python' default: False type: boolean ''' @@ -93,22 +92,13 @@ except ImportError: HAS_PYVMOMI = False 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 - from com.vmware.cis.tagging_client import Tag, TagAssociation - HAS_VCLOUD = True -except ImportError: - HAS_VCLOUD = False - -try: - from vmware.vapi.stdlib.client.factories import StubConfigurationFactory + from vmware.vapi.vsphere.client import create_vsphere_client HAS_VSPHERE = True except ImportError: HAS_VSPHERE = False + from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable @@ -144,28 +134,13 @@ class BaseVMwareInventory: # Disable warning shown at stdout requests.packages.urllib3.disable_warnings() - vcenter_url = "https://%s/api" % self.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(self.username, self.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 = session_svc.create() - - # 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: + client = create_vsphere_client(server=self.hostname, + username=self.username, + password=self.password, + session=session) + if client is None: raise AnsibleError("Failed to login to %s using %s" % (self.hostname, self.username)) - return stub_config + return client def _login(self): """ @@ -235,11 +210,6 @@ class BaseVMwareInventory: " Please refer this URL for installation steps" " - https://code.vmware.com/web/sdk/65/vsphere-automation-python") - if not HAS_VCLOUD and self.with_tags: - raise AnsibleError("Unable to find 'vCloud Suite SDK' Python library which is required." - " Please refer this URL for installation steps" - " - https://code.vmware.com/web/sdk/60/vcloudsuite-python") - if not all([self.hostname, self.username, self.password]): raise AnsibleError("Missing one of the following : hostname, username, password. Please read " "the documentation for more information.") @@ -394,8 +364,8 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): properties=['name']) if self.pyv.with_tags: - tag_svc = Tag(self.pyv.rest_content) - tag_association = TagAssociation(self.pyv.rest_content) + tag_svc = self.pyv.rest_content.tagging.Tag + tag_association = self.pyv.rest_content.tagging.TagAssociation tags_info = dict() tags = tag_svc.list() @@ -428,7 +398,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): self._populate_host_properties(vm_obj, current_host) # Only gather facts related to tag if vCloud and vSphere is installed. - if HAS_VCLOUD and HAS_VSPHERE and self.pyv.with_tags: + if HAS_VSPHERE and self.pyv.with_tags: # Add virtual machine to appropriate tag group vm_mo_id = vm_obj.obj._GetMoId() vm_dynamic_id = DynamicID(type='VirtualMachine', id=vm_mo_id) diff --git a/test/integration/targets/vmware_tag/aliases b/test/integration/targets/vmware_tag/aliases index c2a6921d43..845e8a6dad 100644 --- a/test/integration/targets/vmware_tag/aliases +++ b/test/integration/targets/vmware_tag/aliases @@ -1,2 +1,2 @@ cloud/vcenter -unsupported \ No newline at end of file +unsupported diff --git a/test/integration/targets/vmware_tag/tasks/main.yml b/test/integration/targets/vmware_tag/tasks/main.yml index 1cbd000fe5..95c9753e36 100644 --- a/test/integration/targets/vmware_tag/tasks/main.yml +++ b/test/integration/targets/vmware_tag/tasks/main.yml @@ -3,4 +3,4 @@ # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) - include: tag_crud_ops.yml -- include: tag_manager_ops.yml \ No newline at end of file +- include: tag_manager_ops.yml diff --git a/test/integration/targets/vmware_tag/tasks/tag_crud_ops.yml b/test/integration/targets/vmware_tag/tasks/tag_crud_ops.yml index 36b38757e6..189e8a9809 100644 --- a/test/integration/targets/vmware_tag/tasks/tag_crud_ops.yml +++ b/test/integration/targets/vmware_tag/tasks/tag_crud_ops.yml @@ -1,12 +1,12 @@ # Test code for the vmware_tag CRUD Operations. # Copyright: (c) 2019, Pavan Bidkar +# Copyright: (c) 2019, Abhijeet Kasurde # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) - when: vcsim is not defined block: - - # Testcase Create Category - - name: Create Category + - &cat_create + name: Create Category vmware_category: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' @@ -23,11 +23,20 @@ that: - category_create.changed + - <<: *cat_create + name: Create category again + + - name: Check if no changes are made + assert: + that: + - not category_create.changed + - name: Set Cat_ID Paramter. Required for Tag creation set_fact: Cat_ID={{ category_create['category_results']['category_id'] }} # Testcase Create Tag - - name: Create a tag + - &tag_create + name: Create a tag vmware_tag: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' @@ -44,8 +53,17 @@ that: - tag_creation.changed + - <<: *tag_create + name: Create a tag again + + - name: Check if no changes are made + assert: + that: + - not tag_creation.changed + # Testcase Update Tag Description (reconfig) - - name: Update Tag Description + - &tag_update + name: Update Tag Description vmware_tag: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' @@ -61,8 +79,17 @@ that: - update_tag_desc.changed + - <<: *tag_update + name: Update tag description again + + - name: Check if no changes are made + assert: + that: + - not update_tag_desc.changed + # Testcase Delete the Tag - - name: Delete Tag + - &tag_delete + name: Delete Tag vmware_tag: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' @@ -75,4 +102,11 @@ - name: Check Tag is Deleted assert: that: - - delete_tag.changed \ No newline at end of file + - delete_tag.changed + - <<: *tag_delete + name: Delete tag again + + - name: Check if no changes are made + assert: + that: + - not delete_tag.changed diff --git a/test/integration/targets/vmware_tag/tasks/tag_manager_ops.yml b/test/integration/targets/vmware_tag/tasks/tag_manager_ops.yml index ec68645538..47313323e4 100644 --- a/test/integration/targets/vmware_tag/tasks/tag_manager_ops.yml +++ b/test/integration/targets/vmware_tag/tasks/tag_manager_ops.yml @@ -1,11 +1,10 @@ # Test code for the vmware_tag Manager Operations. # Copyright: (c) 2019, Pavan Bidkar +# Copyright: (c) 2019, Abhijeet Kasurde # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) - when: vcsim is not defined block: - - # Get VM name to attach the tag - name: Get VM Facts vmware_vm_facts: hostname: '{{ vcenter_hostname }}' @@ -17,7 +16,8 @@ - set_fact: vm_name="{{ vm_facts['virtual_machines'][0]['guest_name'] }}" # Get Tagname - - name: Get facts about tag + - &tag_fact + name: Get facts about tag vmware_tag_facts: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' @@ -25,12 +25,17 @@ validate_certs: False register: tag_facts + - <<: *tag_fact + name: Get facts about tag in check mode + check_mode: yes + - set_fact: Tag_Name={{ tag_facts['tag_facts'].keys() | list }} - debug: var=Tag_Name # Testcase Assign tag to virtual Machine - - name: Add tags to a virtual machine + - &tag_assign + name: Add tags to a virtual machine vmware_tag_manager: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' @@ -43,7 +48,15 @@ state: add register: tag_manager_ops - - name: Check Category is created + - name: Check tag is assign to virtual machine assert: that: - - tag_manager_ops.changed" \ No newline at end of file + - tag_manager_ops.changed + + - <<: *tag_assign + name: Add tags to a virtual machine again + + - name: Check tag is not assigned to virtual machine again + assert: + that: + - not tag_manager_ops.changed