Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/datacenter/acitoolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Smith committed Feb 7, 2017
2 parents 9147b94 + 776356e commit 5b8cb93
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ FROM ubuntu
MAINTAINER Kevin Corbin, kecorbin@cisco.com
RUN apt-get update
RUN apt-get -y install git python python-pip
RUN apt-get -y install graphviz graphviz-dev pkg-config
WORKDIR /opt
RUN git clone https://github.com/datacenter/acitoolkit
WORKDIR acitoolkit
Expand Down
24 changes: 24 additions & 0 deletions acitoolkit/acibaseobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,30 @@ def _get_url_extension(self):

def __str__(self):
return self.name

@staticmethod
def get_from_json(self, data, parent=None):
"""
returns a Tenant object from a json
"""
for key in data:
if key in self._get_apic_classes():
for children in data[key]['children']:
for child_key in children:
if child_key in self._get_toolkit_to_apic_classmap():
class_name = self._get_toolkit_to_apic_classmap()[child_key]
child_name = children[child_key]['attributes']['name']
object_exist = False
if parent is not None:
class_objects = parent.get_children(class_name)
for class_object in class_objects:
if class_object.name == child_name:
class_object._populate_from_attributes(children[child_key]['attributes'])
object_exist = True
if not object_exist:
child_obj = class_name(child_name,parent=self)
class_name._populate_from_attributes(child_obj,children[child_key]['attributes'])
class_name.get_from_json(child_obj, children, parent=self)

def get_json(self, obj_class, attributes=None,
children=None, get_children=True):
Expand Down
89 changes: 89 additions & 0 deletions acitoolkit/acitoolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,38 @@ def add_static_leaf_binding(self, leaf_id, encap_type, encap_id, encap_mode="reg
}
}
self._leaf_bindings.append(text)

@staticmethod
def get_from_json(self, data, parent=None):
"""
returns a Tenant object from a json
"""
for child in data['fvAEPg']['children']:
if 'fvRsCons' in child:
contract_name = child['fvRsCons']['attributes']['tnVzBrCPName']
contract = Contract(contract_name)
self.consume(contract)
elif 'fvRsProv' in child:
contract_name = child['fvRsProv']['attributes']['tnVzBrCPName']
contract = Contract(contract_name)
self.provide(contract)
elif 'fvRsPathAtt' in child:
vlan = child['fvRsPathAtt']['attributes']['encap']
vlan_intf = L2Interface(name='',encap_type=vlan.split('-')[0],encap_id=vlan.split('-')[1])
self.attach(vlan_intf)
elif 'fvRsBd' in child:
bd_name = child['fvRsBd']['attributes']['tnFvBDName']
if isinstance(parent._parent, Tenant):
bds = parent._parent.get_children(BridgeDomain)
bd_exist = False
for bd in bds:
if bd.name == bd_name:
self.add_bd(bd)
bd_exist = True
if not bd_exist:
bd = BridgeDomain(bd_name,parent=parent._parent)
self.add_bd(bd)
return super(EPG, self).get_from_json(self, data, parent=parent)

# Output
def get_json(self):
Expand Down Expand Up @@ -2592,6 +2624,22 @@ def set_multidestination(self, multidestination):
if multidestination not in valid_multidestination:
raise ValueError('multidestination must be of: %s, %s or %s' % valid_multidestination)
self.multidestination = multidestination

@staticmethod
def get_from_json(self, data, parent=None):
"""
returns a Tenant object from a json
"""
for child in data['fvBD']['children']:
if 'fvRsCtx' in child:
context_name = child['fvRsCtx']['attributes']['tnFvCtxName']
context = Context(context_name, parent=parent)
self.add_context(context)
elif 'fvRsBDToOut' in child:
outside_l3_name = child['fvRsBDToOut']['attributes']['tnL3extOutName']
outside_l3 = OutsideL3(outside_l3_name, parent=parent)
self.add_l3out(outside_l3)
return super(BridgeDomain, self).get_from_json(self, data, parent=parent)

def get_json(self):
"""
Expand Down Expand Up @@ -3932,6 +3980,25 @@ def _get_parent_class():
:returns: class of parent object
"""
return [Contract, Taboo]

@staticmethod
def get_from_json(self, data, parent=None):
"""
returns a Tenant object from a json
"""
if 'vzSubj' in data:
for child in data['vzSubj']['children']:
if 'vzRsSubjFiltAtt' in child:
filter_name = child['vzRsSubjFiltAtt']['attributes']['tnVzFilterName']
filter_obj = Filter(filter_name)
self._add_relation(filter_obj)
elif 'vzTSubj' in data:
for child in data['vzTSubj']['children']:
if 'vzRsDenyRule' in child:
filter_name = child['vzRsDenyRule']['attributes']['tnVzFilterName']
filter_obj = Filter(filter_name)
self._add_relation(filter_obj)
return super(ContractSubject, self).get_from_json(self, data, parent=parent)

def get_json(self):
"""
Expand Down Expand Up @@ -4074,6 +4141,18 @@ def get_deep(cls, full_data, working_data, parent=None, limit_to=(), subtree='fu
for child in filter_data.get('children', ()):
if 'vzEntry' in child:
FilterEntry.create_from_apic_json(child, filt)

@staticmethod
def get_from_json(self, data, parent=None):
"""
returns a Tenant object from a json
"""
for child in data['vzFilter']['children']:
if 'vzEntry' in child:
filterentry_name = child['vzEntry']['attributes']['name']
filterentry = FilterEntry(filterentry_name,parent=self)
filterentry._populate_from_attributes(child['vzEntry']['attributes'])
return super(Filter, self).get_from_json(self, data, parent=parent)

def get_json(self):
"""
Expand Down Expand Up @@ -4176,6 +4255,16 @@ def get_table(taboos, title=''):

result.append(Table(data, headers, title=title + 'Taboo:{0}'.format(taboo.name)))
return result

@classmethod
def _get_toolkit_to_apic_classmap(cls):
"""
Gets the APIC class to an acitoolkit class mapping dictionary
These are the children objects
:returns: dict of APIC class names to acitoolkit classes
"""
return {'vzTSubj': ContractSubject, }


class FilterEntry(BaseACIObject):
Expand Down
31 changes: 31 additions & 0 deletions samples/aci-get-tenantObject-from-json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Simple application that takes a tenant json from a file and returns a tenant object
"""
import acitoolkit.acitoolkit as ACI
import argparse
import json


def main():
"""
Main execution routine
"""
description = 'Simple application that takes a tenant json from a configfile and returns a tenant object.'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('-t', '--tenantname', required = True, help='name of the tenant')
parser.add_argument('-config','--tenantconfigfile', required=True,
help='file containing tenant json')
args = parser.parse_args()

tenantObject = None
if args.tenantconfigfile:
with open(args.tenantconfigfile) as data_file:
tenant_json = json.load(data_file)

tenant = ACI.Tenant(args.tenantname)
ACI.Tenant.get_from_json(tenant,tenant_json,parent=tenant)
print(tenant.get_json())

if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"py-radix",
"jsonschema",
"graphviz",
"pygraphviz",
"ipaddress",
"deepdiff"],
tests_requires=["mock"],
Expand Down

0 comments on commit 5b8cb93

Please sign in to comment.