summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/general/plugins/modules/memset_server_info.py
blob: 78ea99df313c6e7552cb762ebe8d0e50f3328f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2018, Simon Weald <ansible@simonweald.com>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

DOCUMENTATION = '''
---
module: memset_server_info
author: "Simon Weald (@glitchcrab)"
short_description: Retrieve server information
notes:
    - An API key generated via the Memset customer control panel is needed with the
      following minimum scope - C(server.info).
description:
    - Retrieve server information.
extends_documentation_fragment:
    - community.general.attributes
    - community.general.attributes.info_module
attributes:
    check_mode:
        version_added: 3.3.0
        # This was backported to 2.5.4 and 1.3.11 as well, since this was a bugfix
options:
    api_key:
        required: true
        type: str
        description:
            - The API key obtained from the Memset control panel.
    name:
        required: true
        type: str
        description:
            - The server product name (that is, C(testyaa1)).
'''

EXAMPLES = '''
- name: Get details for testyaa1
  community.general.memset_server_info:
    name: testyaa1
    api_key: 5eb86c9896ab03919abcf03857163741
  delegate_to: localhost
'''

RETURN = '''
---
memset_api:
  description: Info from the Memset API
  returned: always
  type: complex
  contains:
    backups:
      description: Whether this server has a backup service.
      returned: always
      type: bool
      sample: true
    control_panel:
      description: Whether the server has a control panel (i.e. cPanel).
      returned: always
      type: str
      sample: 'cpanel'
    data_zone:
      description: The data zone the server is in.
      returned: always
      type: str
      sample: 'Memset Public Cloud'
    expiry_date:
      description: Current expiry date of the server.
      returned: always
      type: str
      sample: '2018-08-10'
    firewall_rule_group:
      description: Details about the firewall group this server is in.
      returned: always
      type: dict
      sample: {
        "default_outbound_policy": "RETURN",
        "name": "testyaa-fw1",
        "nickname": "testyaa cPanel rules",
        "notes": "",
        "public": false,
        "rules": {
          "51d7db54d39c3544ef7c48baa0b9944f": {
            "action": "ACCEPT",
            "comment": "",
            "dest_ip6s": "any",
            "dest_ips": "any",
            "dest_ports": "any",
            "direction": "Inbound",
            "ip_version": "any",
            "ordering": 2,
            "protocols": "icmp",
            "rule_group_name": "testyaa-fw1",
            "rule_id": "51d7db54d39c3544ef7c48baa0b9944f",
            "source_ip6s": "any",
            "source_ips": "any",
            "source_ports": "any"
          }
        }
      }
    firewall_type:
      description: The type of firewall the server has (i.e. self-managed, managed).
      returned: always
      type: str
      sample: 'managed'
    host_name:
      description: The server's hostname.
      returned: always
      type: str
      sample: 'testyaa1.miniserver.com'
    ignore_monitoring_off:
      description: When true, Memset won't remind the customer that monitoring is disabled.
      returned: always
      type: bool
      sample: true
    ips:
      description: List of dictionaries of all IP addresses assigned to the server.
      returned: always
      type: list
      sample: [
        {
          "address": "1.2.3.4",
          "bytes_in_today": 1000.0,
          "bytes_in_yesterday": 2000.0,
          "bytes_out_today": 1000.0,
          "bytes_out_yesterday": 2000.0
        }
      ]
    monitor:
      description: Whether the server has monitoring enabled.
      returned: always
      type: bool
      sample: true
    monitoring_level:
      description: The server's monitoring level (i.e. basic).
      returned: always
      type: str
      sample: 'basic'
    name:
      description: Server name (same as the service name).
      returned: always
      type: str
      sample: 'testyaa1'
    network_zones:
      description: The network zone(s) the server is in.
      returned: always
      type: list
      sample: [ 'reading' ]
    nickname:
      description: Customer-set nickname for the server.
      returned: always
      type: str
      sample: 'database server'
    no_auto_reboot:
      description: Whether or not to reboot the server if monitoring detects it down.
      returned: always
      type: bool
      sample: true
    no_nrpe:
      description: Whether Memset should use NRPE to monitor this server.
      returned: always
      type: bool
      sample: true
    os:
      description: The server's Operating System.
      returned: always
      type: str
      sample: 'debian_stretch_64'
    penetration_patrol:
      description: Intrusion detection support level for this server.
      returned: always
      type: str
      sample: 'managed'
    penetration_patrol_alert_level:
      description: The alert level at which notifications are sent.
      returned: always
      type: int
      sample: 10
    primary_ip:
      description: Server's primary IP.
      returned: always
      type: str
      sample: '1.2.3.4'
    renewal_price_amount:
      description: Renewal cost for the server.
      returned: always
      type: str
      sample: '30.00'
    renewal_price_currency:
      description: Currency for renewal payments.
      returned: always
      type: str
      sample: 'GBP'
    renewal_price_vat:
      description: VAT rate for renewal payments
      returned: always
      type: str
      sample: '20'
    start_date:
      description: Server's start date.
      returned: always
      type: str
      sample: '2013-04-10'
    status:
      description: Current status of the server (i.e. live, onhold).
      returned: always
      type: str
      sample: 'LIVE'
    support_level:
      description: Support level included with the server.
      returned: always
      type: str
      sample: 'managed'
    type:
      description: What this server is (i.e. dedicated)
      returned: always
      type: str
      sample: 'miniserver'
    vlans:
      description: Dictionary of tagged and untagged VLANs this server is in.
      returned: always
      type: dict
      sample: {
        tagged: [],
        untagged: [ 'testyaa-vlan1', 'testyaa-vlan2' ]
      }
    vulnscan:
      description: Vulnerability scanning level.
      returned: always
      type: str
      sample: 'basic'
'''

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.general.plugins.module_utils.memset import memset_api_call


def get_facts(args=None):
    '''
    Performs a simple API call and returns a JSON blob.
    '''
    retvals, payload = dict(), dict()
    has_changed, has_failed = False, False
    msg, stderr, memset_api = None, None, None

    payload['name'] = args['name']

    api_method = 'server.info'
    has_failed, msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method, payload=payload)

    if has_failed:
        # this is the first time the API is called; incorrect credentials will
        # manifest themselves at this point so we need to ensure the user is
        # informed of the reason.
        retvals['failed'] = has_failed
        retvals['msg'] = msg
        if response.status_code is not None:
            retvals['stderr'] = "API returned an error: {0}" . format(response.status_code)
        else:
            retvals['stderr'] = "{0}" . format(response.stderr)
        return retvals

    # we don't want to return the same thing twice
    msg = None
    memset_api = response.json()

    retvals['changed'] = has_changed
    retvals['failed'] = has_failed
    for val in ['msg', 'memset_api']:
        if val is not None:
            retvals[val] = eval(val)

    return retvals


def main():
    global module
    module = AnsibleModule(
        argument_spec=dict(
            api_key=dict(required=True, type='str', no_log=True),
            name=dict(required=True, type='str')
        ),
        supports_check_mode=True,
    )

    # populate the dict with the user-provided vars.
    args = dict()
    for key, arg in module.params.items():
        args[key] = arg

    retvals = get_facts(args)

    if retvals['failed']:
        module.fail_json(**retvals)
    else:
        module.exit_json(**retvals)


if __name__ == '__main__':
    main()