Skip to content

Commit

Permalink
apis to support bios profile feature (CiscoUcs#90)
Browse files Browse the repository at this point in the history
* apis to support bios profile feature

Signed-off-by: Swapnil Wagh <waghswapnil@gmail.com>

* updated testcases

Signed-off-by: Swapnil Wagh <waghswapnil@gmail.com>

* added examples for bios profile apis

Signed-off-by: Swapnil Wagh <waghswapnil@gmail.com>
  • Loading branch information
waghswapnil authored and vvb committed Jan 20, 2017
1 parent 967ca2f commit b4b83d9
Show file tree
Hide file tree
Showing 3 changed files with 392 additions and 6 deletions.
12 changes: 6 additions & 6 deletions imcsdk/apis/admin/ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,10 @@ def ldap_certificate_download(handle, remote_server, remote_file,
Args:
handle (ImcHandle)
user (str): Username for the remote server
pwd (str): Password for the remote server
remote_server (str): Remote Server IP or Hostname
remote_file (str): Remote file path
user (str): Username for the remote server
pwd (str): Password for the remote server
protocol (str): Protocol for downloading the certificate
['tftp', 'ftp', 'http', 'scp', 'sftp']
kwargs: Key-Value paired arguments for future use
Expand Down Expand Up @@ -469,10 +469,10 @@ def ldap_certificate_export(handle, remote_server, remote_file,
Args:
handle (ImcHandle)
user (str): Username for the remote server
pwd (str): Password for the remote server
remote_server (str): Remote Server IP or Hostname
remote_file (str): Remote file path
user (str): Username for the remote server
pwd (str): Password for the remote server
protocol (str): Protocol for downloading the certificate
['tftp', 'ftp', 'http', 'scp', 'sftp']
kwargs: Key-Value paired arguments for future use
Expand All @@ -498,7 +498,7 @@ def ldap_certificate_export(handle, remote_server, remote_file,
return handle.query_dn(mo.dn)


def ldap_certificate_binding_test(handle, user=None, pwd=None, **kwargs):
def ldap_certificate_binding_check(handle, user=None, pwd=None, **kwargs):
"""
Tests the LDAP CA certificate binding
Expand All @@ -511,7 +511,7 @@ def ldap_certificate_binding_test(handle, user=None, pwd=None, **kwargs):
LdapCACertificate object
Examples:
ldap_certificate_binding_test(handle, user='abcd', pwd='pqrs')
ldap_certificate_binding_check(handle, user='abcd', pwd='pqrs')
"""
mo = _get_mo(handle, dn='sys/ldap-ext/ldap-ca-cert-mgmt/ldap-ca-cert')
params = {
Expand Down
294 changes: 294 additions & 0 deletions imcsdk/apis/server/bios.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
"""

import logging
import json
import imcsdk.imccoreutils as imccoreutils
from imcsdk.mometa.lsboot.LsbootDevPrecision import LsbootDevPrecision
from imcsdk.imcexception import ImcOperationError
from imcsdk.apis.utils import _is_valid_arg

log = logging.getLogger('imc')

Expand Down Expand Up @@ -458,3 +461,294 @@ def boot_order_policy_set(handle, reboot_on_update=False,

boot_policy = handle.query_classid("LsbootDef")
return boot_policy


def _get_bios_dn(handle, server_id=1):
server_dn = imccoreutils.get_server_dn(handle, server_id)
return (server_dn + '/bios')


def _get_bios_profile_mo(handle, name, server_id=1):
bios_dn = _get_bios_dn(handle, server_id)
parent_dn = bios_dn + '/profile-mgmt'
mos = handle.query_children(in_dn=parent_dn)
for mo in mos:
if mo._class_id == 'BiosProfile' and mo.name == name:
return mo
return None


def _get_bios_profile(handle, name, server_id=1):
mo = _get_bios_profile_mo(handle, name=name, server_id=server_id)
if mo is None:
raise ImcOperationError("Get BiosProfile: %s " % name,
"Managed Object not found")
return mo


def bios_profile_backup_running(handle, server_id=1, **kwargs):
"""
Backups up the running configuration of various bios tokens to create a
'cisco_backup_profile'.
Will overwrite the existing backup profile if it exists.
Args:
handle (ImcHandle)
server_id (int): Id of the server to perform
this operation on C3260 platforms
kwargs : Key-Value paired arguments for future use
Returns:
BiosProfile object corresponding to the backup profile created
Raises:
ImcOperationError if the backup profile is not created
Examples:
bios_profile_backup_running(handle, server_id=1)
"""

from imcsdk.mometa.bios.BiosProfileManagement import BiosProfileManagement,\
BiosProfileManagementConsts
mo = BiosProfileManagement(parent_mo_or_dn=_get_bios_dn(handle, server_id))
mo.admin_action = BiosProfileManagementConsts.ADMIN_ACTION_BACKUP
mo.set_prop_multiple(**kwargs)
handle.set_mo(mo)

return _get_bios_profile(handle, name='cisco_backup_profile',
server_id=server_id)


def bios_profile_upload(handle, remote_server, remote_file, protocol='tftp',
user=None, pwd=None, server_id=1, **kwargs):
"""
Uploads a user configured bios profile in json format.
Cisco IMC supports uploading a maximum of 3 profiles
Args:
handle (ImcHandle)
remote_server (str): Remote Server IP or Hostname
remote_file (str): Remote file path
protocol (str): Protocol for downloading the certificate
['tftp', 'ftp', 'http', 'scp', 'sftp']
server_id (int): Id of the server to perform
this operation on C3260 platforms
kwargs: Key-Value paired arguments for future use
Returns:
UploadBiosProfile object
Examples:
bios_profile_upload(handle, remote_server='1.1.1.1',
remote_file='/tmp/bios_profile', protocol='scp',
user='abcd', pwd='pqrs')
"""

from imcsdk.mometa.upload.UploadBiosProfile import UploadBiosProfile
bios_dn = _get_bios_dn(handle, server_id=server_id)
mo = UploadBiosProfile(
parent_mo_or_dn=bios_dn + '/profile-mgmt')
params = {
'remote_server': remote_server,
'remote_file': remote_file,
'protocol': protocol,
'user': user,
'pwd': pwd
}
mo.set_prop_multiple(**params)
mo.set_prop_multiple(**kwargs)
handle.set_mo(mo)
return handle.query_dn(mo.dn)


def bios_profile_get(handle, name, server_id=1):
"""
Gets the bios profile corresponding to the name specified
Args:
handle (ImcHandle)
name (str): Name of the bios profile.
Corresponds to the name field in the json file.
server_id (int): Id of the server to perform
this operation on C3260 platforms
Returns:
BiosProfile object corresponding to the name specified
Raises:
ImcOperationError if the bios profile is not found
Examples:
bios_profile_get(handle, name='simple')
"""

return _get_bios_profile_mo(handle, name=name, server_id=server_id)


def bios_profile_activate(handle, name, backup_on_activate=True,
reboot_on_activate=False, server_id=1, **kwargs):
"""
Activates the bios profile specified by name on the Cisco IMC Server
Args:
handle (ImcHandle)
name (str): Name of the bios profile.
Corresponds to the name field in the json file.
backup_on_activate (bool): Backup running bios configuration
before activating this profile.
Will overwrite the previous backup.
reboot_on_activate (bool): Reboot the host/server for the newer bios
configuration to be applied.
server_id (int): Id of the server to perform
this operation on C3260 platforms.
kwargs: Key-Value paired arguments for future use.
Returns:
BiosProfile object corresponding to the name specified
Raises:
ImcOperationError if the bios profile is not found
Examples:
bios_profile_activate(handle, name='simple',
backup_on_activate=True,
reboot_on_activate=False)
"""

from imcsdk.mometa.bios.BiosProfile import BiosProfileConsts
mo = _get_bios_profile(handle, name=name, server_id=server_id)
params = {
'backup_on_activate': ('no', 'yes')[backup_on_activate],
'reboot_on_activate': ('no', 'yes')[reboot_on_activate],
'enabled': 'yes',
'admin_action': BiosProfileConsts.ADMIN_ACTION_ACTIVATE
}
mo.set_prop_multiple(**params)
mo.set_prop_multiple(**kwargs)
handle.set_mo(mo)
return handle.query_dn(mo.dn)


def bios_profile_delete(handle, name, server_id=1):
"""
Deletes the bios profile specified by the name on the Cisco IMC server
Args:
handle (ImcHandle)
name (str): Name of the bios profile.
Corresponds to the name field in the json file.
server_id (int): Id of the server to perform
this operation on C3260 platforms.
Returns:
None
Raises:
ImcOperationError if the bios profile is not found
Examples:
bios_profile_delete(handle, name='simple', server_id=2)
"""
from imcsdk.mometa.bios.BiosProfile import BiosProfileConsts
mo = _get_bios_profile(handle, name=name, server_id=server_id)
mo.admin_action = BiosProfileConsts.ADMIN_ACTION_DELETE
handle.set_mo(mo)


def is_bios_profile_enabled(handle, name, server_id=1):
"""
Args:
handle (ImcHandle)
name (str): Name of the bios profile.
Corresponds to the name field in the json file.
server_id (int): Id of the server to perform
this operation on C3260 platforms.
Returns:
bool
Raises:
ImcOperationError if the bios profile is not found
Examples:
is_bios_profile_enabled(handle,
name='simple',
server_id=1)
"""
mo = _get_bios_profile(handle, name=name, server_id=server_id)
return mo.enabled.lower() in ['yes', 'true']


def bios_profile_exists(handle, **kwargs):
"""
Checks if the bios profile with the specified params exists
Args:
handle (ImcHandle)
kwargs: Key-Value paired arguments
Returns:
(True, BiosProfile) if the settings match, else (False, None)
Raises:
ImcOperationError if the bios profile is not found
"""

mo = _get_bios_profile(handle, name=name, server_id=server_id)
params = {}
if _is_valid_arg('backup_on_activate', kwargs):
params['backup_on_activate'] = ('no', 'yes')[
kwargs.get('backup_on_activate')]

if _is_valid_arg('reboot_on_activate', kwargs):
params['reboot_on_activate'] = ('no', 'yes')[
kwargs.get('reboot_on_activate')]

if _is_valid_arg('enabled', kwargs):
params['enabled'] = ('no', 'yes')[kwargs.get('enabled')]

if not mo.check_prop_match(**params):
return False, None

return True, mo


def bios_profile_generate_json(handle, name, server_id=1, file_name=None):
"""
Generates a json output of the bios profile specified by the name on
the Cisco IMC server.
If a file name is specified, it writes the output to the file.
Args:
handle (ImcHandle)
name (str): Name of the bios profile.
Corresponds to the name field in the json file.
server_id (int): Id of the server to perform
this operation on C3260 platforms.
Returns:
JSON Output of the Bios Tokens
Raises:
ImcOperationError if the bios profile is not found
Examples:
bios_profile_generate_json(handle, name='simple', server_id=2)
"""

output = {}
output['tokens'] = {}

mo = _get_bios_profile_mo(handle, name=name, server_id=server_id)
output['name'] = mo.name
output['description'] = mo.description

tokens = handle.query_children(in_dn=mo.dn)
output['tokens'] = {x.name: x.configured_value for x in tokens}

if file_name:
f = open(file_name, 'w')
f.write(json.dumps(output))
f.close()

return output
Loading

0 comments on commit b4b83d9

Please sign in to comment.