Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024 xuzhang3 (@xuzhang3), Fred-sun (@Fred-sun)
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_postgresqlflexiblefirewallrule_info
14 version_added: "2.2.0"
15 short_description: Get Azure PostgreSQL Flexible Firewall Rule facts
16 description:
17 - Get facts of Azure PostgreSQL Flexible Firewall Rule.
18
19 options:
20 resource_group:
21 description:
22 - The name of the resource group.
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 server firewall rule.
33 type: str
34
35 extends_documentation_fragment:
36 - azure.azcollection.azure
37
38 author:
39 - xuzhang3 (@xuzhang3)
40 - Fred-sun (@Fred-sun)
41
42 '''
43
44 EXAMPLES = '''
45 - name: Get instance of PostgreSQL Flexible Firewall Rule
46 azure_rm_postgresqlflexiblefirewallrule_info:
47 resource_group: myResourceGroup
48 server_name: server_name
49 name: firewall_rule_name
50
51 - name: List instances of PostgreSQL Flexible Firewall Rule
52 azure_rm_postgresqlflexiblefirewallrule_info:
53 resource_group: myResourceGroup
54 server_name: server_name
55 '''
56
57 RETURN = '''
58 rules:
59 description:
60 - A list of dictionaries containing facts for PostgreSQL Flexible Firewall Rule.
61 returned: always
62 type: complex
63 contains:
64 id:
65 description:
66 - Resource ID.
67 returned: always
68 type: str
69 sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.DBforPostgreSQL/flexibleServers/flexibled9b/firewallRules/firewalld9b"
70 server_name:
71 description:
72 - The name of the server.
73 returned: always
74 type: str
75 sample: testserver
76 name:
77 description:
78 - Resource name.
79 returned: always
80 type: str
81 sample: rule1
82 start_ip_address:
83 description:
84 - The start IP address of the PostgreSQL firewall rule.
85 returned: always
86 type: str
87 sample: 10.0.0.16
88 end_ip_address:
89 description:
90 - The end IP address of the PostgreSQL firewall rule.
91 returned: always
92 type: str
93 sample: 10.0.0.18
94 '''
95
96 try:
97 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
98 from azure.core.exceptions import ResourceNotFoundError
99 except ImportError:
100 # This is handled in azure_rm_common
101 pass
102
103
104 class AzureRMPostgreSQLFlexibleFirewallRulesInfo(AzureRMModuleBase):
105 def __init__(self):
106 # define user inputs into argument
107 self.module_arg_spec = dict(
108 resource_group=dict(
109 type='str',
110 required=True
111 ),
112 server_name=dict(
113 type='str',
114 required=True
115 ),
116 name=dict(
117 type='str'
118 )
119 )
120 # store the results of the module operation
121 self.results = dict(
122 changed=False
123 )
124 self.resource_group = None
125 self.server_name = None
126 self.name = None
127 super(AzureRMPostgreSQLFlexibleFirewallRulesInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False)
128
129 def exec_module(self, **kwargs):
130 for key in self.module_arg_spec:
131 setattr(self, key, kwargs[key])
132
133 if self.name is not None:
134 self.results['firewall_rules'] = self.get()
135 else:
136 self.results['firewall_rules'] = self.list_by_server()
137 return self.results
138
139 def get(self):
140 response = None
141 try:
142 response = self.postgresql_flexible_client.firewall_rules.get(resource_group_name=self.resource_group,
143 server_name=self.server_name,
144 firewall_rule_name=self.name)
145 self.log("Response : {0}".format(response))
146 except ResourceNotFoundError as e:
147 self.log('Could not get facts for FirewallRules. Exception as {0}'.format(str(e)))
148 return []
149
150 return [self.format_item(response)]
151
152 def list_by_server(self):
153 response = None
154 results = []
155 try:
156 response = self.postgresql_flexible_client.firewall_rules.list_by_server(resource_group_name=self.resource_group,
157 server_name=self.server_name)
158 self.log("Response : {0}".format(response))
159 except Exception as e:
160 self.log('Could not get facts for FirewallRules. Exception as {0}'.format(str(e)))
161 return []
162
163 if response is not None:
164 for item in response:
165 results.append(self.format_item(item))
166
167 return results
168
169 def format_item(self, item):
170 d = item.as_dict()
171 d = {
172 'resource_group': self.resource_group,
173 'id': d['id'],
174 'server_name': self.server_name,
175 'name': d['name'],
176 'start_ip_address': d['start_ip_address'],
177 'end_ip_address': d['end_ip_address']
178 }
179 return d
180
181
182 def main():
183 AzureRMPostgreSQLFlexibleFirewallRulesInfo()
184
185
186 if __name__ == '__main__':
187 main()