Skip to content

Commit

Permalink
Network policy attach heat resource enhancements
Browse files Browse the repository at this point in the history
1. Make this resource as ContrailResource
2. Use ref-update api to ensure that only new policy add/delete operation is
   performed.
3. Define new parameter which defines the order in which new policy is inserted

Change-Id: I92449b974eedcf60ed016fc2f052f41aa09f8ea8
Closes-bug: #1507501
(cherry picked from commit b0ea746)
  • Loading branch information
bailkeri committed Nov 13, 2015
1 parent 57d8d35 commit 9213019
Showing 1 changed file with 61 additions and 34 deletions.
95 changes: 61 additions & 34 deletions contrail_heat/resources/attach_policy.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
from heat.common import exception
from heat.engine.resources.neutron import neutron
from heat.engine import properties

from neutronclient.common.exceptions import NeutronClientException
from neutronclient.neutron import v2_0 as neutronV20
from vnc_api import vnc_api
from contrail_heat.resources.contrail import ContrailResource

from heat.openstack.common import log as logging

logger = logging.getLogger(__name__)


class AttachPolicy(neutron.NeutronResource):
class AttachPolicy(ContrailResource):

PROPERTIES = (
NETWORK, POLICY,
NETWORK, POLICY, SEQUENCE
) = (
'network', 'policy', 'sequence'
)

_SEQUENCE_KEYS = (
MAJOR, MINOR
) = (
'network', 'policy',
'major', 'minor'
)

properties_schema = {
Expand All @@ -24,44 +30,65 @@ class AttachPolicy(neutron.NeutronResource):
required=True),
POLICY: properties.Schema(
properties.Schema.STRING,
description=_('policy name FQ name notation'),
description=_('The policy id or fq_name_string'),
required=True),
SEQUENCE: properties.Schema(
properties.Schema.MAP,
_('Order of the policy'),
schema={
MAJOR: properties.Schema(
properties.Schema.INTEGER,
_('Major number to define the order of this policy'),
default=0,
),
MINOR: properties.Schema(
properties.Schema.INTEGER,
_('Minor number to define the order of this policy'),
default=0,
)
}
),
}

def handle_create(self):
if not ":" in self.properties.get(self.NETWORK):
network = self.properties.get(self.NETWORK)
try:
vn_obj = self.vnc_lib().virtual_network_read(
id=self.properties.get(self.NETWORK))
except vnc_api.NoIdError:
vn_obj = self.vnc_lib().virtual_network_read(
fq_name_str=self.properties.get(self.NETWORK))

try:
policy_obj = self.vnc_lib().network_policy_read(
id=self.properties.get(self.POLICY))
except vnc_api.NoIdError:
policy_obj = self.vnc_lib().network_policy_read(
fq_name_str=self.properties.get(self.POLICY))

if self.properties[self.SEQUENCE] is None:
major = 0
minor = 0
else:
network = self.properties.get(self.NETWORK).split(":")[2]
network_id = neutronV20.find_resourceid_by_name_or_id(
self.neutron(), 'network', network)

policies = self.neutron().show_network(network_id).get('network').get('contrail:policys')
if not policies:
policies = []
policies.append(self.properties.get(self.POLICY).split(':'))
self.neutron().update_network(network_id, {'network':
{'contrail:policys': policies}})
self.resource_id_set(
'%s|%s' % (network_id, self.properties.get(self.POLICY)))
major = self.properties[self.SEQUENCE][self.MAJOR]
minor = self.properties[self.SEQUENCE][self.MINOR]

policy_order = vnc_api.VirtualNetworkPolicyType(vnc_api.SequenceType(major, minor))

self.vnc_lib().ref_update('virtual-network', vn_obj.uuid,
'network-policy', policy_obj.uuid, None, 'ADD', policy_order)

self.resource_id_set('%s|%s' % (vn_obj.uuid, policy_obj.uuid))

def handle_delete(self):
if not self.resource_id:
return
(network_id, policy) = self.resource_id.split('|')
(network_id, policy_id) = self.resource_id.split('|')
try:
policies = self.neutron().show_network(
network_id).get('network').get('contrail:policys', [])
try:
policies.remove(policy.split(':'))
except ValueError:
return
self.neutron().update_network(network_id, {'network':
{'contrail:policys': policies}})
except NeutronClientException as ex:
if ex.status_code != 404:
raise ex

self.vnc_lib().ref_update('virtual-network', network_id,
'network-policy', policy_id, None, 'DELETE')
except Exception as ex:
self._ignore_not_found(ex)
LOG.warn(_("Virtual Network %s already deleted.") % network_id)

def resource_mapping():
return {
Expand Down

0 comments on commit 9213019

Please sign in to comment.