-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwaldur_marketplace.py
162 lines (145 loc) · 4.4 KB
/
waldur_marketplace.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/python
# has to be a full import due to Ansible 2.0 compatibility
import yaml
from ansible.module_utils.basic import AnsibleModule, to_text
from waldur_client import (
WaldurClientException,
waldur_client_from_module,
waldur_full_argument_spec,
)
ANSIBLE_METADATA = {
"metadata_version": "1.1",
"status": ["preview"],
"supported_by": "OpenNode",
}
DOCUMENTATION = """
---
module: waldur_marketplace
short_description: Create order in Waldur Marketplace.
version_added: 0.1
description:
- Create marketplace order via Waldur API.
requirements:
- python = 3.8
- requests
- python-waldur-client
options:
access_token:
description:
- An access token which has permissions to create a marketplace order.
required: true
api_url:
description:
- Fully qualified url to the Waldur.
required: true
project:
required: true
description:
- The name or UUID of the project to add an order to.
offering:
required: true
description:
- The name or UUID of the offering to add an order to.
plan:
required: true
description:
- The name or UUID of the order plan.
attributes:
default: None
description:
- order attributes or path to JSON or YAML file with order attributes.
limits:
default: None
description:
- order limits or path to JSON or YAML file with order limits.
"""
EXAMPLES = """
- name: Create a new marketplace order.
hosts: localhost
gather_facts: False
tasks:
- name: Add order
waldur_marketplace:
access_token: b83557fd8e2066e98f27dee8f3b3433cdc4183ce
api_url: https://waldur.example.com:8000/api
project: Project
offering: 7887745b83c74fc38d695ed58648ea20
plan: 4d50f6584ed84df3b6c83075044fd284
- name: Create a new marketplace order using names.
hosts: localhost
gather_facts: False
tasks:
- name: Add order
waldur_marketplace:
access_token: b83557fd8e2066e98f27dee8f3b3433cdc4183ce
api_url: https://waldur.example.com:8000/api
project: Project
offering: Offering name
plan: Plan name
- name: Create a new marketplace order using attributes as file path.
hosts: localhost
gather_facts: False
tasks:
- name: Add order
waldur_marketplace:
access_token: b83557fd8e2066e98f27dee8f3b3433cdc4183ce
api_url: https://waldur.example.com:8000/api
project: Project
offering: Offering name
plan: Plan name
attributes: /home/user/attributes
- name: Create a new marketplace order using attributes as dictionary.
hosts: localhost
gather_facts: False
tasks:
- name: Add order
waldur_marketplace:
access_token: b83557fd8e2066e98f27dee8f3b3433cdc4183ce
api_url: https://waldur.example.com:8000/api
project: Project
offering: Offering name
plan: Plan name
attributes:
name: my name
"""
def send_request_to_waldur(client, module):
project = module.params["project"]
offering = module.params["offering"]
plan = module.params["plan"]
attributes = module.params.get("attributes")
limits = module.params.get("limits")
def get_file_content(path):
if path:
value = yaml.safe_load(path)
if isinstance(value, str):
with open(value) as f:
return yaml.safe_load(f.read())
else:
return value
attributes = get_file_content(attributes) or {}
limits = get_file_content(limits) or {}
response = client.create_marketplace_order(
project, offering, plan, attributes, limits
)
return response, True
def main():
module = AnsibleModule(
argument_spec=waldur_full_argument_spec(
project=dict(type="str", required=True),
offering=dict(type="str", required=True),
plan=dict(type="str", required=True),
attributes=dict(type="str", default=None),
limits=dict(type="str", default=None),
)
)
client = waldur_client_from_module(module)
try:
order, has_changed = send_request_to_waldur(client, module)
except (IOError, OSError) as e:
module.fail_json(msg="Unable to open file: %s" % to_text(e))
except WaldurClientException as e:
module.fail_json(msg=str(e))
else:
module.exit_json(order=order, changed=has_changed)
if __name__ == "__main__":
main()