From 1f8635fbd9ebac4c7663406425bd98d826a94457 Mon Sep 17 00:00:00 2001 From: Zim Kalinowski Date: Tue, 4 Sep 2018 11:25:01 +0800 Subject: [PATCH] Virtual machine facts - retrieving instance view (#45128) --- lib/ansible/module_utils/azure_rm_common.py | 2 +- .../azure/azure_rm_virtualmachine_facts.py | 22 ++++++++++++++++++- packaging/requirements/requirements-azure.txt | 2 +- .../tasks/virtualmachine.yml | 2 ++ .../requirements/integration.cloud.azure.txt | 2 +- 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/ansible/module_utils/azure_rm_common.py b/lib/ansible/module_utils/azure_rm_common.py index e67cfe21f7c..6385db83d3a 100644 --- a/lib/ansible/module_utils/azure_rm_common.py +++ b/lib/ansible/module_utils/azure_rm_common.py @@ -207,7 +207,7 @@ AZURE_PKG_VERSIONS = { }, 'ComputeManagementClient': { 'package_name': 'compute', - 'expected_version': '2.1.0' + 'expected_version': '3.0.0' }, 'ContainerInstanceManagementClient': { 'package_name': 'containerinstance', diff --git a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine_facts.py b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine_facts.py index b81d29e7716..6deb3060db5 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine_facts.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine_facts.py @@ -181,6 +181,11 @@ vms: - Virtual machine size. type: str sample: Standard_D4 + power_state: + description: + - Power state of the virtual machine. + type: str + sample: running ''' try: @@ -286,10 +291,25 @@ class AzureRMVirtualMachineFacts(AzureRMModuleBase): ''' result = self.serialize_obj(vm, AZURE_OBJECT_CLASS, enum_modules=AZURE_ENUM_MODULES) + resource_group = re.sub('\\/.*', '', re.sub('.*resourceGroups\\/', '', result['id'])) + instance = None + power_state = None + + try: + instance = self.compute_client.virtual_machines.instance_view(resource_group, vm.name) + instance = self.serialize_obj(instance, AZURE_OBJECT_CLASS, enum_modules=AZURE_ENUM_MODULES) + except Exception as exc: + self.fail("Error getting virtual machine {0} instance view - {1}".format(vm.name, str(exc))) + + for index in range(len(instance['statuses'])): + code = instance['statuses'][index]['code'].split('/') + if code[0] == 'PowerState': + power_state = code[1] new_result = {} + new_result['power_state'] = power_state new_result['id'] = vm.id - new_result['resource_group'] = re.sub('\\/.*', '', re.sub('.*resourceGroups\\/', '', result['id'])) + new_result['resource_group'] = resource_group new_result['name'] = vm.name new_result['state'] = 'present' new_result['location'] = vm.location diff --git a/packaging/requirements/requirements-azure.txt b/packaging/requirements/requirements-azure.txt index f288a06039a..02e37156de5 100644 --- a/packaging/requirements/requirements-azure.txt +++ b/packaging/requirements/requirements-azure.txt @@ -4,7 +4,7 @@ azure-cli-core==2.0.35 azure-cli-nspkg==3.0.2 azure-common==1.1.11 azure-mgmt-batch==4.1.0 -azure-mgmt-compute==2.1.0 +azure-mgmt-compute==3.0.0 azure-mgmt-containerinstance==0.4.0 azure-mgmt-containerregistry==2.0.0 azure-mgmt-containerservice==3.0.1 diff --git a/test/integration/targets/azure_rm_virtualmachine/tasks/virtualmachine.yml b/test/integration/targets/azure_rm_virtualmachine/tasks/virtualmachine.yml index 335beea2f36..aac665d9758 100644 --- a/test/integration/targets/azure_rm_virtualmachine/tasks/virtualmachine.yml +++ b/test/integration/targets/azure_rm_virtualmachine/tasks/virtualmachine.yml @@ -352,6 +352,8 @@ - results.vms[0].name == "{{ vm_name2 }}" - results.vms[0].location == 'eastus' - results.vms[0].admin_username == 'adminuser' + - results.vms[0].resource_group == "{{ resource_group }}" + - results.vms[0].power_state != None - name: Should be idempotent with a dual NICs azure_rm_virtualmachine: diff --git a/test/runner/requirements/integration.cloud.azure.txt b/test/runner/requirements/integration.cloud.azure.txt index f288a06039a..02e37156de5 100644 --- a/test/runner/requirements/integration.cloud.azure.txt +++ b/test/runner/requirements/integration.cloud.azure.txt @@ -4,7 +4,7 @@ azure-cli-core==2.0.35 azure-cli-nspkg==3.0.2 azure-common==1.1.11 azure-mgmt-batch==4.1.0 -azure-mgmt-compute==2.1.0 +azure-mgmt-compute==3.0.0 azure-mgmt-containerinstance==0.4.0 azure-mgmt-containerregistry==2.0.0 azure-mgmt-containerservice==3.0.1