1#!/usr/local/bin/python3.8
2# -*- coding: utf-8 -*-
3#
4# (c) 2018, Yanis Guenane <yanis+ansible@guenane.org>
5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7from __future__ import (absolute_import, division, print_function)
8__metaclass__ = type
9
10DOCUMENTATION = r'''
11---
12module: scaleway_security_group_info
13short_description: Gather information about the Scaleway security groups available.
14description:
15  - Gather information about the Scaleway security groups available.
16author:
17  - "Yanis Guenane (@Spredzy)"
18  - "Remy Leone (@sieben)"
19options:
20  region:
21    type: str
22    description:
23      - Scaleway region to use (for example C(par1)).
24    required: true
25    choices:
26      - ams1
27      - EMEA-NL-EVS
28      - par1
29      - EMEA-FR-PAR1
30      - par2
31      - EMEA-FR-PAR2
32      - waw1
33      - EMEA-PL-WAW1
34extends_documentation_fragment:
35- community.general.scaleway
36
37'''
38
39EXAMPLES = r'''
40- name: Gather Scaleway security groups information
41  community.general.scaleway_security_group_info:
42    region: par1
43  register: result
44
45- ansible.builtin.debug:
46    msg: "{{ result.scaleway_security_group_info }}"
47'''
48
49RETURN = r'''
50---
51scaleway_security_group_info:
52  description:
53    - Response from Scaleway API.
54    - "For more details please refer to: U(https://developers.scaleway.com/en/products/instance/api/)."
55  returned: success
56  type: list
57  elements: dict
58  sample:
59    "scaleway_security_group_info": [
60        {
61            "description": "test-ams",
62            "enable_default_security": true,
63            "id": "7fcde327-8bed-43a6-95c4-6dfbc56d8b51",
64            "name": "test-ams",
65            "organization": "3f709602-5e6c-4619-b80c-e841c89734af",
66            "organization_default": false,
67            "servers": [
68                {
69                    "id": "12f19bc7-108c-4517-954c-e6b3d0311363",
70                    "name": "scw-e0d158"
71                }
72            ]
73        }
74    ]
75'''
76
77from ansible.module_utils.basic import AnsibleModule
78from ansible_collections.community.general.plugins.module_utils.scaleway import (
79    Scaleway,
80    ScalewayException,
81    scaleway_argument_spec,
82    SCALEWAY_LOCATION,
83)
84
85
86class ScalewaySecurityGroupInfo(Scaleway):
87
88    def __init__(self, module):
89        super(ScalewaySecurityGroupInfo, self).__init__(module)
90        self.name = 'security_groups'
91
92        region = module.params["region"]
93        self.module.params['api_url'] = SCALEWAY_LOCATION[region]["api_endpoint"]
94
95
96def main():
97    argument_spec = scaleway_argument_spec()
98    argument_spec.update(dict(
99        region=dict(required=True, choices=list(SCALEWAY_LOCATION.keys())),
100    ))
101    module = AnsibleModule(
102        argument_spec=argument_spec,
103        supports_check_mode=True,
104    )
105
106    try:
107        module.exit_json(
108            scaleway_security_group_info=ScalewaySecurityGroupInfo(module).get_resources()
109        )
110    except ScalewayException as exc:
111        module.fail_json(msg=exc.message)
112
113
114if __name__ == '__main__':
115    main()
116