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_instance_info
33description:
34- Gather info for GCP Instance
35- This module was called C(gcp_spanner_instance_facts) before Ansible 2.9. The usage
36  has not changed.
37short_description: Gather info for GCP Instance
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: {}
45extends_documentation_fragment: gcp
46'''
47
48EXAMPLES = '''
49- name: get info on an instance
50  gcp_spanner_instance_info:
51    project: test_project
52    auth_kind: serviceaccount
53    service_account_file: "/tmp/auth.pem"
54'''
55
56RETURN = '''
57resources:
58  description: List of resources
59  returned: always
60  type: complex
61  contains:
62    name:
63      description:
64      - A unique identifier for the instance, which cannot be changed after the instance
65        is created. The name must be between 6 and 30 characters in length.
66      returned: success
67      type: str
68    config:
69      description:
70      - The name of the instance's configuration (similar but not quite the same as
71        a region) which defines defines the geographic placement and replication of
72        your databases in this instance. It determines where your data is stored.
73        Values are typically of the form `regional-europe-west1` , `us-central` etc.
74      - In order to obtain a valid list please consult the [Configuration section
75        of the docs](U(https://cloud.google.com/spanner/docs/instances)).
76      returned: success
77      type: str
78    displayName:
79      description:
80      - The descriptive name for this instance as it appears in UIs. Must be unique
81        per project and between 4 and 30 characters in length.
82      returned: success
83      type: str
84    nodeCount:
85      description:
86      - The number of nodes allocated to this instance.
87      returned: success
88      type: int
89    labels:
90      description:
91      - 'An object containing a list of "key": value pairs.'
92      - 'Example: { "name": "wrench", "mass": "1.3kg", "count": "3" }.'
93      returned: success
94      type: dict
95'''
96
97################################################################################
98# Imports
99################################################################################
100from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequest
101import json
102
103################################################################################
104# Main
105################################################################################
106
107
108def main():
109    module = GcpModule(argument_spec=dict())
110
111    if module._name == 'gcp_spanner_instance_facts':
112        module.deprecate("The 'gcp_spanner_instance_facts' module has been renamed to 'gcp_spanner_instance_info'", version='2.13')
113
114    if not module.params['scopes']:
115        module.params['scopes'] = ['https://www.googleapis.com/auth/spanner.admin']
116
117    return_value = {'resources': fetch_list(module, collection(module))}
118    module.exit_json(**return_value)
119
120
121def collection(module):
122    return "https://spanner.googleapis.com/v1/projects/{project}/instances".format(**module.params)
123
124
125def fetch_list(module, link):
126    auth = GcpSession(module, 'spanner')
127    return auth.list(link, return_if_object, array_name='instances')
128
129
130def return_if_object(module, response):
131    # If not found, return nothing.
132    if response.status_code == 404:
133        return None
134
135    # If no content, return nothing.
136    if response.status_code == 204:
137        return None
138
139    try:
140        module.raise_for_status(response)
141        result = response.json()
142    except getattr(json.decoder, 'JSONDecodeError', ValueError) as inst:
143        module.fail_json(msg="Invalid JSON response with error: %s" % inst)
144
145    if navigate_hash(result, ['error', 'errors']):
146        module.fail_json(msg=navigate_hash(result, ['error', 'errors']))
147
148    return result
149
150
151if __name__ == "__main__":
152    main()
153