From 0b029d6792a4f38fb5ee697acdf68e8bb4a99613 Mon Sep 17 00:00:00 2001 From: Zim Kalinowski Date: Tue, 11 Sep 2018 10:20:27 +0800 Subject: [PATCH] adding tags support to postgresql (#45339) --- .../cloud/azure/azure_rm_postgresqlserver.py | 34 ++++++++++--------- .../azure/azure_rm_postgresqlserver_facts.py | 7 +++- .../azure_rm_mysqlserver/tasks/main.yml | 8 ++--- .../azure_rm_postgresqlserver/tasks/main.yml | 32 ++++++++++++++++- 4 files changed, 58 insertions(+), 23 deletions(-) diff --git a/lib/ansible/modules/cloud/azure/azure_rm_postgresqlserver.py b/lib/ansible/modules/cloud/azure/azure_rm_postgresqlserver.py index bfec587100..9d3c2f6acc 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_postgresqlserver.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_postgresqlserver.py @@ -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)) diff --git a/lib/ansible/modules/cloud/azure/azure_rm_postgresqlserver_facts.py b/lib/ansible/modules/cloud/azure/azure_rm_postgresqlserver_facts.py index 4a14d4fabc..80f0ba9416 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_postgresqlserver_facts.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_postgresqlserver_facts.py @@ -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 diff --git a/test/integration/targets/azure_rm_mysqlserver/tasks/main.yml b/test/integration/targets/azure_rm_mysqlserver/tasks/main.yml index ca74c76094..23dbdac04e 100644 --- a/test/integration/targets/azure_rm_mysqlserver/tasks/main.yml +++ b/test/integration/targets/azure_rm_mysqlserver/tasks/main.yml @@ -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 + \ No newline at end of file diff --git a/test/integration/targets/azure_rm_postgresqlserver/tasks/main.yml b/test/integration/targets/azure_rm_postgresqlserver/tasks/main.yml index c799c7af63..3ecbcd1bb6 100644 --- a/test/integration/targets/azure_rm_postgresqlserver/tasks/main.yml +++ b/test/integration/targets/azure_rm_postgresqlserver/tasks/main.yml @@ -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