Skip to content

fix: fix modules validation and tests #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ansible-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
uses: ansible/ansible-lint@main
# optional (see below):
with:
args: ""
args: "--exclude=.github"
setup_python: "true"
working_directory: ""
requirements_file: ""
125 changes: 49 additions & 76 deletions .github/workflows/ansible-test-integration.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
name: Nightly integration tests
name: Integration tests

on:
schedule:
# Will run at 00:00 every day
- cron: "0 0 * * *"
workflow_call:
workflow_dispatch:
pull_request:
push:
branches:
- main

jobs:
integration:
Expand All @@ -14,77 +17,47 @@ jobs:
max-parallel: 1
matrix:
module:
# - scaleway_account_project
# - scaleway_applesilicon_server
# - scaleway_baremetal_server
# - scaleway_container
# - scaleway_container_cron
# - scaleway_container_domain
# - scaleway_container_namespace
# - scaleway_container_token
# - scaleway_domain_ssl_certificate
# - scaleway_flexibleip_flexible_ip
# - scaleway_function
# - scaleway_function_cron
# - scaleway_function_domain
# - scaleway_function_namespace
# - scaleway_function_token
# - scaleway_function_trigger
# - scaleway_iam_api_key
# - scaleway_iam_application
# - scaleway_iam_group
# - scaleway_iam_policy
# - scaleway_iam_ssh_key
# - scaleway_instance_image
# - scaleway_instance_ip
# - scaleway_instance_placement_group
# - scaleway_instance_private_nic
# - scaleway_instance_security_group
# - scaleway_instance_security_group_rule
# - scaleway_instance_snapshot
# - scaleway_instance_volume
# - scaleway_iot_device
# - scaleway_iot_hub
# - scaleway_iot_network
# - scaleway_iot_route
# - scaleway_k8s_cluster
# - scaleway_k8s_pool
# - scaleway_lb
# - scaleway_lb_acl
# - scaleway_lb_backend
# - scaleway_lb_certificate
# - scaleway_lb_frontend
# - scaleway_lb_route
# - scaleway_lb_subscriber
# - scaleway_mnq_credential
# - scaleway_mnq_namespace
# - scaleway_rdb_database_backup
# - scaleway_rdb_endpoint
# - scaleway_rdb_instance
# - scaleway_rdb_read_replica
# - scaleway_rdb_snapshot
# - scaleway_redis_cluster
- scaleway_registry_namespace
# - scaleway_secret
# - scaleway_vpc_private_network
# - scaleway_vpcgw_dhcp
# - scaleway_vpcgw_dhcp_entry
# - scaleway_vpcgw_gateway
# - scaleway_vpcgw_gateway_network
# - scaleway_vpcgw_ip
# - scaleway_vpcgw_pat_rule
- scaleway_secret

ansible-version:
- stable-2.17
- stable-2.18
- stable-2.19
env:
ansible-collections-path: "./ansible_collections/scaleway/scaleway"

steps:
- name: Perform testing
uses: ansible-community/ansible-test-gh-action@release/v1
with:
pre-test-cmd: >-
SCW_ACCESS_KEY=${{ secrets.SCW_ACCESS_KEY }}
SCW_SECRET_KEY=${{ secrets.SCW_SECRET_KEY }}
SCW_DEFAULT_PROJECT_ID=${{ secrets.SCW_DEFAULT_PROJECT_ID }}
eval
"echo \"$(cat "tests/integration/integration_config.yml.template")\""
> tests/integration/integration_config.yml
controller-python-version: auto
target: ${{ matrix.module }}
testing-type: integration
- name: Create directory
run: |
mkdir -p ${{ env.ansible-collections-path }}

- name: Checkout the repo
uses: actions/checkout@v4
with:
path: ${{ env.ansible-collections-path }}

- name: Setup Python and install ansible
uses: actions/setup-python@v5
with:
python-version: 3.11

- name: Install ansible
run: |
python -m pip install \
https://github.com/ansible/ansible/archive/${{ matrix.ansible-version }}.tar.gz \
--disable-pip-version-check

- name: Create scw config
run: |
mkdir -p $HOME/.config/scw
cat << EOF > $HOME/.config/scw/config.yaml
access_key: ${{ secrets.SCW_ACCESS_KEY }}
secret_key: ${{ secrets.SCW_SECRET_KEY }}
default_region: ${{ secrets.SCW_DEFAULT_REGION }}
default_project_id: ${{ secrets.SCW_DEFAULT_PROJECT_ID }}
EOF

- name: Run integration tests with ansible ${{ matrix.ansible-version }}
working-directory: ${{ env.ansible-collections-path }}
run: |
ansible-test integration -v --requirements ${{ matrix.module }}
7 changes: 2 additions & 5 deletions .github/workflows/ansible-test-sanity.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# We only support ansible-core versions that are not EOL
# https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html

name: sanity
name: Sanity

on:
pull_request:
push:
branches:
- main
# schedule:
# - cron: '0 6 * * *'
workflow_dispatch:

jobs:
Expand All @@ -19,10 +17,9 @@ jobs:
strategy:
matrix:
ansible:
- stable-2.16
- stable-2.19
- stable-2.17
- stable-2.18
- devel
runs-on: ubuntu-latest
steps:
- name: Perform testing
Expand Down
52 changes: 52 additions & 0 deletions .github/workflows/ansible-test-units.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# We only support ansible-core versions that are not EOL
# https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html

name: units

on:
pull_request:
push:
branches:
- main
workflow_dispatch:

jobs:
units:
timeout-minutes: 30
name: Units (Ⓐ$${{ matrix.ansible-version }})
strategy:
fail-fast: false
matrix:
ansible-version:
- stable-2.19
- stable-2.17
- stable-2.18
env:
ansible-collections-path: "./ansible_collections/scaleway/scaleway"

runs-on: ubuntu-latest
steps:
- name: Create directory
run: |
mkdir -p ${{ env.ansible-collections-path }}

- name: Checkout the repo
uses: actions/checkout@v4
with:
path: ${{ env.ansible-collections-path }}

- name: Setup Python and install ansible
uses: actions/setup-python@v5
with:
python-version: 3.11

- name: Install ansible
run: |
python -m pip install \
https://github.com/ansible/ansible/archive/${{ matrix.ansible-version }}.tar.gz \
--disable-pip-version-check

- name: Run unit tests with ansible ${{ matrix.ansible-version }}
working-directory: ${{ env.ansible-collections-path }}
run: |
ansible-test units -v --requirements
9 changes: 9 additions & 0 deletions .github/workflows/integration-nightly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: Integration Nightly

on:
schedule:
- cron: "0 0 * * *"

jobs:
integration:
uses: ./.github/workflows/ansible-test-integration.yml
15 changes: 8 additions & 7 deletions plugins/inventory/scaleway.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"""


import os
from dataclasses import dataclass, field
from types import SimpleNamespace
from typing import List, Optional
Expand Down Expand Up @@ -403,13 +404,13 @@ def _get_hostname(self, host: _Host, hostnames: List[str]) -> str:

def _get_client(self):
config_file = self.get_option("config_file")
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")
profile = self.get_option("profile") or os.getenv("SCW_PROFILE")
access_key = self.get_option("access_key") or os.getenv("SCW_ACCESS_KEY")
secret_key = self.get_option("secret_key") or os.getenv("SCW_SECRET_KEY")
organization_id = self.get_option("organization_id") or os.getenv("SCW_DEFAULT_ORGANIZATION_ID")
project_id = self.get_option("project_id") or os.getenv("SCW_DEFAULT_PROJECT_ID")
api_url = self.get_option("api_url") or os.getenv("SCW_API_URL")
api_allow_insecure = self.get_option("api_allow_insecure") or os.getenv("SCW_INSECURE")
user_agent = self.get_option("user_agent")

if profile:
Expand Down
27 changes: 26 additions & 1 deletion plugins/module_utils/scaleway.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import absolute_import, division, print_function

import os
from typing import Any, Dict

__metaclass__ = type
Expand Down Expand Up @@ -151,3 +151,28 @@ def object_to_dict(obj):
return {key: object_to_dict(value) for key, value in obj.__dict__.items()}
else:
return obj


def build_scaleway_client_and_module(argument_spec: dict, **kwargs: Any):
module = AnsibleModule(argument_spec=argument_spec, **kwargs)
if not HAS_SCALEWAY_SDK:
module.fail_json(msg=missing_required_lib("scaleway"))

client = Client.from_config_file_and_env(
filepath=os.environ.get("SCW_CONFIG_PATH"),
profile_name=os.environ.get("SCW_PROFILE"),
)
# handle a few client validation here because it is not done by the scaleway-sdk
if client.default_region is None:
module.fail_json(
msg="default_region parameter must be set in the configuration"
+ " or via the SCW_DEFAULT_REGION environment variable"
)

if client.default_project_id is None and client.default_organization_id is None:
module.fail_json(
msg="default_project_id or default_organization_id parameter must be set in the configuration"
+ " or via the SCW_DEFAULT_PROJECT_ID or SCW_DEFAULT_ORGANIZATION_ID environment variable"
)

return client, module
25 changes: 25 additions & 0 deletions plugins/module_utils/scaleway_secret.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from __future__ import absolute_import, division, print_function

try:
from scaleway.secret.v1beta1.api import SecretV1Beta1API
except ImportError:
HAS_SCALEWAY_SDK = False

__metaclass__ = type


def get_secret_id(api: "SecretV1Beta1API", params: dict) -> str:
"""
Get a secret id from a secret name or directly from the params dict
Remove the secret_name from the params dict if any
"""
secret_name = params.pop("secret_name", None)

if params.get("secret_id") is None:
secret = api.list_secrets(name=secret_name, scheduled_for_deletion=False)
if len(secret.secrets) == 0:
raise Exception(f"Secret with name {secret_name} was not found")

return secret.secrets[0].id

return params.get("secret_id")
Loading