def createStorageProfile(handle, org): from ucsmsdk.mometa.lstorage.LstorageProfile import LstorageProfile from ucsmsdk.mometa.lstorage.LstorageDasScsiLun import LstorageDasScsiLun mo = LstorageProfile(parent_mo_or_dn=org, policy_owner="local", name="kube", descr="Kubernetes Storage Profile") mo_1 = LstorageDasScsiLun(parent_mo_or_dn=mo, local_disk_policy_name="kube_boot", auto_deploy="auto-deploy", expand_to_avail="yes", lun_map_type="non-shared", # this is not available in 2.2(8g) size="1", fractional_size="0", admin_state="online", deferred_naming="no", # this is not available in 2.2(8g) order="not-applicable", # not available in 2.2(8g) name="KubeLUN") handle.add_mo(mo, modify_present=True) try: handle.commit() except UcsException as err: if err.error_code == "103": print "\talready exists" else: return 1, err.error_descr return 0, ""
def main(): argument_spec = ucs_argument_spec argument_spec.update(name=dict(type='str'), descr=dict(type='str', default=''), state=dict(type='str', default='present', choices=['present', 'absent'])) module = AnsibleModule(argument_spec, supports_check_mode=True) ucs = UCSModule(module) from ucsmsdk.mometa.lstorage.LstorageProfile import LstorageProfile err = False changed = False mo_exists = False try: dn_base = 'org-root' mo = LstorageProfile(parent_mo_or_dn=dn_base, name=module.params['name'], descr=module.params['descr']) dn = dn_base + '/profile-' + module.params['name'] existing_mo = ucs.login_handle.query_dn(dn) if existing_mo: # check top-level mo props kwargs = dict(descr=module.params['descr']) kwargs['name'] = module.params['name'] if existing_mo.check_prop_match(**kwargs): mo_exists = True if module.params['state'] == 'absent': if mo_exists: if not module.check_mode: # delete mo if dn already exist ucs.login_handle.remove_mo(mo) ucs.login_handle.commit() changed = True else: if not mo_exists: if not module.check_mode: # create mo if dn does not already exist ucs.login_handle.add_mo(mo, True) ucs.login_handle.commit() changed = True 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)
def setup_storage_profile(server, module): from ucsmsdk.mometa.lstorage.LstorageProfile import LstorageProfile from ucsmsdk.mometa.lstorage.LstorageDasScsiLun import LstorageDasScsiLun ansible = module.params args_mo = _get_mo_params(ansible) changed = False mo = server.query_dn(args_mo['org_dn'] + '/profile-' + args_mo['storage_profile']['name']) if mo: exists = True else: exists = False if ansible['state'] == 'absent': if exists: changed = True if not module.check_mode: server.remove_mo(mo) server.commit() else: if not exists: changed = True if not module.check_mode: # create if mo does not already exist mo = LstorageProfile(parent_mo_or_dn=args_mo['org_dn'], name=args_mo['storage_profile']['name']) if (len(args_mo['storage_profile']['lun_list']) <> 0): for lun in args_mo['storage_profile']['lun_list']: if not "expand" in lun: lun["expand"] = "no" mo_1 = LstorageDasScsiLun( parent_mo_or_dn=mo, local_disk_policy_name=lun['disk_group_policy'], expand_to_avail=lun['expand'], name=lun['lun_name'], size=lun['size']) server.add_mo(mo, True) server.commit() return changed
def push_object(self, commit=True): if commit: self.logger(message="Pushing " + self._CONFIG_NAME + " configuration: " + str(self.name)) else: self.logger(message="Adding to the handle " + self._CONFIG_NAME + " configuration: " + str(self.name) + ", waiting for a commit") if hasattr(self._parent, '_dn'): parent_mo = self._parent._dn else: self.logger(level="error", message="Impossible to find the parent dn of " + self._CONFIG_NAME + " : " + str(self.name)) return False mo_lstorage_profile = LstorageProfile(parent_mo_or_dn=parent_mo, name=self.name, descr=self.descr) if self.security_policy: mo_security = LstorageSecurity(parent_mo_or_dn=mo_lstorage_profile) mo_drive_security = LstorageDriveSecurity(parent_mo_or_dn=mo_security) for policy in self.security_policy: if policy["type"] == "remote_policy": mo_remote = LstorageRemote(parent_mo_or_dn=mo_drive_security, primary_server=policy['primary_ip_address'], port=policy['port'], secondary_server=policy['secondary_ip_address'], server_cert=policy['kmip_server_public_certificate'], deployed_security_key=policy['deployed_key']) LstorageLogin(parent_mo_or_dn=mo_remote, user_name=policy['username'], password=policy['password']) elif policy["type"] == "local_policy": LstorageLocal(parent_mo_or_dn=mo_drive_security, security_key=policy["key"]) # deployed_security_key=policy['deployed_key'] if self.local_luns: for local_lun in self.local_luns: LstorageDasScsiLun(parent_mo_or_dn=mo_lstorage_profile, fractional_size=local_lun['fractional_size'], expand_to_avail=local_lun['expand_to_available'], local_disk_policy_name=local_lun['disk_group_policy'], size=local_lun['size'], name=local_lun['name'], auto_deploy=local_lun['auto_deploy']) if self.controller_definitions: for controller_definition in self.controller_definitions: mo_controller_def = LstorageControllerDef(parent_mo_or_dn=mo_lstorage_profile, name=controller_definition['name']) LstorageControllerModeConfig(parent_mo_or_dn=mo_controller_def, protect_config=controller_definition['protected_configuration'], raid_mode=controller_definition['raid_level']) if self.lun_sets: for lun_set in self.lun_sets: mo_ls_storage_lun_set_config = LstorageLunSetConfig(parent_mo_or_dn=mo_lstorage_profile, disk_slot_range=lun_set['disk_slot_range'], name=lun_set['name']) LstorageVirtualDriveDef(parent_mo_or_dn=mo_ls_storage_lun_set_config, access_policy=lun_set['access_policy'], drive_cache=lun_set['drive_cache'], io_policy=lun_set['io_policy'], read_policy=lun_set['read_policy'], security=lun_set['security'], strip_size=lun_set['strip_size'], write_cache_policy=lun_set['write_cache_policy']) self._handle.add_mo(mo=mo_lstorage_profile, modify_present=True) if commit: if self.commit(detail=self.name) != True: return False return True
def main(): local_lun = dict( name=dict(type='str', required=True), state=dict(type='str', default='present', choices=['present', 'absent']), size=dict(type='str', default='1'), auto_deploy=dict(type='str', default='auto-deploy', choices=['auto-deploy', 'no-auto-deploy']), expand_to_avail=dict(type='str', default='no', choices=['no', 'yes']), fractional_size=dict(type='str', default='0'), disk_policy_name=dict(type='str', default=''), ) argument_spec = ucs_argument_spec argument_spec.update( org_dn=dict(type='str', default='org-root'), name=dict(type='str', required=True), description=dict(type='str', aliases=['descr'], default=''), local_luns=dict(type='list', elements='dict', options=local_lun), state=dict(type='str', default='present', choices=['present', 'absent']), ) module = AnsibleModule( argument_spec, supports_check_mode=True, ) ucs = UCSModule(module) err = False # UCSModule creation above verifies ucsmsdk is present and exits on failure. Additional imports are done below. from ucsmsdk.mometa.lstorage.LstorageProfile import LstorageProfile from ucsmsdk.mometa.lstorage.LstorageDasScsiLun import LstorageDasScsiLun ucs.result['changed'] = False try: mo_exists = False props_match = False # dn is <org_dn>/profile-<name> dn = module.params['org_dn'] + '/profile-' + module.params['name'] mo = ucs.login_handle.query_dn(dn) if mo: mo_exists = True if module.params['state'] == 'absent': # mo must exist but all properties do not have to match if mo_exists: if not module.check_mode: ucs.login_handle.remove_mo(mo) ucs.login_handle.commit() ucs.result['changed'] = True else: if mo_exists: # check top-level mo props kwargs = dict(descr=module.params['description']) if mo.check_prop_match(**kwargs): # top-level props match, check next level mo/props if not module.params.get('local_luns'): props_match = True else: # check local lun props for lun in module.params['local_luns']: child_dn = dn + '/das-scsi-lun-' + lun['name'] mo_1 = ucs.login_handle.query_dn(child_dn) if lun['state'] == 'absent': if mo_1: props_match = False break else: if mo_1: kwargs = dict(size=str(lun['size'])) kwargs['auto_deploy'] = lun['auto_deploy'] kwargs['expand_to_avail'] = lun[ 'expand_to_avail'] kwargs['fractional_size'] = str( lun['fractional_size']) kwargs['local_disk_policy_name'] = lun[ 'disk_policy_name'] if mo_1.check_prop_match(**kwargs): props_match = True else: props_match = False break if not props_match: if not module.check_mode: # create if mo does not already exist mo = LstorageProfile( parent_mo_or_dn=module.params['org_dn'], name=module.params['name'], descr=module.params['description'], ) if module.params.get('local_luns'): for lun in module.params['local_luns']: if lun['state'] == 'absent': child_dn = dn + '/das-scsi-lun-' + lun['name'] mo_1 = ucs.login_handle.query_dn(child_dn) ucs.login_handle.remove_mo(mo_1) else: mo_1 = LstorageDasScsiLun( parent_mo_or_dn=mo, name=lun['name'], size=str(lun['size']), auto_deploy=lun['auto_deploy'], expand_to_avail=lun['expand_to_avail'], fractional_size=str( lun['fractional_size']), local_disk_policy_name=lun[ 'disk_policy_name'], ) ucs.login_handle.add_mo(mo, True) ucs.login_handle.commit() ucs.result['changed'] = True except Exception as e: err = True ucs.result['msg'] = "setup error: %s " % str(e) if err: module.fail_json(**ucs.result) module.exit_json(**ucs.result)
def storage_profile(input): name = input['name'] local_lun_list = input['local_lun_list'] state = input['state'] ip = input['ip'] username = input['username'] password = input['password'] mo = "" mo_block = "" ucs_handle = pickle.loads(str(ucs_login.main(ip, username, password))) ###-------CHECK IF MO EXISTS--------------------------------- try: mo = ucs_handle.query_dn("org-root/profile-" + name) except Exception as e: results['error'] = "Could not query storage profile mo " + str(e) ###----if expected state is "present"------------------------ if state == "present": if (mo): current_conf_dict = {} current_conf_dict['name'] = mo.name try: mo_children = ucs_handle.query_children( in_dn="org-root/profile-" + mo.name, class_id="lstorageDasScsiLun") except: results[ 'error'] = "Could not query children of storage profile mo " + str( e) if (mo_children): temp_list = [] for object in mo_children: temp_dict = {} temp_dict['name'] = object.name temp_dict['size'] = object.size if (object.oper_local_disk_policy_name <> ""): local_disk_policy_name = object.oper_local_disk_policy_name.replace( 'org-root/disk-group-config-', '') else: local_disk_policy_name = "" temp_dict[ 'disk_group_configuration_name'] = local_disk_policy_name temp_list.append(temp_dict) current_conf_dict['local_lun_list'] = temp_list else: current_conf_dict['local_lun_list'] = "" if (len(local_lun_list) <> 0): current_local_lun_config_list = current_conf_dict[ 'local_lun_list'] local_lun_exists = True for object in local_lun_list: if (contains(current_local_lun_config_list, object['name'], object['disk_group_configuration_name'], object['size'])): temp_exists = True local_lun_exists = (local_lun_exists and temp_exists) else: local_lun_exists = False local_lun_modify(ucs_handle, mo, object) if (local_lun_exists): results['name'] = name results['present'] = True results['removed'] = False results['changed'] = False else: results['name'] = name results['present'] = True results['removed'] = False results['changed'] = True else: results['name'] = name results['present'] = True results['removed'] = False results['changed'] = False ###----------if not, create boot policy with desired config ---------------- else: try: parent_mo = LstorageProfile(parent_mo_or_dn="org-root", name=name) if (len(local_lun_list) <> 0): for object in local_lun_list: mo_1 = LstorageDasScsiLun( parent_mo_or_dn=parent_mo, local_disk_policy_name=object[ 'disk_group_configuration_name'], name=object['name'], size=object['size']) ucs_handle.add_mo(parent_mo) ucs_handle.commit() results['name'] = name results['present'] = False results['created'] = True results['changed'] = True except Exception as e: results['error'] = "storage profile creation failed " + str(e) return results ###------if expected state is "absent"---------------------------- if state == "absent": if mo: try: ucs_handle.remove_mo(mo) ucs_handle.commit() results['name'] = name results['present'] = False results['removed'] = True except Exception as e: results[ 'error'] = "Removal of storage profile mo failed " + str(e) else: results['name'] = name results['removed'] = False results['present'] = False ucs_handle = pickle.dumps(ucs_handle) ucs_logout.main(ucs_handle) return results