Browse Source

Proxy object to communicate with vcenter server

Vcenter provides apis through web services (WSDL).
VcenterProxy provides a proxy object which can be
instantiated to communicate with a vcenter server.

Change-Id: I92e92a2ca17df8c6dfda13c29dfea525f812ad8b
Adolfo Duarte 1 year ago
parent
commit
1b031ad584

+ 10
- 0
networking_vsphere/common/constants.py View File

@@ -1,4 +1,5 @@
1 1
 # Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
2
+# (c) Copyright 2017-2018 SUSE LLC
2 3
 #
3 4
 # All Rights Reserved.
4 5
 #
@@ -122,3 +123,12 @@ RESOURCE_IN_USE = "is in use."
122 123
 # EPHEMERAL ports
123 124
 MIN_EPHEMERAL_PORT = 32768
124 125
 MAX_EPHEMERAL_PORT = 65535
126
+
127
+VLAN_TYPE_TRUNK = 'trunk'
128
+VLAN_TYPE_VLAN = 'vlan'
129
+VIM_API_RETRY_COUNT = 10
130
+VIM_TASK_POLL_INTERVAL = 0.1
131
+VIM_MAX_OBJETS = 100
132
+
133
+# Minimum supported esxi version.
134
+MIN_SUPPORTED_VERSION = '6.0'

+ 74
- 0
networking_vsphere/tests/unit/utils/test_vim_objects.py View File

@@ -0,0 +1,74 @@
1
+# (c) Copyright 2018 SUSE LLC
2
+# All Rights Reserved.
3
+#
4
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5
+#    not use this file except in compliance with the License. You may obtain
6
+#    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, WITHOUT
12
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+#    License for the specific language governing permissions and limitations
14
+#    under the License.
15
+#
16
+
17
+from mock import MagicMock
18
+from mock import patch
19
+
20
+from networking_vsphere.common import constants as const
21
+from networking_vsphere.utils import vim_objects
22
+from oslotest import base
23
+
24
+
25
+class TestBase(base.BaseTestCase):
26
+    def setUp(self):
27
+        super(TestBase, self).setUp()
28
+        self.addCleanup(patch.stopall)
29
+        self.connection_patcher = patch(
30
+            'networking_vsphere.utils.vim_objects.api.VMwareAPISession')
31
+        self.mocked_session = self.connection_patcher.start()
32
+        session_instance = MagicMock()
33
+        session_instance.invoke_api.return_value = [MagicMock(), MagicMock()]
34
+        self.mocked_session.return_value = session_instance
35
+
36
+
37
+class TestVcenterProxy(TestBase):
38
+    def setUp(self):
39
+        super(TestVcenterProxy, self).setUp()
40
+        self.sut = vim_objects.VcenterProxy(name='test_dvs',
41
+                                            vcenter_user="username",
42
+                                            vcenter_ip='127.0.0.1',
43
+                                            vcenter_port=443,
44
+                                            vcenter_password='test'
45
+                                            )
46
+        self.sut.connect_to_vcenter()
47
+
48
+    def test_connect_to_vcenter(self):
49
+        self.assertIsNotNone(self.sut.cf)
50
+
51
+    def test_get_type(self):
52
+        self.sut.connect_to_vcenter()
53
+        self.sut.get_type('fake_type')
54
+        self.sut.cf.create.called_with('ns0:fake_type')
55
+
56
+    def test_get_all_objects_of_type(self):
57
+        self.assertIsNotNone(self.sut.get_all_objects_of_type('some_type'))
58
+        self.sut.session.invoke_api.assert_called_with(
59
+            vim_objects.vim_util,
60
+            'get_objects',
61
+            self.sut.session.vim,
62
+            'some_type',
63
+            const.VIM_MAX_OBJETS
64
+        )
65
+
66
+    def test_get_vcenter_hosts(self):
67
+        self.assertIsNotNone(self.sut.get_hosts())
68
+        self.sut.session.invoke_api.assert_called_with(
69
+            vim_objects.vim_util,
70
+            'get_objects',
71
+            self.sut.session.vim,
72
+            'HostSystem',
73
+            const.VIM_MAX_OBJETS
74
+        )

+ 99
- 0
networking_vsphere/utils/vim_objects.py View File

@@ -0,0 +1,99 @@
1
+# (c) Copyright 2018 SUSE LLC
2
+# All Rights Reserved.
3
+#
4
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5
+#    not use this file except in compliance with the License. You may obtain
6
+#    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, WITHOUT
12
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+#    License for the specific language governing permissions and limitations
14
+#    under the License.
15
+#
16
+
17
+
18
+from networking_vsphere.common import constants as const
19
+from oslo_vmware import api
20
+from oslo_vmware import vim_util
21
+
22
+
23
+class VcenterProxy(object):
24
+    def __init__(self, name, vcenter_ip=None, vcenter_port=443,
25
+                 vcenter_user=None, vcenter_password=None):
26
+        self.name = name
27
+        self.vcenter_ip = vcenter_ip
28
+        self.vcenter_port = vcenter_port
29
+        self.vcenter_user = vcenter_user
30
+        self.vcenter_password = vcenter_password
31
+        self.cf = None
32
+        self.session = None
33
+        self._connected = False
34
+
35
+    def connect_to_vcenter(self):
36
+        self._connected = False
37
+
38
+        self.session = api.VMwareAPISession(self.vcenter_ip,
39
+                                            self.vcenter_user,
40
+                                            self.vcenter_password,
41
+                                            const.VIM_API_RETRY_COUNT,
42
+                                            const.VIM_TASK_POLL_INTERVAL,
43
+                                            port=self.vcenter_port,
44
+                                            create_session=True
45
+                                            )
46
+        self._connected = True
47
+        self.cf = self.session.vim.client.factory
48
+
49
+    def get_type(self, type_name):
50
+        type_ns = "ns0:" + str(type_name)
51
+        return self.cf.create(type_ns)
52
+
53
+    def get_all_objects_of_type(self, vim_type, **kwargs):
54
+        return self.session.invoke_api(vim_util,
55
+                                       'get_objects',
56
+                                       self.session.vim,
57
+                                       vim_type,
58
+                                       const.VIM_MAX_OBJETS,
59
+                                       **kwargs)
60
+
61
+    def get_mob_by_name(self, vim_type, obj_name):
62
+        results = self.get_all_objects_of_type(vim_type)
63
+        while results:
64
+            for _mob in results.objects:
65
+                for prop in _mob.propSet:
66
+                    if prop.name == 'name' and obj_name == prop.val:
67
+                        vim_util.cancel_retrieval(self.session.vim, results)
68
+                        return _mob
69
+            results = vim_util.continue_retrieval(self.session.vim, results)
70
+
71
+    def get_mob_by_mobid(self, vim_type, mob_id):
72
+        results = self.get_all_objects_of_type(vim_type)
73
+        while results:
74
+            for _mob in results.objects:
75
+                if _mob.obj.value == mob_id:
76
+                    vim_util.cancel_retrieval(self.session.vim, results)
77
+                    return _mob
78
+            results = vim_util.continue_retrieval(self.session.vim, results)
79
+
80
+    def get_hosts(self):
81
+        return self.get_all_objects_of_type("HostSystem")
82
+
83
+    def get_all_properties(self, mob):
84
+        return self.session.invoke_api(vim_util,
85
+                                       'get_object_properties',
86
+                                       self.session.vim,
87
+                                       mob.obj,
88
+                                       [])
89
+
90
+    def get_property(self, moref, property_name):
91
+        return self.session.invoke_api(vim_util, 'get_object_property',
92
+                                       self.session.vim,
93
+                                       moref,
94
+                                       property_name
95
+                                       )
96
+
97
+    @staticmethod
98
+    def make_moref(value, type_):
99
+        return vim_util.get_moref(value, type_)

Loading…
Cancel
Save