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_sourcerepo_repository_info
33description:
34- Gather info for GCP Repository
35- This module was called C(gcp_sourcerepo_repository_facts) before Ansible 2.9. The
36  usage has not changed.
37short_description: Gather info for GCP Repository
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 repository
50  gcp_sourcerepo_repository_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      - Resource name of the repository, of the form projects/{{project}}/repos/{{repo}}.
65      - The repo name may contain slashes. eg, projects/myproject/repos/name/with/slash
66        .
67      returned: success
68      type: str
69    url:
70      description:
71      - URL to clone the repository from Google Cloud Source Repositories.
72      returned: success
73      type: str
74    size:
75      description:
76      - The disk usage of the repo, in bytes.
77      returned: success
78      type: int
79'''
80
81################################################################################
82# Imports
83################################################################################
84from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequest
85import json
86
87################################################################################
88# Main
89################################################################################
90
91
92def main():
93    module = GcpModule(argument_spec=dict())
94
95    if module._name == 'gcp_sourcerepo_repository_facts':
96        module.deprecate("The 'gcp_sourcerepo_repository_facts' module has been renamed to 'gcp_sourcerepo_repository_info'", version='2.13')
97
98    if not module.params['scopes']:
99        module.params['scopes'] = ['https://www.googleapis.com/auth/cloud-platform']
100
101    return_value = {'resources': fetch_list(module, collection(module))}
102    module.exit_json(**return_value)
103
104
105def collection(module):
106    return "https://sourcerepo.googleapis.com/v1/projects/{project}/repos".format(**module.params)
107
108
109def fetch_list(module, link):
110    auth = GcpSession(module, 'sourcerepo')
111    return auth.list(link, return_if_object, array_name='repos')
112
113
114def return_if_object(module, response):
115    # If not found, return nothing.
116    if response.status_code == 404:
117        return None
118
119    # If no content, return nothing.
120    if response.status_code == 204:
121        return None
122
123    try:
124        module.raise_for_status(response)
125        result = response.json()
126    except getattr(json.decoder, 'JSONDecodeError', ValueError) as inst:
127        module.fail_json(msg="Invalid JSON response with error: %s" % inst)
128
129    if navigate_hash(result, ['error', 'errors']):
130        module.fail_json(msg=navigate_hash(result, ['error', 'errors']))
131
132    return result
133
134
135if __name__ == "__main__":
136    main()
137