Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2016-2017, Hewlett Packard Enterprise Development LP
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: oneview_logical_interconnect_group
11 short_description: Manage OneView Logical Interconnect Group resources
12 description:
13 - Provides an interface to manage Logical Interconnect Group resources. Can create, update, or delete.
14 requirements:
15 - hpOneView >= 4.0.0
16 author:
17 - Felipe Bulsoni (@fgbulsoni)
18 - Thiago Miotto (@tmiotto)
19 - Adriane Cardozo (@adriane-cardozo)
20 attributes:
21 check_mode:
22 support: none
23 diff_mode:
24 support: none
25 options:
26 state:
27 description:
28 - Indicates the desired state for the Logical Interconnect Group resource.
29 - V(absent) removes the resource from OneView, if it exists.
30 - V(present) ensures data properties are compliant with OneView.
31 type: str
32 choices: [absent, present]
33 default: present
34 data:
35 description:
36 - List with the Logical Interconnect Group 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 Logical Interconnect Group is present
47 community.general.oneview_logical_interconnect_group:
48 config: /etc/oneview/oneview_config.json
49 state: present
50 data:
51 name: Test Logical Interconnect Group
52 uplinkSets: []
53 enclosureType: C7000
54 interconnectMapTemplate:
55 interconnectMapEntryTemplates:
56 - logicalDownlinkUri:
57 logicalLocation:
58 locationEntries:
59 - relativeValue: 1
60 type: Bay
61 - relativeValue: 1
62 type: Enclosure
63 permittedInterconnectTypeName: HP VC Flex-10/10D Module
64 # Alternatively you can inform permittedInterconnectTypeUri
65 delegate_to: localhost
66
67 - name: Ensure that the Logical Interconnect Group has the specified scopes
68 community.general.oneview_logical_interconnect_group:
69 config: /etc/oneview/oneview_config.json
70 state: present
71 data:
72 name: Test Logical Interconnect Group
73 scopeUris:
74 - /rest/scopes/00SC123456
75 - /rest/scopes/01SC123456
76 delegate_to: localhost
77
78 - name: Ensure that the Logical Interconnect Group is present with name 'Test'
79 community.general.oneview_logical_interconnect_group:
80 config: /etc/oneview/oneview_config.json
81 state: present
82 data:
83 name: New Logical Interconnect Group
84 newName: Test
85 delegate_to: localhost
86
87 - name: Ensure that the Logical Interconnect Group is absent
88 community.general.oneview_logical_interconnect_group:
89 config: /etc/oneview/oneview_config.json
90 state: absent
91 data:
92 name: New Logical Interconnect Group
93 delegate_to: localhost
94 """
95
96 RETURN = r"""
97 logical_interconnect_group:
98 description: Has the facts about the OneView Logical Interconnect Group.
99 returned: On O(state=present). Can be null.
100 type: dict
101 """
102
103 from ansible_collections.community.general.plugins.module_utils._oneview import (
104 OneViewModuleBase,
105 OneViewModuleResourceNotFound,
106 )
107
108
109 class LogicalInterconnectGroupModule(OneViewModuleBase):
110 MSG_CREATED = "Logical Interconnect Group created successfully."
111 MSG_UPDATED = "Logical Interconnect Group updated successfully."
112 MSG_DELETED = "Logical Interconnect Group deleted successfully."
113 MSG_ALREADY_PRESENT = "Logical Interconnect Group is already present."
114 MSG_ALREADY_ABSENT = "Logical Interconnect Group is already absent."
115 MSG_INTERCONNECT_TYPE_NOT_FOUND = "Interconnect Type was not found."
116
117 RESOURCE_FACT_NAME = "logical_interconnect_group"
118
119 def __init__(self):
120 argument_spec = dict(
121 state=dict(default="present", choices=["present", "absent"]), data=dict(required=True, type="dict")
122 )
123
124 super().__init__(additional_arg_spec=argument_spec, validate_etag_support=True)
125 self.resource_client = self.oneview_client.logical_interconnect_groups
126
127 def execute_module(self):
128 resource = self.get_by_name(self.data["name"])
129
130 if self.state == "present":
131 return self.__present(resource)
132 elif self.state == "absent":
133 return self.resource_absent(resource)
134
135 def __present(self, resource):
136 scope_uris = self.data.pop("scopeUris", None)
137
138 self.__replace_name_by_uris(self.data)
139 result = self.resource_present(resource, self.RESOURCE_FACT_NAME)
140
141 if scope_uris is not None:
142 result = self.resource_scopes_set(result, "logical_interconnect_group", scope_uris)
143
144 return result
145
146 def __replace_name_by_uris(self, data):
147 map_template = data.get("interconnectMapTemplate")
148
149 if map_template:
150 map_entry_templates = map_template.get("interconnectMapEntryTemplates")
151 if map_entry_templates:
152 for value in map_entry_templates:
153 permitted_interconnect_type_name = value.pop("permittedInterconnectTypeName", None)
154 if permitted_interconnect_type_name:
155 value["permittedInterconnectTypeUri"] = self.__get_interconnect_type_by_name(
156 permitted_interconnect_type_name
157 ).get("uri")
158
159 def __get_interconnect_type_by_name(self, name):
160 i_type = self.oneview_client.interconnect_types.get_by("name", name)
161 if i_type:
162 return i_type[0]
163 else:
164 raise OneViewModuleResourceNotFound(self.MSG_INTERCONNECT_TYPE_NOT_FOUND)
165
166
167 def main():
168 LogicalInterconnectGroupModule().run()
169
170
171 if __name__ == "__main__":
172 main()