forked from ansible/ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneview_connection_template.py
executable file
·141 lines (118 loc) · 4.43 KB
/
oneview_connection_template.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (2016-2017) Hewlett Packard Enterprise Development LP
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['stableinterface'],
'supported_by': 'curated'}
DOCUMENTATION = '''
---
module: oneview_connection_template
short_description: Manage the OneView Connection Template resources.
description:
- "Provides an interface to manage the Connection Template resources. Can update."
version_added: "2.3"
requirements:
- "python >= 2.7.9"
- "hpOneView >= 3.1.0"
author: "Gustavo Hennig (@GustavoHennig)"
options:
state:
description:
- Indicates the desired state for the Connection Template resource.
C(present) will ensure data properties are compliant with OneView.
choices: ['present']
required: true
data:
description:
- List with Connection Template properties and its associated states.
required: true
extends_documentation_fragment:
- oneview
'''
EXAMPLES = '''
- name: Update the Connection Template
oneview_connection_template:
config: "{{ config }}"
state: present
data:
name: 'name1304244267-1467656930023'
type : "connection-template"
bandwidth :
maximumBandwidth : 10000
typicalBandwidth : 2000
newName : "CT-23"
delegate_to: localhost
'''
RETURN = '''
connection_template:
description: Has the OneView facts about the Connection Template.
returned: On 'present' state, but can be null.
type: complex
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.oneview import (OneViewModuleBase,
HPOneViewValueError,
HPOneViewResourceNotFound,
ResourceComparator)
class ConnectionTemplateModule(OneViewModuleBase):
MSG_UPDATED = 'Connection Template updated successfully.'
MSG_NOT_FOUND = 'Connection Template was not found.'
MSG_ALREADY_PRESENT = 'Connection Template is already updated.'
MSG_MANDATORY_FIELD_MISSING = 'Mandatory field was not informed: data.name'
def __init__(self):
argument_spec = dict(
state=dict(
required=True,
choices=['present']
),
data=dict(required=True, type='dict')
)
super(ConnectionTemplateModule, self).__init__(additional_arg_spec=argument_spec)
self.resource_client = self.oneview_client.connection_templates
def execute_module(self):
changed, msg, ansible_facts = False, '', {}
if not self.data.get('name'):
raise HPOneViewValueError(self.MSG_MANDATORY_FIELD_MISSING)
resource = self.get_by_name(self.data['name'])
if self.state == 'present':
changed, msg, ansible_facts = self.__present(self.data, resource)
return dict(changed=changed,
msg=msg,
ansible_facts=ansible_facts)
def __present(self, data, resource):
changed = False
msg = ''
if not resource:
raise HPOneViewResourceNotFound(self.MSG_NOT_FOUND)
else:
if 'newName' in data:
data['name'] = data.pop('newName')
merged_data = resource.copy()
merged_data.update(data)
if not ResourceComparator.compare(resource, merged_data):
resource = self.resource_client.update(merged_data)
changed = True
msg = self.MSG_UPDATED
else:
msg = self.MSG_ALREADY_PRESENT
return changed, msg, dict(connection_template=resource)
def main():
ConnectionTemplateModule().run()
if __name__ == '__main__':
main()