[Scaleway] Add module to manage container registries (#5399)

* [Scaleway] Add module to manage container registries

Signed-off-by: Lunik <lunik@tiwabbit.fr>

* first review

Signed-off-by: Lunik <lunik@tiwabbit.fr>

* lint documentation on return value

Signed-off-by: Lunik <lunik@tiwabbit.fr>

* second review

Signed-off-by: Lunik <lunik@tiwabbit.fr>

* second review *bis

Signed-off-by: Lunik <lunik@tiwabbit.fr>

* second review *ter

Signed-off-by: Lunik <lunik@tiwabbit.fr>

* Fix typo.

Signed-off-by: Lunik <lunik@tiwabbit.fr>
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Guillaume MARTINEZ 2022-10-23 17:18:06 +02:00 committed by GitHub
parent 91cac4c816
commit 2680739153
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 805 additions and 0 deletions

View file

@ -9,6 +9,8 @@ __metaclass__ = type
import json
import re
import sys
import datetime
import time
from ansible.module_utils.basic import env_fallback
from ansible.module_utils.urls import fetch_url
@ -26,6 +28,14 @@ def scaleway_argument_spec():
)
def scaleway_waitable_resource_argument_spec():
return dict(
wait=dict(type="bool", default=True),
wait_timeout=dict(type="int", default=300),
wait_sleep_time=dict(type="int", default=3),
)
def payload_from_object(scw_object):
return dict(
(k, v)
@ -63,6 +73,25 @@ def parse_pagination_link(header):
return parsed_relations
def filter_sensitive_attributes(container, attributes):
for attr in attributes:
container[attr] = "SENSITIVE_VALUE"
return container
def resource_attributes_should_be_changed(target, wished, verifiable_mutable_attributes, mutable_attributes):
diff = dict()
for attr in verifiable_mutable_attributes:
if wished[attr] is not None and target[attr] != wished[attr]:
diff[attr] = wished[attr]
if diff:
return dict((attr, wished[attr]) for attr in mutable_attributes)
else:
return diff
class Response(object):
def __init__(self, resp, info):
@ -169,6 +198,78 @@ class Scaleway(object):
def warn(self, x):
self.module.warn(str(x))
def fetch_state(self, resource):
self.module.debug("fetch_state of resource: %s" % resource["id"])
response = self.get(path=self.api_path + "/%s" % resource["id"])
if response.status_code == 404:
return "absent"
if not response.ok:
msg = 'Error during state fetching: (%s) %s' % (response.status_code, response.json)
self.module.fail_json(msg=msg)
try:
self.module.debug("Resource %s in state: %s" % (resource["id"], response.json["status"]))
return response.json["status"]
except KeyError:
self.module.fail_json(msg="Could not fetch state in %s" % response.json)
def fetch_paginated_resources(self, resource_key, **pagination_kwargs):
response = self.get(
path=self.api_path,
params=pagination_kwargs)
status_code = response.status_code
if not response.ok:
self.module.fail_json(msg='Error getting {0} [{1}: {2}]'.format(
resource_key,
response.status_code, response.json['message']))
return response.json[resource_key]
def fetch_all_resources(self, resource_key, **pagination_kwargs):
resources = []
result = [None]
while len(result) != 0:
result = self.fetch_paginated_resources(resource_key, **pagination_kwargs)
resources += result
if 'page' in pagination_kwargs:
pagination_kwargs['page'] += 1
else:
pagination_kwargs['page'] = 2
return resources
def wait_to_complete_state_transition(self, resource, stable_states, force_wait=False):
wait = self.module.params["wait"]
if not (wait or force_wait):
return
wait_timeout = self.module.params["wait_timeout"]
wait_sleep_time = self.module.params["wait_sleep_time"]
# Prevent requesting the ressource status too soon
time.sleep(wait_sleep_time)
start = datetime.datetime.utcnow()
end = start + datetime.timedelta(seconds=wait_timeout)
while datetime.datetime.utcnow() < end:
self.module.debug("We are going to wait for the resource to finish its transition")
state = self.fetch_state(resource)
if state in stable_states:
self.module.debug("It seems that the resource is not in transition anymore.")
self.module.debug("load-balancer in state: %s" % self.fetch_state(resource))
break
time.sleep(wait_sleep_time)
else:
self.module.fail_json(msg="Server takes too long to finish its transition")
SCALEWAY_LOCATION = {
'par1': {