Beispiel #1
0
#!/usr/bin/env python
'''
Simple script to obtain Meta info about a specific classId

Requires the UCS SDK: pip install ucsmsdk

Michael Petrinovic 2018
'''

from ucsmsdk.ucscoreutils import get_meta_info

# Change the class_id to be whatever Class you want to find out more details about
meta = get_meta_info(class_id="ComputeBlade") # Compute Blades
# meta = get_meta_info(class_id="faultInst") # UCS Fault Instances
# meta = get_meta_info(class_id="networkElement") # Fabric Interconnects
# meta = get_meta_info(class_id="lsServer") # Service Profile
# meta = get_meta_info(class_id="fabricVlan") # VLANs
# meta = get_meta_info(class_id="fabricVsan") # VSANs
# meta = get_meta_info(class_id="aaaModLR") # Audit Logs

'''
To find the Python class of the managed object:
* In the Cisco UCS Manager GUI, right-click any object and select the Copy XML option.
* Paste the XML in a text editor. The first word following < is the class identifier (class ID) of the object.
'''
# Print out all internal information regarding the class selected
print meta
Beispiel #2
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(required=True, type='str'),
        descr=dict(type='str'),
        description=dict(type='str', aliases=['descr']),
        graphics_card_mode=dict(
            type='str', choices=['any-configuration', 'compute', 'graphics']),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'present', ['name']],
        ],
    )

    # UCSModule verifies ucsmsdk is present and exits on failure.
    # Imports are below for UCS object creation.
    ucs = UCSModule(module)
    from importlib import import_module
    from ucsmsdk.ucscoreutils import get_meta_info

    # The Class(es) this module is managing
    module_file = 'ucsmsdk.mometa.compute.ComputeGraphicsCardPolicy'
    module_class = 'ComputeGraphicsCardPolicy'
    mo_module = import_module(module_file)
    mo_class = getattr(mo_module, module_class)

    META = get_meta_info(class_id=module_class)

    err = False
    changed = False
    requested_state = module.params['state']

    kwargs = dict()

    # Manage Aliased Attributes
    for attribute in ['descr:description']:
        attribute_alias = attribute.split(':')
        if module.params[attribute_alias[1]] is not None:
            kwargs[attribute_alias[0]] = module.params[attribute_alias[1]]

    # Manage Attributes
    for attribute in ['graphics_card_mode', 'descr']:
        if module.params[attribute] is not None:
            kwargs[attribute] = module.params[attribute]

    try:
        dn = (module.params['org_dn'] + '/' +
              META.rn[0:META.rn.rindex('-') + 1] + module.params['name'])
        mo = ucs.login_handle.query_dn(dn)

        # Determine state change
        if mo:
            # Object exists, if it should exist has anything changed?
            if requested_state == 'present':
                # Do some or all Object properties not match, that is a change

                if not mo.check_prop_match(**kwargs):
                    changed = True

        # Object does not exist but should, that is a change
        else:
            if requested_state == 'present':
                changed = True

        # Object exists but should not, that is a change
        if mo and requested_state == 'absent':
            changed = True

        # Apply state if not check_mode
        if changed and not module.check_mode:
            if requested_state == 'absent':
                ucs.login_handle.remove_mo(mo)
            else:
                kwargs['parent_mo_or_dn'] = module.params['org_dn']
                kwargs['name'] = module.params['name']

                mo = mo_class(**kwargs)
                ucs.login_handle.add_mo(mo, modify_present=True)
            ucs.login_handle.commit()

    except Exception as e:
        err = True
        ucs.result['msg'] = "setup error: %s " % str(e)

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)

    module.exit_json(**ucs.result)
Beispiel #3
0
def test_include_prop_false():
    meta = get_meta_info(class_id="OrGoRg", include_prop=False)
    properties = len(meta.props)
    assert_equal(properties, 0)
Beispiel #4
0
from ucsmsdk.ucshandle import UcsHandle
HANDLE = UcsHandle("13.58.22.56", "admin", "password")
HANDLE.login()

# What is in the HANDLE
vars(HANDLE)

# What capabilities does the HANDLE have
dir(UcsHandle)

# How can the capabilties of the HANDLE be used
#help(UcsHandle)

# Meta data variations for containment hierarcy and object properties
print("\nMetadata: include_prop=False, show_tree=True")
print(get_meta_info(class_id="FabricVlan", include_prop=False, show_tree=True))

print("\nMetadata: include_prop=True, show_tree=False")
print(get_meta_info(class_id="FabricVlan", include_prop=True, show_tree=False))

# Query Metadata information, include_prop and show_tree are True by default
META = get_meta_info(class_id="FabricVlan")

# Drill into the "id" attribute
print("\nMetadata: vars")
print(vars(META.props['id']))
print("\nMetadata: id atttribute")
print(META.props['id'])

# The "id" restriction object and "range_val" attribute
print("FabricVlan:id - range_val: ", META.props['id'].restriction.range_val)
Beispiel #5
0
def test_known_class_props_meta():
    meta = get_meta_info(class_id="OrGoRg")
    xml_attribute = meta.props['name'].xml_attribute
    assert_equal(xml_attribute, 'name')
Beispiel #6
0
def test_known_class_props():
    meta = get_meta_info(class_id="OrGoRg")
    properties = len(meta.props)
    assert_not_equal(properties, 0)
Beispiel #7
0
def test_unknown_class():
    meta = get_meta_info(class_id="unknown")
    assert_equal(meta, None)
Beispiel #8
0
def test_known_class():
    meta = get_meta_info(class_id="OrGoRg")
    xml_attribute = meta.xml_attribute
    assert_equal(xml_attribute, "orgOrg")
## More Powerful Cisco Compute Python Scripts with UCS Python SDK
## Exercise 1

from ucsmsdk.ucscoreutils import get_meta_info

meta = get_meta_info(class_id="FabricVlan")
print(meta)

vars(meta)

meta.class_id
meta.xml_attribute
meta.rn
meta.min_version
meta.access_privilege
meta.parents
meta.children

vars(meta.min_version)

meta.class_id
meta.xml_attribute
meta.rn
meta.min_version._UcsVersion__version
meta.access_privilege
meta.parents
meta.children

meta.props

meta.props["id"]