|
5 | 5 |
|
6 | 6 | from __future__ import absolute_import, division, print_function
|
7 | 7 |
|
8 |
| -from typing import Any, Optional |
9 |
| - |
10 | 8 | import ansible_collections.linode.cloud.plugins.module_utils.doc_fragments.token as docs_parent
|
11 | 9 | import ansible_collections.linode.cloud.plugins.module_utils.doc_fragments.token_info as docs
|
12 |
| -from ansible_collections.linode.cloud.plugins.module_utils.linode_common import ( |
13 |
| - LinodeModuleBase, |
14 |
| -) |
15 |
| -from ansible_collections.linode.cloud.plugins.module_utils.linode_docs import ( |
16 |
| - global_authors, |
17 |
| - global_requirements, |
| 10 | +from ansible_collections.linode.cloud.plugins.module_utils.linode_common_info import ( |
| 11 | + InfoModule, |
| 12 | + InfoModuleAttr, |
| 13 | + InfoModuleResult, |
18 | 14 | )
|
19 | 15 | from ansible_collections.linode.cloud.plugins.module_utils.linode_helper import (
|
20 |
| - filter_null_values, |
21 |
| -) |
22 |
| -from ansible_specdoc.objects import ( |
23 |
| - FieldType, |
24 |
| - SpecDocMeta, |
25 |
| - SpecField, |
26 |
| - SpecReturnValue, |
| 16 | + safe_find, |
27 | 17 | )
|
| 18 | +from ansible_specdoc.objects import FieldType |
28 | 19 | from linode_api4 import PersonalAccessToken
|
29 | 20 |
|
30 |
| -spec = { |
31 |
| - # Disable the default values |
32 |
| - "state": SpecField(type=FieldType.string, required=False, doc_hide=True), |
33 |
| - "id": SpecField( |
34 |
| - type=FieldType.integer, |
35 |
| - description=["The ID of the token."], |
36 |
| - conflicts_with=["label"], |
| 21 | +module = InfoModule( |
| 22 | + primary_result=InfoModuleResult( |
| 23 | + field_name="token", |
| 24 | + field_type=FieldType.dict, |
| 25 | + display_name="Personal Access Token", |
| 26 | + docs_url="https://techdocs.akamai.com/linode-api/reference/get-personal-access-tokens", |
| 27 | + samples=docs_parent.result_token_samples, |
37 | 28 | ),
|
38 |
| - "label": SpecField( |
39 |
| - type=FieldType.string, |
40 |
| - description=["The label of the token."], |
41 |
| - conflicts_with=["id"], |
42 |
| - ), |
43 |
| -} |
44 |
| - |
45 |
| -SPECDOC_META = SpecDocMeta( |
46 |
| - description=["Get info about a Linode Personal Access Token."], |
47 |
| - requirements=global_requirements, |
48 |
| - author=global_authors, |
49 |
| - options=spec, |
| 29 | + attributes=[ |
| 30 | + InfoModuleAttr( |
| 31 | + display_name="ID", |
| 32 | + name="id", |
| 33 | + type=FieldType.integer, |
| 34 | + get=lambda client, params: client.load( |
| 35 | + PersonalAccessToken, |
| 36 | + params.get("id"), |
| 37 | + )._raw_json, |
| 38 | + ), |
| 39 | + InfoModuleAttr( |
| 40 | + display_name="label", |
| 41 | + name="label", |
| 42 | + type=FieldType.string, |
| 43 | + get=lambda client, params: safe_find( |
| 44 | + client.profile.tokens, |
| 45 | + PersonalAccessToken.label == params.get("label"), |
| 46 | + raise_not_found=True, |
| 47 | + )._raw_json, |
| 48 | + ), |
| 49 | + ], |
50 | 50 | examples=docs.specdoc_examples,
|
51 |
| - return_values={ |
52 |
| - "token": SpecReturnValue( |
53 |
| - description="The token in JSON serialized form.", |
54 |
| - docs_url="https://techdocs.akamai.com/linode-api/reference/post-personal-access-token", |
55 |
| - type=FieldType.dict, |
56 |
| - sample=docs_parent.result_token_samples, |
57 |
| - ) |
58 |
| - }, |
59 | 51 | )
|
60 | 52 |
|
| 53 | +SPECDOC_META = module.spec |
| 54 | + |
61 | 55 | DOCUMENTATION = r"""
|
62 | 56 | """
|
63 | 57 | EXAMPLES = r"""
|
64 | 58 | """
|
65 | 59 | RETURN = r"""
|
66 | 60 | """
|
67 | 61 |
|
68 |
| - |
69 |
| -class Module(LinodeModuleBase): |
70 |
| - """Module for getting info about a Linode token""" |
71 |
| - |
72 |
| - def __init__(self) -> None: |
73 |
| - self.module_arg_spec = SPECDOC_META.ansible_spec |
74 |
| - self.results = {"token": None} |
75 |
| - |
76 |
| - super().__init__( |
77 |
| - module_arg_spec=self.module_arg_spec, |
78 |
| - required_one_of=[("id", "label")], |
79 |
| - mutually_exclusive=[("id", "label")], |
80 |
| - ) |
81 |
| - |
82 |
| - def _get_token_by_label(self, label: str) -> Optional[PersonalAccessToken]: |
83 |
| - try: |
84 |
| - return self.client.profile.tokens( |
85 |
| - PersonalAccessToken.label == label |
86 |
| - )[0] |
87 |
| - except IndexError: |
88 |
| - return self.fail( |
89 |
| - msg="failed to get token with label {0}: " |
90 |
| - "token does not exist".format(label) |
91 |
| - ) |
92 |
| - except Exception as exception: |
93 |
| - return self.fail( |
94 |
| - msg="failed to get token {0}: {1}".format(label, exception) |
95 |
| - ) |
96 |
| - |
97 |
| - def _get_token_by_id(self, token_id: int) -> PersonalAccessToken: |
98 |
| - return self._get_resource_by_id(PersonalAccessToken, token_id) |
99 |
| - |
100 |
| - def exec_module(self, **kwargs: Any) -> Optional[dict]: |
101 |
| - """Entrypoint for token info module""" |
102 |
| - |
103 |
| - params = filter_null_values(self.module.params) |
104 |
| - |
105 |
| - if "id" in params: |
106 |
| - self.results["token"] = self._get_token_by_id( |
107 |
| - params.get("id") |
108 |
| - )._raw_json |
109 |
| - |
110 |
| - if "label" in params: |
111 |
| - self.results["token"] = self._get_token_by_label( |
112 |
| - params.get("label") |
113 |
| - )._raw_json |
114 |
| - |
115 |
| - return self.results |
116 |
| - |
117 |
| - |
118 |
| -def main() -> None: |
119 |
| - """Constructs and calls the module""" |
120 |
| - Module() |
121 |
| - |
122 |
| - |
123 | 62 | if __name__ == "__main__":
|
124 |
| - main() |
| 63 | + module.run() |
0 commit comments