saltext.noderings 0.1.0__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- saltext/noderings/__init__.py +28 -0
- saltext/noderings/clouds/__init__.py +0 -0
- saltext/noderings/clouds/noderings_mod.py +457 -0
- saltext/noderings/modules/__init__.py +0 -0
- saltext/noderings/modules/noderings_mod.py +996 -0
- saltext/noderings/states/__init__.py +0 -0
- saltext/noderings/states/noderings_mod.py +562 -0
- saltext/noderings/utils/__init__.py +0 -0
- saltext/noderings/utils/client.py +593 -0
- saltext/noderings/utils/compute.py +392 -0
- saltext/noderings/utils/dns.py +162 -0
- saltext/noderings/utils/firewall.py +323 -0
- saltext/noderings/utils/marketplace.py +182 -0
- saltext/noderings/version.py +1 -0
- saltext_noderings-0.1.0.dist-info/METADATA +198 -0
- saltext_noderings-0.1.0.dist-info/RECORD +21 -0
- saltext_noderings-0.1.0.dist-info/WHEEL +6 -0
- saltext_noderings-0.1.0.dist-info/entry_points.txt +2 -0
- saltext_noderings-0.1.0.dist-info/licenses/LICENSE +201 -0
- saltext_noderings-0.1.0.dist-info/licenses/NOTICE +10 -0
- saltext_noderings-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,996 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Manage NodeRings resources: virtual machines, DNS, firewalls and SSH keys.
|
|
3
|
+
|
|
4
|
+
`NodeRings <https://noderings.com>`_ is a cloud marketplace where independent
|
|
5
|
+
hosting providers sell virtual machines through one account and one API.
|
|
6
|
+
|
|
7
|
+
:depends: none beyond Salt
|
|
8
|
+
:configuration: The module needs a NodeRings service account token. Put it in
|
|
9
|
+
the minion configuration or in pillar:
|
|
10
|
+
|
|
11
|
+
.. code-block:: yaml
|
|
12
|
+
|
|
13
|
+
noderings:
|
|
14
|
+
token: <service account token>
|
|
15
|
+
# optional
|
|
16
|
+
organization_id: <organization id>
|
|
17
|
+
api_url: https://api.noderings.com
|
|
18
|
+
|
|
19
|
+
Without ``noderings:token``, the ``NODERINGS_TOKEN`` environment variable is
|
|
20
|
+
used. Every function also accepts ``profile`` to read the settings from
|
|
21
|
+
another key, for example ``profile=noderings_staging``.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
import logging
|
|
25
|
+
|
|
26
|
+
from salt.exceptions import CommandExecutionError
|
|
27
|
+
from salt.exceptions import SaltInvocationError
|
|
28
|
+
|
|
29
|
+
from saltext.noderings import __version__
|
|
30
|
+
from saltext.noderings.utils import compute
|
|
31
|
+
from saltext.noderings.utils import dns as dnsutil
|
|
32
|
+
from saltext.noderings.utils import firewall as fwutil
|
|
33
|
+
from saltext.noderings.utils.client import NodeRingsError
|
|
34
|
+
from saltext.noderings.utils.client import TimeoutError_
|
|
35
|
+
from saltext.noderings.utils.client import client_from_config
|
|
36
|
+
from saltext.noderings.utils.client import find_by_name
|
|
37
|
+
from saltext.noderings.utils.client import is_not_found
|
|
38
|
+
from saltext.noderings.utils.client import snake
|
|
39
|
+
from saltext.noderings.utils.client import trim_enum
|
|
40
|
+
from saltext.noderings.utils.marketplace import find_offers
|
|
41
|
+
from saltext.noderings.utils.marketplace import offer_view
|
|
42
|
+
|
|
43
|
+
log = logging.getLogger(__name__)
|
|
44
|
+
|
|
45
|
+
__virtualname__ = "noderings"
|
|
46
|
+
|
|
47
|
+
USER_AGENT = f"saltext-noderings/{__version__} (+https://github.com/noderings/saltext-noderings)"
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def __virtual__():
|
|
51
|
+
return __virtualname__
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# -- plumbing -------------------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _client(profile="noderings"):
|
|
58
|
+
conf = __salt__["config.get"](profile, {}) or {}
|
|
59
|
+
try:
|
|
60
|
+
return client_from_config(conf, user_agent=USER_AGENT)
|
|
61
|
+
except ValueError as exc:
|
|
62
|
+
raise CommandExecutionError(str(exc)) from exc
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _call(what, func, *args, **kwargs):
|
|
66
|
+
"""Run an API call and turn its errors into Salt exceptions."""
|
|
67
|
+
try:
|
|
68
|
+
return func(*args, **kwargs)
|
|
69
|
+
except compute.NodeRingsUsageError as exc:
|
|
70
|
+
raise SaltInvocationError(str(exc)) from exc
|
|
71
|
+
except (NodeRingsError, RuntimeError, TimeoutError_, OSError) as exc:
|
|
72
|
+
raise CommandExecutionError(f"{what}: {exc}") from exc
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _zone(client, zone):
|
|
76
|
+
"""A zone by name or ID."""
|
|
77
|
+
name = dnsutil.normalize_zone_name(zone)
|
|
78
|
+
for z in _call("listing DNS zones", client.list_dns_zones):
|
|
79
|
+
if zone == z.get("id") or dnsutil.normalize_zone_name(z.get("name")) == name:
|
|
80
|
+
return z
|
|
81
|
+
return None
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _require_zone(client, zone):
|
|
85
|
+
z = _zone(client, zone)
|
|
86
|
+
if not z:
|
|
87
|
+
raise SaltInvocationError(
|
|
88
|
+
f"DNS zone {zone} does not exist in this organization; create it with "
|
|
89
|
+
"noderings.create_zone or the noderings.dns_zone_present state"
|
|
90
|
+
)
|
|
91
|
+
return z
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _zone_view(zone):
|
|
95
|
+
out = snake(zone or {})
|
|
96
|
+
out["state"] = trim_enum((zone or {}).get("state"), "DNS_ZONE_STATE_")
|
|
97
|
+
out["dnssec_mode"] = trim_enum((zone or {}).get("dnssecMode"), "DNSSEC_MODE_")
|
|
98
|
+
# The nameservers to set at the registrar.
|
|
99
|
+
out["nameservers"] = list((zone or {}).get("assignedNameservers") or [])
|
|
100
|
+
return out
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _record_view(zone_id, rel, rrtype, rec):
|
|
104
|
+
if not rec:
|
|
105
|
+
return {}
|
|
106
|
+
return {
|
|
107
|
+
"zone_id": zone_id,
|
|
108
|
+
"name": rel,
|
|
109
|
+
"type": rrtype,
|
|
110
|
+
"ttl": int(rec.get("ttl") or 0),
|
|
111
|
+
"values": [dnsutil.from_api(rrtype, v) for v in rec.get("rdata") or []],
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _find_record(client, zone_id, rel, rrtype):
|
|
116
|
+
"""The RRset with exactly this name and type (the API's name filter matches substrings)."""
|
|
117
|
+
recs = _call(
|
|
118
|
+
"listing DNS records",
|
|
119
|
+
client.list_dns_records,
|
|
120
|
+
zone_id,
|
|
121
|
+
"" if rel == "@" else rel,
|
|
122
|
+
rrtype,
|
|
123
|
+
)
|
|
124
|
+
for rec in recs:
|
|
125
|
+
if (
|
|
126
|
+
dnsutil.relative_name(rec.get("name"), "") == rel
|
|
127
|
+
and (rec.get("type") or "").upper() == rrtype
|
|
128
|
+
and not rec.get("system")
|
|
129
|
+
):
|
|
130
|
+
return rec
|
|
131
|
+
return None
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _ruleset(client, ruleset):
|
|
135
|
+
for rs in _call("listing firewall rulesets", client.list_firewall_rulesets):
|
|
136
|
+
if ruleset in (rs.get("id"), rs.get("name")):
|
|
137
|
+
return rs
|
|
138
|
+
return None
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _vm(client, name):
|
|
142
|
+
return _call(f"looking up virtual machine {name}", compute.find_vm, client, name)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _require_vm(client, name):
|
|
146
|
+
vm = _vm(client, name)
|
|
147
|
+
if not vm:
|
|
148
|
+
raise SaltInvocationError(f"no virtual machine is named {name}")
|
|
149
|
+
return vm
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
# -- account --------------------------------------------------------------------
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def get_organization(profile="noderings"):
|
|
156
|
+
"""
|
|
157
|
+
Return the organization the token belongs to.
|
|
158
|
+
|
|
159
|
+
CLI Example:
|
|
160
|
+
|
|
161
|
+
.. code-block:: bash
|
|
162
|
+
|
|
163
|
+
salt '*' noderings.get_organization
|
|
164
|
+
"""
|
|
165
|
+
client = _client(profile)
|
|
166
|
+
return snake(_call("reading the organization", client.get_organization) or {})
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def get_credit_balance(profile="noderings"):
|
|
170
|
+
"""
|
|
171
|
+
Return the organization's prepaid credit balance. Hourly (usage-billed)
|
|
172
|
+
servers draw from it.
|
|
173
|
+
|
|
174
|
+
CLI Example:
|
|
175
|
+
|
|
176
|
+
.. code-block:: bash
|
|
177
|
+
|
|
178
|
+
salt '*' noderings.get_credit_balance
|
|
179
|
+
"""
|
|
180
|
+
client = _client(profile)
|
|
181
|
+
return snake(_call("reading the credit balance", client.get_credit_balance))
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
# -- catalog --------------------------------------------------------------------
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def list_distros(profile="noderings"):
|
|
188
|
+
"""
|
|
189
|
+
List the operating systems a VM can be installed with, as the names
|
|
190
|
+
``create_vm`` takes (for example ``ubuntu_24_04``).
|
|
191
|
+
|
|
192
|
+
CLI Example:
|
|
193
|
+
|
|
194
|
+
.. code-block:: bash
|
|
195
|
+
|
|
196
|
+
salt '*' noderings.list_distros
|
|
197
|
+
"""
|
|
198
|
+
client = _client(profile)
|
|
199
|
+
out = []
|
|
200
|
+
for d in _call("listing distros", client.list_distros):
|
|
201
|
+
item = snake(d)
|
|
202
|
+
item["name"] = trim_enum(d.get("distro"), "DISTRO_")
|
|
203
|
+
out.append(item)
|
|
204
|
+
return out
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def list_regions(profile="noderings"):
|
|
208
|
+
"""
|
|
209
|
+
List the cities with servers for sale.
|
|
210
|
+
|
|
211
|
+
CLI Example:
|
|
212
|
+
|
|
213
|
+
.. code-block:: bash
|
|
214
|
+
|
|
215
|
+
salt '*' noderings.list_regions
|
|
216
|
+
"""
|
|
217
|
+
client = _client(profile)
|
|
218
|
+
return snake(_call("listing regions", client.list_marketplace_regions, "ASSET_TYPE_VM"))
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def list_plans(country=None, city=None, billing_type=None, profile="noderings"):
|
|
222
|
+
"""
|
|
223
|
+
List plan codes for sale, optionally in one city or of one billing type
|
|
224
|
+
(``usage``, ``subscription`` or ``free``).
|
|
225
|
+
|
|
226
|
+
CLI Example:
|
|
227
|
+
|
|
228
|
+
.. code-block:: bash
|
|
229
|
+
|
|
230
|
+
salt '*' noderings.list_plans country="United States" city=Ashburn billing_type=usage
|
|
231
|
+
"""
|
|
232
|
+
client = _client(profile)
|
|
233
|
+
billing = {
|
|
234
|
+
"usage": "BILLING_TYPE_USAGE",
|
|
235
|
+
"subscription": "BILLING_TYPE_SUBSCRIPTION",
|
|
236
|
+
"free": "BILLING_TYPE_FREE",
|
|
237
|
+
}
|
|
238
|
+
return snake(
|
|
239
|
+
_call(
|
|
240
|
+
"listing plans",
|
|
241
|
+
client.list_available_plans,
|
|
242
|
+
country,
|
|
243
|
+
city,
|
|
244
|
+
billing.get(billing_type or "", None),
|
|
245
|
+
"ASSET_TYPE_VM",
|
|
246
|
+
)
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def list_offers(
|
|
251
|
+
plan_code,
|
|
252
|
+
country,
|
|
253
|
+
city,
|
|
254
|
+
billing_type="usage",
|
|
255
|
+
sort="price",
|
|
256
|
+
require_capabilities=None,
|
|
257
|
+
profile="noderings",
|
|
258
|
+
):
|
|
259
|
+
"""
|
|
260
|
+
List the offers for a plan code in a city, best first. An offer is one
|
|
261
|
+
provider's plan, with its prices. ``sort`` is ``price`` or ``rating``.
|
|
262
|
+
|
|
263
|
+
CLI Example:
|
|
264
|
+
|
|
265
|
+
.. code-block:: bash
|
|
266
|
+
|
|
267
|
+
salt '*' noderings.list_offers SC-GC-1C-1G-20 "United States" Ashburn
|
|
268
|
+
"""
|
|
269
|
+
client = _client(profile)
|
|
270
|
+
offers, _ = _call(
|
|
271
|
+
"listing offers",
|
|
272
|
+
find_offers,
|
|
273
|
+
client,
|
|
274
|
+
plan_code,
|
|
275
|
+
country,
|
|
276
|
+
city,
|
|
277
|
+
billing_type=billing_type,
|
|
278
|
+
require_capabilities=require_capabilities or [],
|
|
279
|
+
sort=sort,
|
|
280
|
+
)
|
|
281
|
+
return [offer_view(o) for o in offers]
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
# -- SSH keys -------------------------------------------------------------------
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def list_ssh_keys(profile="noderings"):
|
|
288
|
+
"""
|
|
289
|
+
List the organization's SSH keys.
|
|
290
|
+
|
|
291
|
+
CLI Example:
|
|
292
|
+
|
|
293
|
+
.. code-block:: bash
|
|
294
|
+
|
|
295
|
+
salt '*' noderings.list_ssh_keys
|
|
296
|
+
"""
|
|
297
|
+
client = _client(profile)
|
|
298
|
+
return snake(_call("listing SSH keys", client.list_ssh_keys))
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def get_ssh_key(name, profile="noderings"):
|
|
302
|
+
"""
|
|
303
|
+
Return the SSH key with this name or ID, or ``None``.
|
|
304
|
+
|
|
305
|
+
CLI Example:
|
|
306
|
+
|
|
307
|
+
.. code-block:: bash
|
|
308
|
+
|
|
309
|
+
salt '*' noderings.get_ssh_key laptop
|
|
310
|
+
"""
|
|
311
|
+
client = _client(profile)
|
|
312
|
+
for key in _call("listing SSH keys", client.list_ssh_keys):
|
|
313
|
+
if name in (key.get("id"), key.get("name")):
|
|
314
|
+
return snake(key)
|
|
315
|
+
return None
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def create_ssh_key(name, public_key, profile="noderings"):
|
|
319
|
+
"""
|
|
320
|
+
Upload a public key. VMs get it as root's authorized key.
|
|
321
|
+
|
|
322
|
+
CLI Example:
|
|
323
|
+
|
|
324
|
+
.. code-block:: bash
|
|
325
|
+
|
|
326
|
+
salt '*' noderings.create_ssh_key laptop "ssh-ed25519 AAAA... me@laptop"
|
|
327
|
+
"""
|
|
328
|
+
client = _client(profile)
|
|
329
|
+
return snake(_call(f"creating SSH key {name}", client.create_ssh_key, name, public_key.strip()))
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def delete_ssh_key(name, profile="noderings"):
|
|
333
|
+
"""
|
|
334
|
+
Delete an SSH key by name or ID. Returns ``False`` when it does not exist.
|
|
335
|
+
|
|
336
|
+
CLI Example:
|
|
337
|
+
|
|
338
|
+
.. code-block:: bash
|
|
339
|
+
|
|
340
|
+
salt '*' noderings.delete_ssh_key laptop
|
|
341
|
+
"""
|
|
342
|
+
client = _client(profile)
|
|
343
|
+
key = get_ssh_key(name, profile)
|
|
344
|
+
if not key:
|
|
345
|
+
return False
|
|
346
|
+
_call(f"deleting SSH key {name}", client.delete_ssh_key, key["id"])
|
|
347
|
+
return True
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
# -- DNS ------------------------------------------------------------------------
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def list_zones(profile="noderings"):
|
|
354
|
+
"""
|
|
355
|
+
List DNS zones.
|
|
356
|
+
|
|
357
|
+
CLI Example:
|
|
358
|
+
|
|
359
|
+
.. code-block:: bash
|
|
360
|
+
|
|
361
|
+
salt '*' noderings.list_zones
|
|
362
|
+
"""
|
|
363
|
+
client = _client(profile)
|
|
364
|
+
return [_zone_view(z) for z in _call("listing DNS zones", client.list_dns_zones)]
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def get_zone(zone, profile="noderings"):
|
|
368
|
+
"""
|
|
369
|
+
Return a DNS zone by name or ID, with its nameservers, or ``None``.
|
|
370
|
+
|
|
371
|
+
CLI Example:
|
|
372
|
+
|
|
373
|
+
.. code-block:: bash
|
|
374
|
+
|
|
375
|
+
salt '*' noderings.get_zone example.com
|
|
376
|
+
"""
|
|
377
|
+
client = _client(profile)
|
|
378
|
+
z = _zone(client, zone)
|
|
379
|
+
if not z:
|
|
380
|
+
return None
|
|
381
|
+
return _zone_view(_call(f"reading zone {zone}", client.get_dns_zone, z["id"]) or z)
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def create_zone(zone, profile="noderings"):
|
|
385
|
+
"""
|
|
386
|
+
Host a domain on NodeRings DNS. Set the returned nameservers at the
|
|
387
|
+
registrar to make it live.
|
|
388
|
+
|
|
389
|
+
CLI Example:
|
|
390
|
+
|
|
391
|
+
.. code-block:: bash
|
|
392
|
+
|
|
393
|
+
salt '*' noderings.create_zone example.com
|
|
394
|
+
"""
|
|
395
|
+
client = _client(profile)
|
|
396
|
+
name = dnsutil.normalize_zone_name(zone)
|
|
397
|
+
created = _call(f"creating zone {name}", client.create_dns_zone, name) or {}
|
|
398
|
+
# Read the zone back so the result always carries its nameservers.
|
|
399
|
+
if created.get("id"):
|
|
400
|
+
created = _call(f"reading zone {name}", client.get_dns_zone, created["id"]) or created
|
|
401
|
+
return _zone_view(created)
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def delete_zone(zone, force=False, profile="noderings"):
|
|
405
|
+
"""
|
|
406
|
+
Delete a DNS zone and all its records. ``force=True`` also deletes a zone
|
|
407
|
+
whose DS record is still at the registrar, which breaks resolution for
|
|
408
|
+
validating resolvers. Returns ``False`` when it does not exist.
|
|
409
|
+
|
|
410
|
+
CLI Example:
|
|
411
|
+
|
|
412
|
+
.. code-block:: bash
|
|
413
|
+
|
|
414
|
+
salt '*' noderings.delete_zone example.com
|
|
415
|
+
"""
|
|
416
|
+
client = _client(profile)
|
|
417
|
+
z = _zone(client, zone)
|
|
418
|
+
if not z:
|
|
419
|
+
return False
|
|
420
|
+
_call(f"deleting zone {zone}", client.delete_dns_zone, z["id"], force)
|
|
421
|
+
return True
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def list_records(zone, profile="noderings"):
|
|
425
|
+
"""
|
|
426
|
+
List the records of a zone, one entry per name and type.
|
|
427
|
+
|
|
428
|
+
CLI Example:
|
|
429
|
+
|
|
430
|
+
.. code-block:: bash
|
|
431
|
+
|
|
432
|
+
salt '*' noderings.list_records example.com
|
|
433
|
+
"""
|
|
434
|
+
client = _client(profile)
|
|
435
|
+
z = _require_zone(client, zone)
|
|
436
|
+
out = []
|
|
437
|
+
for rec in _call("listing DNS records", client.list_dns_records, z["id"]):
|
|
438
|
+
rrtype = (rec.get("type") or "").upper()
|
|
439
|
+
item = _record_view(
|
|
440
|
+
z["id"], dnsutil.relative_name(rec.get("name"), z.get("name")), rrtype, rec
|
|
441
|
+
)
|
|
442
|
+
item["system"] = bool(rec.get("system"))
|
|
443
|
+
out.append(item)
|
|
444
|
+
return out
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
def get_record(zone, name, rtype, profile="noderings"):
|
|
448
|
+
"""
|
|
449
|
+
Return the records of one name and type, or ``None``. ``name`` is relative
|
|
450
|
+
to the zone (``www``, ``@`` for the apex) or fully qualified.
|
|
451
|
+
|
|
452
|
+
CLI Example:
|
|
453
|
+
|
|
454
|
+
.. code-block:: bash
|
|
455
|
+
|
|
456
|
+
salt '*' noderings.get_record example.com www A
|
|
457
|
+
"""
|
|
458
|
+
client = _client(profile)
|
|
459
|
+
z = _require_zone(client, zone)
|
|
460
|
+
rrtype = rtype.upper()
|
|
461
|
+
rel = dnsutil.relative_name(name, z.get("name"))
|
|
462
|
+
return _record_view(z["id"], rel, rrtype, _find_record(client, z["id"], rel, rrtype)) or None
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
def set_record(zone, name, rtype, values, ttl=3600, profile="noderings"):
|
|
466
|
+
"""
|
|
467
|
+
Create or replace the records of one name and type. ``values`` is a list;
|
|
468
|
+
hostnames should end with a dot, and TXT values are plain text without
|
|
469
|
+
quotes.
|
|
470
|
+
|
|
471
|
+
CLI Example:
|
|
472
|
+
|
|
473
|
+
.. code-block:: bash
|
|
474
|
+
|
|
475
|
+
salt '*' noderings.set_record example.com www A '["192.0.2.10"]' ttl=300
|
|
476
|
+
salt '*' noderings.set_record example.com @ MX '["10 mx1.example.net."]'
|
|
477
|
+
"""
|
|
478
|
+
client = _client(profile)
|
|
479
|
+
z = _require_zone(client, zone)
|
|
480
|
+
rrtype = rtype.upper()
|
|
481
|
+
if rrtype not in dnsutil.RECORD_TYPES:
|
|
482
|
+
raise SaltInvocationError(
|
|
483
|
+
f"unsupported record type {rtype}; use one of {', '.join(dnsutil.RECORD_TYPES)}"
|
|
484
|
+
)
|
|
485
|
+
if isinstance(values, str):
|
|
486
|
+
values = [values]
|
|
487
|
+
values = [str(v).strip() for v in values or []]
|
|
488
|
+
err = dnsutil.validate_values(rrtype, values)
|
|
489
|
+
if err:
|
|
490
|
+
raise SaltInvocationError(f"{name} {rrtype}: {err}")
|
|
491
|
+
ttl = int(ttl)
|
|
492
|
+
if not 30 <= ttl <= 86400:
|
|
493
|
+
raise SaltInvocationError("ttl must be between 30 and 86400 seconds")
|
|
494
|
+
rel = dnsutil.relative_name(name, z.get("name"))
|
|
495
|
+
_call(
|
|
496
|
+
f"writing DNS record {rel} {rrtype}",
|
|
497
|
+
client.upsert_dns_record,
|
|
498
|
+
z["id"],
|
|
499
|
+
{
|
|
500
|
+
"name": rel,
|
|
501
|
+
"type": rrtype,
|
|
502
|
+
"ttl": ttl,
|
|
503
|
+
"rdata": [dnsutil.to_api(rrtype, v) for v in values],
|
|
504
|
+
},
|
|
505
|
+
)
|
|
506
|
+
return _record_view(z["id"], rel, rrtype, _find_record(client, z["id"], rel, rrtype))
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
def delete_record(zone, name, rtype, profile="noderings"):
|
|
510
|
+
"""
|
|
511
|
+
Delete the records of one name and type. Returns ``False`` when there are
|
|
512
|
+
none.
|
|
513
|
+
|
|
514
|
+
CLI Example:
|
|
515
|
+
|
|
516
|
+
.. code-block:: bash
|
|
517
|
+
|
|
518
|
+
salt '*' noderings.delete_record example.com www A
|
|
519
|
+
"""
|
|
520
|
+
client = _client(profile)
|
|
521
|
+
z = _require_zone(client, zone)
|
|
522
|
+
rrtype = rtype.upper()
|
|
523
|
+
rel = dnsutil.relative_name(name, z.get("name"))
|
|
524
|
+
if not _find_record(client, z["id"], rel, rrtype):
|
|
525
|
+
return False
|
|
526
|
+
try:
|
|
527
|
+
client.delete_dns_record(z["id"], rel, rrtype)
|
|
528
|
+
except NodeRingsError as exc:
|
|
529
|
+
if not is_not_found(exc):
|
|
530
|
+
raise CommandExecutionError(f"deleting DNS record {rel} {rrtype}: {exc}") from exc
|
|
531
|
+
return True
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
def enable_dnssec(zone, profile="noderings"):
|
|
535
|
+
"""
|
|
536
|
+
Sign a zone with DNSSEC and return the DS record to add at the registrar.
|
|
537
|
+
|
|
538
|
+
CLI Example:
|
|
539
|
+
|
|
540
|
+
.. code-block:: bash
|
|
541
|
+
|
|
542
|
+
salt '*' noderings.enable_dnssec example.com
|
|
543
|
+
"""
|
|
544
|
+
client = _client(profile)
|
|
545
|
+
z = _require_zone(client, zone)
|
|
546
|
+
_call(f"enabling DNSSEC on {zone}", client.enable_dnssec, z["id"])
|
|
547
|
+
return snake(_call(f"reading DNSSEC of {zone}", client.get_dnssec, z["id"]))
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
def get_dnssec(zone, profile="noderings"):
|
|
551
|
+
"""
|
|
552
|
+
Return the DNSSEC state of a zone, including its DS record.
|
|
553
|
+
|
|
554
|
+
CLI Example:
|
|
555
|
+
|
|
556
|
+
.. code-block:: bash
|
|
557
|
+
|
|
558
|
+
salt '*' noderings.get_dnssec example.com
|
|
559
|
+
"""
|
|
560
|
+
client = _client(profile)
|
|
561
|
+
z = _require_zone(client, zone)
|
|
562
|
+
return snake(_call(f"reading DNSSEC of {zone}", client.get_dnssec, z["id"]))
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
# -- firewall -------------------------------------------------------------------
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
def _rules_to_api(client, rules):
|
|
569
|
+
if not isinstance(rules, list):
|
|
570
|
+
raise SaltInvocationError("rules must be a list of rule mappings")
|
|
571
|
+
names = set()
|
|
572
|
+
for i, rule in enumerate(rules):
|
|
573
|
+
if not isinstance(rule, dict) or not rule.get("direction") or not rule.get("action"):
|
|
574
|
+
raise SaltInvocationError(f"rule {i} needs at least direction and action")
|
|
575
|
+
rule.setdefault("protocol", "tcp")
|
|
576
|
+
err = fwutil.validate_rule(rule, i)
|
|
577
|
+
if err:
|
|
578
|
+
raise SaltInvocationError(err)
|
|
579
|
+
for side in ("source", "destination"):
|
|
580
|
+
names.update((rule.get(side) or {}).get("address_groups") or [])
|
|
581
|
+
groups = {}
|
|
582
|
+
if names:
|
|
583
|
+
existing = _call("listing address groups", client.list_firewall_address_groups)
|
|
584
|
+
for name in names:
|
|
585
|
+
match = find_by_name(existing, name)
|
|
586
|
+
if not match:
|
|
587
|
+
raise SaltInvocationError(f"no firewall address group is named {name}")
|
|
588
|
+
groups[name] = match["id"]
|
|
589
|
+
if len(rules) > fwutil.MAX_RULES:
|
|
590
|
+
raise SaltInvocationError(f"a ruleset holds at most {fwutil.MAX_RULES} rules")
|
|
591
|
+
return [fwutil.rule_to_api(r, groups) for r in rules]
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
def list_rulesets(profile="noderings"):
|
|
595
|
+
"""
|
|
596
|
+
List firewall rulesets.
|
|
597
|
+
|
|
598
|
+
CLI Example:
|
|
599
|
+
|
|
600
|
+
.. code-block:: bash
|
|
601
|
+
|
|
602
|
+
salt '*' noderings.list_rulesets
|
|
603
|
+
"""
|
|
604
|
+
client = _client(profile)
|
|
605
|
+
return snake(_call("listing firewall rulesets", client.list_firewall_rulesets))
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
def get_ruleset(ruleset, profile="noderings"):
|
|
609
|
+
"""
|
|
610
|
+
Return a firewall ruleset by name or ID with its rules in order, or ``None``.
|
|
611
|
+
|
|
612
|
+
CLI Example:
|
|
613
|
+
|
|
614
|
+
.. code-block:: bash
|
|
615
|
+
|
|
616
|
+
salt '*' noderings.get_ruleset web
|
|
617
|
+
"""
|
|
618
|
+
client = _client(profile)
|
|
619
|
+
rs = _ruleset(client, ruleset)
|
|
620
|
+
if not rs:
|
|
621
|
+
return None
|
|
622
|
+
out = snake(rs)
|
|
623
|
+
out["rules"] = [
|
|
624
|
+
fwutil.rule_view(r) for r in _call("listing rules", client.list_firewall_rules, rs["id"])
|
|
625
|
+
]
|
|
626
|
+
return out
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
def create_ruleset(ruleset, profile="noderings"):
|
|
630
|
+
"""
|
|
631
|
+
Create an empty firewall ruleset.
|
|
632
|
+
|
|
633
|
+
CLI Example:
|
|
634
|
+
|
|
635
|
+
.. code-block:: bash
|
|
636
|
+
|
|
637
|
+
salt '*' noderings.create_ruleset web
|
|
638
|
+
"""
|
|
639
|
+
client = _client(profile)
|
|
640
|
+
return snake(_call(f"creating ruleset {ruleset}", client.create_firewall_ruleset, ruleset))
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
def set_rules(ruleset, rules, profile="noderings"):
|
|
644
|
+
"""
|
|
645
|
+
Make a ruleset hold exactly ``rules``, in this order. Rules run top to
|
|
646
|
+
bottom; traffic that matches no rule is accepted, so end with a drop rule.
|
|
647
|
+
|
|
648
|
+
Each rule is a mapping with ``direction`` (``ingress`` or ``egress``),
|
|
649
|
+
``action`` (``accept`` or ``drop``), ``protocol`` (default ``tcp``),
|
|
650
|
+
``source`` and ``destination`` (each with ``cidrs``, ``address_groups``,
|
|
651
|
+
``address_group_ids`` or ``vm_tags``), and ``destination_ports`` or
|
|
652
|
+
``source_ports`` (one port such as ``443`` or one range such as
|
|
653
|
+
``8000-8100``).
|
|
654
|
+
|
|
655
|
+
CLI Example:
|
|
656
|
+
|
|
657
|
+
.. code-block:: bash
|
|
658
|
+
|
|
659
|
+
salt '*' noderings.set_rules web '[{"direction": "ingress", "action": "accept", "protocol": "tcp", "destination_ports": "22", "source": {"cidrs": ["0.0.0.0/0"]}}]'
|
|
660
|
+
"""
|
|
661
|
+
client = _client(profile)
|
|
662
|
+
rs = _ruleset(client, ruleset)
|
|
663
|
+
if not rs:
|
|
664
|
+
raise SaltInvocationError(f"no firewall ruleset is named {ruleset}")
|
|
665
|
+
desired = _rules_to_api(client, rules)
|
|
666
|
+
final = _call(f"writing rules of {ruleset}", fwutil.reconcile, client, rs["id"], desired)
|
|
667
|
+
return [fwutil.rule_view(r) for r in final]
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
def delete_ruleset(ruleset, force=False, profile="noderings"):
|
|
671
|
+
"""
|
|
672
|
+
Delete a firewall ruleset. ``force=True`` also deletes one that is still
|
|
673
|
+
attached to VMs. Returns ``False`` when it does not exist.
|
|
674
|
+
|
|
675
|
+
CLI Example:
|
|
676
|
+
|
|
677
|
+
.. code-block:: bash
|
|
678
|
+
|
|
679
|
+
salt '*' noderings.delete_ruleset web
|
|
680
|
+
"""
|
|
681
|
+
client = _client(profile)
|
|
682
|
+
rs = _ruleset(client, ruleset)
|
|
683
|
+
if not rs:
|
|
684
|
+
return False
|
|
685
|
+
_call(f"deleting ruleset {ruleset}", client.delete_firewall_ruleset, rs["id"], force)
|
|
686
|
+
return True
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
# -- virtual machines -----------------------------------------------------------
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
def list_vms(tags=None, profile="noderings"):
|
|
693
|
+
"""
|
|
694
|
+
List virtual machines, optionally only those with all of ``tags``.
|
|
695
|
+
|
|
696
|
+
CLI Example:
|
|
697
|
+
|
|
698
|
+
.. code-block:: bash
|
|
699
|
+
|
|
700
|
+
salt '*' noderings.list_vms
|
|
701
|
+
salt '*' noderings.list_vms tags='["web"]'
|
|
702
|
+
"""
|
|
703
|
+
client = _client(profile)
|
|
704
|
+
want = set(tags or [])
|
|
705
|
+
return [
|
|
706
|
+
compute.vm_view(v)
|
|
707
|
+
for v in _call("listing virtual machines", client.list_virtual_machines)
|
|
708
|
+
if want.issubset(set(v.get("tags") or []))
|
|
709
|
+
]
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
def get_vm(name, profile="noderings"):
|
|
713
|
+
"""
|
|
714
|
+
Return a virtual machine by hostname or ID, or ``None``.
|
|
715
|
+
|
|
716
|
+
CLI Example:
|
|
717
|
+
|
|
718
|
+
.. code-block:: bash
|
|
719
|
+
|
|
720
|
+
salt '*' noderings.get_vm web-1
|
|
721
|
+
"""
|
|
722
|
+
client = _client(profile)
|
|
723
|
+
return compute.vm_view(_vm(client, name)) or None
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
def create_vm(
|
|
727
|
+
name,
|
|
728
|
+
plan_code=None,
|
|
729
|
+
country=None,
|
|
730
|
+
city=None,
|
|
731
|
+
distro="ubuntu_24_04",
|
|
732
|
+
billing_type="usage",
|
|
733
|
+
ssh_keys=None,
|
|
734
|
+
tags=None,
|
|
735
|
+
firewall_ruleset=None,
|
|
736
|
+
password=None,
|
|
737
|
+
plan_id=None,
|
|
738
|
+
region_id=None,
|
|
739
|
+
provider_id=None,
|
|
740
|
+
sort="price",
|
|
741
|
+
wait=True,
|
|
742
|
+
wait_timeout=900,
|
|
743
|
+
profile="noderings",
|
|
744
|
+
):
|
|
745
|
+
"""
|
|
746
|
+
Order a virtual machine and return it once it is running.
|
|
747
|
+
|
|
748
|
+
Give either ``plan_code``, ``country`` and ``city`` (the cheapest matching
|
|
749
|
+
offer is ordered, or the best rated with ``sort=rating``), or ``plan_id``
|
|
750
|
+
and ``region_id`` from ``noderings.list_offers``. ``billing_type=usage``
|
|
751
|
+
bills by the hour from prepaid credit and stops when the VM is deleted.
|
|
752
|
+
|
|
753
|
+
CLI Example:
|
|
754
|
+
|
|
755
|
+
.. code-block:: bash
|
|
756
|
+
|
|
757
|
+
salt '*' noderings.create_vm web-1 plan_code=SC-GC-1C-1G-20 country="United States" city=Ashburn ssh_keys='["laptop"]'
|
|
758
|
+
"""
|
|
759
|
+
client = _client(profile)
|
|
760
|
+
if _vm(client, name):
|
|
761
|
+
raise SaltInvocationError(f"a virtual machine named {name} already exists")
|
|
762
|
+
ruleset_id = _call(
|
|
763
|
+
"resolving firewall ruleset", compute.resolve_ruleset, client, firewall_ruleset
|
|
764
|
+
)
|
|
765
|
+
if not plan_id:
|
|
766
|
+
if not (plan_code and country and city):
|
|
767
|
+
raise SaltInvocationError("give plan_code, country and city, or plan_id and region_id")
|
|
768
|
+
caps = ["guest_firewall"] if ruleset_id else []
|
|
769
|
+
plan_id, region_id, _ = _call(
|
|
770
|
+
"choosing an offer",
|
|
771
|
+
compute.choose_offer,
|
|
772
|
+
client,
|
|
773
|
+
plan_code,
|
|
774
|
+
country,
|
|
775
|
+
city,
|
|
776
|
+
billing_type=billing_type,
|
|
777
|
+
provider_id=provider_id,
|
|
778
|
+
require_capabilities=caps,
|
|
779
|
+
sort=sort,
|
|
780
|
+
)
|
|
781
|
+
elif not region_id:
|
|
782
|
+
raise SaltInvocationError("plan_id needs region_id")
|
|
783
|
+
key_ids = _call("resolving SSH keys", compute.resolve_ssh_keys, client, ssh_keys)
|
|
784
|
+
vm = _call(
|
|
785
|
+
f"ordering virtual machine {name}",
|
|
786
|
+
compute.order_vm,
|
|
787
|
+
client,
|
|
788
|
+
name,
|
|
789
|
+
plan_id,
|
|
790
|
+
region_id,
|
|
791
|
+
distro=distro,
|
|
792
|
+
ssh_key_ids=key_ids,
|
|
793
|
+
password=password,
|
|
794
|
+
tags=tags,
|
|
795
|
+
ruleset_id=ruleset_id,
|
|
796
|
+
wait=wait,
|
|
797
|
+
timeout=int(wait_timeout),
|
|
798
|
+
)
|
|
799
|
+
return compute.vm_view(vm)
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
def delete_vm(name, profile="noderings"):
|
|
803
|
+
"""
|
|
804
|
+
Delete a virtual machine. An hourly VM stops billing; a monthly
|
|
805
|
+
subscription is cancelled without a refund. Returns ``False`` when it does
|
|
806
|
+
not exist.
|
|
807
|
+
|
|
808
|
+
CLI Example:
|
|
809
|
+
|
|
810
|
+
.. code-block:: bash
|
|
811
|
+
|
|
812
|
+
salt '*' noderings.delete_vm web-1
|
|
813
|
+
"""
|
|
814
|
+
client = _client(profile)
|
|
815
|
+
vm = _vm(client, name)
|
|
816
|
+
if not vm:
|
|
817
|
+
return False
|
|
818
|
+
_call(f"deleting virtual machine {name}", client.delete_virtual_machine, vm["id"])
|
|
819
|
+
return True
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
def _power(name, want, wait, wait_timeout, profile):
|
|
823
|
+
client = _client(profile)
|
|
824
|
+
vm = _require_vm(client, name)
|
|
825
|
+
return compute.vm_view(
|
|
826
|
+
_call(
|
|
827
|
+
f"changing power of {name}",
|
|
828
|
+
compute.set_power,
|
|
829
|
+
client,
|
|
830
|
+
vm["id"],
|
|
831
|
+
want,
|
|
832
|
+
wait,
|
|
833
|
+
int(wait_timeout),
|
|
834
|
+
)
|
|
835
|
+
)
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
def start_vm(name, wait=True, wait_timeout=900, profile="noderings"):
|
|
839
|
+
"""
|
|
840
|
+
Start a stopped virtual machine.
|
|
841
|
+
|
|
842
|
+
CLI Example:
|
|
843
|
+
|
|
844
|
+
.. code-block:: bash
|
|
845
|
+
|
|
846
|
+
salt '*' noderings.start_vm web-1
|
|
847
|
+
"""
|
|
848
|
+
return _power(name, "running", wait, wait_timeout, profile)
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
def stop_vm(name, wait=True, wait_timeout=900, profile="noderings"):
|
|
852
|
+
"""
|
|
853
|
+
Stop a virtual machine. Its disk and IP addresses are kept.
|
|
854
|
+
|
|
855
|
+
CLI Example:
|
|
856
|
+
|
|
857
|
+
.. code-block:: bash
|
|
858
|
+
|
|
859
|
+
salt '*' noderings.stop_vm web-1
|
|
860
|
+
"""
|
|
861
|
+
return _power(name, "stopped", wait, wait_timeout, profile)
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
def restart_vm(name, wait=True, wait_timeout=900, profile="noderings"):
|
|
865
|
+
"""
|
|
866
|
+
Restart a virtual machine.
|
|
867
|
+
|
|
868
|
+
CLI Example:
|
|
869
|
+
|
|
870
|
+
.. code-block:: bash
|
|
871
|
+
|
|
872
|
+
salt '*' noderings.restart_vm web-1
|
|
873
|
+
"""
|
|
874
|
+
return _power(name, "restarted", wait, wait_timeout, profile)
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
def reinstall_vm(
|
|
878
|
+
name, distro, ssh_keys=None, password=None, wait=True, wait_timeout=900, profile="noderings"
|
|
879
|
+
):
|
|
880
|
+
"""
|
|
881
|
+
Reinstall a virtual machine with another operating system. This erases the
|
|
882
|
+
disk. The VM keeps its IP addresses.
|
|
883
|
+
|
|
884
|
+
CLI Example:
|
|
885
|
+
|
|
886
|
+
.. code-block:: bash
|
|
887
|
+
|
|
888
|
+
salt '*' noderings.reinstall_vm web-1 debian_13 ssh_keys='["laptop"]'
|
|
889
|
+
"""
|
|
890
|
+
client = _client(profile)
|
|
891
|
+
vm = _require_vm(client, name)
|
|
892
|
+
key_ids = _call("resolving SSH keys", compute.resolve_ssh_keys, client, ssh_keys)
|
|
893
|
+
return compute.vm_view(
|
|
894
|
+
_call(
|
|
895
|
+
f"reinstalling {name}",
|
|
896
|
+
compute.reinstall,
|
|
897
|
+
client,
|
|
898
|
+
vm["id"],
|
|
899
|
+
vm.get("hostname"),
|
|
900
|
+
distro,
|
|
901
|
+
ssh_key_ids=key_ids,
|
|
902
|
+
password=password,
|
|
903
|
+
wait=wait,
|
|
904
|
+
timeout=int(wait_timeout),
|
|
905
|
+
)
|
|
906
|
+
)
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
def attach_ruleset(name, ruleset, wait=True, wait_timeout=900, profile="noderings"):
|
|
910
|
+
"""
|
|
911
|
+
Put a firewall ruleset in front of a virtual machine, replacing any other.
|
|
912
|
+
|
|
913
|
+
CLI Example:
|
|
914
|
+
|
|
915
|
+
.. code-block:: bash
|
|
916
|
+
|
|
917
|
+
salt '*' noderings.attach_ruleset web-1 web
|
|
918
|
+
"""
|
|
919
|
+
client = _client(profile)
|
|
920
|
+
vm = _require_vm(client, name)
|
|
921
|
+
ruleset_id = _call("resolving firewall ruleset", compute.resolve_ruleset, client, ruleset)
|
|
922
|
+
if vm.get("firewallRulesetId") == ruleset_id:
|
|
923
|
+
return compute.vm_view(vm)
|
|
924
|
+
if vm.get("firewallRulesetId"):
|
|
925
|
+
_call(
|
|
926
|
+
"detaching the current firewall",
|
|
927
|
+
compute.detach_firewall,
|
|
928
|
+
client,
|
|
929
|
+
vm["id"],
|
|
930
|
+
vm["firewallRulesetId"],
|
|
931
|
+
wait,
|
|
932
|
+
int(wait_timeout),
|
|
933
|
+
)
|
|
934
|
+
return compute.vm_view(
|
|
935
|
+
_call(
|
|
936
|
+
"attaching firewall",
|
|
937
|
+
compute.attach_firewall,
|
|
938
|
+
client,
|
|
939
|
+
vm["id"],
|
|
940
|
+
ruleset_id,
|
|
941
|
+
wait,
|
|
942
|
+
int(wait_timeout),
|
|
943
|
+
)
|
|
944
|
+
)
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
def detach_ruleset(name, wait=True, wait_timeout=900, profile="noderings"):
|
|
948
|
+
"""
|
|
949
|
+
Remove the firewall ruleset from a virtual machine.
|
|
950
|
+
|
|
951
|
+
CLI Example:
|
|
952
|
+
|
|
953
|
+
.. code-block:: bash
|
|
954
|
+
|
|
955
|
+
salt '*' noderings.detach_ruleset web-1
|
|
956
|
+
"""
|
|
957
|
+
client = _client(profile)
|
|
958
|
+
vm = _require_vm(client, name)
|
|
959
|
+
if not vm.get("firewallRulesetId"):
|
|
960
|
+
return compute.vm_view(vm)
|
|
961
|
+
return compute.vm_view(
|
|
962
|
+
_call(
|
|
963
|
+
"detaching firewall",
|
|
964
|
+
compute.detach_firewall,
|
|
965
|
+
client,
|
|
966
|
+
vm["id"],
|
|
967
|
+
vm["firewallRulesetId"],
|
|
968
|
+
wait,
|
|
969
|
+
int(wait_timeout),
|
|
970
|
+
)
|
|
971
|
+
)
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
def set_tags(name, tags, profile="noderings"):
|
|
975
|
+
"""
|
|
976
|
+
Make a virtual machine carry exactly ``tags``.
|
|
977
|
+
|
|
978
|
+
CLI Example:
|
|
979
|
+
|
|
980
|
+
.. code-block:: bash
|
|
981
|
+
|
|
982
|
+
salt '*' noderings.set_tags web-1 '["web", "prod"]'
|
|
983
|
+
"""
|
|
984
|
+
client = _client(profile)
|
|
985
|
+
vm = _require_vm(client, name)
|
|
986
|
+
have = set(compute.vm_view(vm)["tags"])
|
|
987
|
+
want = set(tags or [])
|
|
988
|
+
if have != want:
|
|
989
|
+
_call(
|
|
990
|
+
f"updating tags of {name}",
|
|
991
|
+
client.update_virtual_machine_tags,
|
|
992
|
+
vm["id"],
|
|
993
|
+
sorted(want - have),
|
|
994
|
+
sorted(have - want),
|
|
995
|
+
)
|
|
996
|
+
return compute.vm_view(_call(f"reading {name}", client.get_virtual_machine, vm["id"]))
|