| 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_ethernet_network |
| 10 |
short_description: Manage OneView Ethernet Network resources |
| 11 |
description: |
| 12 |
- Provides an interface to manage Ethernet Network resources. Can create, update, or delete. |
| 13 |
requirements: |
| 14 |
- hpOneView >= 3.1.0 |
| 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 Ethernet Network resource. |
| 28 |
- V(present) ensures data properties are compliant with OneView. |
| 29 |
- V(absent) removes the resource from OneView, if it exists. |
| 30 |
- V(default_bandwidth_reset) resets the network connection template to the default. |
| 31 |
type: str |
| 32 |
default: present |
| 33 |
choices: [present, absent, default_bandwidth_reset] |
| 34 |
data: |
| 35 |
description: |
| 36 |
- List with Ethernet Network properties. |
| 37 |
type: dict |
| 38 |
required: true |
| 39 |
extends_documentation_fragment: |
| 40 |
- community.general._oneview |
| 41 |
- community.general._oneview.validateetag |
| 42 |
- community.general._attributes |
| 43 |
""" |
| 44 |
|
| 45 |
EXAMPLES = r""" |
| 46 |
- name: Ensure that the Ethernet Network is present using the default configuration |
| 47 |
community.general.oneview_ethernet_network: |
| 48 |
config: '/etc/oneview/oneview_config.json' |
| 49 |
state: present |
| 50 |
data: |
| 51 |
name: 'Test Ethernet Network' |
| 52 |
vlanId: '201' |
| 53 |
delegate_to: localhost |
| 54 |
|
| 55 |
- name: Update the Ethernet Network changing bandwidth and purpose |
| 56 |
community.general.oneview_ethernet_network: |
| 57 |
config: '/etc/oneview/oneview_config.json' |
| 58 |
state: present |
| 59 |
data: |
| 60 |
name: 'Test Ethernet Network' |
| 61 |
purpose: Management |
| 62 |
bandwidth: |
| 63 |
maximumBandwidth: 3000 |
| 64 |
typicalBandwidth: 2000 |
| 65 |
delegate_to: localhost |
| 66 |
|
| 67 |
- name: Ensure that the Ethernet Network is present with name 'Renamed Ethernet Network' |
| 68 |
community.general.oneview_ethernet_network: |
| 69 |
config: '/etc/oneview/oneview_config.json' |
| 70 |
state: present |
| 71 |
data: |
| 72 |
name: 'Test Ethernet Network' |
| 73 |
newName: 'Renamed Ethernet Network' |
| 74 |
delegate_to: localhost |
| 75 |
|
| 76 |
- name: Ensure that the Ethernet Network is absent |
| 77 |
community.general.oneview_ethernet_network: |
| 78 |
config: '/etc/oneview/oneview_config.json' |
| 79 |
state: absent |
| 80 |
data: |
| 81 |
name: 'New Ethernet Network' |
| 82 |
delegate_to: localhost |
| 83 |
|
| 84 |
- name: Create Ethernet networks in bulk |
| 85 |
community.general.oneview_ethernet_network: |
| 86 |
config: '/etc/oneview/oneview_config.json' |
| 87 |
state: present |
| 88 |
data: |
| 89 |
vlanIdRange: '1-10,15,17' |
| 90 |
purpose: General |
| 91 |
namePrefix: TestNetwork |
| 92 |
smartLink: false |
| 93 |
privateNetwork: false |
| 94 |
bandwidth: |
| 95 |
maximumBandwidth: 10000 |
| 96 |
typicalBandwidth: 2000 |
| 97 |
delegate_to: localhost |
| 98 |
|
| 99 |
- name: Reset to the default network connection template |
| 100 |
community.general.oneview_ethernet_network: |
| 101 |
config: '/etc/oneview/oneview_config.json' |
| 102 |
state: default_bandwidth_reset |
| 103 |
data: |
| 104 |
name: 'Test Ethernet Network' |
| 105 |
delegate_to: localhost |
| 106 |
""" |
| 107 |
|
| 108 |
RETURN = r""" |
| 109 |
ethernet_network: |
| 110 |
description: Has the facts about the Ethernet Networks. |
| 111 |
returned: On O(state=present). Can be null. |
| 112 |
type: dict |
| 113 |
|
| 114 |
ethernet_network_bulk: |
| 115 |
description: Has the facts about the Ethernet Networks affected by the bulk insert. |
| 116 |
returned: When V(vlanIdRange) attribute is in O(data) argument. Can be null. |
| 117 |
type: dict |
| 118 |
|
| 119 |
ethernet_network_connection_template: |
| 120 |
description: Has the facts about the Ethernet Network Connection Template. |
| 121 |
returned: On O(state=default_bandwidth_reset). Can be null. |
| 122 |
type: dict |
| 123 |
""" |
| 124 |
|
| 125 |
from ansible_collections.community.general.plugins.module_utils._oneview import ( |
| 126 |
OneViewModuleBase, |
| 127 |
OneViewModuleResourceNotFound, |
| 128 |
) |
| 129 |
|
| 130 |
|
| 131 |
class EthernetNetworkModule(OneViewModuleBase): |
| 132 |
MSG_CREATED = "Ethernet Network created successfully." |
| 133 |
MSG_UPDATED = "Ethernet Network updated successfully." |
| 134 |
MSG_DELETED = "Ethernet Network deleted successfully." |
| 135 |
MSG_ALREADY_PRESENT = "Ethernet Network is already present." |
| 136 |
MSG_ALREADY_ABSENT = "Ethernet Network is already absent." |
| 137 |
|
| 138 |
MSG_BULK_CREATED = "Ethernet Networks created successfully." |
| 139 |
MSG_MISSING_BULK_CREATED = "Some missing Ethernet Networks were created successfully." |
| 140 |
MSG_BULK_ALREADY_EXIST = "The specified Ethernet Networks already exist." |
| 141 |
MSG_CONNECTION_TEMPLATE_RESET = "Ethernet Network connection template was reset to the default." |
| 142 |
MSG_ETHERNET_NETWORK_NOT_FOUND = "Ethernet Network was not found." |
| 143 |
|
| 144 |
RESOURCE_FACT_NAME = "ethernet_network" |
| 145 |
|
| 146 |
def __init__(self): |
| 147 |
argument_spec = dict( |
| 148 |
state=dict(type="str", default="present", choices=["absent", "default_bandwidth_reset", "present"]), |
| 149 |
data=dict(type="dict", required=True), |
| 150 |
) |
| 151 |
|
| 152 |
super().__init__(additional_arg_spec=argument_spec, validate_etag_support=True) |
| 153 |
|
| 154 |
self.resource_client = self.oneview_client.ethernet_networks |
| 155 |
|
| 156 |
def execute_module(self): |
| 157 |
changed, msg, ansible_facts, resource = False, "", {}, None |
| 158 |
|
| 159 |
if self.data.get("name"): |
| 160 |
resource = self.get_by_name(self.data["name"]) |
| 161 |
|
| 162 |
if self.state == "present": |
| 163 |
if self.data.get("vlanIdRange"): |
| 164 |
return self._bulk_present() |
| 165 |
else: |
| 166 |
return self._present(resource) |
| 167 |
elif self.state == "absent": |
| 168 |
return self.resource_absent(resource) |
| 169 |
elif self.state == "default_bandwidth_reset": |
| 170 |
changed, msg, ansible_facts = self._default_bandwidth_reset(resource) |
| 171 |
return dict(changed=changed, msg=msg, ansible_facts=ansible_facts) |
| 172 |
|
| 173 |
def _present(self, resource): |
| 174 |
bandwidth = self.data.pop("bandwidth", None) |
| 175 |
scope_uris = self.data.pop("scopeUris", None) |
| 176 |
result = self.resource_present(resource, self.RESOURCE_FACT_NAME) |
| 177 |
|
| 178 |
if bandwidth: |
| 179 |
if self._update_connection_template(result["ansible_facts"]["ethernet_network"], bandwidth)[0]: |
| 180 |
result["changed"] = True |
| 181 |
result["msg"] = self.MSG_UPDATED |
| 182 |
|
| 183 |
if scope_uris is not None: |
| 184 |
result = self.resource_scopes_set(result, "ethernet_network", scope_uris) |
| 185 |
|
| 186 |
return result |
| 187 |
|
| 188 |
def _bulk_present(self): |
| 189 |
vlan_id_range = self.data["vlanIdRange"] |
| 190 |
result = dict(ansible_facts={}) |
| 191 |
ethernet_networks = self.resource_client.get_range(self.data["namePrefix"], vlan_id_range) |
| 192 |
|
| 193 |
if not ethernet_networks: |
| 194 |
self.resource_client.create_bulk(self.data) |
| 195 |
result["changed"] = True |
| 196 |
result["msg"] = self.MSG_BULK_CREATED |
| 197 |
|
| 198 |
else: |
| 199 |
vlan_ids = self.resource_client.dissociate_values_or_ranges(vlan_id_range) |
| 200 |
for net in ethernet_networks[:]: |
| 201 |
vlan_ids.remove(net["vlanId"]) |
| 202 |
|
| 203 |
if len(vlan_ids) == 0: |
| 204 |
result["msg"] = self.MSG_BULK_ALREADY_EXIST |
| 205 |
result["changed"] = False |
| 206 |
else: |
| 207 |
if len(vlan_ids) == 1: |
| 208 |
self.data["vlanIdRange"] = f"{vlan_ids[0]}-{vlan_ids[0]}" |
| 209 |
else: |
| 210 |
self.data["vlanIdRange"] = ",".join(str(s) for s in vlan_ids) |
| 211 |
|
| 212 |
self.resource_client.create_bulk(self.data) |
| 213 |
result["changed"] = True |
| 214 |
result["msg"] = self.MSG_MISSING_BULK_CREATED |
| 215 |
result["ansible_facts"]["ethernet_network_bulk"] = self.resource_client.get_range( |
| 216 |
self.data["namePrefix"], vlan_id_range |
| 217 |
) |
| 218 |
|
| 219 |
return result |
| 220 |
|
| 221 |
def _update_connection_template(self, ethernet_network, bandwidth): |
| 222 |
if "connectionTemplateUri" not in ethernet_network: |
| 223 |
return False, None |
| 224 |
|
| 225 |
connection_template = self.oneview_client.connection_templates.get(ethernet_network["connectionTemplateUri"]) |
| 226 |
|
| 227 |
merged_data = connection_template.copy() |
| 228 |
merged_data.update({"bandwidth": bandwidth}) |
| 229 |
|
| 230 |
if not self.compare(connection_template, merged_data): |
| 231 |
connection_template = self.oneview_client.connection_templates.update(merged_data) |
| 232 |
return True, connection_template |
| 233 |
else: |
| 234 |
return False, None |
| 235 |
|
| 236 |
def _default_bandwidth_reset(self, resource): |
| 237 |
if not resource: |
| 238 |
raise OneViewModuleResourceNotFound(self.MSG_ETHERNET_NETWORK_NOT_FOUND) |
| 239 |
|
| 240 |
default_connection_template = self.oneview_client.connection_templates.get_default() |
| 241 |
|
| 242 |
changed, connection_template = self._update_connection_template( |
| 243 |
resource, default_connection_template["bandwidth"] |
| 244 |
) |
| 245 |
|
| 246 |
return ( |
| 247 |
changed, |
| 248 |
self.MSG_CONNECTION_TEMPLATE_RESET, |
| 249 |
dict(ethernet_network_connection_template=connection_template), |
| 250 |
) |
| 251 |
|
| 252 |
|
| 253 |
def main(): |
| 254 |
EthernetNetworkModule().run() |
| 255 |
|
| 256 |
|
| 257 |
if __name__ == "__main__": |
| 258 |
main() |