Completed
Pull Request — master (#544)
by
unknown
05:43
created

GetServerAttributes   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 11
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
from lib.icsp import ICSPBaseActions
17
18
19
class GetServerAttributes(ICSPBaseActions):
20
    def run(self, mids, connection_details=None,
21
            attribute_key=None, attribute_type="all"):
22
        self.set_connection(connection_details)
23
        self.get_sessionid()
24
        endpoint = "/rest/os-deployment-servers"
25
        results = {}
26
        for mid in mids:
27
            try:
28
                isinstance(mid, int)
29
            except:
30
                raise ValueError("MIDs must be integers")
31
            getreq = self.icsp_get(endpoint + "/%s" % (mid))
32
            allattr = getreq['customAttributes']
33
            results[mid] = allattr
34
35
        output = results
36
37
        # Filter based on Attribute Type
38
        if not attribute_type == "all":
39
            filtereddata = {}
40
            for server in results:
41
                filteredelements = []
42
                for element in results[server]:
43
                    if element['values'][0]['scope'] == attribute_type:
44
                        filteredelements.append(element)
45
                filtereddata[server] = filteredelements
46
            output = filtereddata
47
48
        # Filter staged results to key words provided
49
        if attribute_key:
50
            filtereddata = {}
51
            for server in output:
52
                filteredelements = []
53
                for element in output[server]:
54
                    if element['key'] == attribute_key:
55
                        filteredelements.append(element)
56
                filtereddata[server] = filteredelements
57
            output = filtereddata
58
59
        return {"attributes": output}
60