Skip to content

Commit

Permalink
Merge branches 'origin/hotfixdelete_template_f5' and 'feature/plugin_…
Browse files Browse the repository at this point in the history
…dell_ftos' into 'master'
  • Loading branch information
henriquebonadio-zz committed Jan 18, 2016
2 parents e205fe1 + 0ea83cf commit 0a85bb3
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 61 deletions.
3 changes: 2 additions & 1 deletion networkapi/api_deploy/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def __applyConfig(equipment,filename, equipment_access=None,source_server=None,p

if source_server == None:
source_server = TFTP_SERVER_ADDR


#TODO: Handle exceptions from the following methods and generate response for the caller
equip_plugin = PluginFactory.factory(equipment)
equip_plugin.connect()
equip_plugin.ensure_privilege_level()
Expand Down
1 change: 0 additions & 1 deletion networkapi/api_network/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ def networkIPv4_deploy(request, network_id):
equipments_id_list = request.DATA.get("equipments", None)

equipment_list = []

if equipments_id_list is not None:
if type(equipments_id_list) is not list:
raise api_exceptions.ValidationException("equipments")
Expand Down
4 changes: 2 additions & 2 deletions networkapi/plugins/Dell/FTOS/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

log = logging.getLogger(__name__)

class IOS(BasePlugin):
class FTOS(BasePlugin):

INVALID_REGEX = '([Ii]nvalid)|overlaps with'

Expand Down Expand Up @@ -67,7 +67,7 @@ def ensure_privilege_level(self, privilege_level=None):
recv = self.waitString(">|#")
self.channel.send("show privilege\n")
recv = self.waitString("Current privilege level is")
level = re.search('Current privilege level is ([0-9]+?).*', recv, re.DOTALL ).group(1)
level = re.search('Current privilege level is ([0-9]+).*', recv, re.DOTALL ).group(1)

level = (level.split(' '))[-1]
if int(level) < privilege_level:
Expand Down
16 changes: 16 additions & 0 deletions networkapi/plugins/Dell/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding:utf-8 -*-

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
59 changes: 39 additions & 20 deletions networkapi/plugins/F5/Generic.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from networkapi.plugins import exceptions as base_exceptions
import logging
from ..base import BasePlugin
import bigsuds
import itertools
import logging


from ..base import BasePlugin
from networkapi.plugins import exceptions as base_exceptions
from networkapi.plugins.F5 import pool, poolmember, util, monitor


log = logging.getLogger(__name__)


Expand Down Expand Up @@ -148,8 +149,8 @@ def updatePool(self, pools):
pl = pool.Pool(self._lb)
mon = monitor.Monitor(self._lb)

# deletes template's association and templates
mon.deleteTemplateAssoc(names=pls['pools_names'])
# get template currents
monitor_associations_old = pl.getMonitorAssociation(names=pls['pools_names'])

# creates templates
monitor_associations = mon.createTemplate(
Expand All @@ -160,7 +161,8 @@ def updatePool(self, pools):
try:
self._lb._channel.System.Session.start_transaction()

# creates template's association
pl.removeMonitorAssociation(names=pls['pools_names'])

pl.setMonitorAssociation(monitor_associations=monitor_associations)

pl.setLbMethod(
Expand Down Expand Up @@ -201,28 +203,45 @@ def updatePool(self, pools):

except Exception, e:
self._lb._channel.System.Session.rollback_transaction()
if monitor_associations != []:
template_names = [m for m in list(itertools.chain(*[m['monitor_rule']['monitor_templates'] for m in monitor_associations])) if 'MONITOR' in m]
if template_names:
mon.deleteTemplate(
template_names=template_names
)

# delete templates created
template_names = [m for m in list(itertools.chain(*[m['monitor_rule']['monitor_templates'] for m in monitor_associations_old])) if 'MONITOR' in m]
if template_names:
mon.deleteTemplate(
template_names=template_names
)
raise base_exceptions.CommandErrorException(e)
else:
self._lb._channel.System.Session.submit_transaction()

@util.connection
def deletePool(self, pools):
log.info('deletePool')

pls = util._trataParam(pools)

pl = pool.Pool(self._lb)
mon = monitor.Monitor(self._lb)

monitor_associations_old = pl.getMonitorAssociation(names=pls['pools_names'])
mon.deleteTemplateAssoc(names=pls['pools_names'])
pl.delete(names=pls['pools_names'])
template_names = [m for m in list(itertools.chain(*[m['monitor_rule']['monitor_templates'] for m in monitor_associations_old])) if 'MONITOR' in m]
if template_names:
mon.deleteTemplate(names=template_names)
self._lb._channel.System.Session.start_transaction()
try:
monitor_associations = pl.getMonitorAssociation(names=pls['pools_names'])
pl.removeMonitorAssociation(names=pls['pools_names'])
except Exception, e:
self._lb._channel.System.Session.rollback_transaction()
raise base_exceptions.CommandErrorException(e)
else:
self._lb._channel.System.Session.submit_transaction()
try:
template_names = [m for m in list(itertools.chain(*[m['monitor_rule']['monitor_templates'] for m in monitor_associations])) if 'MONITOR' in m]
if template_names:
mon.deleteTemplate(template_names=template_names)
except bigsuds.OperationFailed:
pass
finally:
self._lb._channel.System.Session.start_transaction()
try:
pl.delete(names=pls['pools_names'])
self._lb._channel.System.Session.submit_transaction()
except Exception, e:
self._lb._channel.System.Session.rollback_transaction()
raise base_exceptions.CommandErrorException(e)
60 changes: 25 additions & 35 deletions networkapi/plugins/F5/monitor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import bigsuds
import itertools
import logging
import time

from networkapi.plugins import exceptions as base_exceptions
from networkapi.plugins.F5 import lb, types, pool, util
from bigsuds import OperationFailed
from networkapi.plugins.F5 import lb, types, pool

log = logging.getLogger(__name__)

Expand All @@ -27,7 +27,6 @@ def createTemplate(self, **kwargs):
monitor_associations = []

try:
templates_exists = self._lb._channel.LocalLB.Monitor.get_template_list()

for i, pool in enumerate(kwargs['names']):

Expand All @@ -47,9 +46,6 @@ def createTemplate(self, **kwargs):
'template_name': name,
'template_type': types.TemplateType(kwargs['healthcheck'][i]['healthcheck_type'])
}
if util.search_dict(templates_exists, template):
template['template_name'] += str(time.time())
name = template['template_name']

templates.append(template)
template_attributes.append({
Expand Down Expand Up @@ -115,50 +111,44 @@ def createTemplate(self, **kwargs):
log.error(e)
return monitor_associations

def deleteTemplateAssoc(self, **kwargs):
log.info('monitor:deleteTemplateAssoc:%s' % kwargs)
for k, v in kwargs.items():
if v == []:
return

try:
pl = pool.Pool(self._lb)
monitor_associations = pl.getMonitorAssociation(names=kwargs['names'])
# def deleteTemplateAssoc(self, **kwargs):
# log.info('monitor:deleteTemplateAssoc:%s' % kwargs)
# for k, v in kwargs.items():
# if v == []:
# return

self._lb._channel.System.Session.start_transaction()
pl.removeMonitorAssociation(names=kwargs['names'])

except Exception, e:
self._lb._channel.System.Session.rollback_transaction()
raise base_exceptions.CommandErrorException(e)
else:
self._lb._channel.System.Session.submit_transaction()
# self._lb._channel.System.Session.start_transaction()
# try:
# pl = pool.Pool(self._lb)
# monitor_associations = pl.getMonitorAssociation(names=kwargs['names'])

d = pl.get_list()
monitor_associations_all = pl.getMonitorAssociation(names=d)
monitor_associations_all = [m for m in list(itertools.chain(*[m['monitor_rule']['monitor_templates'] for m in monitor_associations_all])) if 'MONITOR' in m]
monitor_associations = [m for m in list(itertools.chain(*[m['monitor_rule']['monitor_templates'] for m in monitor_associations])) if 'MONITOR' in m]
# pl.removeMonitorAssociation(names=kwargs['names'])

template_names = list()
for m in monitor_associations:
if not util.search_dict(monitor_associations_all, m):
template_names.append(m)
# except Exception, e:
# self._lb._channel.System.Session.rollback_transaction()
# raise base_exceptions.CommandErrorException(e)
# else:
# self._lb._channel.System.Session.submit_transaction()

if template_names:
self.deleteTemplate(template_names=template_names)
# try:
# template_names = [m for m in list(itertools.chain(*[m['monitor_rule']['monitor_templates'] for m in monitor_associations])) if 'MONITOR' in m]
# if template_names:
# self.deleteTemplate(template_names=template_names)
# except bigsuds.OperationFailed:
# pass

def deleteTemplate(self, **kwargs):
log.info('monitor:deleteTemplate:%s' % kwargs)
for k, v in kwargs.items():
if v == []:
return
self._lb._channel.System.Session.start_transaction()
try:
self._lb._channel.System.Session.start_transaction()
self._lb._channel.LocalLB.Monitor.delete_template(template_names=kwargs['template_names'])
except Exception:
self._lb._channel.System.Session.rollback_transaction()
else:
self._lb._channel.System.Session.submit_transaction()

def generateName(self, identifier, number=0):
return '/Common/MONITOR_POOL_%s_%s' % (identifier, str(number))
return '/Common/MONITOR_POOL_%s_%s_%s' % (identifier, str(number), str(time.time()))
6 changes: 5 additions & 1 deletion networkapi/plugins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,14 @@ def exec_command(self, command, success_regex='', invalid_regex=None, error_rege
'''
Send single command to equipment and than closes connection channel
'''
if self.channel == None:
log.error("No channel connection to the equipment %s. Was the connect() funcion ever called?" % self.equipment.nome)
raise exceptions.PluginNotConnected()

try:
stdin, stdout, stderr = self.channel.exec_command('%s' % (command))
except Exception, e:
log.error("Error in connection. Cannot send command.", e)
log.error("Error in connection. Cannot send command %s: %s"% (command,e))
raise api_exceptions.NetworkAPIException

equip_output_lines = stdout.readlines()
Expand Down
8 changes: 8 additions & 0 deletions networkapi/plugins/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ class PluginUninstanced(APIException):
def __init__(self, msg=None):
self.detail = u'Plugin uninstanced: <<%s>>' % (msg)

class PluginNotConnected(APIException):

"""Return message error: Plugin uninstanced"""
status_code = status.HTTP_400_BAD_REQUEST
default_detail = u'Plugin not connected'

def __init__(self):
self.detail = u'Plugin not connected'

class NamePropertyInvalid(APIException):

Expand Down
2 changes: 1 addition & 1 deletion networkapi/plugins/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def get_plugin(self, **kwargs):
if re.search('F5', marca.upper(), re.DOTALL):
from .F5.Generic import Generic
return Generic
if re.search('Dell', marca.upper(), re.DOTALL):
if re.search('DELL', marca.upper(), re.DOTALL):
from .Dell.FTOS.plugin import FTOS
return FTOS

Expand Down
1 change: 1 addition & 0 deletions networkapi/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def LOCAL_FILES(path):

# Aplicação rodando em modo Debug
DEBUG = os.getenv('NETWORKAPI_DEBUG', '0') == '1'
DEBUG = 1
CI = os.getenv('CI', '0') == '1'

NETWORKAPI_LOG_FILE=os.getenv('NETWORKAPI_LOG_FILE','/tmp/networkapi.log')
Expand Down

0 comments on commit 0a85bb3

Please sign in to comment.