Completed
Pull Request — master (#474)
by
unknown
02:45
created

Apply.run()   D

Complexity

Conditions 9

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 9
dl 0
loc 52
rs 4.6097

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
import json
18
19
20
class Apply(ICSPBaseActions):
21
    def run(self, buildplan_uris, server_data, connection_details):
22
        if connection_details:
23
            self.setConnection(connection_details)
24
        self.getSessionID()
25
26
        # Prepare Endpoint within ICSP API
27
        endpoint = "/rest/os-deployment-jobs"
28
29
        # Prepare Pesonality Data data collection
30
        pload = {}
31
        pload['osbpUris'] = []
32
        pload['failMode'] = None
33
        pload['serverData'] = []
34
35
        for plan in buildplan_uris:
36
            # Confirm input are integers
37
            try:
38
                isinstance(plan, int)
39
            except:
40
                raise ValueError("Build plans must be \
41
                                 Integers (comma seperated)")
42
43
            pload["osbpUris"].append("/rest/os-deployment-build-plans/%s"
44
                                     % plan)
45
46
        for server in server_data:
47
            data = {}
48
            pdata = {}
49
            data['serverUri'] = "/rest/os-deployment-servers/%s" % server
50
51
            # Prepare server personality Data
52
            # Initially not including network data Although
53
            # this can be included later
54
            # TODO extend variable values to include IP configuration
55
56
            if "hostname" in server_data[server]:
57
                pdata['hostName'] = server_data[server]['hostname']
58
            if "domain" in server_data[server]:
59
                pdata['domain'] = server_data[server]['domain']
60
            if "workgroup" in server_data[server]:
61
                pdata['workgroup'] = server_data[server]['workgroup']
62
            data['personalityData'] = pdata
63
64
            pload['serverData'].append(data)
65
66
        payload = json.dumps(pload)
67
        try:
68
            results = self.icspPOST(endpoint, payload)
69
        except Exception as e:
70
            raise Exception("Error: %s" % e)
71
72
        return {"jobid": int(results['uri'].rsplit("/")[-1])}
73