Debian Codegraph @

ansible_collections/community/aws/plugins/modules/storagegateway_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Loic BLOT (@nerzhul) <[email protected]>
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 # This module is sponsored by E.T.A.I. (www.etai.fr)
8
9 DOCUMENTATION = r"""
10 ---
11 module: storagegateway_info
12 version_added: 1.0.0
13 short_description: Fetch AWS Storage Gateway information
14 description:
15 - Fetch AWS Storage Gateway information
16 - Prior to release 5.0.0 this module was called C(community.aws.aws_sgw_info).
17 The usage did not change.
18 author:
19 - Loic Blot (@nerzhul) <[email protected]>
20 options:
21 gather_local_disks:
22 description:
23 - Gather local disks attached to the storage gateway.
24 type: bool
25 required: false
26 default: true
27 gather_tapes:
28 description:
29 - Gather tape information for storage gateways in tape mode.
30 type: bool
31 required: false
32 default: true
33 gather_file_shares:
34 description:
35 - Gather file share information for storage gateways in s3 mode.
36 type: bool
37 required: false
38 default: true
39 gather_volumes:
40 description:
41 - Gather volume information for storage gateways in iSCSI (cached & stored) modes.
42 type: bool
43 required: false
44 default: true
45 extends_documentation_fragment:
46 - amazon.aws.common.modules
47 - amazon.aws.region.modules
48 - amazon.aws.boto3
49 """
50
51 RETURN = r"""
52 gateways:
53 description: list of gateway objects
54 returned: always
55 type: complex
56 contains:
57 gateway_arn:
58 description: "Storage Gateway ARN"
59 returned: always
60 type: str
61 sample: "arn:aws:storagegateway:eu-west-1:123456789012:gateway/sgw-9999F888"
62 gateway_id:
63 description: "Storage Gateway ID"
64 returned: always
65 type: str
66 sample: "sgw-9999F888"
67 gateway_name:
68 description: "Storage Gateway friendly name"
69 returned: always
70 type: str
71 sample: "my-sgw-01"
72 gateway_operational_state:
73 description: "Storage Gateway operational state"
74 returned: always
75 type: str
76 sample: "ACTIVE"
77 gateway_type:
78 description: "Storage Gateway type"
79 returned: always
80 type: str
81 sample: "FILE_S3"
82 file_shares:
83 description: "Storage gateway file shares"
84 returned: when gateway_type == "FILE_S3"
85 type: complex
86 contains:
87 file_share_arn:
88 description: "File share ARN"
89 returned: always
90 type: str
91 sample: "arn:aws:storagegateway:eu-west-1:123456789012:share/share-AF999C88"
92 file_share_id:
93 description: "File share ID"
94 returned: always
95 type: str
96 sample: "share-AF999C88"
97 file_share_status:
98 description: "File share status"
99 returned: always
100 type: str
101 sample: "AVAILABLE"
102 tapes:
103 description: "Storage Gateway tapes"
104 returned: when gateway_type == "VTL"
105 type: complex
106 contains:
107 tape_arn:
108 description: "Tape ARN"
109 returned: always
110 type: str
111 sample: "arn:aws:storagegateway:eu-west-1:123456789012:tape/tape-AF999C88"
112 tape_barcode:
113 description: "Tape ARN"
114 returned: always
115 type: str
116 sample: "tape-AF999C88"
117 tape_size_in_bytes:
118 description: "Tape ARN"
119 returned: always
120 type: int
121 sample: 555887569
122 tape_status:
123 description: "Tape ARN"
124 returned: always
125 type: str
126 sample: "AVAILABLE"
127 local_disks:
128 description: "Storage gateway local disks"
129 returned: always
130 type: complex
131 contains:
132 disk_allocation_type:
133 description: "Disk allocation type"
134 returned: always
135 type: str
136 sample: "CACHE STORAGE"
137 disk_id:
138 description: "Disk ID on the system"
139 returned: always
140 type: str
141 sample: "pci-0000:00:1f.0"
142 disk_node:
143 description: "Disk parent block device"
144 returned: always
145 type: str
146 sample: "/dev/sdb"
147 disk_path:
148 description: "Disk path used for the cache"
149 returned: always
150 type: str
151 sample: "/dev/nvme1n1"
152 disk_size_in_bytes:
153 description: "Disk size in bytes"
154 returned: always
155 type: int
156 sample: 107374182400
157 disk_status:
158 description: "Disk status"
159 returned: always
160 type: str
161 sample: "present"
162 """
163
164 EXAMPLES = r"""
165 # Note: These examples do not set authentication details, see the AWS Guide for details.
166
167 - name: "Get AWS storage gateway information"
168 community.aws.storagegateway_info:
169
170 - name: "Get AWS storage gateway information for region eu-west-3"
171 community.aws.storagegateway_info:
172 region: eu-west-3
173 """
174
175 try:
176 from botocore.exceptions import BotoCoreError
177 from botocore.exceptions import ClientError
178 except ImportError:
179 pass # caught by AnsibleAWSModule
180
181 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
182
183 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
184
185
186 class SGWInformationManager(object):
187 def __init__(self, client, module):
188 self.client = client
189 self.module = module
190 self.name = self.module.params.get("name")
191
192 def fetch(self):
193 gateways = self.list_gateways()
194 for gateway in gateways:
195 if self.module.params.get("gather_local_disks"):
196 self.list_local_disks(gateway)
197 # File share gateway
198 if gateway["gateway_type"] == "FILE_S3" and self.module.params.get("gather_file_shares"):
199 self.list_gateway_file_shares(gateway)
200 # Volume tape gateway
201 elif gateway["gateway_type"] == "VTL" and self.module.params.get("gather_tapes"):
202 self.list_gateway_vtl(gateway)
203 # iSCSI gateway
204 elif gateway["gateway_type"] in ["CACHED", "STORED"] and self.module.params.get("gather_volumes"):
205 self.list_gateway_volumes(gateway)
206
207 self.module.exit_json(gateways=gateways)
208
209 def list_gateways(self):
210 """
211 List all storage gateways for the AWS endpoint.
212 """
213 try:
214 paginator = self.client.get_paginator("list_gateways")
215 response = paginator.paginate(
216 PaginationConfig={
217 "PageSize": 100,
218 }
219 ).build_full_result()
220
221 gateways = []
222 for gw in response["Gateways"]:
223 gateways.append(camel_dict_to_snake_dict(gw))
224
225 return gateways
226
227 except (BotoCoreError, ClientError) as e:
228 self.module.fail_json_aws(e, msg="Couldn't list storage gateways")
229
230 @staticmethod
231 def _read_gateway_fileshare_response(fileshares, aws_reponse):
232 """
233 Read file share objects from AWS API response.
234 Drop the gateway_arn attribute from response, as it will be duplicate with parent object.
235 """
236 for share in aws_reponse["FileShareInfoList"]:
237 share_obj = camel_dict_to_snake_dict(share)
238 if "gateway_arn" in share_obj:
239 del share_obj["gateway_arn"]
240 fileshares.append(share_obj)
241
242 return aws_reponse["NextMarker"] if "NextMarker" in aws_reponse else None
243
244 def list_gateway_file_shares(self, gateway):
245 """
246 List file shares attached to AWS storage gateway when in S3 mode.
247 """
248 try:
249 response = self.client.list_file_shares(GatewayARN=gateway["gateway_arn"], Limit=100)
250
251 gateway["file_shares"] = []
252 marker = self._read_gateway_fileshare_response(gateway["file_shares"], response)
253
254 while marker is not None:
255 response = self.client.list_file_shares(GatewayARN=gateway["gateway_arn"], Marker=marker, Limit=100)
256
257 marker = self._read_gateway_fileshare_response(gateway["file_shares"], response)
258 except (BotoCoreError, ClientError) as e:
259 self.module.fail_json_aws(e, msg="Couldn't list gateway file shares")
260
261 def list_local_disks(self, gateway):
262 """
263 List storage gateway local disks
264 """
265 try:
266 gateway["local_disks"] = [
267 camel_dict_to_snake_dict(disk)
268 for disk in self.client.list_local_disks(GatewayARN=gateway["gateway_arn"])["Disks"]
269 ]
270 except (BotoCoreError, ClientError) as e:
271 self.module.fail_json_aws(e, msg="Couldn't list storage gateway local disks")
272
273 @staticmethod
274 def _read_gateway_tape_response(tapes, aws_response):
275 """
276 Read tape objects from AWS API response.
277 Drop the gateway_arn attribute from response, as it will be duplicate with parent object.
278 """
279 for tape in aws_response["TapeInfos"]:
280 tape_obj = camel_dict_to_snake_dict(tape)
281 if "gateway_arn" in tape_obj:
282 del tape_obj["gateway_arn"]
283 tapes.append(tape_obj)
284
285 return aws_response["Marker"] if "Marker" in aws_response else None
286
287 def list_gateway_vtl(self, gateway):
288 """
289 List VTL & VTS attached to AWS storage gateway in VTL mode
290 """
291 try:
292 response = self.client.list_tapes(Limit=100)
293
294 gateway["tapes"] = []
295 marker = self._read_gateway_tape_response(gateway["tapes"], response)
296
297 while marker is not None:
298 response = self.client.list_tapes(Marker=marker, Limit=100)
299
300 marker = self._read_gateway_tape_response(gateway["tapes"], response)
301 except (BotoCoreError, ClientError) as e:
302 self.module.fail_json_aws(e, msg="Couldn't list storage gateway tapes")
303
304 def list_gateway_volumes(self, gateway):
305 """
306 List volumes attached to AWS storage gateway in CACHED or STORAGE mode
307 """
308 try:
309 paginator = self.client.get_paginator("list_volumes")
310 response = paginator.paginate(
311 GatewayARN=gateway["gateway_arn"],
312 PaginationConfig={
313 "PageSize": 100,
314 },
315 ).build_full_result()
316
317 gateway["volumes"] = []
318 for volume in response["VolumeInfos"]:
319 volume_obj = camel_dict_to_snake_dict(volume)
320 if "gateway_arn" in volume_obj:
321 del volume_obj["gateway_arn"]
322 if "gateway_id" in volume_obj:
323 del volume_obj["gateway_id"]
324
325 gateway["volumes"].append(volume_obj)
326 except (BotoCoreError, ClientError) as e:
327 self.module.fail_json_aws(e, msg="Couldn't list storage gateway volumes")
328
329
330 def main():
331 argument_spec = dict(
332 gather_local_disks=dict(type="bool", default=True),
333 gather_tapes=dict(type="bool", default=True),
334 gather_file_shares=dict(type="bool", default=True),
335 gather_volumes=dict(type="bool", default=True),
336 )
337
338 module = AnsibleAWSModule(
339 argument_spec=argument_spec,
340 supports_check_mode=True,
341 )
342
343 client = module.client("storagegateway")
344
345 if client is None: # this should never happen
346 module.fail_json(msg="Unknown error, failed to create storagegateway client, no information available.")
347
348 SGWInformationManager(client, module).fetch()
349
350
351 if __name__ == "__main__":
352 main()