30
30
description:
31
31
- Setting this option will change the existing name (lookup via name).
32
32
type: str
33
+ copy_from:
34
+ description:
35
+ - Name of the existing credential to copy.
36
+ - If set, copies the specified credential.
37
+ - The new credential will be created with the name given in the C(name) parameter.
38
+ type: str
39
+ version_added: '3.0.0'
33
40
inputs:
34
41
description:
35
42
- Credential inputs where the keys are var names used in templating.
75
82
ansible.eda.credential:
76
83
name: "Example Credential"
77
84
state: absent
85
+
86
+ - name: Copy an existing EDA Credential
87
+ ansible.eda.credential:
88
+ name: "New Copied Credential"
89
+ copy_from: "Existing Credential"
78
90
"""
79
91
80
92
@@ -136,6 +148,7 @@ def main() -> None:
136
148
argument_spec = dict (
137
149
name = dict (type = "str" , required = True ),
138
150
new_name = dict (type = "str" ),
151
+ copy_from = dict (type = "str" ),
139
152
description = dict (type = "str" ),
140
153
inputs = dict (type = "dict" ),
141
154
credential_type_name = dict (type = "str" ),
@@ -145,13 +158,18 @@ def main() -> None:
145
158
146
159
argument_spec .update (AUTH_ARGSPEC )
147
160
148
- required_if = [
149
- (
150
- "state" ,
151
- "present" ,
152
- ("name" , "credential_type_name" , "inputs" , "organization_name" ),
153
- )
154
- ]
161
+ module = AnsibleModule (argument_spec = argument_spec , supports_check_mode = True )
162
+ copy_from = module .params .get ("copy_from" , None )
163
+
164
+ required_if = []
165
+ if not copy_from :
166
+ required_if = [
167
+ (
168
+ "state" ,
169
+ "present" ,
170
+ ("name" , "credential_type_name" , "inputs" , "organization_name" ),
171
+ )
172
+ ]
155
173
156
174
module = AnsibleModule (
157
175
argument_spec = argument_spec , required_if = required_if , supports_check_mode = True
@@ -179,7 +197,9 @@ def main() -> None:
179
197
180
198
# Attempt to look up credential based on the provided name
181
199
try :
182
- credential = controller .get_exactly_one (credential_endpoint , name = name )
200
+ credential = controller .get_exactly_one (
201
+ credential_endpoint , name = copy_from if copy_from else name
202
+ )
183
203
except EDAError as e :
184
204
module .fail_json (msg = f"Failed to get credential: { e } " )
185
205
@@ -201,6 +221,23 @@ def main() -> None:
201
221
else (controller .get_item_name (credential ) if credential else name )
202
222
)
203
223
224
+ # we attempt to copy only when copy_from is passed
225
+ if copy_from :
226
+ try :
227
+ result = controller .copy_if_needed (
228
+ name ,
229
+ copy_from ,
230
+ endpoint = f"eda-credentials/{ credential ['id' ]} /copy" ,
231
+ item_type = "credential" ,
232
+ )
233
+ module .exit_json (changed = True )
234
+ except KeyError as e :
235
+ module .fail_json (
236
+ msg = f"Unable to access { e } of the credential to copy from."
237
+ )
238
+ except EDAError as e :
239
+ module .fail_json (msg = f"Failed to copy credential: { e } " )
240
+
204
241
# If the state was present and we can let the module build or update the
205
242
# existing credential, this will return on its own
206
243
try :
0 commit comments