fixing facts for mysql server (#45374)

This commit is contained in:
Zim Kalinowski 2018-09-11 14:27:50 +08:00 committed by Yunge Zhu
parent 796d8b5dc8
commit 8a221d81dd
3 changed files with 67 additions and 23 deletions

View file

@ -82,6 +82,7 @@ options:
extends_documentation_fragment:
- azure
- azure_tags
author:
- "Zim Kalinowski (@zikalino)"
@ -200,20 +201,20 @@ class AzureRMServers(AzureRMModuleBase):
self.resource_group = None
self.name = None
self.parameters = dict()
self.tags = None
self.results = dict(changed=False)
self.mgmt_client = None
self.state = None
self.to_do = Actions.NoAction
super(AzureRMServers, self).__init__(derived_arg_spec=self.module_arg_spec,
supports_check_mode=True,
supports_tags=False)
supports_tags=True)
def exec_module(self, **kwargs):
"""Main module execution method"""
for key in list(self.module_arg_spec.keys()):
for key in list(self.module_arg_spec.keys()) + ['tags']:
if hasattr(self, key):
setattr(self, key, kwargs[key])
elif kwargs[key] is not None:
@ -243,9 +244,6 @@ class AzureRMServers(AzureRMModuleBase):
old_response = None
response = None
self.mgmt_client = self.get_mgmt_svc_client(MySQLManagementClient,
base_url=self._cloud_environment.endpoints.resource_manager)
resource_group = self.get_resource_group(self.resource_group)
if "location" not in self.parameters:
@ -265,6 +263,9 @@ class AzureRMServers(AzureRMModuleBase):
self.to_do = Actions.Delete
elif self.state == 'present':
self.log("Need to check if MySQL Server instance has to be deleted or may be updated")
update_tags, newtags = self.update_tags(old_response.get('tags', {}))
if update_tags:
self.tags = newtags
self.to_do = Actions.Update
if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
@ -315,14 +316,15 @@ class AzureRMServers(AzureRMModuleBase):
self.log("Creating / Updating the MySQL Server instance {0}".format(self.name))
try:
if (self.to_do == Actions.Create):
response = self.mgmt_client.servers.create(resource_group_name=self.resource_group,
server_name=self.name,
parameters=self.parameters)
self.parameters['tags'] = self.tags
if self.to_do == Actions.Create:
response = self.mysql_client.servers.create(resource_group_name=self.resource_group,
server_name=self.name,
parameters=self.parameters)
else:
response = self.mgmt_client.servers.update(resource_group_name=self.resource_group,
server_name=self.name,
parameters=self.parameters)
response = self.mysql_client.servers.update(resource_group_name=self.resource_group,
server_name=self.name,
parameters=self.parameters)
if isinstance(response, LROPoller):
response = self.get_poller_result(response)
@ -339,8 +341,8 @@ class AzureRMServers(AzureRMModuleBase):
'''
self.log("Deleting the MySQL Server instance {0}".format(self.name))
try:
response = self.mgmt_client.servers.delete(resource_group_name=self.resource_group,
server_name=self.name)
response = self.mysql_client.servers.delete(resource_group_name=self.resource_group,
server_name=self.name)
except CloudError as e:
self.log('Error attempting to delete the MySQL Server instance.')
self.fail("Error deleting the MySQL Server instance: {0}".format(str(e)))
@ -356,8 +358,8 @@ class AzureRMServers(AzureRMModuleBase):
self.log("Checking if the MySQL Server instance {0} is present".format(self.name))
found = False
try:
response = self.mgmt_client.servers.get(resource_group_name=self.resource_group,
server_name=self.name)
response = self.mysql_client.servers.get(resource_group_name=self.resource_group,
server_name=self.name)
found = True
self.log("Response : {0}".format(response))
self.log("MySQL Server instance : {0} found".format(response.name))

View file

@ -63,7 +63,13 @@ servers:
- Resource ID
returned: always
type: str
sample: /subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/TestGroup/providers/Microsoft.DBforMySQL/servers/myabdud1223
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/TestGroup/providers/Microsoft.DBforMySQL/servers/myabdud1223
resource_group:
description:
- Resource group name.
returned: always
type: str
sample: testresourcegroup
name:
description:
- Resource name.
@ -118,6 +124,10 @@ servers:
returned: always
type: str
sample: myabdud1223.mys.database.azure.com
tags:
description: Tags assigned to the resource. Dictionary of string:string pairs.
type: dict
sample: { tag1: abc }
'''
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
@ -189,7 +199,7 @@ class AzureRMServersFacts(AzureRMModuleBase):
response = self.mysql_client.servers.list_by_resource_group(resource_group_name=self.resource_group)
self.log("Response : {0}".format(response))
except CloudError as e:
self.fail("Error listing for resource group {0} - {1}".format(self.resource_group, str(e)))
self.log('Could not get facts for MySQL Servers.')
if response is not None:
for item in response:
@ -211,7 +221,8 @@ class AzureRMServersFacts(AzureRMModuleBase):
'enforce_ssl': (d['ssl_enforcement'] == 'Enabled'),
'admin_username': d['administrator_login'],
'user_visible_state': d['user_visible_state'],
'fully_qualified_domain_name': d['fully_qualified_domain_name']
'fully_qualified_domain_name': d['fully_qualified_domain_name'],
'tags': d.get('tags')
}
return d

View file

@ -76,11 +76,29 @@
enforce_ssl: True
admin_username: zimxyz
admin_password: Testpasswordxyz12!
tags:
aaa: bbb
- name: Create second instance of MySQL Server
azure_rm_mysqlserver:
resource_group: "{{ resource_group }}"
name: mysqlsrv{{ rpfx }}second
sku:
name: B_Gen5_1
tier: Basic
location: westus2
storage_mb: 51200
version: 5.6
enforce_ssl: True
admin_username: zimxyz
admin_password: Testpasswordxyz12!
tags:
ccc: ddd
- name: Gather facts MySQL Server
azure_rm_mysqlserver_facts:
resource_group: "{{ resource_group }}"
name: mysqlsrv{{ rpfx }}
name: mysqlsrv{{ rpfx }}second
register: output
- name: Assert that facts are returned
@ -96,6 +114,8 @@
- output.servers[0]['version'] != None
- output.servers[0]['user_visible_state'] != None
- output.servers[0]['fully_qualified_domain_name'] != None
- output.servers[0]['tags']['aaa'] == 'bbb'
- output.servers[0]['tags']['ccc'] == 'ddd'
- name: Gather facts MySQL Server
azure_rm_mysqlserver_facts:
@ -241,8 +261,9 @@
- output.changed == false
#
# clean up azure_rm_mysqlserver test
# azure_rm_mysqlserver continuation / clean up
#
- name: Delete instance of MySQL Server -- check mode
azure_rm_mysqlserver:
resource_group: "{{ resource_group }}"
@ -271,6 +292,16 @@
resource_group: "{{ resource_group }}"
name: mysqlsrv{{ rpfx }}
state: absent
register: output
- name: Assert the state has changed
assert:
that:
- output.changed == false
- name: Delete second instance of MySQL Server
azure_rm_mysqlserver:
resource_group: "{{ resource_group }}"
name: mysqlsrv{{ rpfx }}second
state: absent
async: 400
poll: 0