Skip to content

Commit

Permalink
feat: add project_id and organization_id options
Browse files Browse the repository at this point in the history
  • Loading branch information
quantumsheep committed Jun 5, 2023
1 parent 4c8a60e commit d9c7898
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 44 deletions.
10 changes: 10 additions & 0 deletions plugins/doc_fragments/scaleway.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ class ModuleDocFragment(object):
- Scaleway API URL.
type: str
default: https://api.scaleway.com
project_id:
description:
- Scaleway project ID.
type: str
required: false
organization_id:
description:
- Scaleway organization ID.
type: str
required: false
api_allow_insecure:
description:
- Allow insecure connection to Scaleway API.
Expand Down
101 changes: 57 additions & 44 deletions plugins/inventory/scaleway.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,55 @@
DOCUMENTATION = r"""
name: scaleway
author:
- Nathanael Demacon (@quantumsheep)
- Nathanael Demacon (@quantumsheep)
short_description: Scaleway inventory source
version_added: "1.0.0"
requirements:
- scaleway >= 0.6.0
- scaleway >= 0.6.0
description:
- Scaleway inventory plugin.
- Uses configuration file that ends with '(scaleway|scw).(yaml|yml)'.
- Scaleway inventory plugin.
- Uses configuration file that ends with '(scaleway|scw).(yaml|yml)'.
extends_documentation_fragment:
- scaleway.scaleway.scaleway
- constructed
- inventory_cache
- scaleway.scaleway.scaleway
- constructed
- inventory_cache
options:
plugin:
description:
- The name of the Scaleway Inventory Plugin, this should always be C(scaleway.scaleway.scaleway).
required: true
choices: ['scaleway.scaleway.scaleway']
zones:
description:
- List of zones to filter on.
type: list
elements: str
default:
- fr-par-1
- fr-par-2
- fr-par-3
- nl-ams-1
- nl-ams-2
- pl-waw-1
- pl-waw-2
tags:
description:
- List of tags to filter on.
type: list
elements: str
default: []
hostnames:
description: List of preference about what to use as an hostname.
type: list
elements: str
default:
- public_ipv4
choices:
- public_ipv4
- private_ipv4
- public_ipv6
- hostname
- id
plugin:
description:
- The name of the Scaleway Inventory Plugin, this should always be C(scaleway.scaleway.scaleway).
required: true
choices: ['scaleway.scaleway.scaleway']
zones:
description:
- List of zones to filter on.
type: list
elements: str
default:
- fr-par-1
- fr-par-2
- fr-par-3
- nl-ams-1
- nl-ams-2
- pl-waw-1
- pl-waw-2
tags:
description:
- List of tags to filter on.
type: list
elements: str
default: []
hostnames:
description: List of preference about what to use as an hostname.
type: list
elements: str
default:
- public_ipv4
choices:
- public_ipv4
- private_ipv4
- public_ipv6
- hostname
- id
"""

EXAMPLES = r"""
Expand Down Expand Up @@ -177,7 +177,12 @@ def populate(self, results: List[_Host]):

for result in results:
groups = self.get_host_groups(result)
hostname = self._get_hostname(result, hostnames)

try:
hostname = self._get_hostname(result, hostnames)
except AnsibleError as e:
self.display.warning(f"Skipping host {result.id}: {e}")
continue

for group in groups:
self.inventory.add_group(group=group)
Expand Down Expand Up @@ -308,6 +313,8 @@ def _get_client(self):
profile = self.get_option("profile")
access_key = self.get_option("access_key")
secret_key = self.get_option("secret_key")
organization_id = self.get_option("organization_id")
project_id = self.get_option("project_id")
api_url = self.get_option("api_url")
api_allow_insecure = self.get_option("api_allow_insecure")
user_agent = self.get_option("user_agent")
Expand All @@ -326,6 +333,12 @@ def _get_client(self):
if secret_key:
client.secret_key = secret_key

if organization_id:
client.default_organization_id = organization_id

if project_id:
client.default_project_id = project_id

if api_url:
client.api_url = api_url

Expand Down
12 changes: 12 additions & 0 deletions plugins/module_utils/scaleway.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def scaleway_argument_spec() -> Dict[str, Dict[str, Any]]:
fallback=(env_fallback, [ENV_KEY_SCW_API_URL]),
default="https://api.scaleway.com",
),
organization_id=dict(type="str", required=False),
project_id=dict(type="str", required=False),
api_allow_insecure=dict(type="bool", default=False),
user_agent=dict(type="str", required=False),
)
Expand All @@ -73,6 +75,8 @@ def scaleway_get_client_from_module(module: AnsibleModule):
profile = module.params["profile"]
access_key = module.params["access_key"]
secret_key = module.params["secret_key"]
organization_id = module.params["organization_id"]
project_id = module.params["project_id"]
api_url = module.params["api_url"]
api_allow_insecure = module.params["api_allow_insecure"]
user_agent = module.params["user_agent"]
Expand All @@ -91,6 +95,12 @@ def scaleway_get_client_from_module(module: AnsibleModule):
if secret_key:
client.secret_key = secret_key

if organization_id:
client.default_organization_id = organization_id

if project_id:
client.default_project_id = project_id

if api_url:
client.api_url = api_url

Expand All @@ -109,6 +119,8 @@ def scaleway_pop_client_params(module: AnsibleModule) -> None:
"profile",
"access_key",
"secret_key",
"organization_id",
"project_id",
"api_url",
"api_allow_insecure",
"user_agent",
Expand Down

0 comments on commit d9c7898

Please sign in to comment.