1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3#
4# Copyright (C) 2017 Google
5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6# ----------------------------------------------------------------------------
7#
8#     ***     AUTO GENERATED CODE    ***    AUTO GENERATED CODE     ***
9#
10# ----------------------------------------------------------------------------
11#
12#     This file is automatically generated by Magic Modules and manual
13#     changes will be clobbered when the file is regenerated.
14#
15#     Please read more about how to change this file at
16#     https://www.github.com/GoogleCloudPlatform/magic-modules
17#
18# ----------------------------------------------------------------------------
19
20from __future__ import absolute_import, division, print_function
21
22__metaclass__ = type
23
24################################################################################
25# Documentation
26################################################################################
27
28ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ["preview"], 'supported_by': 'community'}
29
30DOCUMENTATION = '''
31---
32module: gcp_spanner_database_info
33description:
34- Gather info for GCP Database
35- This module was called C(gcp_spanner_database_facts) before Ansible 2.9. The usage
36  has not changed.
37short_description: Gather info for GCP Database
38version_added: 2.8
39author: Google Inc. (@googlecloudplatform)
40requirements:
41- python >= 2.6
42- requests >= 2.18.4
43- google-auth >= 1.3.0
44options:
45  instance:
46    description:
47    - The instance to create the database on.
48    - 'This field represents a link to a Instance resource in GCP. It can be specified
49      in two ways. First, you can place a dictionary with key ''name'' and value of
50      your resource''s name Alternatively, you can add `register: name-of-resource`
51      to a gcp_spanner_instance task and then set this instance field to "{{ name-of-resource
52      }}"'
53    required: true
54    type: dict
55extends_documentation_fragment: gcp
56'''
57
58EXAMPLES = '''
59- name: get info on a database
60  gcp_spanner_database_info:
61    instance: "{{ instance }}"
62    project: test_project
63    auth_kind: serviceaccount
64    service_account_file: "/tmp/auth.pem"
65'''
66
67RETURN = '''
68resources:
69  description: List of resources
70  returned: always
71  type: complex
72  contains:
73    name:
74      description:
75      - A unique identifier for the database, which cannot be changed after the instance
76        is created. Values are of the form [a-z][-a-z0-9]*[a-z0-9].
77      returned: success
78      type: str
79    extraStatements:
80      description:
81      - 'An optional list of DDL statements to run inside the newly created database.
82        Statements can create tables, indexes, etc. These statements execute atomically
83        with the creation of the database: if there is an error in any statement,
84        the database is not created.'
85      returned: success
86      type: list
87    instance:
88      description:
89      - The instance to create the database on.
90      returned: success
91      type: dict
92'''
93
94################################################################################
95# Imports
96################################################################################
97from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequest, replace_resource_dict
98import json
99
100################################################################################
101# Main
102################################################################################
103
104
105def main():
106    module = GcpModule(argument_spec=dict(instance=dict(required=True, type='dict')))
107
108    if module._name == 'gcp_spanner_database_facts':
109        module.deprecate("The 'gcp_spanner_database_facts' module has been renamed to 'gcp_spanner_database_info'", version='2.13')
110
111    if not module.params['scopes']:
112        module.params['scopes'] = ['https://www.googleapis.com/auth/spanner.admin']
113
114    return_value = {'resources': fetch_list(module, collection(module))}
115    module.exit_json(**return_value)
116
117
118def collection(module):
119    res = {'project': module.params['project'], 'instance': replace_resource_dict(module.params['instance'], 'name')}
120    return "https://spanner.googleapis.com/v1/projects/{project}/instances/{instance}/databases".format(**res)
121
122
123def fetch_list(module, link):
124    auth = GcpSession(module, 'spanner')
125    return auth.list(link, return_if_object, array_name='databases')
126
127
128def return_if_object(module, response):
129    # If not found, return nothing.
130    if response.status_code == 404:
131        return None
132
133    # If no content, return nothing.
134    if response.status_code == 204:
135        return None
136
137    try:
138        module.raise_for_status(response)
139        result = response.json()
140    except getattr(json.decoder, 'JSONDecodeError', ValueError) as inst:
141        module.fail_json(msg="Invalid JSON response with error: %s" % inst)
142
143    if navigate_hash(result, ['error', 'errors']):
144        module.fail_json(msg=navigate_hash(result, ['error', 'errors']))
145
146    return result
147
148
149if __name__ == "__main__":
150    main()
151