adding tags support to postgresql (#45339)

This commit is contained in:
Zim Kalinowski 2018-09-11 10:20:27 +08:00 committed by Yunge Zhu
parent d4ce1b9f31
commit 0b029d6792
4 changed files with 58 additions and 23 deletions

View file

@ -82,6 +82,7 @@ options:
extends_documentation_fragment:
- azure
- azure_tags
author:
- "Zim Kalinowski (@zikalino)"
@ -199,20 +200,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:
@ -242,9 +243,6 @@ class AzureRMServers(AzureRMModuleBase):
old_response = None
response = None
self.mgmt_client = self.get_mgmt_svc_client(PostgreSQLManagementClient,
base_url=self._cloud_environment.endpoints.resource_manager)
resource_group = self.get_resource_group(self.resource_group)
if "location" not in self.parameters:
@ -264,6 +262,9 @@ class AzureRMServers(AzureRMModuleBase):
self.to_do = Actions.Delete
elif self.state == 'present':
self.log("Need to check if PostgreSQL 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):
@ -314,14 +315,15 @@ class AzureRMServers(AzureRMModuleBase):
self.log("Creating / Updating the PostgreSQL Server instance {0}".format(self.name))
try:
self.parameters['tags'] = self.tags
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)
response = self.postgresql_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.postgresql_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)
@ -338,8 +340,8 @@ class AzureRMServers(AzureRMModuleBase):
'''
self.log("Deleting the PostgreSQL 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.postgresql_client.servers.delete(resource_group_name=self.resource_group,
server_name=self.name)
except CloudError as e:
self.log('Error attempting to delete the PostgreSQL Server instance.')
self.fail("Error deleting the PostgreSQL Server instance: {0}".format(str(e)))
@ -355,8 +357,8 @@ class AzureRMServers(AzureRMModuleBase):
self.log("Checking if the PostgreSQL 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.postgresql_client.servers.get(resource_group_name=self.resource_group,
server_name=self.name)
found = True
self.log("Response : {0}".format(response))
self.log("PostgreSQL Server instance : {0} found".format(response.name))

View file

@ -124,6 +124,10 @@ servers:
returned: always
type: str
sample: postgreabdud1223.postgres.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
@ -217,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

@ -271,8 +271,6 @@
resource_group: "{{ resource_group }}"
name: mysqlsrv{{ rpfx }}
state: absent
register: output
- name: Assert the state has changed
assert:
that:
- output.changed == false
async: 400
poll: 0

View file

@ -72,11 +72,28 @@
enforce_ssl: True
admin_username: zimxyz
admin_password: Testpasswordxyz12!
tags:
aaa: bbb
- name: Create second instance of PostgreSQL Server -- add tags
azure_rm_postgresqlserver:
resource_group: "{{ resource_group }}"
name: postgresqlsrv{{ rpfx }}second
sku:
name: B_Gen5_1
tier: Basic
location: westus2
storage_mb: 51200
enforce_ssl: True
admin_username: zimxyz
admin_password: Testpasswordxyz12!
tags:
ccc: ddd
- name: Gather facts PostgreSQL Server
azure_rm_postgresqlserver_facts:
resource_group: "{{ resource_group }}"
name: postgresqlsrv{{ rpfx }}
name: postgresqlsrv{{ rpfx }}second
register: output
- name: Assert that facts are returned
@ -92,6 +109,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 PostgreSQL Server
azure_rm_postgresqlserver_facts:
@ -274,3 +293,14 @@
assert:
that:
- output.changed == false
- name: Delete second instance of PostgreSQL Server
azure_rm_postgresqlserver:
resource_group: "{{ resource_group }}"
name: postgresqlsrv{{ rpfx }}second
state: absent
register: output
- name: Assert the state has changed
assert:
that:
- output.changed