Skip to content
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

fix(instance): add possibility to choose serverState to list #43

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 23 additions & 3 deletions plugins/inventory/scaleway.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
type: list
elements: str
default: []
state:
description:
- State of the instance server
type: str
default: running
hostnames:
description: List of preference about what to use as an hostname.
type: list
Expand Down Expand Up @@ -89,6 +94,8 @@
- nl-ams-1
tags:
- dev
state:
- stopped
variables:
ansible_host: public_ipv4
"""
Expand All @@ -104,15 +111,13 @@

try:
from scaleway_core.bridge import Zone

from scaleway import Client, ScalewayException
from scaleway.applesilicon.v1alpha1 import ApplesiliconV1Alpha1API
from scaleway.applesilicon.v1alpha1 import Server as ApplesiliconServer
from scaleway.baremetal.v1 import BaremetalV1API, IPVersion
from scaleway.baremetal.v1 import Server as BaremetalServer
from scaleway.instance.v1 import InstanceV1API
from scaleway.instance.v1 import Server as InstanceServer
from scaleway.instance.v1 import ServerState
from scaleway.dedibox.v1 import DediboxV1API
from scaleway.dedibox.v1 import ServerSummary as DediboxServer

Expand All @@ -128,10 +133,21 @@
)


@dataclass
class InstanceServerState:
RUNNING = "running"
STOPPED = "stopped"
STOPPED_IN_PLACE = "stopped_in_place"
STARTING = "starting"
STOPPING = "stopping"
LOCKED = "locked"


@dataclass
class _Filters:
zones: List[str] = field(default_factory=list)
tags: List[str] = field(default_factory=list)
state: Optional[str] = InstanceServerState.RUNNING


@dataclass
Expand Down Expand Up @@ -259,7 +275,7 @@ def _get_instances(self, client: "Client", filters: _Filters) -> List[_Host]:
api.list_servers_all(
zone=zone,
tags=filters.tags if filters.tags else None,
state=ServerState.RUNNING,
state=filters.state,
)
)

Expand Down Expand Up @@ -442,6 +458,7 @@ def _get_client(self):
def _get_filters(self):
zones = self.get_option("zones")
tags = self.get_option("tags")
state = self.get_option("state")

filters = _Filters()

Expand All @@ -451,4 +468,7 @@ def _get_filters(self):
if tags:
filters.tags = tags

if state:
filters.state = state

return filters
4 changes: 4 additions & 0 deletions tests/integration/targets/inventory/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource_name: "ansible-test-inventory-instance"
resource_description: "Ansible Scaleway Instance Inventory integration tests"
resource_is_public: false
resource_region: "fr-par"
41 changes: 41 additions & 0 deletions tests/integration/targets/inventory/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
- name: Test Instance Inventory namespace
block:
- name: Ensure Access Key is provided
ansible.builtin.fail:
msg: scw_access_key should be defined in tests/integration/integration_config.yml
when:
- scw_access_key is not defined
- scw_access_key | length == 0

- name: Ensure Secret Key is provided
ansible.builtin.fail:
msg: scw_secret_key should be defined in tests/integration/integration_config.yml
when:
- scw_secret_key is not defined
- scw_secret_key | length == 0

- name: Ensure Default Project ID is provided
ansible.builtin.fail:
msg: scw_default_project_id should be defined in tests/integration/integration_config.yml
when:
- scw_default_project_id is not defined
- scw_default_project_id | length == 0

- name: Create instance_namespace
scaleway.scaleway.scaleway_instance_image:
name: "{{ resource_name }}"
region: "{{ resource_region }}"
project_id: "{{ scw_default_project_id }}"
access_key: "{{ scw_access_key }}"
secret_key: "{{ scw_secret_key }}"
description: "{{ resource_description }}"
is_public: "{{ resource_is_public }}"

register: integration_instance_namespace_creation

- name: Debug instance namespace creation output
ansible.builtin.debug:
var: integration_instance_namespace_creation


Check failure on line 41 in tests/integration/targets/inventory/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Ansible Lint

yaml[empty-lines]

Too many blank lines (2 > 0)
Loading