From 896e320142586a88efe4cfdae9e269379a177382 Mon Sep 17 00:00:00 2001 From: Zim Kalinowski Date: Mon, 18 Feb 2019 15:14:56 +0800 Subject: [PATCH] adding postgresqlconfiguration facts (#45071) --- .../azure_rm_postgresqlconfiguration_facts.py | 208 ++++++++++++++++++ .../targets/azure_rm_postgresqlserver/aliases | 1 + .../azure_rm_postgresqlserver/tasks/main.yml | 35 +++ 3 files changed, 244 insertions(+) create mode 100644 lib/ansible/modules/cloud/azure/azure_rm_postgresqlconfiguration_facts.py diff --git a/lib/ansible/modules/cloud/azure/azure_rm_postgresqlconfiguration_facts.py b/lib/ansible/modules/cloud/azure/azure_rm_postgresqlconfiguration_facts.py new file mode 100644 index 0000000000..f65c4c853a --- /dev/null +++ b/lib/ansible/modules/cloud/azure/azure_rm_postgresqlconfiguration_facts.py @@ -0,0 +1,208 @@ +#!/usr/bin/python +# +# Copyright (c) 2019 Zim Kalinowski, (@zikalino) +# +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} + + +DOCUMENTATION = ''' +--- +module: azure_rm_postgresqlconfiguration_facts +version_added: "2.8" +short_description: Get Azure PostgreSQL Configuration facts. +description: + - Get facts of Azure PostgreSQL Configuration. + +options: + resource_group: + description: + - The name of the resource group that contains the resource. + required: True + server_name: + description: + - The name of the server. + required: True + name: + description: + - Setting name. + +extends_documentation_fragment: + - azure + +author: + - "Zim Kalinowski (@zikalino)" + +''' + +EXAMPLES = ''' + - name: Get specific setting of PostgreSQL configuration + azure_rm_postgresqlconfiguration_facts: + resource_group: testrg + server_name: testpostgresqlserver + name: deadlock_timeout + + - name: Get all settings of PostgreSQL Configuration + azure_rm_postgresqlconfiguration_facts: + resource_group: testrg + server_name: testpostgresqlserver +''' + +RETURN = ''' +settings: + description: A list of dictionaries containing MySQL Server settings. + returned: always + type: complex + contains: + id: + description: + - Setting resource ID + returned: always + type: str + sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/testrg/providers/Microsoft.DBforPostgreSQL/servers/testpostgresqlser + ver/configurations/deadlock_timeout" + name: + description: + - Setting name. + returned: always + type: str + sample: deadlock_timeout + value: + description: + - Setting value. + returned: always + type: raw + sample: 1000 + description: + description: + - Description of the configuration. + returned: always + type: str + sample: Deadlock timeout. + source: + description: + - Source of the configuration. + returned: always + type: str + sample: system-default +''' + +from ansible.module_utils.azure_rm_common import AzureRMModuleBase + +try: + from msrestazure.azure_exceptions import CloudError + from msrestazure.azure_operation import AzureOperationPoller + from azure.mgmt.rdbms.postgresql import PostgreSQLManagementClient + from msrest.serialization import Model +except ImportError: + # This is handled in azure_rm_common + pass + + +class AzureRMPostgreSQLConfigurationFacts(AzureRMModuleBase): + def __init__(self): + # define user inputs into argument + self.module_arg_spec = dict( + resource_group=dict( + type='str', + required=True + ), + server_name=dict( + type='str', + required=True + ), + name=dict( + type='str' + ) + ) + # store the results of the module operation + self.results = dict( + changed=False + ) + self.mgmt_client = None + self.resource_group = None + self.server_name = None + self.name = None + super(AzureRMPostgreSQLConfigurationFacts, self).__init__(self.module_arg_spec, supports_tags=False) + + def exec_module(self, **kwargs): + for key in self.module_arg_spec: + setattr(self, key, kwargs[key]) + self.mgmt_client = self.get_mgmt_svc_client(PostgreSQLManagementClient, + base_url=self._cloud_environment.endpoints.resource_manager) + + if self.name is not None: + self.results['settings'] = self.get() + else: + self.results['settings'] = self.list_by_server() + return self.results + + def get(self): + ''' + Gets facts of the specified PostgreSQL Configuration. + + :return: deserialized PostgreSQL Configurationinstance state dictionary + ''' + response = None + results = [] + try: + response = self.mgmt_client.configurations.get(resource_group_name=self.resource_group, + server_name=self.server_name, + configuration_name=self.name) + self.log("Response : {0}".format(response)) + except CloudError as e: + self.fail('Could not get requested setting.') + + if response is not None: + results.append(self.format_item(response)) + + return results + + def list_by_server(self): + ''' + Gets facts of the specified PostgreSQL Configuration. + + :return: deserialized PostgreSQL Configurationinstance state dictionary + ''' + response = None + results = [] + try: + response = self.mgmt_client.configurations.list_by_server(resource_group_name=self.resource_group, + server_name=self.server_name) + self.log("Response : {0}".format(response)) + except CloudError as e: + self.fail('Could not get settings for server.') + + if response is not None: + for item in response: + results.append(self.format_item(item)) + + return results + + def format_item(self, item): + d = item.as_dict() + d = { + 'resource_group': self.resource_group, + 'server_name': self.server_name, + 'id': d['id'], + 'name': d['name'], + 'value': d['value'], + 'description': d['description'], + 'source': d['source'] + } + return d + + +def main(): + AzureRMPostgreSQLConfigurationFacts() + + +if __name__ == '__main__': + main() diff --git a/test/integration/targets/azure_rm_postgresqlserver/aliases b/test/integration/targets/azure_rm_postgresqlserver/aliases index 7e5b111d5b..476b361a71 100644 --- a/test/integration/targets/azure_rm_postgresqlserver/aliases +++ b/test/integration/targets/azure_rm_postgresqlserver/aliases @@ -6,3 +6,4 @@ azure_rm_postgresqldatabase azure_rm_postgresqldatabase_facts azure_rm_postgresqlfirewallrule azure_rm_postgresqlfirewallrule_facts +azure_rm_postgresqlserverconfiguration_facts diff --git a/test/integration/targets/azure_rm_postgresqlserver/tasks/main.yml b/test/integration/targets/azure_rm_postgresqlserver/tasks/main.yml index 2bd0778b07..6579f1a3d4 100644 --- a/test/integration/targets/azure_rm_postgresqlserver/tasks/main.yml +++ b/test/integration/targets/azure_rm_postgresqlserver/tasks/main.yml @@ -170,6 +170,41 @@ - output.servers[1]['user_visible_state'] != None - output.servers[1]['fully_qualified_domain_name'] != None +# +# azure_rm_postgresqlconfiguration_facts tests below +# +- name: Gather facts PostgreSQL Configuration + azure_rm_postgresqlconfiguration_facts: + resource_group: "{{ resource_group }}" + server_name: postgresqlsrv{{ rpfx }} + name: deadlock_timeout + register: output +- name: Assert that facts are returned + assert: + that: + - output.changed == False + - output.settings[0].id != None + - output.settings[0].name != None + - output.settings[0].value != None + - output.settings[0].description != None + - output.settings[0].source != None + - output.settings | length == 1 + +- name: Gather facts PostgreSQL Configuration + azure_rm_postgresqlconfiguration_facts: + resource_group: "{{ resource_group }}" + server_name: postgresqlsrv{{ rpfx }} + register: output +- name: Get all settings + assert: + that: + - output.changed == False + - output.settings[0].id != None + - output.settings[0].name != None + - output.settings[0].value != None + - output.settings[0].description != None + - output.settings[0].source != None + - output.settings | length > 1 # # azure_rm_postgresqldatabase tests below