summaryrefslogtreecommitdiffstats
path: root/ansible_collections/purestorage/flashblade/plugins/modules/purefb_hardware.py
blob: 49849156bcd14fcdfb5010f67eef5438f12503ac (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
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2021, Simon Dodsley (simon@purestorage.com)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function

__metaclass__ = type

ANSIBLE_METADATA = {
    "metadata_version": "1.1",
    "status": ["preview"],
    "supported_by": "community",
}

DOCUMENTATION = r"""
---
module: purefb_hardware
version_added: '1.15.0'
short_description: Manage FlashBlade Hardware
description:
- Enable or disable FlashBlade visual identification lights and set connector parameters
author:
- Pure Storage Ansible Team (@sdodsley) <pure-ansible-team@purestorage.com>
options:
  name:
    description:
    - Name of hardware component
    type: str
    required: true
  enabled:
    description:
    - State of the component identification LED
    type: bool
  speed:
    description:
    - If the component specified is a connector, set the configured speed
      of each lane in the connector in gigabits-per-second
    type: int
    choices: [ 10, 25, 40 ]
  ports:
    description:
    - If the component specificed is a connector, the number of configured
      ports in the connector
    type: int
    choices: [ 1, 4 ]
extends_documentation_fragment:
- purestorage.flashblade.purestorage.fb
"""

EXAMPLES = r"""
- name: Set connector to be 4 x 40Gb ports
  purestorage.flashblade.purefb_hardware:
    name: "CH1.FM1.ETH1"
    speed: 40
    ports: 4
    fb_url: 10.10.10.2
    api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3

- name: Enable identification LED
  purestorage.flashblade.purefb_hardware:
    name: "CH1.FB1"
    enabled: True
    fb_url: 10.10.10.2
    api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3

- name: Disable identification LED
  purestorage.flashblade.purefb_hardware:
    name: "CH1.FB1"
    enabled: False
    fb_url: 10.10.10.2
    api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3
"""

RETURN = r"""
"""

HAS_PURESTORAGE = True
try:
    from pypureclient import flashblade
except ImportError:
    HAS_PURESTORAGE = False

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import (
    get_system,
    purefb_argument_spec,
)

MIN_REQUIRED_API_VERSION = "2.2"


def main():
    argument_spec = purefb_argument_spec()
    argument_spec.update(
        dict(
            enabled=dict(type="bool"),
            name=dict(type="str", required=True),
            speed=dict(
                type="int",
                choices=[10, 25, 40],
            ),
            ports=dict(
                type="int",
                choices=[1, 4],
            ),
        )
    )

    module = AnsibleModule(argument_spec, supports_check_mode=True)
    if not HAS_PURESTORAGE:
        module.fail_json(msg="py-pure-client sdk is required for this module")

    blade = get_system(module)
    api_version = list(blade.get_versions().items)

    if MIN_REQUIRED_API_VERSION not in api_version:
        module.fail_json(
            msg="FlashBlade REST version not supported. "
            "Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION)
        )
    if module.params["speed"]:
        speed = module.params["speed"] * 1000000000
    changed = False
    change_connector = False
    hardware = None
    res = blade.get_hardware(names=[module.params["name"]])
    if res.status_code == 200:
        hardware = list(res.items)[0]
        if hardware.identify_enabled != module.params["enabled"]:
            changed = True
            if not module.check_mode:
                res = blade.patch_hardware(
                    names=[module.params["name"]],
                    hardware=flashblade.Hardware(
                        identify_enabled=module.params["enabled"]
                    ),
                )
                if res.status_code != 200:
                    module.fail_json(
                        msg="Failed to set identification LED for {0}. Error: {1}".format(
                            module.params["name"], res.errors[0].message
                        )
                    )
    res = blade.get_hardware_connectors(names=[module.params["name"]])
    if res.status_code == 200:
        if res.status_code == 200:
            connector = list(res.items)[0]
            if connector.port_count != module.params["ports"]:
                new_port = module.params["ports"]
                changed = True
                if not module.check_mode:
                    res = blade.patch_hardware_connectors(
                        names=[module.params["name"]],
                        hardware_connector=flashblade.HardwareConnector(
                            port_count=module.params["ports"]
                        ),
                    )
                    if res.status_code != 200:
                        module.fail_json(
                            msg="Failed to change connector port count {0}. Error: Invalid port count".format(
                                module.params["name"]
                            )
                        )
            if connector.lane_speed != speed:
                new_speed = speed
                changed = True
                if not module.check_mode:
                    res = blade.patch_hardware_connectors(
                        names=[module.params["name"]],
                        hardware_connector=flashblade.HardwareConnector(
                            lane_speed=speed
                        ),
                    )
                    if res.status_code != 200:
                        module.fail_json(
                            msg="Failed to change connector lane speed {0}. Error: Invalid lane speed".format(
                                module.params["name"]
                            )
                        )

    module.exit_json(changed=changed)


if __name__ == "__main__":
    main()