Completed
Pull Request — master (#513)
by
unknown
03:04
created

BaseAction.establish_connection()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
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
import atexit
17
import eventlet
18
19
from pyVim import connect
20
from pyVmomi import vim
21
from st2actions.runners.pythonrunner import Action
22
23
24
class BaseAction(Action):
25
    def __init__(self, config):
26
        super(BaseAction, self).__init__(config)
27
        if "vsphere" in self.config:
28
            pass
29
        elif "host" not in self.config:
30
            raise ValueError("host: Check Connection configuration details")
31
        elif "port" not in self.config:
32
            raise ValueError("port: Check Connection configuration details")
33
        elif "user" not in self.config:
34
            raise ValueError("user: Check Connection configuration details")
35
        elif "passwd" not in self.config:
36
            raise ValueError("passwd: Check Connection configuration details")
37
        else:
38
            pass
39
40
    def establish_connection(self, vsphere):
41
        self.si = self._connect(vsphere)
0 ignored issues
show
Coding Style introduced by
The attribute si was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
42
        self.si_content = self.si.RetrieveContent()
0 ignored issues
show
Coding Style introduced by
The attribute si_content was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
43
44
    def _connect(self, vsphere):
45
        if vsphere:
46
            connection = self.config['vsphere'].get(vsphere)
47
        else:
48
            connection = self.config
49
50
        try:
51
            si = connect.SmartConnect(host=connection['host'],
52
                                      port=connection['port'],
53
                                      user=connection['user'],
54
                                      pwd=connection['passwd'])
55
        except:
56
            raise Exception("Unable to connect to vsphere.")
57
58
        atexit.register(connect.Disconnect, si)
59
        return si
60
61
    def _wait_for_task(self, task):
62
        while task.info.state == vim.TaskInfo.State.running:
63
            eventlet.sleep(1)
64
        return task.info.state == vim.TaskInfo.State.success
65