Skip to content

Commit 9fae98f

Browse files
committed
Add plugins/modules/credential_type_info.py
Signed-off-by: Alina Buzachis <[email protected]>
1 parent 10cd880 commit 9fae98f

File tree

3 files changed

+102
-30
lines changed

3 files changed

+102
-30
lines changed

plugins/module_utils/controller.py

+30-26
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ def fail_wanted_one(self, response, endpoint, query_params):
9191
total_results=response.json["count"],
9292
)
9393

94-
def get_one(
95-
self, endpoint, name=None, allow_none=True, check_exists=False, **kwargs
94+
def get_one_or_many(
95+
self, endpoint, name=None, allow_none=True, check_exists=False, want_one=True, **kwargs
9696
):
9797
new_kwargs = kwargs.copy()
9898
response = None
@@ -103,44 +103,48 @@ def get_one(
103103
new_data["{0}".format(name_field)] = name
104104
new_kwargs["data"] = new_data
105105

106-
response = self.get_endpoint(endpoint, **new_kwargs)
106+
response = self.get_endpoint(endpoint, **new_kwargs)
107107

108-
if response.status != 200:
109-
fail_msg = "Got a {0} when trying to get from {1}".format(
110-
response.status, endpoint
111-
)
112-
if "detail" in response.json:
113-
fail_msg += ",detail: {0}".format(response.json["detail"])
114-
self.module.fail_json(msg=fail_msg)
108+
if response.status != 200:
109+
fail_msg = "Got a {0} when trying to get from {1}".format(
110+
response.status, endpoint
111+
)
112+
if "detail" in response.json:
113+
fail_msg += ",detail: {0}".format(response.json["detail"])
114+
self.module.fail_json(msg=fail_msg)
115115

116-
if "count" not in response.json or "results" not in response.json:
117-
self.module.fail_json(msg="The endpoint did not provide count, results")
116+
logging.debug(response.json)
117+
118+
if "count" not in response.json or "results" not in response.json:
119+
self.module.fail_json(msg="The endpoint did not provide count, results")
118120

119121
if response.json["count"] == 0:
120122
if allow_none:
121123
return None
122124
else:
123125
self.fail_wanted_one(response, endpoint, new_kwargs.get("data"))
126+
if response.json["count"] == 1:
127+
return response.json["results"][0]
124128
elif response.json["count"] > 1:
125-
if name:
126-
# Since we did a name or ID search and got > 1 return
127-
# something if the id matches
128-
for asset in response.json["results"]:
129-
if str(asset["id"]) == name:
130-
return asset
131-
# We got > 1 and either didn't find something by ID (which means
132-
# multiple names)
133-
# Or we weren't running with a or search and just got back too
134-
# many to begin with.
135-
self.fail_wanted_one(response, endpoint, new_kwargs.get("data"))
129+
if want_one:
130+
if name:
131+
# Since we did a name or ID search and got > 1 return
132+
# something if the id matches
133+
for asset in response.json["results"]:
134+
if str(asset["id"]) == name:
135+
return asset
136+
# We got > 1 and either didn't find something by ID (which means
137+
# multiple names)
138+
# Or we weren't running with a or search and just got back too
139+
# many to begin with.
140+
self.fail_wanted_one(response, endpoint, new_kwargs.get("data"))
141+
else:
142+
return response.json["results"]
136143

137144
if check_exists:
138145
self.json_output["id"] = response.json["results"][0]["id"]
139146
self.module.exit_json(**self.json_output)
140147

141-
return response.json["results"][0]
142-
143-
144148
def create_if_needed(
145149
self,
146150
existing_item,

plugins/modules/credential_type.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def main():
124124
controller = Controller(client, module)
125125

126126
# Attempt to look up credential_type based on the provided name
127-
credential_type = controller.get_one('credential-types', name=name, check_exists=(state == 'exists'))
127+
credential_type = controller.get_one_or_many('credential-types', name=name, check_exists=(state == 'exists'))
128128

129129
if state == 'absent':
130130
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
@@ -136,8 +136,5 @@ def main():
136136
controller.create_or_update_if_needed(credential_type, credential_type_params, endpoint='credential-types', item_type='credential type')
137137

138138

139-
module.exit_json(**credential_type)
140-
141-
142139
if __name__ == "__main__":
143140
main()
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
DOCUMENTATION = """
2+
---
3+
module: credential_type_info
4+
author:
5+
- Alina Buzachis (@alinabuzachis)
6+
short_description: List credential types in EDA Controller
7+
description:
8+
- List credentail types in EDA controller.
9+
version_added: 2.0.0
10+
options:
11+
name:
12+
description:
13+
- The name of the credential type.
14+
type: str
15+
required: false
16+
requirements:
17+
- The 'requests' Python module must be installed.
18+
extends_documentation_fragment:
19+
- ansible.eda.eda_controller.auths
20+
"""
21+
22+
23+
EXAMPLES = """
24+
- name: Get information about a credential type
25+
ansible.eda.credential_type_info:
26+
name: "Test"
27+
28+
- name: List all credential types
29+
ansible.eda.credential_type:
30+
"""
31+
32+
33+
RETURN = """ # """
34+
35+
36+
from ansible.module_utils.basic import AnsibleModule
37+
38+
from ..module_utils.arguments import AUTH_ARGSPEC
39+
40+
from ansible_collections.ansible.eda.plugins.module_utils.client import Client
41+
from ansible_collections.ansible.eda.plugins.module_utils.controller import Controller
42+
43+
44+
def main():
45+
argument_spec = dict(
46+
name=dict(type="str", required=False),
47+
)
48+
49+
argument_spec.update(AUTH_ARGSPEC)
50+
51+
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
52+
53+
client = Client(
54+
host=module.params.get("controller_host"),
55+
username=module.params.get("controller_username"),
56+
password=module.params.get("controller_password"),
57+
timeout=module.params.get("request_timeout"),
58+
validate_certs=module.params.get("validate_certs"),
59+
)
60+
61+
name = module.params.get('name')
62+
controller = Controller(client, module)
63+
64+
# Attempt to look up credential_type based on the provided name
65+
result = controller.get_one_or_many('credential-types', name=name, want_one=False)
66+
67+
module.exit_json(**result)
68+
69+
70+
if __name__ == "__main__":
71+
main()

0 commit comments

Comments
 (0)