1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

adding mysql database facts (#43322)

This commit is contained in:
Zim Kalinowski 2018-08-17 10:36:13 +08:00 committed by Yunge Zhu
parent c8649f8b73
commit 5f36c73fe5
2 changed files with 244 additions and 3 deletions

View file

@ -0,0 +1,202 @@
#!/usr/bin/python
#
# Copyright (c) 2017 Zim Kalinowski, <zikalino@microsoft.com>
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: azure_rm_mysqldatabase_facts
version_added: "2.7"
short_description: Get Azure MySQL Database facts.
description:
- Get facts of MySQL Database.
options:
resource_group:
description:
- The name of the resource group that contains the resource. You can obtain this value from the Azure Resource Manager API or the portal.
required: True
server_name:
description:
- The name of the server.
required: True
name:
description:
- The name of the database.
extends_documentation_fragment:
- azure
author:
- "Zim Kalinowski (@zikalino)"
'''
EXAMPLES = '''
- name: Get instance of MySQL Database
azure_rm_mysqldatabase_facts:
resource_group: resource_group_name
server_name: server_name
name: database_name
- name: List instances of MySQL Database
azure_rm_mysqldatabase_facts:
resource_group: resource_group_name
server_name: server_name
'''
RETURN = '''
databases:
description: A list of dictionaries containing facts for MySQL Databases.
returned: always
type: complex
contains:
id:
description:
- Resource ID
returned: always
type: str
sample: "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/TestGroup/providers/Microsoft.DBforMySQL/servers/testser
ver/databases/db1"
resource_group:
description:
- Resource group name.
returned: always
type: str
sample: testrg
server_name:
description:
- Server name.
returned: always
type: str
sample: testserver
name:
description:
- Resource name.
returned: always
type: str
sample: db1
charset:
description:
- The charset of the database.
returned: always
type: str
sample: UTF8
collation:
description:
- The collation of the database.
returned: always
type: str
sample: English_United States.1252
'''
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
try:
from msrestazure.azure_exceptions import CloudError
from msrestazure.azure_operation import AzureOperationPoller
from azure.mgmt.rdbms.mysql import MySQLManagementClient
from msrest.serialization import Model
except ImportError:
# This is handled in azure_rm_common
pass
class AzureRMDatabasesFacts(AzureRMModuleBase):
def __init__(self):
# define user inputs into argument
self.module_arg_spec = dict(
resource_group=dict(
type='str',
required=True
),
server_name=dict(
type='str',
required=True
),
name=dict(
type='str'
)
)
# store the results of the module operation
self.results = dict(
changed=False
)
self.resource_group = None
self.server_name = None
self.name = None
super(AzureRMDatabasesFacts, self).__init__(self.module_arg_spec, supports_tags=False)
def exec_module(self, **kwargs):
for key in self.module_arg_spec:
setattr(self, key, kwargs[key])
if (self.resource_group is not None and
self.server_name is not None and
self.name is not None):
self.results['databases'] = self.get()
elif (self.resource_group is not None and
self.server_name is not None):
self.results['databases'] = self.list_by_server()
return self.results
def get(self):
response = None
results = []
try:
response = self.mysql_client.databases.get(resource_group_name=self.resource_group,
server_name=self.server_name,
database_name=self.name)
self.log("Response : {0}".format(response))
except CloudError as e:
self.log('Could not get facts for Databases.')
if response is not None:
results.append(self.format_item(response))
return results
def list_by_server(self):
response = None
results = []
try:
response = self.mysql_client.databases.list_by_server(resource_group_name=self.resource_group,
server_name=self.server_name)
self.log("Response : {0}".format(response))
except CloudError as e:
self.fail("Error listing for server {0} - {1}".format(self.server_name, str(e)))
if response is not None:
for item in response:
results.append(self.format_item(item))
return results
def format_item(self, item):
d = item.as_dict()
d = {
'resource_group': self.resource_group,
'server_name': self.server_name,
'name': d['name'],
'charset': d['charset'],
'collation': d['collation']
}
return d
def main():
AzureRMDatabasesFacts()
if __name__ == '__main__':
main()

View file

@ -8,11 +8,10 @@
resource_group: "{{ resource_group }}"
name: mysqlsrv{{ rpfx }}
sku:
name: GP_Gen4_2
tier: GeneralPurpose
name: GP_Gen4_2
tier: GeneralPurpose
location: westus
storage_mb: 51200
version: 5.6
enforce_ssl: True
admin_username: zimxyz
admin_password: Testpasswordxyz12!
@ -53,6 +52,46 @@
- output.changed == false
- output.name == 'testdatabase'
- name: Create second instance of MySQL Database
azure_rm_mysqldatabase:
resource_group: "{{ resource_group }}"
server_name: mysqlsrv{{ rpfx }}
name: testdatabase2
- name: Gather facts MySQL Database
azure_rm_mysqldatabase_facts:
resource_group: "{{ resource_group }}"
server_name: mysqlsrv{{ rpfx }}
name: testdatabase
register: output
- name: Assert that facts are returned
assert:
that:
- output.changed == False
- output.databases[0]['server_name'] != None
- output.databases[0]['name'] != None
- output.databases[0]['charset'] != None
- output.databases[0]['collation'] != None
- name: Gather facts MySQL Database
azure_rm_mysqldatabase_facts:
resource_group: "{{ resource_group }}"
server_name: mysqlsrv{{ rpfx }}
register: output
- name: Assert that facts are returned
assert:
that:
- output.changed == False
- output.databases[0]['server_name'] != None
- output.databases[0]['name'] != None
- output.databases[0]['charset'] != None
- output.databases[0]['collation'] != None
- output.databases[1]['server_name'] != None
- output.databases[1]['name'] != None
- output.databases[1]['charset'] != None
- output.databases[1]['collation'] != None
- name: Delete instance of MySQL Database -- check mode
azure_rm_mysqldatabase:
resource_group: "{{ resource_group }}"