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_resourcemanager_project_info
33description:
34- Gather info for GCP Project
35- This module was called C(gcp_resourcemanager_project_facts) before Ansible 2.9.
36  The usage has not changed.
37short_description: Gather info for GCP Project
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 a project
50  gcp_resourcemanager_project_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    number:
63      description:
64      - Number uniquely identifying the project.
65      returned: success
66      type: int
67    lifecycleState:
68      description:
69      - The Project lifecycle state.
70      returned: success
71      type: str
72    name:
73      description:
74      - 'The user-assigned display name of the Project. It must be 4 to 30 characters.
75        Allowed characters are: lowercase and uppercase letters, numbers, hyphen,
76        single-quote, double-quote, space, and exclamation point.'
77      returned: success
78      type: str
79    createTime:
80      description:
81      - Time of creation.
82      returned: success
83      type: str
84    labels:
85      description:
86      - The labels associated with this Project.
87      - 'Label keys must be between 1 and 63 characters long and must conform to the
88        following regular expression: `[a-z]([-a-z0-9]*[a-z0-9])?`.'
89      - Label values must be between 0 and 63 characters long and must conform to
90        the regular expression `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
91      - No more than 256 labels can be associated with a given resource.
92      - Clients should store labels in a representation such as JSON that does not
93        depend on specific characters being disallowed .
94      returned: success
95      type: dict
96    parent:
97      description:
98      - A parent organization.
99      returned: success
100      type: complex
101      contains:
102        type:
103          description:
104          - Must be organization.
105          returned: success
106          type: str
107        id:
108          description:
109          - Id of the organization.
110          returned: success
111          type: str
112    id:
113      description:
114      - The unique, user-assigned ID of the Project. It must be 6 to 30 lowercase
115        letters, digits, or hyphens. It must start with a letter.
116      - Trailing hyphens are prohibited.
117      returned: success
118      type: str
119'''
120
121################################################################################
122# Imports
123################################################################################
124from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequest
125import json
126
127################################################################################
128# Main
129################################################################################
130
131
132def main():
133    module = GcpModule(argument_spec=dict())
134
135    if module._name == 'gcp_resourcemanager_project_facts':
136        module.deprecate("The 'gcp_resourcemanager_project_facts' module has been renamed to 'gcp_resourcemanager_project_info'", version='2.13')
137
138    if not module.params['scopes']:
139        module.params['scopes'] = ['https://www.googleapis.com/auth/cloud-platform']
140
141    return_value = {'resources': fetch_list(module, collection(module))}
142    module.exit_json(**return_value)
143
144
145def collection(module):
146    return "https://cloudresourcemanager.googleapis.com/v1/projects".format(**module.params)
147
148
149def fetch_list(module, link):
150    auth = GcpSession(module, 'resourcemanager')
151    return auth.list(link, return_if_object, array_name='projects')
152
153
154def return_if_object(module, response):
155    # If not found, return nothing.
156    if response.status_code == 404:
157        return None
158
159    # If no content, return nothing.
160    if response.status_code == 204:
161        return None
162
163    try:
164        module.raise_for_status(response)
165        result = response.json()
166    except getattr(json.decoder, 'JSONDecodeError', ValueError) as inst:
167        module.fail_json(msg="Invalid JSON response with error: %s" % inst)
168
169    if navigate_hash(result, ['error', 'errors']):
170        module.fail_json(msg=navigate_hash(result, ['error', 'errors']))
171
172    return result
173
174
175if __name__ == "__main__":
176    main()
177