Skip to content

Commit

Permalink
azure_rm_sqldatabase: parse datetime module arguments (#623)
Browse files Browse the repository at this point in the history
* rm_sqldatabase: parse datetime arguments

* Remove unused sanity test exception on rm_sqldatabase module schema

* Remove unused sanity test exception on rm_sqldatabase module schema bis

* sqldatabase: import dateutil in try/except

* Add dateutil install to test suite

* sqldatabase_info: Add earliest_restore_date value to returned facts

* sqldatabase: add point in time restore test
  • Loading branch information
nbr23 authored Feb 25, 2022
1 parent a879770 commit 094fff8
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 8 deletions.
15 changes: 11 additions & 4 deletions plugins/modules/azure_rm_sqldatabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase, format_resource_id

try:
import dateutil.parser
from msrestazure.azure_exceptions import CloudError
from msrest.polling import LROPoller
from azure.mgmt.sql import SqlManagementClient
Expand Down Expand Up @@ -308,10 +309,10 @@ def __init__(self):
type='str'
),
source_database_deletion_date=dict(
type='datetime'
type='str'
),
restore_point_in_time=dict(
type='datetime'
type='str'
),
recovery_services_recovery_point_resource_id=dict(
type='str'
Expand Down Expand Up @@ -393,9 +394,15 @@ def exec_module(self, **kwargs):
elif key == "source_database_id":
self.parameters["source_database_id"] = kwargs[key]
elif key == "source_database_deletion_date":
self.parameters["source_database_deletion_date"] = kwargs[key]
try:
self.parameters["source_database_deletion_date"] = dateutil.parser.parse(kwargs[key])
except dateutil.parser._parser.ParserError:
self.fail("Error parsing date from source_database_deletion_date: {0}".format(kwargs[key]))
elif key == "restore_point_in_time":
self.parameters["restore_point_in_time"] = kwargs[key]
try:
self.parameters["restore_point_in_time"] = dateutil.parser.parse(kwargs[key])
except dateutil.parser._parser.ParserError:
self.fail("Error parsing date from restore_point_in_time: {0}".format(kwargs[key]))
elif key == "recovery_services_recovery_point_resource_id":
self.parameters["recovery_services_recovery_point_resource_id"] = kwargs[key]
elif key == "edition":
Expand Down
9 changes: 8 additions & 1 deletion plugins/modules/azure_rm_sqldatabase_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@
returned: always
type: bool
sample: true
earliest_restore_date:
description:
- The earliest restore point available for the SQL Database.
returned: always
type: str
sample: '2021-09-01T00:59:59.000Z'
'''

from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
Expand Down Expand Up @@ -270,7 +276,8 @@ def format_item(self, item):
'kind': d.get('kind', None),
'collation': d.get('collation', None),
'status': d.get('status', None),
'zone_redundant': d.get('zone_redundant', None)
'zone_redundant': d.get('zone_redundant', None),
'earliest_restore_date': d.get('earliest_restore_date', None)
}
return d

Expand Down
42 changes: 42 additions & 0 deletions tests/integration/targets/azure_rm_sqlserver/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,48 @@
- output.changed == false
- output.status == 'Online'

# test database point in time restore
- name: Gather facts SQL Database and wait for restore point
azure_rm_sqldatabase_info:
resource_group: "{{ resource_group }}"
server_name: sqlsrv{{ random_postfix }}
name: database{{ random_postfix }}
register: output
until: output.databases[0].earliest_restore_date != None
retries: 10
delay: 20
- name: Assert that it can be restored from
assert:
that:
- output.databases[0].id != None
- output.databases[0].earliest_restore_date != None

- name: Create second SQL Database, restoring from the previous Database
azure_rm_sqldatabase:
resource_group: "{{ resource_group }}"
create_mode: point_in_time_restore
restore_point_in_time: "{{ output.databases[0].earliest_restore_date }}"
source_database_id: "{{ output.databases[0].id }}"
server_name: sqlsrv{{ random_postfix }}
name: database{{ random_postfix }}PITR
register: output
- name: Assert the state has changed
assert:
that:
- output.changed

- name: Delete instance of SQL Database Point in time recovery
azure_rm_sqldatabase:
resource_group: "{{ resource_group }}"
server_name: sqlsrv{{ random_postfix }}
name: database{{ random_postfix }}PITR
state: absent
register: output
- name: Assert the state has changed
assert:
that:
- output.changed

# test database facter:
- name: Create second SQL Database
azure_rm_sqldatabase:
Expand Down
1 change: 0 additions & 1 deletion tests/sanity/ignore-2.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ plugins/modules/azure_rm_servicebussaspolicy.py validate-modules:doc-required-mi
plugins/modules/azure_rm_snapshot.py validate-modules:doc-required-mismatch
plugins/modules/azure_rm_snapshot.py validate-modules:invalid-ansiblemodule-schema
plugins/modules/azure_rm_sqldatabase.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_sqldatabase.py validate-modules:invalid-ansiblemodule-schema
plugins/modules/azure_rm_sqldatabase_info.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_sqldatabase_info.py validate-modules:parameter-list-no-elements
plugins/modules/azure_rm_sqlfirewallrule.py validate-modules:parameter-type-not-in-doc
Expand Down
1 change: 0 additions & 1 deletion tests/sanity/ignore-2.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ plugins/modules/azure_rm_servicebussaspolicy.py validate-modules:doc-required-mi
plugins/modules/azure_rm_snapshot.py validate-modules:doc-required-mismatch
plugins/modules/azure_rm_snapshot.py validate-modules:invalid-ansiblemodule-schema
plugins/modules/azure_rm_sqldatabase.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_sqldatabase.py validate-modules:invalid-ansiblemodule-schema
plugins/modules/azure_rm_sqldatabase_info.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_sqldatabase_info.py validate-modules:parameter-list-no-elements
plugins/modules/azure_rm_sqlfirewallrule.py validate-modules:parameter-type-not-in-doc
Expand Down
1 change: 0 additions & 1 deletion tests/sanity/ignore-2.12.txt
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ plugins/modules/azure_rm_servicebussaspolicy.py validate-modules:doc-required-mi
plugins/modules/azure_rm_snapshot.py validate-modules:doc-required-mismatch
plugins/modules/azure_rm_snapshot.py validate-modules:invalid-ansiblemodule-schema
plugins/modules/azure_rm_sqldatabase.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_sqldatabase.py validate-modules:invalid-ansiblemodule-schema
plugins/modules/azure_rm_sqldatabase_info.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_sqldatabase_info.py validate-modules:parameter-list-no-elements
plugins/modules/azure_rm_sqlfirewallrule.py validate-modules:parameter-type-not-in-doc
Expand Down
1 change: 1 addition & 0 deletions tests/utils/ado/ado.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ else
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python"$2" -y
sudo apt install python3-dateutil
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python"$2" 1
fi

Expand Down

0 comments on commit 094fff8

Please sign in to comment.