|
4 | 4 | """This module allows users to list Linode regions."""
|
5 | 5 | from __future__ import absolute_import, division, print_function
|
6 | 6 |
|
7 |
| -from typing import Any, Dict, Optional |
8 |
| - |
9 | 7 | import ansible_collections.linode.cloud.plugins.module_utils.doc_fragments.region_list as docs
|
10 |
| -from ansible_collections.linode.cloud.plugins.module_utils.linode_common import ( |
11 |
| - LinodeModuleBase, |
12 |
| -) |
13 |
| -from ansible_collections.linode.cloud.plugins.module_utils.linode_docs import ( |
14 |
| - global_authors, |
15 |
| - global_requirements, |
16 |
| -) |
17 |
| -from ansible_collections.linode.cloud.plugins.module_utils.linode_helper import ( |
18 |
| - construct_api_filter, |
19 |
| - get_all_paginated, |
| 8 | +from ansible_collections.linode.cloud.plugins.module_utils.linode_common_list import ( |
| 9 | + ListModule, |
20 | 10 | )
|
21 |
| -from ansible_specdoc.objects import ( |
22 |
| - FieldType, |
23 |
| - SpecDocMeta, |
24 |
| - SpecField, |
25 |
| - SpecReturnValue, |
26 |
| -) |
27 |
| - |
28 |
| -spec_filter = { |
29 |
| - "name": SpecField( |
30 |
| - type=FieldType.string, |
31 |
| - required=True, |
32 |
| - description=[ |
33 |
| - "The name of the field to filter on.", |
34 |
| - "Valid filterable attributes can be found here: " |
35 |
| - "https://techdocs.akamai.com/linode-api/reference/get-regions", |
36 |
| - ], |
37 |
| - ), |
38 |
| - "values": SpecField( |
39 |
| - type=FieldType.list, |
40 |
| - element_type=FieldType.string, |
41 |
| - required=True, |
42 |
| - description=[ |
43 |
| - "A list of values to allow for this field.", |
44 |
| - "Fields will pass this filter if at least one of these values matches.", |
45 |
| - ], |
46 |
| - ), |
47 |
| -} |
48 | 11 |
|
49 |
| -spec = { |
50 |
| - # Disable the default values |
51 |
| - "state": SpecField(type=FieldType.string, required=False, doc_hide=True), |
52 |
| - "label": SpecField(type=FieldType.string, required=False, doc_hide=True), |
53 |
| - "order": SpecField( |
54 |
| - type=FieldType.string, |
55 |
| - description=["The order to list regions in."], |
56 |
| - default="asc", |
57 |
| - choices=["desc", "asc"], |
58 |
| - ), |
59 |
| - "order_by": SpecField( |
60 |
| - type=FieldType.string, |
61 |
| - description=["The attribute to order regions by."], |
62 |
| - ), |
63 |
| - "filters": SpecField( |
64 |
| - type=FieldType.list, |
65 |
| - element_type=FieldType.dict, |
66 |
| - suboptions=spec_filter, |
67 |
| - description=["A list of filters to apply to the resulting regions."], |
68 |
| - ), |
69 |
| - "count": SpecField( |
70 |
| - type=FieldType.integer, |
71 |
| - description=[ |
72 |
| - "The number of results to return.", |
73 |
| - "If undefined, all results will be returned.", |
74 |
| - ], |
75 |
| - ), |
76 |
| -} |
77 |
| - |
78 |
| -SPECDOC_META = SpecDocMeta( |
79 |
| - description=["List and filter on Linode Regions."], |
80 |
| - requirements=global_requirements, |
81 |
| - author=global_authors, |
82 |
| - options=spec, |
| 12 | +module = ListModule( |
| 13 | + result_display_name="Regions", |
| 14 | + result_field_name="regions", |
| 15 | + endpoint_template="/regions", |
| 16 | + result_docs_url="https://techdocs.akamai.com/linode-api/reference/get-regions", |
83 | 17 | examples=docs.specdoc_examples,
|
84 |
| - return_values={ |
85 |
| - "regions": SpecReturnValue( |
86 |
| - description="The returned regions.", |
87 |
| - docs_url="https://techdocs.akamai.com/linode-api/reference/get-regions", |
88 |
| - type=FieldType.list, |
89 |
| - elements=FieldType.dict, |
90 |
| - sample=docs.result_regions_samples, |
91 |
| - ) |
92 |
| - }, |
| 18 | + result_samples=docs.result_regions_samples, |
93 | 19 | )
|
94 | 20 |
|
| 21 | +SPECDOC_META = module.spec |
| 22 | + |
95 | 23 | DOCUMENTATION = r"""
|
96 | 24 | """
|
97 | 25 | EXAMPLES = r"""
|
98 | 26 | """
|
99 | 27 | RETURN = r"""
|
100 | 28 | """
|
101 | 29 |
|
102 |
| - |
103 |
| -class Module(LinodeModuleBase): |
104 |
| - """Module for getting a list of Linode regions""" |
105 |
| - |
106 |
| - def __init__(self) -> None: |
107 |
| - self.module_arg_spec = SPECDOC_META.ansible_spec |
108 |
| - self.results: Dict[str, Any] = {"regions": []} |
109 |
| - |
110 |
| - super().__init__(module_arg_spec=self.module_arg_spec) |
111 |
| - |
112 |
| - def exec_module(self, **kwargs: Any) -> Optional[dict]: |
113 |
| - """Entrypoint for region list module""" |
114 |
| - |
115 |
| - filter_dict = construct_api_filter(self.module.params) |
116 |
| - |
117 |
| - self.results["regions"] = get_all_paginated( |
118 |
| - self.client, |
119 |
| - "/regions", |
120 |
| - filter_dict, |
121 |
| - num_results=self.module.params["count"], |
122 |
| - ) |
123 |
| - return self.results |
124 |
| - |
125 |
| - |
126 |
| -def main() -> None: |
127 |
| - """Constructs and calls the module""" |
128 |
| - Module() |
129 |
| - |
130 |
| - |
131 | 30 | if __name__ == "__main__":
|
132 |
| - main() |
| 31 | + module.run() |
0 commit comments