Skip to content

Commit

Permalink
Fixed an issue where obtaining flexible VMSS instances failed (#1529)
Browse files Browse the repository at this point in the history
* Fixed an issue where obtaining flexible VMSS instances failed

* Synchronous change azure_rm_virtualmachinescalesetinstance.py

* small change
  • Loading branch information
Fred-sun authored May 22, 2024
1 parent 5571a07 commit ec99336
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 24 deletions.
43 changes: 32 additions & 11 deletions plugins/modules/azure_rm_virtualmachinescalesetinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,24 +295,45 @@ def update_protection_policy(self, instance_id, protect_from_scale_in, protect_f

def format_response(self, item):
d = item.as_dict()
iv = self.mgmt_client.virtual_machine_scale_set_vms.get_instance_view(resource_group_name=self.resource_group,
vm_scale_set_name=self.vmss_name,
instance_id=d.get('instance_id', None)).as_dict()
power_state = ""
for index in range(len(iv['statuses'])):
code = iv['statuses'][index]['code'].split('/')
if code[0] == 'PowerState':
power_state = code[1]
break
d = {
instance = None
power_state = ''
if d.get('provisioning_state', None) is not None:
iv = self.mgmt_client.virtual_machine_scale_set_vms.get_instance_view(resource_group_name=self.resource_group,
vm_scale_set_name=self.vmss_name,
instance_id=d.get('instance_id', None)).as_dict()
for index in range(len(iv['statuses'])):
code = iv['statuses'][index]['code'].split('/')
if code[0] == 'PowerState':
power_state = code[1]
break
else:
try:
instance = self.compute_client.virtual_machines.instance_view(self.resource_group, d.get('instance_id', None)).as_dict()
vm_instance = self.compute_client.virtual_machines.get(self.resource_group, d.get('instance_id', None)).as_dict()
except Exception as exc:
self.fail("Getting Flexible VMSS instance instance failed, name {0} instance view - {1}".format(d.get('instance_id'), str(exc)))

for index in range(len(instance['statuses'])):
code = instance['statuses'][index]['code'].split('/')
if code[0] == 'PowerState':
power_state = code[1]
elif code[0] == 'OSState' and code[1] == 'generalized':
power_state = 'generalized'
break
elif code[0] == 'ProvisioningState' and code[1] == 'failed':
power_state = ''
break
dd = {
'id': d.get('id'),
'tags': d.get('tags'),
'instance_id': d.get('instance_id'),
'latest_model': d.get('latest_model_applied'),
'power_state': power_state,
'protection_policy': d.get('protection_policy')
}
return d
if d.get('provisioning_state') is None:
dd['tags'] = vm_instance.get('tags', None)
return dd


def main():
Expand Down
52 changes: 39 additions & 13 deletions plugins/modules/azure_rm_virtualmachinescalesetinstance_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,35 @@ def list(self):
def format_response(self, item):
d = item.as_dict()

iv = self.mgmt_client.virtual_machine_scale_set_vms.get_instance_view(resource_group_name=self.resource_group,
vm_scale_set_name=self.vmss_name,
instance_id=d.get('instance_id', None)).as_dict()
power_state = ""
for index in range(len(iv['statuses'])):
code = iv['statuses'][index]['code'].split('/')
if code[0] == 'PowerState':
power_state = code[1]
break
d = {
instance = None
power_state = ''
if d.get('provisioning_state') is not None:
iv = self.mgmt_client.virtual_machine_scale_set_vms.get_instance_view(resource_group_name=self.resource_group,
vm_scale_set_name=self.vmss_name,
instance_id=d.get('instance_id', None)).as_dict()
for index in range(len(iv['statuses'])):
code = iv['statuses'][index]['code'].split('/')
if code[0] == 'PowerState':
power_state = code[1]
break
else:
try:
instance = self.compute_client.virtual_machines.instance_view(self.resource_group, d.get('instance_id', None)).as_dict()
vm_instance = self.compute_client.virtual_machines.get(self.resource_group, d.get('instance_id', None)).as_dict()
except Exception as exc:
self.fail("Getting Flexible VMSS instance instance failed, name {0} instance view - {1}".format(d.get('instance_id'), str(exc)))

for index in range(len(instance['statuses'])):
code = instance['statuses'][index]['code'].split('/')
if code[0] == 'PowerState':
power_state = code[1]
elif code[0] == 'OSState' and code[1] == 'generalized':
power_state = 'generalized'
break
elif code[0] == 'ProvisioningState' and code[1] == 'failed':
power_state = ''
break
dd = {
'resource_group': self.resource_group,
'id': d.get('id', None),
'tags': d.get('tags', None),
Expand All @@ -230,10 +249,17 @@ def format_response(self, item):
'provisioning_state': d.get('provisioning_state', None),
'power_state': power_state,
'vm_id': d.get('vm_id', None),
'image_reference': d.get('storage_profile').get('image_reference', None),
'computer_name': d.get('os_profile').get('computer_name', None)
}
return d
if d.get('provisioning_state') is not None:
dd['image_reference'] = d.get('storage_profile').get('image_reference', None)
dd['computer_name'] = d.get('os_profile').get('computer_name', None)
else:
dd['image_reference'] = vm_instance.get('storage_profile').get('image_reference', None)
dd['computer_name'] = vm_instance.get('os_profile').get('computer_name', None)
dd['provisioning_state'] = vm_instance.get('provisioning_state', None)
dd['tags'] = vm_instance.get('tags', None)
dd['vm_id'] = vm_instance.get('vm_id')
return dd


def main():
Expand Down

0 comments on commit ec99336

Please sign in to comment.