Completed
Pull Request — master (#513)
by
unknown
03:15
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
CONNECTION_ITEMS = ['host', 'port', 'user', 'passwd']
24
25
26
class BaseAction(Action):
27
    def __init__(self, config):
28
        super(BaseAction, self).__init__(config)
29
        if config is None:
30
            raise ValueError("No connection configuration details found")
31
        if "vsphere" in config:
32
            if config['vsphere'] is None:
33
                raise ValueError("'vsphere' config defined but empty.")
34
            else:
35
                pass
36
        elif set(CONNECTION_ITEMS).issubset(config):
37
            pass
38
        else:
39
            raise ValueError("Incomplete configuration details")
40
41
    def establish_connection(self, vsphere):
42
        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...
43
        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...
44
45
    def _connect(self, vsphere):
46
        if vsphere:
47
            connection = self.config['vsphere'].get(vsphere)
48
            if set(CONNECTION_ITEMS).issubset(connection):
49
                pass
50
            else:
51
                raise ValueError("Incomplete configuration details")
52
        else:
53
            connection = self.config
54
55
        try:
56
            si = connect.SmartConnect(host=connection['host'],
57
                                      port=connection['port'],
58
                                      user=connection['user'],
59
                                      pwd=connection['passwd'])
60
        except Exception as e:
61
            raise Exception(e)
62
63
        atexit.register(connect.Disconnect, si)
64
        return si
65
66
    def _wait_for_task(self, task):
67
        while task.info.state == vim.TaskInfo.State.running:
68
            eventlet.sleep(1)
69
        return task.info.state == vim.TaskInfo.State.success
70