-
Notifications
You must be signed in to change notification settings - Fork 1
/
metal_resource.py
146 lines (122 loc) · 3.73 KB
/
metal_resource.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/python
# -*- coding: utf-8 -*-
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# DOCUMENTATION, EXAMPLES, and RETURN are generated by
# ansible_specdoc. Do not edit them directly.
DOCUMENTATION = ""
EXAMPLES = ""
RETURN = ""
# End of generated documentation
# This is a template for a new module. It is not meant to be used as is.
# It is meant to be copied and modified to create a new module.
# Replace all occurrences of "metal_resource" with the name of the new
# module, for example "metal_vlan".
from ansible.module_utils._text import to_native
from ansible_specdoc.objects import (
SpecField,
FieldType,
SpecReturnValue,
)
import traceback
from ansible_collections.equinix.cloud.plugins.module_utils.equinix import (
EquinixModule,
get_diff,
getSpecDocMeta,
)
module_spec = dict(
id=SpecField(
type=FieldType.string,
description=['UUID of the resource.'],
),
name=SpecField(
type=FieldType.string,
description=['The name of the resource.'],
editable=True,
),
some_attribute=SpecField(
type=FieldType.string,
description=['Some attribute of the resource.'],
editable=True,
),
)
specdoc_examples = [
'''
- name: Create new resource
hosts: localhost
tasks:
- equinix.cloud.metal_resource:
name: "new resource"
some_attribute: 42
''',
]
return_values = ['''
{
"changed": false,
"id": "7624f0f7-75b6-4271-bc64-632b80f87de2",
"name": "new resource",
"some_attribute": "42"
}
''']
MUTABLE_ATTRIBUTES = [
k for k, v in module_spec.items() if v.editable
]
SPECDOC_META = getSpecDocMeta(
short_description='Manage a particular resource type in Equinix Metal',
description=(
'Manage the resource kind in Equinix Metal. '
'You can use *id* or *name* to lookup the resource. '
'If you want to create new resource, you must provide *name*.'
),
examples=specdoc_examples,
options=module_spec,
return_values={
"metal_resource": SpecReturnValue(
description='The module object',
type=FieldType.dict,
sample=return_values,
),
},
)
def main():
module = EquinixModule(
argument_spec=SPECDOC_META.ansible_spec,
required_one_of=[("name", "id")],
)
state = module.params.get("state")
changed = False
try:
module.params_syntax_check()
if module.params.get("id"):
tolerate_not_found = state == "absent"
fetched = module.get_by_id("metal_resource", tolerate_not_found)
else:
fetched = module.get_one_from_list(
"metal_resource",
["name"],
)
if fetched:
module.params['id'] = fetched['id']
if state == "present":
diff = get_diff(module.params, fetched, MUTABLE_ATTRIBUTES)
if diff:
fetched = module.update_by_id(diff, "metal_resource")
changed = True
else:
module.delete_by_id("metal_resource")
changed = True
else:
if state == "present":
fetched = module.create("metal_resource")
if 'id' not in fetched:
module.fail_json(msg="UUID not found in resource creation response")
changed = True
else:
fetched = {}
except Exception as e:
tb = traceback.format_exc()
module.fail_json(msg="Error in metal_resource: {0}".format(to_native(e)),
exception=tb)
fetched.update({'changed': changed})
module.exit_json(**fetched)
if __name__ == '__main__':
main()