vultr: improve handling of dicontinued plans (#66792)

* vultr: improve handling of dicontinued plans

* add changelog
This commit is contained in:
René Moser 2020-01-28 10:46:17 +01:00 committed by GitHub
parent 2dc9841806
commit 78e666dd39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 20 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- vultr_server_info, vultr_server - Improved handling of discontinued plans (https://github.com/ansible/ansible/issues/66707).

View file

@ -216,7 +216,7 @@ class Vultr:
except ValueError as e:
self.module.fail_json(msg="Could not process response into json: %s" % e)
def query_resource_by_key(self, key, value, resource='regions', query_by='list', params=None, use_cache=False, id_key=None):
def query_resource_by_key(self, key, value, resource='regions', query_by='list', params=None, use_cache=False, id_key=None, optional=False):
if not value:
return {}
@ -246,12 +246,13 @@ class Vultr:
return r_data
if id_key is not None and to_text(r_data[id_key]) == to_text(value):
return r_data
if id_key:
msg = "Could not find %s with ID or %s: %s" % (resource, key, value)
else:
msg = "Could not find %s with %s: %s" % (resource, key, value)
self.module.fail_json(msg=msg)
if not optional:
if id_key:
msg = "Could not find %s with ID or %s: %s" % (resource, key, value)
else:
msg = "Could not find %s with %s: %s" % (resource, key, value)
self.module.fail_json(msg=msg)
return {}
@staticmethod
def normalize_result(resource, schema, remove_missing_keys=True):
@ -288,14 +289,16 @@ class Vultr:
return self.result
def get_plan(self, plan=None, key='name'):
def get_plan(self, plan=None, key='name', optional=False):
value = plan or self.module.params.get('plan')
return self.query_resource_by_key(
key=key,
value=value,
resource='plans',
use_cache=True
use_cache=True,
id_key='VPSPLANID',
optional=optional,
)
def get_firewallgroup(self, firewallgroup=None, key='description'):

View file

@ -452,15 +452,6 @@ class AnsibleVultrServer(Vultr):
id_key='DCID',
)
def get_plan(self):
return self.query_resource_by_key(
key='name',
value=self.module.params.get('plan'),
resource='plans',
use_cache=True,
id_key='VPSPLANID',
)
def get_firewall_group(self):
return self.query_resource_by_key(
key='description',
@ -642,7 +633,16 @@ class AnsibleVultrServer(Vultr):
return server
def _update_plan_setting(self, server, start_server):
plan = self.get_plan()
# Verify the exising plan is not discontined by Vultr and therefore won't be found by the API
server_plan = self.get_plan(plan=server.get('VPSPLANID'), optional=True)
if not server_plan:
plan = self.get_plan(optional=True)
if not plan:
self.module.warn("The plan used to create the server is not longer available as well as the desired plan. Assuming same plan, keeping as is.")
return server
else:
plan = self.get_plan()
plan_changed = True if plan and plan['VPSPLANID'] != server.get('VPSPLANID') else False
if plan_changed:
server, warned = self._handle_power_status_for_update(server, start_server)

View file

@ -165,7 +165,7 @@ class AnsibleVultrServerInfo(Vultr):
return self.get_firewallgroup(firewallgroup, 'FIREWALLGROUPID').get('description')
def _get_plan_name(self, plan):
return self.get_plan(plan, 'VPSPLANID').get('name')
return self.get_plan(plan, 'VPSPLANID', optional=True).get('name') or 'N/A'
def get_servers(self):
return self.api_query(path="/v1/server/list")