mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +00:00
Reformat everything.
This commit is contained in:
@@ -141,9 +141,7 @@ def _fetch_information(token, url):
|
||||
paginated_url = url
|
||||
while True:
|
||||
try:
|
||||
response = open_url(paginated_url,
|
||||
headers={'X-Auth-Token': token,
|
||||
'Content-type': 'application/json'})
|
||||
response = open_url(paginated_url, headers={"X-Auth-Token": token, "Content-type": "application/json"})
|
||||
except Exception as e:
|
||||
raise AnsibleError(f"Error while fetching {url}: {e}")
|
||||
try:
|
||||
@@ -156,13 +154,13 @@ def _fetch_information(token, url):
|
||||
except KeyError:
|
||||
raise AnsibleError("Incorrect format from the Scaleway API response")
|
||||
|
||||
link = response.headers['Link']
|
||||
link = response.headers["Link"]
|
||||
if not link:
|
||||
return results
|
||||
relations = parse_pagination_link(link)
|
||||
if 'next' not in relations:
|
||||
if "next" not in relations:
|
||||
return results
|
||||
paginated_url = urllib_parse.urljoin(paginated_url, relations['next'])
|
||||
paginated_url = urllib_parse.urljoin(paginated_url, relations["next"])
|
||||
|
||||
|
||||
def _build_server_url(api_endpoint):
|
||||
@@ -223,12 +221,12 @@ extractors = {
|
||||
"private_ipv4": extract_private_ipv4,
|
||||
"public_ipv6": extract_public_ipv6,
|
||||
"hostname": extract_hostname,
|
||||
"id": extract_server_id
|
||||
"id": extract_server_id,
|
||||
}
|
||||
|
||||
|
||||
class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
NAME = 'community.general.scaleway'
|
||||
NAME = "community.general.scaleway"
|
||||
|
||||
def _fill_host_variables(self, host, server_info):
|
||||
targeted_attributes = (
|
||||
@@ -275,7 +273,6 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
return matching_tags.union((server_zone,))
|
||||
|
||||
def _filter_host(self, host_infos, hostname_preferences):
|
||||
|
||||
for pref in hostname_preferences:
|
||||
if extractors[pref](host_infos):
|
||||
return extractors[pref](host_infos)
|
||||
@@ -290,9 +287,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
raw_zone_hosts_infos = make_unsafe(_fetch_information(url=url, token=token))
|
||||
|
||||
for host_infos in raw_zone_hosts_infos:
|
||||
|
||||
hostname = self._filter_host(host_infos=host_infos,
|
||||
hostname_preferences=hostname_preferences)
|
||||
hostname = self._filter_host(host_infos=host_infos, hostname_preferences=hostname_preferences)
|
||||
|
||||
# No suitable hostname were found in the attributes and the host won't be in the inventory
|
||||
if not hostname:
|
||||
@@ -306,38 +301,38 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
self._fill_host_variables(host=hostname, server_info=host_infos)
|
||||
|
||||
# Composed variables
|
||||
self._set_composite_vars(self.get_option('variables'), host_infos, hostname, strict=False)
|
||||
self._set_composite_vars(self.get_option("variables"), host_infos, hostname, strict=False)
|
||||
|
||||
def get_oauth_token(self):
|
||||
oauth_token = self.get_option('oauth_token')
|
||||
oauth_token = self.get_option("oauth_token")
|
||||
|
||||
if 'SCW_CONFIG_PATH' in os.environ:
|
||||
scw_config_path = os.getenv('SCW_CONFIG_PATH')
|
||||
elif 'XDG_CONFIG_HOME' in os.environ:
|
||||
scw_config_path = os.path.join(os.getenv('XDG_CONFIG_HOME'), 'scw', 'config.yaml')
|
||||
if "SCW_CONFIG_PATH" in os.environ:
|
||||
scw_config_path = os.getenv("SCW_CONFIG_PATH")
|
||||
elif "XDG_CONFIG_HOME" in os.environ:
|
||||
scw_config_path = os.path.join(os.getenv("XDG_CONFIG_HOME"), "scw", "config.yaml")
|
||||
else:
|
||||
scw_config_path = os.path.join(os.path.expanduser('~'), '.config', 'scw', 'config.yaml')
|
||||
scw_config_path = os.path.join(os.path.expanduser("~"), ".config", "scw", "config.yaml")
|
||||
|
||||
if not oauth_token and os.path.exists(scw_config_path):
|
||||
with open(scw_config_path) as fh:
|
||||
scw_config = yaml.safe_load(fh)
|
||||
ansible_profile = self.get_option('scw_profile')
|
||||
ansible_profile = self.get_option("scw_profile")
|
||||
|
||||
if ansible_profile:
|
||||
active_profile = ansible_profile
|
||||
else:
|
||||
active_profile = scw_config.get('active_profile', 'default')
|
||||
active_profile = scw_config.get("active_profile", "default")
|
||||
|
||||
if active_profile == 'default':
|
||||
oauth_token = scw_config.get('secret_key')
|
||||
if active_profile == "default":
|
||||
oauth_token = scw_config.get("secret_key")
|
||||
else:
|
||||
oauth_token = scw_config['profiles'][active_profile].get('secret_key')
|
||||
oauth_token = scw_config["profiles"][active_profile].get("secret_key")
|
||||
|
||||
return oauth_token
|
||||
|
||||
def parse(self, inventory, loader, path, cache=True):
|
||||
if YAML_IMPORT_ERROR:
|
||||
raise AnsibleError('PyYAML is probably missing') from YAML_IMPORT_ERROR
|
||||
raise AnsibleError("PyYAML is probably missing") from YAML_IMPORT_ERROR
|
||||
super().parse(inventory, loader, path)
|
||||
self._read_config_data(path=path)
|
||||
|
||||
@@ -345,8 +340,12 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
tags = self.get_option("tags")
|
||||
token = self.get_oauth_token()
|
||||
if not token:
|
||||
raise AnsibleError("'oauth_token' value is null, you must configure it either in inventory, envvars or scaleway-cli config.")
|
||||
raise AnsibleError(
|
||||
"'oauth_token' value is null, you must configure it either in inventory, envvars or scaleway-cli config."
|
||||
)
|
||||
hostname_preference = self.get_option("hostnames")
|
||||
|
||||
for zone in self._get_zones(config_zones):
|
||||
self.do_zone_inventory(zone=make_unsafe(zone), token=token, tags=tags, hostname_preferences=hostname_preference)
|
||||
self.do_zone_inventory(
|
||||
zone=make_unsafe(zone), token=token, tags=tags, hostname_preferences=hostname_preference
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user