本文整理汇总了Python中cobra.mit.access.MoDirectory.query方法的典型用法代码示例。如果您正苦于以下问题:Python MoDirectory.query方法的具体用法?Python MoDirectory.query怎么用?Python MoDirectory.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cobra.mit.access.MoDirectory
的用法示例。
在下文中一共展示了MoDirectory.query方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from cobra.mit.access import MoDirectory [as 别名]
# 或者: from cobra.mit.access.MoDirectory import query [as 别名]
def main(host, username, password, tenant):
apic = "https://%s" % host
print("Connecting to APIC : %s" % apic)
moDir = MoDirectory(LoginSession(apic, username, password))
moDir.login()
dn_name = "uni/tn-" + tenant
print(dn_name)
dnq = DnQuery(dn_name)
dnq.subtree = 'children'
tenantMO = moDir.query(dnq)
for bdMO in tenantMO.BD:
print("BD NAME => {", bdMO.name, "}")
示例2: test_get_tn
# 需要导入模块: from cobra.mit.access import MoDirectory [as 别名]
# 或者: from cobra.mit.access.MoDirectory import query [as 别名]
def test_get_tn(self, apics, certobject, userobject):
apic = apics[0]
secure = False if apics[1] == 'False' else True
userobject.pkey = certobject.readFile(
fileName=certobject.pkeyfile)
session = CertSession(apic, userobject.certDn, userobject.pkey,
secure=secure, requestFormat='xml')
moDir = MoDirectory(session)
dnQuery = DnQuery('uni/tn-common')
#dnQuery.subtree = "full"
tq = moDir.query(dnQuery)
assert len(tq) == 1
tq = tq[0]
assert str(tq.parentDn) == 'uni'
assert str(tq.dn) == 'uni/tn-common'
示例3: __init__
# 需要导入模块: from cobra.mit.access import MoDirectory [as 别名]
# 或者: from cobra.mit.access.MoDirectory import query [as 别名]
class apic_base:
def __init__(self):
self.session = None
self.moDir = None
self.configReq = None
self.uniMo = None
""" Authentication """
def login(self, url, user, password):
"""
Login to the APIC
:param url:
:param user:
:param password:
:return:
"""
self.session = LoginSession(url, user, password)
self.moDir = MoDirectory(self.session)
self.moDir.login()
self.configReq = ConfigRequest()
self.uniMo = self.moDir.lookupByDn('uni')
def logout(self):
"""
Logout from the APIC
:return:
"""
self.moDir.logout()
""" Commits """
def commit(self, commit_object):
"""
Commits object changes to controller
:param commit_object:
:return:
"""
self.configReq = ConfigRequest()
self.configReq.addMo(commit_object)
self.moDir.commit(self.configReq)
""" Queries """
def query_child_objects(self, dn_query_name):
"""
Retrieve the object using the dn and return all the children under it
:param dn_query_name: dn of the management object
:return:
"""
dn_query = DnQuery(dn_query_name)
dn_query.queryTarget = QUERY_TARGET_CHILDREN
child_mos = self.moDir.query(dn_query)
return child_mos
""" Generic Deletes """
def delete_dn_by_pattern(self, dn_object_list, dn_pattern, recursive):
"""
Travers a dn list and compare each member with a pattern. If there is a match that object will be removed.
If recursive is true, the algorithm will also do a recursive look for the children of each object looking
for the pattern: will stop only when there is no more children to look for.
:param dn_object_list:
:param dn_pattern:
:param recursive:
:return:
"""
for dn_object in dn_object_list:
if dn_pattern in str(dn_object.dn):
try:
self.delete_by_dn(str(dn_object.dn))
except CommitError as e:
print 'Could not delete ' + str(dn_object.dn) + ' -> ' + str(e)
elif recursive:
children = self.query_child_objects(dn_object.dn)
if children is not None:
self.delete_dn_by_pattern(children, dn_pattern, recursive)
def delete_by_dn(self, dn_name):
"""
Retrieve a mo and it removes it from the APIC
:param dn_name:
:return:
"""
dn_object = self.moDir.lookupByDn(dn_name)
if dn_object is not None:
dn_object.delete()
self.commit(dn_object)
""" Tenants """
def create_tenant(self, tenant_name):
"""
Creates a tenant and commit changes to controller
:param tenant_name:
:return:
"""
fv_tenant_mo = Tenant(self.uniMo, tenant_name)
self.commit(fv_tenant_mo)
return fv_tenant_mo
def delete_tenant(self, tenant_dn):
"""
Deletes a tenant and commit changes to controller
#.........这里部分代码省略.........
示例4: LoginSession
# 需要导入模块: from cobra.mit.access import MoDirectory [as 别名]
# 或者: from cobra.mit.access.MoDirectory import query [as 别名]
print 'Usage: apic_show-mac-address.py <hostname> <username> <password> <mac-address>'
sys.exit()
else:
hostname, username, password, macaddress = sys.argv[1:]
url = 'https://' + hostname
print "Logging on APIC..."
try:
#
lls = LoginSession(url, username, password, secure=False)
md = MoDirectory(lls)
md.login()
q = ClassQuery('fvCEp')
q.subtree = 'children'
q.subtreeClassFilter = 'fvRsCEpToPathEp'
mos = md.query(q)
#Other variables
hasmacaddress = False
epglists = {}
i = -1
## Verifying all mac address:
for mo in mos:
for child in mo.rscEpToPathEp:
line = str(child.dn)
i = i + 1
if (macaddress in line):
hasmacaddress = True
epglists[i] = line
示例5: home
# 需要导入模块: from cobra.mit.access import MoDirectory [as 别名]
# 或者: from cobra.mit.access.MoDirectory import query [as 别名]
def home():
with open("/home/app/data/logs.txt", "a") as log_file:
log_file.write("==================================================" + "\n")
log_file.write("Received API Request from Client. Sending Response" + "\n")
log_file.write("==================================================" + "\n")
reply = None
try:
apicUrl = 'https://172.17.0.1/'
loginSession = createCertSession()
#loginSession = cobra.mit.session.LoginSession('https://10.22.47.171', 'admin', 'ins3965!')
moDir = MoDirectory(loginSession)
moDir.login()
tableList = []
#row = ('TN', 'AP/L2OUT', 'EPG/InstP', 'CEP', 'IP', 'Type', 'PATH', 'PORT', 'POD', 'ENCAP', 'BD:CTX')
#tableList.append(row)
try:
row ={}
q = ClassQuery('fvCEp')
q.subtree = 'children'
tenantMo = moDir.query(q)
except cobra.mit.request.QueryError as e:
log('Reason: ' + e.reason)
log('Error: ' + e.error)
log('HTTP code: ' + e.httpCode)
log(traceback.format_exc())
data = {}
for mo in tenantMo:
for child in mo.rscEpToPathEp:
#print child.dn
ip = mo.ip
tn, ap, epg, cep, varPod, varStrPath, varStrPort = tDnToPath(child.dn)
if 'protpaths' in child.tDn: portType = 'vPC'
elif 'paths' in child.tDn and 'eth' in child.tDn: portType = '-'
else: portType = 'PC'
encap = (mo.encap).split('-')[1]
#if args.macSearch: bd,ctx = getAncestorDnStrFromDnString(md, str(mo.dn), 1)
#else: bd='-'; ctx='-'
bd='-'; ctx='-'
#row = (tn,ap,epg,cep,mo.ip,portType,varStrPath,varStrPort,varPod,encap,'%s:%s' %(bd,ctx))
row = {
"tn": tn,
"ap/l2out":ap,
"epg":epg,
"cep":cep,
"ip":mo.ip,
"type":portType,
"path":varStrPath,
"port":varStrPort,
"pod":varPod,
"encap":encap,
"bd":"-:-"
}
tableList.append(row)
#data[child.tDn]= row
moDir.logout()
#print json.dumps(data)
#return render_template('home.html', table=tableList)
#return respFormatJsonMos(data, len(data))
log(tableList)
reply = jsonify({'results': tableList})
except Exception as e:
log(traceback.format_exc())
return reply
示例6: AEPg
# 需要导入模块: from cobra.mit.access import MoDirectory [as 别名]
# 或者: from cobra.mit.access.MoDirectory import query [as 别名]
# Making a new epg and calling it "vmepg"
application_profile = moDir.lookupByDn("uni/tn-{0}/ap-{1}".format(tenant, ap))
new_epg = AEPg(application_profile, "vmepg")
# Committing the changes
config_request = ConfigRequest()
config_request.addMo(new_epg)
moDir.commit(config_request)
# Enter epg to use as template for vmepg
epg_template= "default"
dnQuery = DnQuery('uni/tn-{0}/ap-{1}/epg-{2}'.format(tenant, ap, epg_template))
dnQuery.subtree = 'children'
epgMO = moDir.query(dnQuery)
# Traversing every property within epg_template and copying it to vmepg
for epg in epgMO:
for epgChild in epg.children:
for name, obj in inspect.getmembers(sys.modules[__name__]):
if inspect.isclass(obj):
copy_of_property = str(epgChild.rn)
if (copy_of_property.lower().startswith(obj.__name__.lower())):
exec("object_made = " + obj.__name__ + "(epg, \"" + copy_of_property + "\")")
config_request.addMo(object_made)
moDir.commit(config_request)