Request Module¶
The request module handles configuration and queries to the APIC.
You can use the request module to:
- Create or update a managed object (MO)
- Call a method within an MO
- Delete an MO
- Run a query to read the properties and status of an MO or discover objects
Using Queries¶
Queries return information about an MO or MO properties within the APIC management information tree (MIT). You can apply queries that are based on a distinguished name (DN) and MO class.
Specifying a Query Scope¶
You can limit the scope of the response to an API query by applying scoping filters. You can limit the scope to the first level of an object or to one or more of its subtrees or children based on class, properties, categories, or qualification by a logical filter expression. This list describes the available scopes:
- self-(Default) Considers only the MO itself, not children or subtrees.
- children-Considers only the children of the MO, not the MO itself.
- subtree-Considers only the subtrees of the MO, not the MO itself.
Applying Query Filters¶
You can query on a variety of query filters, including:
- MO class
- Property
- Subtree
- Subtree and class
You can also include optional subtree values, including:
- audit-logs
- event-logs
- faults
- fault-records
- health
- health-records
- relations
- stats
- tasks
- count
- no-scoped
- required
Applying Configuration Requests¶
The request module handles configuration requests that are issued by the access module. The ConfigRequest class enables you to:
- Add an MO
- Remove an MO
- Verify if an MO is present in an uncommitted configuration
- Return the root MO for a given object
AbstractRequest¶
Class that represents an abstract request. AbstractQuery and ConfigRequest derive from this class.
-
class
cobra.mit.request.
AbstractRequest
[source]¶ Abstract base class for all other request types.
-
options
¶ The HTTP request query string for this object - readonly
Type: str
-
id
¶ An internal troubleshooting value useful for tracing the processing of a request within the cluster
Type: None or int
-
uriBase
¶ The base URI used to build the URL for queries and requests
Type: str
-
getHeaders
(session, data=None)[source]¶ Get the headers for the session.
The data may be needed if a signature is needed to be calculated for a transaction.
Parameters: - session (cobra.mit.session.AbstractSession) – The session the headers should be for.
- data (str, optional) – The data for the request. The default is None
Returns: A dictionary with the headers for the session.
Return type: dict
-
getUriPathAndOptions
(session)[source]¶ Get the uri path and options.
Returns the full URI path and options portion of the URL that will be used in a query
Parameters: session (cobra.mit.session.AbstractSession) – The session object which contains information needed to build the URI Returns: The URI and options strings Return type: str
-
id
Get the id.
Returns: The id for this request. Return type: str
-
classmethod
makeOptions
(options)[source]¶ Make the request options.
Returns a string containing the concatenated values of all key/value pairs for the options defined in dict options
Parameters: options (list) – A list of options to turn into an option string Returns: The options strings Return type: str
-
options
Get the options.
Returns: - All the options for this abstract request as a string
- joined by &’s.
Return type: str
-
uriBase
Get the base uri.
Returns: A string representing the base URI for this request. Return type: str
-
AbstractQuery¶
Class that represents an abstract query. ClassQuery and DnQuery derive from this class.
-
class
cobra.mit.request.
AbstractQuery
[source]¶ Abstract base class for a query.
-
options
¶ The HTTP request query string for this object - readonly
Type: str
-
propInclude
¶ the current response property include filter. This filter can be used to specify the properties that should be included in the response. Valid values are:
- _all_
- naming-only
- config-explicit
- config-all
- config-only
- oper
Type: str
-
subtreePropFilter
¶ The response subtree filter can be used to limit what is returned in a subtree response by property values
Type: str
-
subtreeClassFilter
¶ The response subtree class filter can be used to filter a subtree response down to one or more classes. Setting this can be done with either a list or a string, the value is always stored as a comma separated string.
Type: str
-
subtreeInclude
¶ The response subtree include filter can be used to limit the response to a specific type of information from the subtree, these include:
- audit-logs
- event-logs
- faults
- fault-records
- health
- health-records
- relations
- stats
- tasks
- count
- no-scoped
- required
Type: str
-
queryTarget
¶ The query target filter can be used to specify what part of the MIT to query. You can query:
- self - The object itself
- children - The children of the object
- subtree - All the objects lower in the heirarchy
Type: str
-
classFilter
¶ The target subtree class filter can be used to specify which subtree class to filter by. You can set this using a list or a string. The value is always stored as a comma separated string.
Type: str
-
propFilter
¶ The query target property filter can be used to limit which objects are returned based on the value that is set in the specific property within those objects.
Type: str
-
subtree
¶ The response subtree filter can be used to define what objects you want in the response. The possible values are:
- no - No subtree requested
- children - Only the children objects
- full - A full subtree
Type: str
-
replica
¶ The replica option can direct a query to a specific replica. The possible values are:
- 1
- 2
- 3
Type: int
-
orderBy
¶ Request that the results be ordered in a certain way. This can be a list of property sort specifiers or a comma separated string. An example sort specifier: ‘aaaUser.name|desc’.
Type: list or str
-
pageSize
¶ Request that the results that are returned are limited to a certain number, the pageSize.
Type: int
-
id
¶ An internal troubleshooting value useful for tracing the processing of a request within the cluster
Type: None or int
-
uriBase
¶ The base URI used to build the URL for queries and requests
Type: str
-
classFilter
Get the class filter.
Returns: The class filter (target-subtree-class) Return type: str
-
options
Get the options.
Returns: - All the options for this abstract query as a string
- joined by &’s.
Return type: str
-
orderBy
Get the orderBy sort specifiers string.
Returns: The order-by string of sort specifiers. Return type: str
-
pageSize
Get the pageSize value.
Returns: The number of results to be returned by a query. Return type: int
-
propFilter
Get the the property filter.
Returns: The property filter (query-target-filter) Return type: str
-
propInclude
Get the property include.
Returns: The property include (rsp-prop-include) value. Return type: str
-
queryTarget
Get the query target.
Returns: The query target (query-target). Return type: str
-
replica
Get the replica.
Returns: The replica option to be set on this query (replica). Return type: int
-
subtree
Get the subtree.
Returns: The subtree specifier (rsp-subtree). Return type: str
-
subtreeClassFilter
Get the the subtree class filter.
Returns: The subtree class filter (rsp-subtree-class) Return type: str
-
subtreeInclude
Get the subtree include.
Returns: The subtree include (rsp-subtree-include) value. Return type: str
-
subtreePropFilter
Get the subtree property filter.
Returns: The subtree property filter (rsp-subtree-filter) value. Return type: str
-
DnQuery¶
Class that creates a query object based on distinguished name (DN).
-
class
cobra.mit.request.
DnQuery
(dn)[source]¶ Query based on distinguished name (Dn).
-
options
¶ The HTTP request query string string for this DnQuery object - readonly
Type: str
-
dnStr
¶ The base dn string for this DnQuery object - readonly
Type: str
-
propInclude
¶ the current response property include filter. This filter can be used to specify the properties that should be included in the response. Valid values are:
- _all_
- naming-only
- config-explicit
- config-all
- config-only
- oper
Type: str
-
subtreePropFilter
¶ The response subtree filter can be used to limit what is returned in a subtree response by property values
Type: str
-
subtreeClassFilter
¶ The response subtree class filter can be used to filter a subtree response down to one or more classes. Setting this can be done with either a list or a string, the value is always stored as a comma separated string.
Type: str
-
subtreeInclude
¶ The response subtree include filter can be used to limit the response to a specific type of information from the subtree, these include:
- audit-logs
- event-logs
- faults
- fault-records
- health
- health-records
- relations
- stats
- tasks
- count
- no-scoped
- required
Type: str
-
queryTarget
¶ The query target filter can be used to specify what part of the MIT to query. You can query:
- self - The object itself
- children - The children of the object
- subtree - All the objects lower in the heirarchy
Type: str
-
classFilter
¶ The target subtree class filter can be used to specify which subtree class to filter by. You can set this using a list or a string. The value is always stored as a comma separated string.
Type: str
-
propFilter
¶ The query target property filter can be used to limit which objects are returned based on the value that is set in the specific property within those objects.
Type: str
-
subtree
¶ The response subtree filter can be used to define what objects you want in the response. The possible values are:
- no - No subtree requested
- children - Only the children objects
- full - A full subtree
Type: str
-
orderBy
¶ Request that the results be ordered in a certain way. This can be a list of property sort specifiers or a comma separated string. An example sort specifier: ‘aaaUser.name|desc’.
Type: list or str
-
pageSize
¶ Request that the results that are returned are limited to a certain number, the pageSize.
Type: int
-
replica
¶ The replica option can direct a query to a specific replica. The possible values are:
- 1
- 2
- 3
Type: int
-
id
¶ An internal troubleshooting value useful for tracing the processing of a request within the cluster
Type: None or int
-
uriBase
¶ The base URI used to build the URL for queries and requests
Type: str
-
__init__
(dn)[source]¶ Initialize a DnQuery object.
Parameters: dn (str or cobra.mit.naming.Dn) – The Dn to query
-
dnStr
Get the dn string.
Returns: The dn string for this dn query. Return type: str
-
getUrl
(session)[source]¶ Get the URL containing all the query options.
Parameters: session (cobra.mit.session.AbstractSession) – The session to use for this query. Returns: The url Return type: str
-
options
Get the options.
Returns: - All the options for this dn queryas a string
- joined by &’s.
Return type: str
-
ClassQuery¶
Class that creates a query object based on object class.
-
class
cobra.mit.request.
ClassQuery
(className)[source]¶ Query based on class name.
-
options
¶ The HTTP request query string string for this DnQuery object - readonly
Type: str
-
className
¶ The className to query for - readonly
Type: str
-
propInclude
¶ the current response property include filter. This filter can be used to specify the properties that should be included in the response. Valid values are:
- _all_
- naming-only
- config-explicit
- config-all
- config-only
- oper
Type: str
-
subtreePropFilter
¶ The response subtree filter can be used to limit what is returned in a subtree response by property values
Type: str
-
subtreeClassFilter
¶ The response subtree class filter can be used to filter a subtree response down to one or more classes. Setting this can be done with either a list or a string, the value is always stored as a comma separated string.
Type: str
-
subtreeInclude
¶ The response subtree include filter can be used to limit the response to a specific type of information from the subtree, these include:
- audit-logs
- event-logs
- faults
- fault-records
- health
- health-records
- relations
- stats
- tasks
- count
- no-scoped
- required
Type: str
-
queryTarget
¶ The query target filter can be used to specify what part of the MIT to query. You can query:
- self - The object itself
- children - The children of the object
- subtree - All the objects lower in the heirarchy
Type: str
-
classFilter
¶ The target subtree class filter can be used to specify which subtree class to filter by. You can set this using a list or a string. The value is always stored as a comma separated string.
Type: str
-
propFilter
¶ The query target property filter can be used to limit which objects are returned based on the value that is set in the specific property within those objects.
Type: str
-
subtree
¶ The response subtree filter can be used to define what objects you want in the response. The possible values are:
- no - No subtree requested
- children - Only the children objects
- full - A full subtree
Type: str
-
orderBy
¶ Request that the results be ordered in a certain way. This can be a list of property sort specifiers or a comma separated string. An example sort specifier: ‘aaaUser.name|desc’.
Type: list or str
-
pageSize
¶ Request that the results that are returned are limited to a certain number, the pageSize.
Type: int
-
replica
¶ The replica option can direct a query to a specific replica. The possible values are:
- 1
- 2
- 3
Type: int
-
id
¶ An internal troubleshooting value useful for tracing the processing of a request within the cluster
Type: None or int
-
uriBase
¶ The base URI used to build the URL for queries and requests
Type: str
-
__init__
(className)[source]¶ Initialize a ClassQuery instance.
Parameters: className (str) – The className to query for
-
className
Get the class name.
Returns: The class name for this class query Return type: str
-
getUrl
(session)[source]¶ Get the URL containing all the query options.
Parameters: session (cobra.mit.session.AbstractSession) – The session to use for this query. Returns: The url Return type: str
-
options
Get the options.
Returns: - All the options for this class query as a string
- joined by &’s.
Return type: str
-
ConfigRequest¶
Class that handles configuration requests. The
cobra.mit.access.MoDirectory.commit()
function uses this class.:
# Import the config request
from cobra.mit.request import ConfigRequest
configReq = ConfigRequest()
-
class
cobra.mit.request.
ConfigRequest
[source]¶ Change the configuration.
cobra.mit.access.MoDirectory.commit()
function uses this class.-
options
¶ The HTTP request query string string for this DnQuery object - readonly
Type: str
-
data
¶ The payload for this request in JSON format - readonly
Type: str
-
xmldata
¶ The payload for this request in XML format - readonly
Type: str
-
subtree
¶ The response subtree filter can be used to define what objects you want in the response. The possible values are:
- no - No subtree requested
- children - Only the children objects
- full - A full subtree
Type: str
-
id
¶ An internal troubleshooting value useful for tracing the processing of a request within the cluster
Type: None or int
-
uriBase
¶ The base URI used to build the URL for queries and requests
Type: str
-
addMo
(mo)[source]¶ Add a managed object (MO) to the configuration request.
- Args
- mo (cobra.mit.mo.Mo): The managed object to add
Raises: ValueError
– If the context root of the MO is not allowed. This can happen if the MO being added does not have a common context root with the MOs that are already added to the configuration request
-
data
Get the data as JSON.
Raises: CommitError
– If no Mo’s have been added to this config request.Returns: The data that will be committed as a JSON string. Return type: str
-
getRootMo
()[source]¶ Get the Root Mo for this configuration request.
Returns: The root Mo for the config request Return type: None or cobra.mit.mo.Mo
-
getUriPathAndOptions
(session)[source]¶ Get the full URI path and options portion of the URL.
Parameters: session (cobra.mit.session.AbstractSession) – The session object which contains information needed to build the URI Returns: The URI and options string Return type: str
-
getUrl
(session)[source]¶ Get the URL containing all the query options.
Parameters: session (cobra.mit.session.AbstractSession) – The session to use for this query. Returns: The url Return type: str
-
hasMo
(dn)[source]¶ Check if the configuration request has a specific MO.
Args: dn (str): The distinguished name of the mo to check
- Returns (bool): True if the MO is in the configuration request,
- otherwise False
-
options
Get the options.
Returns: - All the options for this config request as a string
- joined by &’s.
Return type: str
-
removeMo
(mo)[source]¶ Remove a managed object (MO) from the configuration request.
Parameters: mo (cobra.mit.mo.Mo) – The managed object to add
-
requestargs
(session)[source]¶ Get the arguments to be used by the HTTP request.
- session (cobra.mit.session.AbstractSession): The session to be used to
- build the the request arguments
Returns: The arguments Return type: dict
-
subtree
Get the subtree.
Returns: The subtree specifier. Return type: str
-
xmldata
Get the data as XML.
Raises: CommitError
– If no Mo’s ahve been added to this config request.Returns: The data as a XML string. Return type: str
-
Tag Request¶
Tags can be added to select MOs and become objects of type TagInst contained by that MO. Rather than having to instantiate an object of type tagInst and query for the containing MO, instantiate a tagInst object and add it to the containing MO then commit the whole thing, the REST API offers the ability to add one or more tags to a specific Dn using a specific API call. Cobra utilizes this API call in the TagsRequest class.
Tags can then be used to group or label objects and do quick and easy searches for objects with a specific tag using a normal ClassQuery with a property filter.
Tag queries allow you to provide a Dn and either a list of tags or a string (which should be comma separated in the form: tag1,tag2,tag3) for the add or remove properties. The class then builds the proper REST API queries as needed to add the tag(s) to the MO.
The class can also be used to do tag queries (HTTP GETs) against specific Dn’s using the cobra.mit.access.MoDirectory.query() method with the cobra.mit.request.TagRequest instance provided as the query object.
Example Usage:
>>> from cobra.mit.session import LoginSession
>>> from cobra.mit.access import MoDirectory
>>> from cobra.mit.request import TagsRequest
>>> session = LoginSession('https://192.168.10.10', 'george', 'pa$sW0rd!', secure=False)
>>> modir = MoDirectory(session)
>>> modir.login()
>>> tags = TagsRequest('uni/tn-common/ap-default')
>>> q = modir.query(tags)
>>> print q[0].name
pregnantSnake
>>> tags.remove = "pregnantSnake"
>>> modir.commit(tags)
<Response [200]>
>>> tags.add = ['That','is','1','dead','bird']
>>> modir.commit(tags)
<Response [200]>
>>> tags.add = "" ; tags.remove = []
>>> q = modir.query(tags)
>>> tags.remove = ','.join([rem.name for rem in q])
>>> print tags.remove
u'is,That,dead,bird,1'
>>> print tags.getUrl(session)
https://192.168.10.10/api/tag/mo/uni/tn-common/ap-default.json?remove=bird,1,is,That,dead
>>> modir.commit(tags)
<Response [200]>
>>> modir.query(tags)
[]
>>>
-
class
cobra.mit.request.
TagsRequest
(dn, add=None, remove=None)[source]¶ Hybrid query and request for tags.
This class does both setting of tags (request) and retrieving of tags (query).
-
options
¶ The HTTP request query string string for this DnQuery object - readonly
Type: str
-
data
¶ The payload for this request in JSON format - readonly
Type: str
-
dnStr
¶ The base Dn for this request/query - readonly
Type: str
-
add
¶ The tag(s) to add, default is None
Type: None or str or list
-
remove
¶ The tag(s) to remove, default is None
Type: None or str or list
-
id
¶ An internal troubleshooting value useful for tracing the processing of a request within the cluster
Type: None or int
-
uriBase
¶ The base URI used to build the URL for queries and requests
Type: str
-
__init__
(dn, add=None, remove=None)[source]¶ Initialize a tags query/request object.
Parameters: - dn (str or cobra.mit.naming.Dn) – The base Dn for this request/query
- add (None or str or list) – The tag(s) to add, default is None
- remove (None or str or list) – The tag(s) to remove, default is None
-
add
Get the add string.
Returns: The string of tags that will be added by this request. Return type: str
-
data
Get the data.
Currently only JSON is supported
Returns: The data that will be committed as a JSON string. Return type: str
-
dnStr
Get the dn string.
Returns: - The string representing the Dn that the tags will be committed
- to.
Return type: str
-
getUrl
(session)[source]¶ Get the URL containing all the query options.
Parameters: session (cobra.mit.session.AbstractSession) – The session to use for this query. Returns: The url Return type: str
-
options
Get the options.
Returns: - All the options for this tags request as a string
- joined by &’s.
Return type: str
-
remove
Get the remove string.
Returns: The string of tags that will be removed by this request. Return type: str
-
TraceQuery¶
A class that creates a trace query
-
class
cobra.mit.request.
TraceQuery
(dn, targetClass)[source]¶ Trace Query using a base Dn and a target class.
-
options
¶ The HTTP request query string string for this DnQuery object - readonly
Type: str
-
targetClass
¶ The targetClass for this trace query
Type: str
-
dnStr
¶ The base Dn string for this trace query
Type: str
-
propInclude
¶ the current response property include filter. This filter can be used to specify the properties that should be included in the response. Valid values are:
- _all_
- naming-only
- config-explicit
- config-all
- config-only
- oper
Type: str
-
subtreePropFilter
¶ The response subtree filter can be used to limit what is returned in a subtree response by property values
Type: str
-
subtreeClassFilter
¶ The response subtree class filter can be used to filter a subtree response down to one or more classes. Setting this can be done with either a list or a string, the value is always stored as a comma separated string.
Type: str
-
subtreeInclude
¶ The response subtree include filter can be used to limit the response to a specific type of information from the subtree, these include:
- audit-logs
- event-logs
- faults
- fault-records
- health
- health-records
- relations
- stats
- tasks
- count
- no-scoped
- required
Type: str
-
queryTarget
¶ The query target filter can be used to specify what part of the MIT to query. You can query:
- self - The object itself
- children - The children of the object
- subtree - All the objects lower in the heirarchy
Type: str
-
classFilter
¶ The target subtree class filter can be used to specify which subtree class to filter by. You can set this using a list or a string. The value is always stored as a comma separated string.
Type: str
-
propFilter
¶ The query target property filter can be used to limit which objects are returned based on the value that is set in the specific property within those objects.
Type: str
-
subtree
¶ The response subtree filter can be used to define what objects you want in the response. The possible values are:
- no - No subtree requested
- children - Only the children objects
- full - A full subtree
Type: str
-
orderBy
¶ Request that the results be ordered in a certain way. This can be a list of property sort specifiers or a comma separated string. An example sort specifier: ‘aaaUser.name|desc’.
Type: list or str
-
pageSize
¶ Request that the results that are returned are limited to a certain number, the pageSize.
Type: int
-
replica
¶ The replica option can direct a query to a specific replica. The possible values are:
- 1
- 2
- 3
Type: int
-
id
¶ An internal troubleshooting value useful for tracing the processing of a request within the cluster
Type: None or int
-
uriBase
¶ The base URI used to build the URL for queries and requests
Type: str
-
__init__
(dn, targetClass)[source]¶ Initialize a TraceQuery instance.
Parameters: - dn (str or cobra.mit.naming.Dn) – The base Dn for this query
- targetClass (str) – The target class for this query
-
dnStr
Get the base dn string.
Returns: The string representing the base Dn for this trace query. Return type: str
-
getUrl
(session)[source]¶ Get the URL containing all the query options.
Parameters: session (cobra.mit.session.AbstractSession) – The session to use for this query. Returns: The url Return type: str
-
options
Get the options.
Returns: - All the options for this trace query as a string
- joined by &’s.
Return type: str
-
targetClass
Get the target class.
Returns: The string representing the target class for this trace query. Return type: str
-