Skip to content

Commit

Permalink
complete tag samples
Browse files Browse the repository at this point in the history
  • Loading branch information
Parvin Taheri committed Oct 11, 2017
1 parent 2446865 commit bb86cb6
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 11 deletions.
83 changes: 83 additions & 0 deletions samples/aci-delete-epgs-tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
################################################################################
# _ ____ ___ _____ _ _ _ _ #
# / \ / ___|_ _| |_ _|__ ___ | | | _(_) |_ #
# / _ \| | | | | |/ _ \ / _ \| | |/ / | __| #
# / ___ \ |___ | | | | (_) | (_) | | <| | |_ #
# ____ /_/ \_\____|___|___|_|\___/ \___/|_|_|\_\_|\__| #
# / ___|___ __| | ___ / ___| __ _ _ __ ___ _ __ | | ___ ___ #
# | | / _ \ / _` |/ _ \ \___ \ / _` | '_ ` _ \| '_ \| |/ _ \/ __| #
# | |__| (_) | (_| | __/ ___) | (_| | | | | | | |_) | | __/\__ \ #
# \____\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|\___||___/ #
# |_| #
################################################################################
# #
# Copyright (c) 2015 Cisco Systems #
# All Rights Reserved. #
# #
# Licensed 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. #
# #
################################################################################
"""
delete a tag from epgs of a tenant or all tenants
"""


import acitoolkit.acitoolkit as aci


def main():

# Get the APIC login credentials
description = 'testing tags'
creds = aci.Credentials('apic', description)
creds.add_argument('--tenant', help='delete this tag from epgs of the given tenant')
creds.add_argument('--tag', help='the tag to be deleted')
args = creds.get()

if not args.tag:
print("please pass tag argument")
return
# Login to APIC and push the config
session = aci.Session(args.url, args.login, args.password)
resp = session.login()
if not resp.ok:
print('%% Could not login to APIC')
return
# Get tenants from the APIC
if args.tenant:
tenants = aci.Tenant.get_deep(session, limit_to=['fvAp','fvAEPg','tagInst'], names=[args.tenant])
else:
# Get all Tenants within APIC
tenants = aci.Tenant.get(session)
names_tenants = [tenant.name for tenant in tenants]
tenants = aci.Tenant.get_deep(session, limit_to=['fvAp','fvAEPg','tagInst'], names=names_tenants)
# get all EPGs with their tag
for tenant in tenants:
apps = tenant.get_children(only_class=aci.AppProfile)
for app in apps:
epgs = app.get_children(only_class=aci.EPG)
for epg in epgs:
epg.delete_tag(args.tag)
resp = tenant.push_to_apic(session)
if resp.ok:
print ('Success')

# Print what was sent
print ('Pushed the following JSON to the APIC')
print ('URL:', tenant.get_url())
print ('JSON:', tenant.get_json())

if __name__ == '__main__':
main()


79 changes: 79 additions & 0 deletions samples/aci-delete-tenants-tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
################################################################################
# _ ____ ___ _____ _ _ _ _ #
# / \ / ___|_ _| |_ _|__ ___ | | | _(_) |_ #
# / _ \| | | | | |/ _ \ / _ \| | |/ / | __| #
# / ___ \ |___ | | | | (_) | (_) | | <| | |_ #
# ____ /_/ \_\____|___|___|_|\___/ \___/|_|_|\_\_|\__| #
# / ___|___ __| | ___ / ___| __ _ _ __ ___ _ __ | | ___ ___ #
# | | / _ \ / _` |/ _ \ \___ \ / _` | '_ ` _ \| '_ \| |/ _ \/ __| #
# | |__| (_) | (_| | __/ ___) | (_| | | | | | | |_) | | __/\__ \ #
# \____\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|\___||___/ #
# |_| #
################################################################################
# #
# Copyright (c) 2015 Cisco Systems #
# All Rights Reserved. #
# #
# Licensed 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. #
# #
################################################################################
"""
delete a tag from a tenant or all tenants
"""


import acitoolkit.acitoolkit as aci


def main():

# Get the APIC login credentials
description = 'testing tags'
creds = aci.Credentials('apic', description)
creds.add_argument('--tenant', help='delete this tag from the given tenant')
creds.add_argument('--tag', help='the tag to be deleted')
args = creds.get()

if not args.tag:
print("please pass tag argument")
return
# Login to APIC and push the config
session = aci.Session(args.url, args.login, args.password)
resp = session.login()
if not resp.ok:
print('%% Could not login to APIC')
return
# Get tenants from the APIC
if args.tenant:
tenants = aci.Tenant.get_deep(session, limit_to=['tagInst'], names=[args.tenant])
else:
# Get all Tenants within APIC
tenants = aci.Tenant.get(session)
names_tenants = [tenant.name for tenant in tenants]
tenants = aci.Tenant.get_deep(session, limit_to=['tagInst'], names=names_tenants)
# get all EPGs with their tag
for tenant in tenants:
tenant.delete_tag(args.tag)
resp = tenant.push_to_apic(session)
if resp.ok:
print ('Success')

# Print what was sent
print ('Pushed the following JSON to the APIC')
print ('URL:', tenant.get_url())
print ('JSON:', tenant.get_json())

if __name__ == '__main__':
main()


12 changes: 3 additions & 9 deletions samples/aci-show-epgs-tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,14 @@ def main():
data = []
for tenant in tenants:
apps = aci.AppProfile.get(session, tenant)


for app in apps:
tag_list = aci.Tag.get(session, parent=app, tenant=tenant)
tag_list = [tag.name for tag in tag_list]
print(app.name)
print(tag_list)
epgs = aci.EPG.get(session, app, tenant)
#print(len(epgs))
for epg in epgs:
tag_list = aci.Tag.get(session, parent=epg, tenant=tenant)
if len(tag_list):
tag_list = [tag.name for tag in tag_list if tag._deleted is False]
data.append((tenant.name, app.name, epg.name, ",".join(tag_list)))
tag_list = [tag.name for tag in tag_list]
if len(tag_list):
data.append((tenant.name, app.name, epg.name, ",".join(tag_list)))

template = "{0:20} {1:20} {2:20} {3:20}"
if len(data):
Expand Down
5 changes: 3 additions & 2 deletions samples/aci-show-tenants-tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ def main():
tenants = aci.Tenant.get(session)

names_tenants = [tenant.name for tenant in tenants]
tenants_with_tag = aci.Tenant.get_deep(session, limit_to=['tagInst'], names=[names_tenants])
tenants_with_tag = aci.Tenant.get_deep(session, limit_to=['tagInst'], names=names_tenants)

data = []
for tenant in tenants_with_tag:
if tenant.has_tags():
tag_list = [tag.name for tag in tenant.get_tags()]
data.append((tenant.name,",".join(tag_list)))
if len(tag_list):
data.append((tenant.name,",".join(tag_list)))

template = "{0:20} {1:20}"
if len(data):
Expand Down

0 comments on commit bb86cb6

Please sign in to comment.