| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: oneview_san_manager |
| 10 |
short_description: Manage OneView SAN Manager resources |
| 11 |
description: |
| 12 |
- Provides an interface to manage SAN Manager resources. Can create, update, or delete. |
| 13 |
requirements: |
| 14 |
- hpOneView >= 3.1.1 |
| 15 |
author: |
| 16 |
- Felipe Bulsoni (@fgbulsoni) |
| 17 |
- Thiago Miotto (@tmiotto) |
| 18 |
- Adriane Cardozo (@adriane-cardozo) |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
support: none |
| 22 |
diff_mode: |
| 23 |
support: none |
| 24 |
options: |
| 25 |
state: |
| 26 |
description: |
| 27 |
- Indicates the desired state for the Uplink Set resource. |
| 28 |
- V(present) ensures data properties are compliant with OneView. |
| 29 |
- V(absent) removes the resource from OneView, if it exists. |
| 30 |
- V(connection_information_set) updates the connection information for the SAN Manager. This operation is non-idempotent. |
| 31 |
type: str |
| 32 |
default: present |
| 33 |
choices: [present, absent, connection_information_set] |
| 34 |
data: |
| 35 |
description: |
| 36 |
- List with SAN Manager properties. |
| 37 |
type: dict |
| 38 |
required: true |
| 39 |
|
| 40 |
extends_documentation_fragment: |
| 41 |
- community.general._oneview |
| 42 |
- community.general._oneview.validateetag |
| 43 |
- community.general._attributes |
| 44 |
""" |
| 45 |
|
| 46 |
EXAMPLES = r""" |
| 47 |
- name: Creates a Device Manager for the Brocade SAN provider with the given hostname and credentials |
| 48 |
community.general.oneview_san_manager: |
| 49 |
config: /etc/oneview/oneview_config.json |
| 50 |
state: present |
| 51 |
data: |
| 52 |
providerDisplayName: Brocade Network Advisor |
| 53 |
connectionInfo: |
| 54 |
- name: Host |
| 55 |
value: 172.18.15.1 |
| 56 |
- name: Port |
| 57 |
value: 5989 |
| 58 |
- name: Username |
| 59 |
value: username |
| 60 |
- name: Password |
| 61 |
value: password |
| 62 |
- name: UseSsl |
| 63 |
value: true |
| 64 |
delegate_to: localhost |
| 65 |
|
| 66 |
- name: Ensure a Device Manager for the Cisco SAN Provider is present |
| 67 |
community.general.oneview_san_manager: |
| 68 |
config: /etc/oneview/oneview_config.json |
| 69 |
state: present |
| 70 |
data: |
| 71 |
name: 172.18.20.1 |
| 72 |
providerDisplayName: Cisco |
| 73 |
connectionInfo: |
| 74 |
- name: Host |
| 75 |
value: 172.18.20.1 |
| 76 |
- name: SnmpPort |
| 77 |
value: 161 |
| 78 |
- name: SnmpUserName |
| 79 |
value: admin |
| 80 |
- name: SnmpAuthLevel |
| 81 |
value: authnopriv |
| 82 |
- name: SnmpAuthProtocol |
| 83 |
value: sha |
| 84 |
- name: SnmpAuthString |
| 85 |
value: password |
| 86 |
delegate_to: localhost |
| 87 |
|
| 88 |
- name: Sets the SAN Manager connection information |
| 89 |
community.general.oneview_san_manager: |
| 90 |
config: /etc/oneview/oneview_config.json |
| 91 |
state: connection_information_set |
| 92 |
data: |
| 93 |
connectionInfo: |
| 94 |
- name: Host |
| 95 |
value: '172.18.15.1' |
| 96 |
- name: Port |
| 97 |
value: '5989' |
| 98 |
- name: Username |
| 99 |
value: 'username' |
| 100 |
- name: Password |
| 101 |
value: 'password' |
| 102 |
- name: UseSsl |
| 103 |
value: true |
| 104 |
delegate_to: localhost |
| 105 |
|
| 106 |
- name: Refreshes the SAN Manager |
| 107 |
community.general.oneview_san_manager: |
| 108 |
config: /etc/oneview/oneview_config.json |
| 109 |
state: present |
| 110 |
data: |
| 111 |
name: 172.18.15.1 |
| 112 |
refreshState: RefreshPending |
| 113 |
delegate_to: localhost |
| 114 |
|
| 115 |
- name: Delete the SAN Manager recently created |
| 116 |
community.general.oneview_san_manager: |
| 117 |
config: /etc/oneview/oneview_config.json |
| 118 |
state: absent |
| 119 |
data: |
| 120 |
name: '172.18.15.1' |
| 121 |
delegate_to: localhost |
| 122 |
""" |
| 123 |
|
| 124 |
RETURN = r""" |
| 125 |
san_manager: |
| 126 |
description: Has the OneView facts about the SAN Manager. |
| 127 |
returned: On O(state=present). Can be null. |
| 128 |
type: dict |
| 129 |
""" |
| 130 |
|
| 131 |
from ansible_collections.community.general.plugins.module_utils._oneview import ( |
| 132 |
OneViewModuleBase, |
| 133 |
OneViewModuleValueError, |
| 134 |
) |
| 135 |
|
| 136 |
|
| 137 |
class SanManagerModule(OneViewModuleBase): |
| 138 |
MSG_CREATED = "SAN Manager created successfully." |
| 139 |
MSG_UPDATED = "SAN Manager updated successfully." |
| 140 |
MSG_DELETED = "SAN Manager deleted successfully." |
| 141 |
MSG_ALREADY_PRESENT = "SAN Manager is already present." |
| 142 |
MSG_ALREADY_ABSENT = "SAN Manager is already absent." |
| 143 |
|
| 144 |
argument_spec = dict( |
| 145 |
state=dict(type="str", default="present", choices=["absent", "present", "connection_information_set"]), |
| 146 |
data=dict(type="dict", required=True), |
| 147 |
) |
| 148 |
|
| 149 |
def __init__(self): |
| 150 |
super().__init__(additional_arg_spec=self.argument_spec, validate_etag_support=True) |
| 151 |
self.resource_client = self.oneview_client.san_managers |
| 152 |
|
| 153 |
def execute_module(self): |
| 154 |
if self.data.get("connectionInfo"): |
| 155 |
for connection_hash in self.data.get("connectionInfo"): |
| 156 |
if connection_hash.get("name") == "Host": |
| 157 |
resource_name = connection_hash.get("value") |
| 158 |
elif self.data.get("name"): |
| 159 |
resource_name = self.data.get("name") |
| 160 |
else: |
| 161 |
msg = 'A "name" or "connectionInfo" must be provided inside the "data" field for this operation. ' |
| 162 |
msg += 'If a "connectionInfo" is provided, the "Host" name is considered as the "name" for the resource.' |
| 163 |
raise OneViewModuleValueError(msg) |
| 164 |
|
| 165 |
resource = self.resource_client.get_by_name(resource_name) |
| 166 |
|
| 167 |
if self.state == "present": |
| 168 |
changed, msg, san_manager = self._present(resource) |
| 169 |
return dict(changed=changed, msg=msg, ansible_facts=dict(san_manager=san_manager)) |
| 170 |
|
| 171 |
elif self.state == "absent": |
| 172 |
return self.resource_absent(resource, method="remove") |
| 173 |
|
| 174 |
elif self.state == "connection_information_set": |
| 175 |
changed, msg, san_manager = self._connection_information_set(resource) |
| 176 |
return dict(changed=changed, msg=msg, ansible_facts=dict(san_manager=san_manager)) |
| 177 |
|
| 178 |
def _present(self, resource): |
| 179 |
if not resource: |
| 180 |
provider_uri = self.data.get("providerUri", self._get_provider_uri_by_display_name(self.data)) |
| 181 |
return True, self.MSG_CREATED, self.resource_client.add(self.data, provider_uri) |
| 182 |
else: |
| 183 |
merged_data = resource.copy() |
| 184 |
merged_data.update(self.data) |
| 185 |
|
| 186 |
# Remove 'connectionInfo' from comparison, since it is not possible to validate it. |
| 187 |
resource.pop("connectionInfo", None) |
| 188 |
merged_data.pop("connectionInfo", None) |
| 189 |
|
| 190 |
if self.compare(resource, merged_data): |
| 191 |
return False, self.MSG_ALREADY_PRESENT, resource |
| 192 |
else: |
| 193 |
updated_san_manager = self.resource_client.update(resource=merged_data, id_or_uri=resource["uri"]) |
| 194 |
return True, self.MSG_UPDATED, updated_san_manager |
| 195 |
|
| 196 |
def _connection_information_set(self, resource): |
| 197 |
if not resource: |
| 198 |
return self._present(resource) |
| 199 |
else: |
| 200 |
merged_data = resource.copy() |
| 201 |
merged_data.update(self.data) |
| 202 |
merged_data.pop("refreshState", None) |
| 203 |
if not self.data.get("connectionInfo", None): |
| 204 |
raise OneViewModuleValueError("A connectionInfo field is required for this operation.") |
| 205 |
updated_san_manager = self.resource_client.update(resource=merged_data, id_or_uri=resource["uri"]) |
| 206 |
return True, self.MSG_UPDATED, updated_san_manager |
| 207 |
|
| 208 |
def _get_provider_uri_by_display_name(self, data): |
| 209 |
display_name = data.get("providerDisplayName") |
| 210 |
provider_uri = self.resource_client.get_provider_uri(display_name) |
| 211 |
|
| 212 |
if not provider_uri: |
| 213 |
raise OneViewModuleValueError(f"The provider '{display_name}' was not found.") |
| 214 |
|
| 215 |
return provider_uri |
| 216 |
|
| 217 |
|
| 218 |
def main(): |
| 219 |
SanManagerModule().run() |
| 220 |
|
| 221 |
|
| 222 |
if __name__ == "__main__": |
| 223 |
main() |