Skip to content

Commit ce05f8a

Browse files
Migrated VLAN-related list module to new framework (#579)
* Migrated vlan info and list modules * Fixed docs
1 parent abd4b35 commit ce05f8a

File tree

5 files changed

+55
-203
lines changed

5 files changed

+55
-203
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Name | Description |
110110
[linode.cloud.token_list](./docs/modules/token_list.md)|List and filter on Tokens.|
111111
[linode.cloud.type_list](./docs/modules/type_list.md)|List and filter on Types.|
112112
[linode.cloud.user_list](./docs/modules/user_list.md)|List and filter on Users.|
113-
[linode.cloud.vlan_list](./docs/modules/vlan_list.md)|List and filter on Linode VLANs.|
113+
[linode.cloud.vlan_list](./docs/modules/vlan_list.md)|List and filter on VLANs.|
114114
[linode.cloud.volume_list](./docs/modules/volume_list.md)|List and filter on Linode Volumes.|
115115
[linode.cloud.vpc_ip_list](./docs/modules/vpc_ip_list.md)|List and filter on VPC IP Addresses.|
116116
[linode.cloud.vpc_list](./docs/modules/vpc_list.md)|List and filter on VPCs.|

docs/modules/vlan_info.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
Get info about a Linode VLAN.
44

5-
WARNING! This module makes use of beta endpoints and requires the C(api_version) field be explicitly set to C(v4beta).
6-
75
- [Minimum Required Fields](#minimum-required-fields)
86
- [Examples](#examples)
97
- [Parameters](#parameters)
@@ -28,11 +26,12 @@ WARNING! This module makes use of beta endpoints and requires the C(api_version)
2826
2927
| Field | Type | Required | Description |
3028
|-----------|------|----------|------------------------------------------------------------------------------|
31-
| `label` | <center>`str`</center> | <center>**Required**</center> | The VLAN’s label. |
29+
| `label` | <center>`str`</center> | <center>Optional</center> | The label of the VLAN to resolve. **(Conflicts With: `id`)** |
30+
| `id` | <center>`int`</center> | <center>Optional</center> | The ID of the VLAN to resolve. **(Conflicts With: `label`)** |
3231

3332
## Return Values
3433

35-
- `vlan` - The VLAN in JSON serialized form.
34+
- `vlan` - The returned VLAN.
3635

3736
- Sample Response:
3837
```json

docs/modules/vlan_list.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# vlan_list
22

3-
List and filter on Linode VLANs.
4-
5-
WARNING! This module makes use of beta endpoints and requires the C(api_version) field be explicitly set to C(v4beta).
3+
List and filter on VLANs.
64

75
- [Minimum Required Fields](#minimum-required-fields)
86
- [Examples](#examples)
@@ -40,13 +38,13 @@ WARNING! This module makes use of beta endpoints and requires the C(api_version)
4038
| `order` | <center>`str`</center> | <center>Optional</center> | The order to list VLANs in. **(Choices: `desc`, `asc`; Default: `asc`)** |
4139
| `order_by` | <center>`str`</center> | <center>Optional</center> | The attribute to order VLANs by. |
4240
| [`filters` (sub-options)](#filters) | <center>`list`</center> | <center>Optional</center> | A list of filters to apply to the resulting VLANs. |
43-
| `count` | <center>`int`</center> | <center>Optional</center> | The number of results to return. If undefined, all results will be returned. |
41+
| `count` | <center>`int`</center> | <center>Optional</center> | The number of VLANs to return. If undefined, all results will be returned. |
4442

4543
### filters
4644

4745
| Field | Type | Required | Description |
4846
|-----------|------|----------|------------------------------------------------------------------------------|
49-
| `name` | <center>`str`</center> | <center>**Required**</center> | The name of the field to filter on. Valid filterable attributes can be found here: https://techdocs.akamai.com/linode-api/reference/get-vlans |
47+
| `name` | <center>`str`</center> | <center>**Required**</center> | The name of the field to filter on. Valid filterable fields can be found [here](https://techdocs.akamai.com/linode-api/reference/get-vlans). |
5048
| `values` | <center>`list`</center> | <center>**Required**</center> | A list of values to allow for this field. Fields will pass this filter if at least one of these values matches. |
5149

5250
## Return Values

plugins/modules/vlan_info.py

+37-78
Original file line numberDiff line numberDiff line change
@@ -5,99 +5,58 @@
55

66
from __future__ import absolute_import, division, print_function
77

8-
from typing import Any, List, Optional
9-
108
import ansible_collections.linode.cloud.plugins.module_utils.doc_fragments.vlan_info as docs
11-
from ansible_collections.linode.cloud.plugins.module_utils.linode_common import (
12-
LinodeModuleBase,
13-
)
14-
from ansible_collections.linode.cloud.plugins.module_utils.linode_docs import (
15-
BETA_DISCLAIMER,
16-
global_authors,
17-
global_requirements,
9+
from ansible_collections.linode.cloud.plugins.module_utils.linode_common_info import (
10+
InfoModule,
11+
InfoModuleAttr,
12+
InfoModuleResult,
1813
)
19-
from ansible_specdoc.objects import (
20-
FieldType,
21-
SpecDocMeta,
22-
SpecField,
23-
SpecReturnValue,
14+
from ansible_collections.linode.cloud.plugins.module_utils.linode_helper import (
15+
safe_find,
2416
)
17+
from ansible_specdoc.objects import FieldType
2518
from linode_api4 import VLAN
2619

27-
linode_vlan_info_spec = {
28-
# We need to overwrite attributes to exclude them as requirements
29-
"state": SpecField(type=FieldType.string, required=False, doc_hide=True),
30-
"label": SpecField(
31-
type=FieldType.string, required=True, description=["The VLAN’s label."]
20+
module = InfoModule(
21+
primary_result=InfoModuleResult(
22+
field_name="vlan",
23+
field_type=FieldType.dict,
24+
display_name="VLAN",
25+
docs_url="https://techdocs.akamai.com/linode-api/reference/get-vlans",
26+
samples=docs.result_vlan_samples,
3227
),
33-
}
34-
35-
SPECDOC_META = SpecDocMeta(
36-
description=[
37-
"Get info about a Linode VLAN.",
38-
BETA_DISCLAIMER,
28+
attributes=[
29+
InfoModuleAttr(
30+
display_name="ID",
31+
name="id",
32+
type=FieldType.integer,
33+
get=lambda client, params: client.load(
34+
VLAN,
35+
params.get("id"),
36+
)._raw_json,
37+
),
38+
InfoModuleAttr(
39+
display_name="label",
40+
name="label",
41+
type=FieldType.string,
42+
get=lambda client, params: safe_find(
43+
client.networking.vlans,
44+
VLAN.label == params.get("label"),
45+
raise_not_found=True,
46+
)._raw_json,
47+
),
3948
],
40-
requirements=global_requirements,
41-
author=global_authors,
42-
options=linode_vlan_info_spec,
4349
examples=docs.specdoc_examples,
44-
return_values={
45-
"vlan": SpecReturnValue(
46-
description="The VLAN in JSON serialized form.",
47-
docs_url="https://techdocs.akamai.com/linode-api/reference/get-vlans",
48-
type=FieldType.dict,
49-
sample=docs.result_vlan_samples,
50-
)
51-
},
5250
)
5351

52+
SPECDOC_META = module.spec
53+
5454
DOCUMENTATION = r"""
5555
"""
5656
EXAMPLES = r"""
5757
"""
5858
RETURN = r"""
5959
"""
6060

61-
62-
class LinodeVLANInfo(LinodeModuleBase):
63-
"""Module for getting info about a Linode VLAN"""
64-
65-
def __init__(self) -> None:
66-
self.module_arg_spec = SPECDOC_META.ansible_spec
67-
self.required_one_of: List[str] = []
68-
self.results = {"vlan": None}
69-
70-
super().__init__(
71-
module_arg_spec=self.module_arg_spec,
72-
required_one_of=self.required_one_of,
73-
)
74-
75-
def _get_vlan_by_label(self, label: str) -> Optional[VLAN]:
76-
try:
77-
return self.client.networking.vlans(VLAN.label == label)[0]
78-
except IndexError:
79-
return None
80-
except Exception as exception:
81-
return self.fail(msg="failed to get VLAN {0}".format(exception))
82-
83-
def exec_module(self, **kwargs: Any) -> Optional[dict]:
84-
"""Entrypoint for VLAN info module"""
85-
86-
label: str = kwargs.get("label")
87-
vlan = self._get_vlan_by_label(label)
88-
89-
if vlan is None:
90-
self.fail("failed to get vlan")
91-
92-
self.results["vlan"] = vlan._raw_json
93-
94-
return self.results
95-
96-
97-
def main() -> None:
98-
"""Constructs and calls the Linode VLAN info module"""
99-
LinodeVLANInfo()
100-
101-
10261
if __name__ == "__main__":
103-
main()
62+
module.run()

plugins/modules/vlan_list.py

+11-115
Original file line numberDiff line numberDiff line change
@@ -5,132 +5,28 @@
55

66
from __future__ import absolute_import, division, print_function
77

8-
from typing import Any, Dict, Optional
9-
108
import ansible_collections.linode.cloud.plugins.module_utils.doc_fragments.vlan_list as docs
11-
from ansible_collections.linode.cloud.plugins.module_utils.linode_common import (
12-
LinodeModuleBase,
13-
)
14-
from ansible_collections.linode.cloud.plugins.module_utils.linode_docs import (
15-
BETA_DISCLAIMER,
16-
global_authors,
17-
global_requirements,
18-
)
19-
from ansible_collections.linode.cloud.plugins.module_utils.linode_helper import (
20-
construct_api_filter,
21-
get_all_paginated,
9+
from ansible_collections.linode.cloud.plugins.module_utils.linode_common_list import (
10+
ListModule,
2211
)
23-
from ansible_specdoc.objects import (
24-
FieldType,
25-
SpecDocMeta,
26-
SpecField,
27-
SpecReturnValue,
28-
)
29-
30-
spec_filter = {
31-
"name": SpecField(
32-
type=FieldType.string,
33-
required=True,
34-
description=[
35-
"The name of the field to filter on.",
36-
"Valid filterable attributes can be found here: "
37-
"https://techdocs.akamai.com/linode-api/reference/get-vlans",
38-
],
39-
),
40-
"values": SpecField(
41-
type=FieldType.list,
42-
element_type=FieldType.string,
43-
required=True,
44-
description=[
45-
"A list of values to allow for this field.",
46-
"Fields will pass this filter if at least one of these values matches.",
47-
],
48-
),
49-
}
5012

51-
spec = {
52-
# Disable the default values
53-
"state": SpecField(type=FieldType.string, required=False, doc_hide=True),
54-
"label": SpecField(type=FieldType.string, required=False, doc_hide=True),
55-
"order": SpecField(
56-
type=FieldType.string,
57-
description=["The order to list VLANs in."],
58-
default="asc",
59-
choices=["desc", "asc"],
60-
),
61-
"order_by": SpecField(
62-
type=FieldType.string, description=["The attribute to order VLANs by."]
63-
),
64-
"filters": SpecField(
65-
type=FieldType.list,
66-
element_type=FieldType.dict,
67-
suboptions=spec_filter,
68-
description=["A list of filters to apply to the resulting VLANs."],
69-
),
70-
"count": SpecField(
71-
type=FieldType.integer,
72-
description=[
73-
"The number of results to return.",
74-
"If undefined, all results will be returned.",
75-
],
76-
),
77-
}
78-
79-
SPECDOC_META = SpecDocMeta(
80-
description=[
81-
"List and filter on Linode VLANs.",
82-
BETA_DISCLAIMER,
83-
],
84-
requirements=global_requirements,
85-
author=global_authors,
86-
options=spec,
13+
module = ListModule(
14+
result_display_name="VLANs",
15+
result_field_name="vlans",
16+
endpoint_template="/networking/vlans",
17+
result_docs_url="https://techdocs.akamai.com/linode-api/reference/get-vlans",
8718
examples=docs.specdoc_examples,
88-
return_values={
89-
"vlans": SpecReturnValue(
90-
description="The returned VLANs.",
91-
docs_url="https://techdocs.akamai.com/linode-api/reference/get-vlans",
92-
type=FieldType.list,
93-
elements=FieldType.dict,
94-
sample=docs.result_vlan_samples,
95-
)
96-
},
19+
result_samples=docs.result_vlan_samples,
9720
)
9821

22+
SPECDOC_META = module.spec
23+
9924
DOCUMENTATION = r"""
10025
"""
10126
EXAMPLES = r"""
10227
"""
10328
RETURN = r"""
10429
"""
10530

106-
107-
class Module(LinodeModuleBase):
108-
"""Module for getting info about a Linode VLANs"""
109-
110-
def __init__(self) -> None:
111-
self.module_arg_spec = SPECDOC_META.ansible_spec
112-
self.results: Dict[str, Any] = {"vlans": []}
113-
114-
super().__init__(module_arg_spec=self.module_arg_spec)
115-
116-
def exec_module(self, **kwargs: Any) -> Optional[dict]:
117-
"""Entrypoint for VLANs list module"""
118-
119-
filter_dict = construct_api_filter(self.module.params)
120-
121-
self.results["vlans"] = get_all_paginated(
122-
self.client,
123-
"/networking/vlans",
124-
filter_dict,
125-
num_results=self.module.params["count"],
126-
)
127-
return self.results
128-
129-
130-
def main() -> None:
131-
"""Constructs and calls the module"""
132-
Module()
133-
134-
13531
if __name__ == "__main__":
136-
main()
32+
module.run()

0 commit comments

Comments
 (0)