Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarber-akamai committed Feb 7, 2025
1 parent 6804795 commit 0953de4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 111 deletions.
28 changes: 23 additions & 5 deletions plugins/modules/database_mysql_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from __future__ import absolute_import, division, print_function

import copy
from typing import Any, Optional

import ansible_collections.linode.cloud.plugins.module_utils.doc_fragments.vpc as docs
Expand All @@ -21,7 +22,7 @@
from ansible_collections.linode.cloud.plugins.module_utils.linode_helper import (
filter_null_values,
mapping_to_dict,
safe_find,
safe_find, poll_condition,
)
from ansible_specdoc.objects import (
FieldType,
Expand Down Expand Up @@ -103,21 +104,21 @@
docs_url="https://techdocs.akamai.com/linode-api/reference/"
"get-databases-mysql-instance",
type=FieldType.dict,
sample=docs.result_database_samples,
# sample=docs.result_database_samples,
),
"ssl_cert": SpecReturnValue(
description="The SSL CA certificate for an accessible Managed MySQL Database.",
docs_url="https://techdocs.akamai.com/linode-api/reference/"
"get-databases-mysql-instance-ssl",
type=FieldType.dict,
sample=docs.result_ssl_cert_samples,
# sample=docs.result_ssl_cert_samples,
),
"credentials": SpecReturnValue(
description="The root username and password for an accessible Managed MySQL Database.",
docs_url="https://techdocs.akamai.com/linode-api/reference/"
"get-databases-mysql-instance-credentials",
type=FieldType.dict,
sample=docs.result_credentials_samples,
# sample=docs.result_credentials_samples,
),
},
)
Expand Down Expand Up @@ -148,6 +149,19 @@ def __init__(self) -> None:
module_arg_spec=self.module_arg_spec,
)

@staticmethod
def _wait_for_database_status(
database: MySQLDatabase,
desired_status: str,
timeout: int = 30 * 60,
step: int = 5,
) -> None:
def __poll_status() -> bool:
database._api_get()
return database.status == desired_status

poll_condition(__poll_status, timeout=timeout, step=step)

def _create(self) -> MySQLDatabase:
params = filter_null_values(
{
Expand All @@ -166,6 +180,10 @@ def _create(self) -> MySQLDatabase:
}
)

# This is necessary because `type` is a Python-reserved keyword
if "type" in params:
params["ltype"] = params.pop("type")

create_poller = self.client.polling.event_poller_create(
"database", "database_create"
)
Expand All @@ -175,7 +193,7 @@ def _create(self) -> MySQLDatabase:
create_poller.set_entity_id(database.id)
create_poller.wait_for_next_event_finished(timeout=30 * 60)

# TODO: status polling
self._wait_for_database_status(database, "active")

return database

Expand Down
128 changes: 22 additions & 106 deletions tests/integration/targets/database_mysql_v2_basic/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
- name: mysql_basic
tags: never
- name: database_mysql_v2_basic
block:
- set_fact:
r: "{{ 1000000000 | random }}"

- name: Get available database_engine_list
- name: List regions
linode.cloud.region_list: {}
register: all_regions

- set_fact:
target_region: '{{ (all_regions.regions | selectattr("capabilities", "search", "Databases") | list)[0]["id"] }}'

- name: Get an available MySQL engine
linode.cloud.database_engine_list:
filters:
- name: engine
Expand All @@ -21,121 +27,31 @@
engine_version: "{{ available_engines.database_engines[0]['version'] }}"

- name: Create a database
linode.cloud.database_mysql:
label: 'ansible-test-{{ r }}'
region: us-ord
engine: '{{engine_id}}'
linode.cloud.database_mysql_v2:
label: "ansible-test-{{ r }}"
region: "{{ target_region }}"
engine: "{{ engine_id }}"
type: g6-standard-1
allow_list:
- 0.0.0.0/0
state: present
register: db_create

- name: Assert database is created
assert:
that:
- db_create.database.allow_list | length == 1
- db_create.database.allow_list[0] == '0.0.0.0/0'
- db_create.database.engine == 'mysql'
- db_create.database.version == '{{ engine_version }}'
- db_create.database.region == 'us-ord'
- db_create.database.type == 'g6-standard-1'

- name: Get info about the database by ID
linode.cloud.database_mysql_info:
id: '{{ db_create.database.id }}'
register: by_id

- name: Get database_mysql_info by label
linode.cloud.database_mysql_info:
label: '{{ db_create.database.label }}'
register: by_label

- name: Assert database_mysql_info by label
assert:
that:
- by_label.database.allow_list | length == 1
- by_label.database.allow_list[0] == '0.0.0.0/0'
- by_label.database.engine == 'mysql'
- by_label.database.version == '{{ engine_version }}'
- by_label.database.region == 'us-ord'
- by_label.database.type == 'g6-standard-1'
- by_label.ssl_cert != None
- by_label.credentials != None
- by_label.backups != None
- by_id.database.allow_list | length == 1
- by_id.database.allow_list[0] == '0.0.0.0/0'
- by_id.database.engine == 'mysql'
- by_id.database.version == '{{ engine_version }}'
- by_id.database.region == 'us-ord'
- by_id.database.type == 'g6-standard-1'
- by_id.ssl_cert != None
- by_id.credentials != None
- by_id.backups != None


- name: Update the database
linode.cloud.database_mysql:
label: 'ansible-test-{{ r }}'
region: us-ord
engine: "{{ engine_id }}"
type: g6-standard-1
allow_list:
- 10.0.0.1/32
state: present
register: db_update

- name: Assert database is updated
assert:
that:
- db_update.database.allow_list | length == 1
- db_update.database.allow_list[0] == '10.0.0.1/32'

# Let's test database_list here to speed up our test runs
- name: Get all databases
linode.cloud.database_list:
register: all_dbs

- name: Assert listing all databases
assert:
that:
- all_dbs.databases | length > 0

# This is not a collection-related issue, so we'll ignore this for now
# - name: Filter to this database
# linode.cloud.database_list:
#
#
# filters:
# - name: engine
# values: mysql
# - name: label
# values: '{{ db_create.database.label }}'
# register: resolve_dbs
#
# - assert:
# - name: Assert database is created
# assert:
# that:
# - resolve_dbs.databases | length == 1
# - resolve_dbs.databases[0].label == db_create.database.label

- name: Update the database
linode.cloud.database_mysql:
label: 'ansible-test-{{ r }}'
region: us-ord
engine: "{{ engine_id }}"
type: g6-standard-1
allow_list:
- 10.0.0.1/32
cluster_size: 3
state: present
register: db_update_invalid
failed_when: '"non-updatable" not in db_update_invalid.msg'
# - db_create.database.allow_list | length == 1
# - db_create.database.allow_list[0] == '0.0.0.0/0'
# - db_create.database.engine == 'mysql'
# - db_create.database.version == '{{ engine_version }}'
# - db_create.database.region == 'us-ord'
# - db_create.database.type == 'g6-standard-1'

always:
- ignore_errors: true
block:
- name: Delete mysql db
linode.cloud.database_mysql:
linode.cloud.database_mysql_v2:
label: '{{ db_create.database.label }}'
state: absent

Expand Down

0 comments on commit 0953de4

Please sign in to comment.