Skip to content

Commit

Permalink
add-plugin_1_5 (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadiesa-abu authored Sep 27, 2023
1 parent a0dd4f5 commit 7437ae0
Show file tree
Hide file tree
Showing 20 changed files with 1,663 additions and 63 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
2.20.8:
- add plugin_1_5.yaml and handle ctx.plugin properties.
2.20.7:
- Fix Extra_config when creating new server.
- Make cpu/memory hot_add/remove configurable.
Expand Down
2 changes: 1 addition & 1 deletion cloudify_vsphere/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '2.20.7'
version = '2.20.8'
14 changes: 12 additions & 2 deletions cloudify_vsphere/contentlibrary/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@

# This package imports
from vsphere_plugin_common import with_server_client
from vsphere_plugin_common.utils import op, is_node_deprecated
from vsphere_plugin_common.utils import (
op,
is_node_deprecated,
get_plugin_properties
)
from vsphere_plugin_common import (
remove_runtime_properties,
)
Expand All @@ -48,7 +52,13 @@ def create(ctx, connection_config, library_name, template_name, target,
vm_name=repr(runtime_properties.get(CONTENT_LIBRARY_VM_NAME))))
return

content = ContentLibrary(connection_config)
vsphere_config = get_plugin_properties(
getattr(ctx.plugin, 'properties', {}))
connection_config = ctx.node.properties['connection_config']
if connection_config:
vsphere_config.update(connection_config)

content = ContentLibrary(vsphere_config)
library = content.content_library_get(library_name)
content_library_id = library["id"]
runtime_properties[CONTENT_LIBRARY_ID] = content_library_id
Expand Down
15 changes: 13 additions & 2 deletions cloudify_vsphere/contentlibrary/tests/test_content_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import unittest

from mock import Mock, patch
from mock import Mock, patch, MagicMock
from pyVmomi import vim

from cloudify.state import current_ctx
Expand All @@ -24,11 +24,22 @@
import cloudify_vsphere.contentlibrary.deployment as deployment


class SpecialMockCloudifyContext(MockCloudifyContext):

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._plugin = MagicMock(properties={})

@property
def plugin(self):
return self._plugin


class ContentDeploymentTest(unittest.TestCase):

def setUp(self):
super(ContentDeploymentTest, self).setUp()
self.mock_ctx = MockCloudifyContext(
self.mock_ctx = SpecialMockCloudifyContext(
'node_name',
properties={
"connection_config": {
Expand Down
103 changes: 78 additions & 25 deletions cloudify_vsphere/devices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
from vsphere_plugin_common.utils import (
op,
find_rels_by_type,
is_node_deprecated)
is_node_deprecated,
get_plugin_properties)
from vsphere_plugin_common.clients.server import (
ServerClient,
set_boot_order)
Expand Down Expand Up @@ -115,8 +116,14 @@ def attach_scsi_controller(ctx, **kwargs):
ctx.logger.debug("Source {0}".format(repr(scsi_properties)))
ctx.logger.debug("Target {0}".format(repr(hostvm_properties)))

vsphere_config = get_plugin_properties(
getattr(ctx.plugin, 'properties', {}))
connection_config = ctx.source.node.properties.get('connection_config')
if connection_config:
vsphere_config.update(connection_config)

cl = ControllerClient()
cl.get(config=ctx.source.node.properties.get("connection_config"))
cl.get(config=vsphere_config)

run_deferred_task(cl, ctx.source.instance)

Expand All @@ -142,8 +149,14 @@ def attach_ethernet_card(ctx, **kwargs):
ctx.logger.info("Controller attached with {buskey} key.".format(
buskey=ctx.source.instance.runtime_properties['busKey']))
return
vsphere_config = get_plugin_properties(
getattr(ctx.plugin, 'properties', {}))
connection_config = ctx.source.node.properties.get('connection_config')
if connection_config:
vsphere_config.update(connection_config)

attachment = _attach_ethernet_card(
ctx.source.node.properties.get("connection_config"),
vsphere_config,
ctx.target.instance.runtime_properties.get(VSPHERE_SERVER_ID),
controller_without_connected_networks(
ctx.source.instance.runtime_properties),
Expand All @@ -162,17 +175,27 @@ def attach_server_to_ethernet_card(ctx, **kwargs):
if ctx.target.instance.id not in \
ctx.source.instance.runtime_properties.get(
VSPHERE_SERVER_CONNECTED_NICS, []):
vsphere_config = get_plugin_properties(
getattr(ctx.plugin, 'properties', {}))
connection_config = ctx.target.node.properties.get('connection_config')
if connection_config:
vsphere_config.update(connection_config)
attachment = _attach_ethernet_card(
ctx.target.node.properties.get("connection_config"),
vsphere_config,
ctx.source.instance.runtime_properties.get(VSPHERE_SERVER_ID),
controller_without_connected_networks(
ctx.target.instance.runtime_properties),
instance=ctx.target.instance)
ctx.target.instance.runtime_properties.update(attachment)
ctx.target.instance.runtime_properties.dirty = True
ctx.target.instance.update()
vsphere_config = get_plugin_properties(
getattr(ctx.plugin, 'properties', {}))
connection_config = ctx.source.node.properties.get('connection_config')
if connection_config:
vsphere_config.update(connection_config)
ip = _get_card_ip(
ctx.source.node.properties.get("connection_config"),
vsphere_config,
ctx.source.instance.runtime_properties.get(VSPHERE_SERVER_ID),
ctx.target.instance.runtime_properties.get('name'))
ctx.source.instance.runtime_properties[IP] = ip
Expand All @@ -185,8 +208,13 @@ def detach_controller(ctx, **kwargs):
if 'busKey' not in ctx.source.instance.runtime_properties:
ctx.logger.info("Controller was not attached, skipping.")
return
vsphere_config = get_plugin_properties(
getattr(ctx.plugin, 'properties', {}))
connection_config = ctx.source.node.properties.get('connection_config')
if connection_config:
vsphere_config.update(connection_config)
_detach_controller(
ctx.source.node.properties.get("connection_config"),
vsphere_config,
ctx.target.instance.runtime_properties.get(VSPHERE_SERVER_ID),
ctx.source.instance.runtime_properties.get('busKey'),
instance=ctx.source.instance)
Expand All @@ -202,8 +230,13 @@ def detach_server_from_controller(ctx, **kwargs):
if 'busKey' not in ctx.target.instance.runtime_properties:
ctx.logger.info("Controller was not attached, skipping.")
return
vsphere_config = get_plugin_properties(
getattr(ctx.plugin, 'properties', {}))
connection_config = ctx.target.node.properties.get('connection_config')
if connection_config:
vsphere_config.update(connection_config)
_detach_controller(
ctx.target.node.properties.get("connection_config"),
vsphere_config,
ctx.source.instance.runtime_properties.get(VSPHERE_SERVER_ID),
ctx.target.instance.runtime_properties.get('busKey'),
instance=ctx.target.instance)
Expand Down Expand Up @@ -300,11 +333,16 @@ def attach_usb_device(ctx, **kwargs):
return
vsphere_server_id = ctx.target.instance.runtime_properties.get(
'vsphere_server_id')
connection_config_props = ctx.source.node.properties.get(
'connection_config')

vsphere_config = get_plugin_properties(
getattr(ctx.plugin, 'properties', {}))
connection_config = ctx.source.node.properties.get('connection_config')
if connection_config:
vsphere_config.update(connection_config)

device_name_from_props = ctx.source.node.properties.get('device_name')
cl = ServerClient()
cl.get(config=connection_config_props)
cl.get(config=vsphere_config)
vm = cl._get_obj_by_id(vim.VirtualMachine,
vsphere_server_id)
device_name = get_usb_physical_path(cl.si.content,
Expand Down Expand Up @@ -357,11 +395,14 @@ def attach_usb_device(ctx, **kwargs):
def detach_usb_device(ctx, **kwargs):
vsphere_server_id = ctx.target.instance.runtime_properties.get(
'vsphere_server_id')
connection_config_props = ctx.source.node.properties.get(
'connection_config')
vsphere_config = get_plugin_properties(
getattr(ctx.plugin, 'properties', {}))
connection_config = ctx.source.node.properties.get('connection_config')
if connection_config:
vsphere_config.update(connection_config)
device_name_from_props = ctx.source.node.properties.get('device_name')
cl = ServerClient()
cl.get(config=connection_config_props)
cl.get(config=vsphere_config)
vm = cl._get_obj_by_id(vim.VirtualMachine,
vsphere_server_id)
usb_device = None
Expand Down Expand Up @@ -389,11 +430,14 @@ def attach_serial_port(ctx, **kwargs):
return
vsphere_server_id = ctx.target.instance.runtime_properties.get(
'vsphere_server_id')
connection_config_props = ctx.source.node.properties.get(
'connection_config')
vsphere_config = get_plugin_properties(
getattr(ctx.plugin, 'properties', {}))
connection_config = ctx.source.node.properties.get('connection_config')
if connection_config:
vsphere_config.update(connection_config)
device_name_from_props = ctx.source.node.properties.get('device_name')
cl = ServerClient()
cl.get(config=connection_config_props)
cl.get(config=vsphere_config)
vm = cl._get_obj_by_id(vim.VirtualMachine,
vsphere_server_id)
device_changes = []
Expand Down Expand Up @@ -431,11 +475,14 @@ def attach_serial_port(ctx, **kwargs):
def detach_serial_port(ctx, **kwargs):
vsphere_server_id = ctx.target.instance.runtime_properties.get(
'vsphere_server_id')
connection_config_props = ctx.source.node.properties.get(
'connection_config')
vsphere_config = get_plugin_properties(
getattr(ctx.plugin, 'properties', {}))
connection_config = ctx.source.node.properties.get('connection_config')
if connection_config:
vsphere_config.update(connection_config)
device_name_from_props = ctx.source.node.properties.get('device_name')
cl = ServerClient()
cl.get(config=connection_config_props)
cl.get(config=vsphere_config)
vm = cl._get_obj_by_id(vim.VirtualMachine,
vsphere_server_id)
serial_port = None
Expand Down Expand Up @@ -501,11 +548,14 @@ def attach_pci_device(ctx, **kwargs):
return
vsphere_server_id = ctx.target.instance.runtime_properties.get(
'vsphere_server_id')
connection_config_props = ctx.source.node.properties.get(
'connection_config')
vsphere_config = get_plugin_properties(
getattr(ctx.plugin, 'properties', {}))
connection_config = ctx.source.node.properties.get('connection_config')
if connection_config:
vsphere_config.update(connection_config)
device_name_from_props = ctx.source.node.properties.get('device_name')
cl = ServerClient()
cl.get(config=connection_config_props)
cl.get(config=vsphere_config)
vm = cl._get_obj_by_id(vim.VirtualMachine,
vsphere_server_id)
pci_device = get_pci_device(cl.si.content,
Expand Down Expand Up @@ -556,11 +606,14 @@ def attach_pci_device(ctx, **kwargs):
def detach_pci_device(ctx, **kwargs):
vsphere_server_id = ctx.target.instance.runtime_properties.get(
'vsphere_server_id')
connection_config_props = ctx.source.node.properties.get(
'connection_config')
vsphere_config = get_plugin_properties(
getattr(ctx.plugin, 'properties', {}))
connection_config = ctx.source.node.properties.get('connection_config')
if connection_config:
vsphere_config.update(connection_config)
device_name_from_props = ctx.source.node.properties.get('device_name')
cl = ServerClient()
cl.get(config=connection_config_props)
cl.get(config=vsphere_config)
vm = cl._get_obj_by_id(vim.VirtualMachine,
vsphere_server_id)
pci_details = get_pci_device(cl.si.content,
Expand Down
19 changes: 15 additions & 4 deletions cloudify_vsphere/devices/tests/test_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,25 @@
from cloudify_vsphere import devices


class SpecialMockCloudifyContext(MockCloudifyContext):

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._plugin = MagicMock(properties={})

@property
def plugin(self):
return self._plugin


class VsphereControllerTest(unittest.TestCase):

def tearDown(self):
current_ctx.clear()
super(VsphereControllerTest, self).tearDown()

def _gen_ctx(self):
_ctx = MockCloudifyContext(
_ctx = SpecialMockCloudifyContext(
'node_name',
properties={},
runtime_properties={}
Expand All @@ -45,18 +56,18 @@ def _gen_ctx(self):
return _ctx

def _gen_relation_ctx(self):
_target = MockCloudifyContext(
_target = SpecialMockCloudifyContext(
'target',
properties={},
runtime_properties={}
)
_source = MockCloudifyContext(
_source = SpecialMockCloudifyContext(
'source',
properties={},
runtime_properties={}
)

_ctx = MockCloudifyContext(
_ctx = SpecialMockCloudifyContext(
target=_target,
source=_source
)
Expand Down
13 changes: 10 additions & 3 deletions cloudify_vsphere/ovf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from vsphere_plugin_common.clients.server import (
ServerClient,
get_boot_order_obj)
from vsphere_plugin_common.utils import op
from vsphere_plugin_common.utils import op, get_plugin_properties
from vsphere_plugin_common import (
remove_runtime_properties,
)
Expand Down Expand Up @@ -262,12 +262,19 @@ def create(ctx, connection_config, target, ovf_name, ovf_source,
esxi_node = target.get('host')
vm_folder = target.get('folder')
resource_pool = target.get('resource_pool')

vsphere_config = get_plugin_properties(
getattr(ctx.plugin, 'properties', {}))
connection_config = ctx.node.properties['connection_config']
if connection_config:
vsphere_config.update(connection_config)

client = ServerClient(ctx_logger=ctx.logger).get(
config=connection_config)
config=vsphere_config)
datacenter = client.si.content.rootFolder.childEntity[0]

if not resource_pool:
resource_pool = connection_config.get("resource_pool_name")
resource_pool = vsphere_config.get("resource_pool_name")

resource_pool = get_obj_in_list(resource_pool,
client._get_resource_pools())
Expand Down
15 changes: 13 additions & 2 deletions cloudify_vsphere/resource_pool/tests/test_resource_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import unittest

from mock import Mock, patch
from mock import Mock, patch, MagicMock

from cloudify.state import current_ctx
from cloudify.mocks import MockCloudifyContext
Expand All @@ -24,11 +24,22 @@
from cloudify_vsphere import resource_pool


class SpecialMockCloudifyContext(MockCloudifyContext):

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._plugin = MagicMock(properties={})

@property
def plugin(self):
return self._plugin


class ResourcePoolTest(unittest.TestCase):

def setUp(self):
super(ResourcePoolTest, self).setUp()
self.mock_ctx = MockCloudifyContext(
self.mock_ctx = SpecialMockCloudifyContext(
'node_name',
properties={},
runtime_properties={}
Expand Down
Loading

0 comments on commit 7437ae0

Please sign in to comment.