forked from HewlettPackard/oneview-ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneview_network_set.py
211 lines (178 loc) · 7.07 KB
/
oneview_network_set.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
###
# Copyright (2016-2020) Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###
ANSIBLE_METADATA = {'status': ['stableinterface'],
'supported_by': 'community',
'metadata_version': '1.1'}
DOCUMENTATION = '''
---
module: oneview_network_set
short_description: Manage OneView Network Set resources.
description:
- Provides an interface to manage Network Set resources. Can create, update, or delete.
version_added: "2.3"
requirements:
- "python >= 2.7.9"
- "hpeOneView >= 5.4.0"
author: "Mariana Kreisig (@marikrg)"
options:
state:
description:
- Indicates the desired state for the Network Set resource.
C(present) ensures data properties are compliant with OneView.
C(absent) removes the resource from OneView, if it exists.
choices: ['present', 'absent']
data:
description:
- List with the Network Set properties.
required: true
extends_documentation_fragment:
- oneview
- oneview.validateetag
'''
EXAMPLES = '''
- name: Create a Network Set
oneview_network_set:
hostname: 172.16.101.48
username: administrator
password: my_password
api_version: 1200
state: present
data:
name: 'OneViewSDK Test Network Set'
networkUris:
- 'Test Ethernet Network_1' # can be a name
- '/rest/ethernet-networks/e4360c9d-051d-4931-b2aa-7de846450dd8' # or a URI
- name: Update the Network Set name to 'OneViewSDK Test Network Set - Renamed' and change the associated networks
oneview_network_set:
hostname: 172.16.101.48
username: administrator
password: my_password
api_version: 1200
state: present
data:
name: 'OneViewSDK Test Network Set'
newName: 'OneViewSDK Test Network Set - Renamed'
networkUris:
- 'Test Ethernet Network_1'
- name: Delete the Network Set
oneview_network_set:
hostname: 172.16.101.48
username: administrator
password: my_password
api_version: 600
state: absent
data:
name: 'OneViewSDK Test Network Set - Renamed'
# This feature is only available for V300 and V500
- name: Update the Network set with two scopes
oneview_network_set:
hostname: 172.16.101.48
username: administrator
password: my_password
api_version: 1200
state: present
data:
name: OneViewSDK Test Network Set
scopeUris:
- /rest/scopes/01SC123456
- /rest/scopes/02SC123456
delegate_to: localhost
'''
RETURN = '''
network_set:
description: Has the facts about the Network Set.
returned: On state 'present', but can be null.
type: dict
'''
from ansible.module_utils.oneview import OneViewModule, OneViewModuleResourceNotFound, compare
class NetworkSetModule(OneViewModule):
MSG_CREATED = 'Network Set created successfully.'
MSG_UPDATED = 'Network Set updated successfully.'
MSG_DELETED = 'Network Set deleted successfully.'
MSG_ALREADY_PRESENT = 'Network Set is already present.'
MSG_ALREADY_ABSENT = 'Network Set is already absent.'
MSG_ETHERNET_NETWORK_NOT_FOUND = 'Ethernet Network not found: '
MSG_CONNECTION_TEMPLATE_NOT_FOUND = 'Connection Template not found.'
RESOURCE_FACT_NAME = 'network_set'
argument_spec = dict(
state=dict(
required=True,
choices=['present', 'absent']
),
data=dict(required=True, type='dict'))
def __init__(self):
super(NetworkSetModule, self).__init__(additional_arg_spec=self.argument_spec,
validate_etag_support=True)
self.set_resource_object(self.oneview_client.network_sets)
self.connection_templates = self.oneview_client.connection_templates
def execute_module(self):
if self.state == 'present':
return self.__present()
elif self.state == 'absent':
return self.resource_absent()
def __present(self):
bandwidth = self.data.pop('bandwidth', None)
scope_uris = self.data.pop('scopeUris', None)
self.__replace_network_name_by_uri(self.data)
result = self.resource_present(self.RESOURCE_FACT_NAME)
if bandwidth:
changed, connection_template = self.__update_connection_template(bandwidth)
if changed:
result['changed'] = changed
result['msg'] = self.MSG_UPDATED
result['ansible_facts'][self.RESOURCE_FACT_NAME]['connection_template'] = connection_template
else:
result['changed'] = changed
result['msg'] = self.MSG_ALREADY_PRESENT
if scope_uris is not None:
result = self.resource_scopes_set(result, self.RESOURCE_FACT_NAME, scope_uris)
return result
def __get_ethernet_network_by_name(self, name):
result = self.oneview_client.ethernet_networks.get_by('name', name)
return result[0] if result else None
def __get_network_uri(self, network_name_or_uri):
if network_name_or_uri and network_name_or_uri.startswith('/rest/ethernet-networks'):
return network_name_or_uri
else:
enet_network = self.__get_ethernet_network_by_name(network_name_or_uri)
if enet_network:
return enet_network['uri']
else:
raise OneViewModuleResourceNotFound(self.MSG_ETHERNET_NETWORK_NOT_FOUND + network_name_or_uri)
def __replace_network_name_by_uri(self, data):
if 'networkUris' in data:
data['networkUris'] = [self.__get_network_uri(x) for x in data['networkUris']]
if 'nativeNetworkUri' in data and data['nativeNetworkUri']:
data['nativeNetworkUri'] = self.__get_network_uri(data['nativeNetworkUri'])
# Update network set connection template with bandwidth
def __update_connection_template(self, bandwidth):
if 'connectionTemplateUri' not in self.current_resource.data:
raise OneViewModuleResourceNotFound(self.MSG_CONNECTION_TEMPLATE_NOT_FOUND)
connection_template = self.connection_templates.get_by_uri(
self.current_resource.data['connectionTemplateUri'])
merged_data = connection_template.data.copy()
merged_data.update({'bandwidth': bandwidth})
if not compare(connection_template.data, merged_data):
connection_template.update(merged_data)
return True, connection_template.data
else:
return False, connection_template.data
def main():
NetworkSetModule().run()
if __name__ == '__main__':
main()