Completed
Pull Request — master (#474)
by
unknown
03:25
created

GetServerAttributes.run()   F

Complexity

Conditions 12

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 12
dl 0
loc 41
rs 2.7855

How to fix   Complexity   

Complexity

Complex classes like GetServerAttributes.run() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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,
21
            attribute_key, attribute_type="all"):
22
        if connection_details:
23
            self.setConnection(connection_details)
24
        self.getSessionID()
25
        endpoint = "/rest/os-deployment-servers"
26
        results = {}
27
        for mid in mids:
28
            try:
29
                isinstance(mid, int)
30
            except:
31
                raise ValueError("MIDs must be integers")
32
            getreq = self.icspGET(endpoint + "/%s" % mid)
33
            allattr = getreq['customAttributes']
34
            results[mid] = allattr
35
36
        output = results
37
38
        # Filter based on Attribute Type
39
        if not attribute_type == "all":
40
            filtereddata = {}
41
            for server in results:
42
                filteredelements = []
43
                for element in results[server]:
44
                    if element['values'][0]['scope'] == attribute_type:
45
                        filteredelements.append(element)
46
                filtereddata[server] = filteredelements
47
            output = filtereddata
48
49
        # Filter staged results to key words provided
50
        if attribute_key:
51
            filtereddata = {}
52
            for server in output:
53
                filteredelements = []
54
                for element in output[server]:
55
                    if element['key'] == attribute_key:
56
                        filteredelements.append(element)
57
                filtereddata[server] = filteredelements
58
            output = filtereddata
59
60
        return {"attributes": output}
61