Completed
Push — master ( c616a5...008d9e )
by Tomaz
03:05
created

ApplyBuildPlan.run()   C

Complexity

Conditions 8

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
dl 0
loc 51
rs 5.2591
c 1
b 0
f 0

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