Debian Codegraph @

ansible_collections/community/general/plugins/modules/oneview_network_set.py

raw
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_network_set
10 short_description: Manage HPE OneView Network Set resources
11 description:
12 - Provides an interface to manage Network Set resources. Can create, update, or delete.
13 requirements:
14 - hpOneView >= 4.0.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 Network Set resource.
28 - V(present) ensures data properties are compliant with OneView.
29 - V(absent) removes the resource from OneView, if it exists.
30 type: str
31 default: present
32 choices: ['present', 'absent']
33 data:
34 description:
35 - List with the Network Set properties.
36 type: dict
37 required: true
38
39 extends_documentation_fragment:
40 - community.general._oneview
41 - community.general._oneview.validateetag
42 - community.general._attributes
43 """
44
45 EXAMPLES = r"""
46 - name: Create a Network Set
47 community.general.oneview_network_set:
48 config: /etc/oneview/oneview_config.json
49 state: present
50 data:
51 name: OneViewSDK Test Network Set
52 networkUris:
53 - Test Ethernet Network_1 # can be a name
54 - /rest/ethernet-networks/e4360c9d-051d-4931-b2aa-7de846450dd8 # or a URI
55 delegate_to: localhost
56
57 - name: Update the Network Set name to 'OneViewSDK Test Network Set - Renamed' and change the associated networks
58 community.general.oneview_network_set:
59 config: /etc/oneview/oneview_config.json
60 state: present
61 data:
62 name: OneViewSDK Test Network Set
63 newName: OneViewSDK Test Network Set - Renamed
64 networkUris:
65 - Test Ethernet Network_1
66 delegate_to: localhost
67
68 - name: Delete the Network Set
69 community.general.oneview_network_set:
70 config: /etc/oneview/oneview_config.json
71 state: absent
72 data:
73 name: OneViewSDK Test Network Set - Renamed
74 delegate_to: localhost
75
76 - name: Update the Network set with two scopes
77 community.general.oneview_network_set:
78 config: /etc/oneview/oneview_config.json
79 state: present
80 data:
81 name: OneViewSDK Test Network Set
82 scopeUris:
83 - /rest/scopes/01SC123456
84 - /rest/scopes/02SC123456
85 delegate_to: localhost
86 """
87
88 RETURN = r"""
89 network_set:
90 description: Has the facts about the Network Set.
91 returned: On O(state=present), but can be null.
92 type: dict
93 """
94
95 from ansible_collections.community.general.plugins.module_utils._oneview import (
96 OneViewModuleBase,
97 OneViewModuleResourceNotFound,
98 )
99
100
101 class NetworkSetModule(OneViewModuleBase):
102 MSG_CREATED = "Network Set created successfully."
103 MSG_UPDATED = "Network Set updated successfully."
104 MSG_DELETED = "Network Set deleted successfully."
105 MSG_ALREADY_PRESENT = "Network Set is already present."
106 MSG_ALREADY_ABSENT = "Network Set is already absent."
107 MSG_ETHERNET_NETWORK_NOT_FOUND = "Ethernet Network not found: "
108 RESOURCE_FACT_NAME = "network_set"
109
110 argument_spec = dict(
111 state=dict(default="present", choices=["present", "absent"]), data=dict(required=True, type="dict")
112 )
113
114 def __init__(self):
115 super().__init__(additional_arg_spec=self.argument_spec, validate_etag_support=True)
116 self.resource_client = self.oneview_client.network_sets
117
118 def execute_module(self):
119 resource = self.get_by_name(self.data.get("name"))
120
121 if self.state == "present":
122 return self._present(resource)
123 elif self.state == "absent":
124 return self.resource_absent(resource)
125
126 def _present(self, resource):
127 scope_uris = self.data.pop("scopeUris", None)
128 self._replace_network_name_by_uri(self.data)
129 result = self.resource_present(resource, self.RESOURCE_FACT_NAME)
130 if scope_uris is not None:
131 result = self.resource_scopes_set(result, self.RESOURCE_FACT_NAME, scope_uris)
132 return result
133
134 def _get_ethernet_network_by_name(self, name):
135 result = self.oneview_client.ethernet_networks.get_by("name", name)
136 return result[0] if result else None
137
138 def _get_network_uri(self, network_name_or_uri):
139 if network_name_or_uri.startswith("/rest/ethernet-networks"):
140 return network_name_or_uri
141 else:
142 enet_network = self._get_ethernet_network_by_name(network_name_or_uri)
143 if enet_network:
144 return enet_network["uri"]
145 else:
146 raise OneViewModuleResourceNotFound(self.MSG_ETHERNET_NETWORK_NOT_FOUND + network_name_or_uri)
147
148 def _replace_network_name_by_uri(self, data):
149 if "networkUris" in data:
150 data["networkUris"] = [self._get_network_uri(x) for x in data["networkUris"]]
151
152
153 def main():
154 NetworkSetModule().run()
155
156
157 if __name__ == "__main__":
158 main()