Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_postgresqldatabase_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2017 Zim Kalinowski, <[email protected]>
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_postgresqldatabase_info
14 version_added: "0.1.2"
15 short_description: Get Azure PostgreSQL Database facts
16 description:
17 - Get facts of PostgreSQL Database.
18
19 options:
20 resource_group:
21 description:
22 - The name of the resource group that contains the resource. You can obtain this value from the Azure Resource Manager API or the portal.
23 required: True
24 type: str
25 server_name:
26 description:
27 - The name of the server.
28 required: True
29 type: str
30 name:
31 description:
32 - The name of the database.
33 type: str
34
35 extends_documentation_fragment:
36 - azure.azcollection.azure
37
38 author:
39 - Zim Kalinowski (@zikalino)
40
41 '''
42
43 EXAMPLES = '''
44 - name: Get instance of PostgreSQL Database
45 azure_rm_postgresqldatabase_info:
46 resource_group: myResourceGroup
47 server_name: server_name
48 name: database_name
49
50 - name: List instances of PostgreSQL Database
51 azure_rm_postgresqldatabase_info:
52 resource_group: myResourceGroup
53 server_name: server_name
54 '''
55
56 RETURN = '''
57 databases:
58 description:
59 - A list of dict results where the key is the name of the PostgreSQL Database and the values are the facts for that PostgreSQL Database.
60 returned: always
61 type: complex
62 contains:
63 id:
64 description:
65 - Resource ID.
66 returned: always
67 type: str
68 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.DBforPostgreSQL/servers/testser
69 ver/databases/db1"
70 resource_group:
71 description:
72 - Resource group name.
73 returned: always
74 type: str
75 sample: testrg
76 server_name:
77 description:
78 - Server name.
79 returned: always
80 type: str
81 sample: testserver
82 name:
83 description:
84 - Resource name.
85 returned: always
86 type: str
87 sample: db1
88 charset:
89 description:
90 - The charset of the database.
91 returned: always
92 type: str
93 sample: UTF8
94 collation:
95 description:
96 - The collation of the database.
97 returned: always
98 type: str
99 sample: English_United States.1252
100 '''
101
102 try:
103 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
104 from azure.core.exceptions import ResourceNotFoundError
105 except ImportError:
106 # This is handled in azure_rm_common
107 pass
108
109
110 class AzureRMPostgreSqlDatabasesInfo(AzureRMModuleBase):
111 def __init__(self):
112 # define user inputs into argument
113 self.module_arg_spec = dict(
114 resource_group=dict(
115 type='str',
116 required=True
117 ),
118 server_name=dict(
119 type='str',
120 required=True
121 ),
122 name=dict(
123 type='str'
124 )
125 )
126 # store the results of the module operation
127 self.results = dict(
128 changed=False
129 )
130 self.resource_group = None
131 self.server_name = None
132 self.name = None
133 super(AzureRMPostgreSqlDatabasesInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False)
134
135 def exec_module(self, **kwargs):
136
137 for key in self.module_arg_spec:
138 setattr(self, key, kwargs[key])
139
140 if (self.resource_group is not None and
141 self.server_name is not None and
142 self.name is not None):
143 self.results['databases'] = self.get()
144 elif (self.resource_group is not None and
145 self.server_name is not None):
146 self.results['databases'] = self.list_by_server()
147 return self.results
148
149 def get(self):
150 response = None
151 results = []
152 try:
153 response = self.postgresql_client.databases.get(resource_group_name=self.resource_group,
154 server_name=self.server_name,
155 database_name=self.name)
156 self.log("Response : {0}".format(response))
157 except ResourceNotFoundError as e:
158 self.log('Could not get facts for Databases.')
159
160 if response is not None:
161 results.append(self.format_item(response))
162
163 return results
164
165 def list_by_server(self):
166 response = None
167 results = []
168 try:
169 response = self.postgresql_client.databases.list_by_server(resource_group_name=self.resource_group,
170 server_name=self.server_name)
171 self.log("Response : {0}".format(response))
172 except Exception as e:
173 self.fail("Error listing for server {0} - {1}".format(self.server_name, str(e)))
174
175 if response is not None:
176 for item in response:
177 results.append(self.format_item(item))
178
179 return results
180
181 def format_item(self, item):
182 d = item.as_dict()
183 d = {
184 'resource_group': self.resource_group,
185 'server_name': self.server_name,
186 'name': d['name'],
187 'charset': d['charset'],
188 'collation': d['collation']
189 }
190 return d
191
192
193 def main():
194 AzureRMPostgreSqlDatabasesInfo()
195
196
197 if __name__ == '__main__':
198 main()