Skip to content

Commit

Permalink
Cz 95 connect dhcp to tier1 router (#4)
Browse files Browse the repository at this point in the history
* CZ-95 Connect tier1 to dhcp server

* CZ-95 Update version of nsxt plugin to 0.2.0

* CZ-95 Update blueprint and fix issue with import cloudify context

* CZ-95 Handle dhcp deletion when it has a connection to tier 1 gateway

* CZ-95 Use update API instead of patch for tier 1 resource

* CZ-95 Wait for segment ports to get released before delete segment object

* CZ-95 Update segment deletion process
  • Loading branch information
mabuaisha authored Aug 19, 2020
1 parent df8698b commit 41d2ad2
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 23 deletions.
4 changes: 4 additions & 0 deletions examples/blueprint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ inputs:
password:
type: string

tier1_gateway_id:
type: string

dhcp_server_config:
type: dict

Expand Down Expand Up @@ -69,6 +72,7 @@ node_templates:
type: cloudify.nodes.nsx-t.DhcpServerConfig
properties:
client_config: *client_config
tier1_gateway_id: { get_input: tier1_gateway_id }
resource_config: { get_input: dhcp_server_config }

segment:
Expand Down
2 changes: 2 additions & 0 deletions examples/inputs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ port: -YOUR_PORT-
username: -YOUR_USERNAME-
password: -YOUR_PASSWORD-

tier1_gateway_id: test-tier1

dhcp_server_config:
id: test_dhcp_server
display_name: Test DHCP Server
Expand Down
61 changes: 60 additions & 1 deletion nsx_t_plugin/dhcp_server/dhcp_server_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,75 @@
# * See the License for the specific language governing permissions and
# * limitations under the License.

from cloudify import ctx

from nsx_t_plugin.decorators import with_nsx_t_client
from nsx_t_sdk.resources import DhcpServerConfig
from nsx_t_sdk.resources import DhcpServerConfig, Tier1


def _update_tier_1_gateway(client_config, tier1_gateway_id, dhcp_server_paths):
tier1 = Tier1(
client_config=client_config,
resource_config={
'id': tier1_gateway_id
},
logger=ctx.logger
)
tier1_object = tier1.get()
tier1_object.dhcp_config_paths = dhcp_server_paths
tier1.update(tier1_object)


def _link_dhcp_server_to_tier_1(client_config, tier1_gateway_id):
_update_tier_1_gateway(
client_config,
tier1_gateway_id,
[ctx.instance.runtime_properties['path']]
)


def _unlink_dhcp_server_from_tier_1(client_config, tier1_gateway_id):
_update_tier_1_gateway(
client_config,
tier1_gateway_id,
[]
)


@with_nsx_t_client(DhcpServerConfig)
def create(nsx_t_resource):
resource = nsx_t_resource.create()

# Update the resource_id with the new "id" returned from API
nsx_t_resource.resource_id = resource.id

# Save path as runtime property to use it later on
ctx.instance.runtime_properties['path'] = resource.path


@with_nsx_t_client(DhcpServerConfig)
def configure(nsx_t_resource):
tier1_gateway_id = ctx.node.properties.get('tier1_gateway_id')
if tier1_gateway_id:
_link_dhcp_server_to_tier_1(
nsx_t_resource.client_config,
tier1_gateway_id
)
else:
ctx.logger.debug('No Tier1 gateway to attach DHCP Server to')


@with_nsx_t_client(DhcpServerConfig)
def stop(nsx_t_resource):
tier1_gateway_id = ctx.node.properties.get('tier1_gateway_id')
if tier1_gateway_id:
_unlink_dhcp_server_from_tier_1(
nsx_t_resource.client_config,
tier1_gateway_id
)
else:
ctx.logger.debug('No Tier1 gateway to detach DHCP Server from')


@with_nsx_t_client(DhcpServerConfig)
def delete(nsx_t_resource):
Expand Down
59 changes: 47 additions & 12 deletions nsx_t_plugin/segment/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
from cloudify.exceptions import OperationRetry, NonRecoverableError

from nsx_t_plugin.decorators import with_nsx_t_client
from nsx_t_sdk.resources import Segment, SegmentState
from nsx_t_sdk.resources import (
Segment,
SegmentState,
SegmentPort
)
from nsx_t_sdk.exceptions import NSXTSDKException

SEGMENT_TASK_DELETE = 'segment_delete_task'
Expand Down Expand Up @@ -63,6 +67,26 @@ def start(nsx_t_resource):
)


@with_nsx_t_client(Segment)
def stop(nsx_t_resource):
segment_port = SegmentPort(
client_config=nsx_t_resource.client_config,
logger=ctx.logger,
resource_config={}
)
for nsx_t_port in segment_port.list(
filters={
'segment_id': nsx_t_resource.resource_id
}
):
port = SegmentPort(
client_config=nsx_t_resource.client_config,
logger=ctx.logger,
resource_config={'id': nsx_t_port['id']}
)
port.delete((nsx_t_resource.resource_id,))


@with_nsx_t_client(Segment)
def delete(nsx_t_resource):
try:
Expand All @@ -73,15 +97,26 @@ def delete(nsx_t_resource):
return

if SEGMENT_TASK_DELETE not in ctx.instance.runtime_properties:
nsx_t_resource.delete()
ctx.instance.runtime_properties[SEGMENT_TASK_DELETE] = True

ctx.logger.info(
'Waiting for segment "{0}" to be deleted'.format(
nsx_t_resource.resource_id,
)
)
raise OperationRetry(
message='Segment {0} not deleted yet.'
try:
nsx_t_resource.delete()
except NSXTSDKException:
ctx.logger.info(
'Segment {0} cannot be deleted now, try again'
''.format(nsx_t_resource.resource_id)
)
)
raise OperationRetry(
message='Segment {0} deletion is in progress.'
''.format(nsx_t_resource.resource_id)
)
else:
ctx.instance.runtime_properties[SEGMENT_TASK_DELETE] = True
else:
ctx.logger.info(
'Waiting for segment "{0}" to be deleted'.format(
nsx_t_resource.resource_id,
)
)
raise OperationRetry(
message='Segment {0} deletion is in progress.'
''.format(nsx_t_resource.resource_id)
)
21 changes: 18 additions & 3 deletions nsx_t_sdk/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

ACTION_CREATE = 'create'
ACTION_UPDATE = 'update'
ACTION_PATCH = 'patch'
ACTION_DELETE = 'delete'
ACTION_GET = 'get'
ACTION_LIST = 'list'
Expand All @@ -50,6 +51,7 @@ class NSXTResource(object):
allow_get = True
allow_list = True
allow_update = True
allow_patch = True

def __init__(self, client_config, resource_config, logger):
self.client_config = client_config
Expand Down Expand Up @@ -193,9 +195,21 @@ def update(self, new_config=None):
(self.resource_id, new_config,),
)

def delete(self):
def patch(self, new_config=None):
self._validate_allowed_method(self.allow_patch, ACTION_PATCH)
return self._invoke(
ACTION_PATCH,
(self.resource_id, new_config,),
)

def delete(self, extra_params=None):
extra_params = extra_params or ()
self._validate_allowed_method(self.allow_delete, ACTION_DELETE)
return self._invoke(ACTION_DELETE, (self.resource_id,))
params = (self.resource_id,)
if extra_params:
params += extra_params

return self._invoke(ACTION_DELETE, params)

def get(self):
self._validate_allowed_method(self.allow_get, ACTION_GET)
Expand All @@ -218,4 +232,5 @@ def list(self,
}
if filters:
params.update(filters)
return self._invoke(ACTION_LIST, kwargs=params)
results = self._invoke(ACTION_LIST, kwargs=params).to_dict()
return results['results'] if results.get('results') else []
28 changes: 24 additions & 4 deletions nsx_t_sdk/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ class Segment(NSXTResource):
service_name = 'Segments'


class DhcpServerConfig(NSXTResource):
client_type = 'nsx_infra'
resource_type = 'DhcpServerConfig'
service_name = 'DhcpServerConfigs'
class SegmentPort(NSXTResource):
client_type = 'segment'
resource_type = 'Port'
service_name = 'Ports'

allow_create = False
allow_delete = True
allow_get = True
allow_list = True
allow_update = False
allow_patch = False


class SegmentState(NSXTResource):
Expand All @@ -38,3 +45,16 @@ class SegmentState(NSXTResource):
allow_get = True
allow_list = False
allow_update = False
allow_patch = False


class DhcpServerConfig(NSXTResource):
client_type = 'nsx_infra'
resource_type = 'DhcpServerConfig'
service_name = 'DhcpServerConfigs'


class Tier1(NSXTResource):
client_type = 'nsx_infra'
resource_type = 'Tier1'
service_name = 'Tier1s'
14 changes: 12 additions & 2 deletions plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
plugins:
nsx-t:
package_name: cloudify-nsx-t-plugin
source: https://github.com/Cloudify-PS/cloudify-nsx-t-plugin/archive/0.1.0.zip
package_version: '0.1.0'
source: https://github.com/Cloudify-PS/cloudify-nsx-t-plugin/archive/0.2.0.zip
package_version: '0.2.0'
executor: central_deployment_agent

dsl_definitions:
Expand Down Expand Up @@ -424,13 +424,19 @@ node_types:
implementation: nsx-t.nsx_t_plugin.segment.segment.create
start:
implementation: nsx-t.nsx_t_plugin.segment.segment.start
stop:
implementation: nsx-t.nsx_t_plugin.segment.segment.stop
delete:
implementation: nsx-t.nsx_t_plugin.segment.segment.delete

cloudify.nodes.nsx-t.DhcpServerConfig:
derived_from: cloudify.nodes.Root
properties:
<<: *client_config
tier1_gateway_id:
type: string
required: false
description: The ID of the Tier 1 Gateway to connect DHCP to
resource_config:
type: cloudify.types.nsx-t.DhcpServerConfig
required: true
Expand All @@ -439,6 +445,10 @@ node_types:
cloudify.interfaces.lifecycle:
create:
implementation: nsx-t.nsx_t_plugin.dhcp_server.dhcp_server_config.create
configure:
implementation: nsx-t.nsx_t_plugin.dhcp_server.dhcp_server_config.configure
stop:
implementation: nsx-t.nsx_t_plugin.dhcp_server.dhcp_server_config.stop
delete:
implementation: nsx-t.nsx_t_plugin.dhcp_server.dhcp_server_config.delete

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

setup(
name='cloudify-nsx-t-plugin',
version='0.1.0',
version='0.2.0',
author='Cloudify Platform Ltd.',
author_email='[email protected]',
license='LICENSE',
Expand Down

0 comments on commit 41d2ad2

Please sign in to comment.