-
Notifications
You must be signed in to change notification settings - Fork 21
/
computer_system_collection.py
133 lines (106 loc) · 5.19 KB
/
computer_system_collection.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
# -*- coding: utf-8 -*-
# Copyright (2017-2018) 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.
from collections import OrderedDict
from oneview_redfish_toolkit.api.computer_system \
import ComputerSystem
from oneview_redfish_toolkit.api.redfish_json_validator \
import RedfishJsonValidator
from oneview_redfish_toolkit.api.zone_collection \
import ZoneCollection
class ComputerSystemCollection(RedfishJsonValidator):
"""Creates a Computer System Collection Redfish dict
Populates self.redfish with some hardcoded ComputerSystemCollection
values and with the response of Oneview with all servers with
Server Profile applied.
"""
SCHEMA_NAME = 'ComputerSystemCollection'
def __init__(self,
server_profile_list,
server_profile_templates,
zone_ids):
"""ComputerSystemCollection constructor
Populates self.redfish with a hardcoded ComputerSystemCollection
values and with the response of Oneview with all servers with
Server Profile applied and all server profile templates.
Args:
server_profile_list: A list of dicts of Server Profile applied
server_profile_templates: A list of dicts of server profile
templates
zone_ids: A list of Zone Ids
"""
super().__init__(self.SCHEMA_NAME)
self.redfish["@odata.type"] = self.get_odata_type()
self.redfish["Name"] = "Computer System Collection"
server_profile_members_list = \
self._get_server_profile_members_list(server_profile_list)
self.redfish["Members@odata.count"] = \
len(server_profile_members_list)
self.redfish["Members"] = server_profile_members_list
self._set_collection_capabilities(server_profile_templates, zone_ids)
self.redfish["@odata.context"] = \
"/redfish/v1/$metadata#ComputerSystemCollection" \
".ComputerSystemCollection"
self.redfish["@odata.id"] = "/redfish/v1/Systems"
self._validate()
def _get_server_profile_members_list(self, server_profile_list):
"""Returns a redfish members list with all server profiles applied
Iterate over the server hardware list, filter the
UUIDs of server profiles that are applied and mounts
the member item to be filled on Redfish Members.
Args:
server_profile_list: list of Oneview's Server
Profiles information.
Returns:
list: list of computer system members to be filled on
Redfish Members.
"""
members = list()
for server_profile in server_profile_list:
members.append({
"@odata.id": "/redfish/v1/Systems/" + server_profile["uuid"]
})
return members
def _set_collection_capabilities(self, server_profile_templates, zone_ids):
self.capabilities_key = "@Redfish.CollectionCapabilities"
self.redfish[self.capabilities_key] = dict()
self.redfish[self.capabilities_key]["@odata.type"] = \
self.get_odata_type_by_schema('CollectionCapabilities')
self.redfish[self.capabilities_key]["Capabilities"] = list()
for server_profile_template in server_profile_templates:
spt_id = server_profile_template["uri"].split("/")[-1]
capability = self._get_capability_object(spt_id, zone_ids)
self.redfish[self.capabilities_key]["Capabilities"].\
append(capability)
def _get_capability_object(self, spt_id, zone_ids):
capability = OrderedDict()
capability["CapabilitiesObject"] = dict()
capability["CapabilitiesObject"]["@odata.id"] = \
ComputerSystem.BASE_URI + "/" + spt_id
capability["UseCase"] = "ComputerSystemComposition"
capability["Links"] = dict()
capability["Links"]["TargetCollection"] = dict()
capability["Links"]["TargetCollection"]["@odata.id"] = \
ComputerSystem.BASE_URI
zone_ids_filtered = filter(lambda zone_id: spt_id in zone_id, zone_ids)
self._build_capability_related_items(capability, zone_ids_filtered)
return capability
def _build_capability_related_items(self,
capability,
zone_ids_of_capability):
capability["Links"]["RelatedItem"] = list()
for zone_id in zone_ids_of_capability:
related_item = dict()
related_item["@odata.id"] = ZoneCollection.BASE_URI + "/" + zone_id
capability["Links"]["RelatedItem"].append(related_item)