def disk_group_policy(input): name = input['name'] raid_level = input['raid_level'] slot_numbers = input['slot_numbers'] state = input['state'] ip = input['ip'] username = input['username'] password = input['password'] mo = "" mo_block = "" results = {} ucs_handle = pickle.loads(str(ucs_login.main(ip, username, password))) ###-------CHECK IF MO EXISTS--------------------------------- try: mo = ucs_handle.query_dn("org-root/disk-group-config-" + name) mo_block = ucs_handle.query_children( in_dn="org-root/disk-group-config-" + name, class_id="lstorageLocalDiskConfigRef") except: print("Could not query children of disk group policy") ###----if expected state is "present"------------------------ if state == "present": if mo: slot_numbers_exist = False current_slot_numbers = [] for obj in mo_block: current_slot_numbers.append(obj.slot_num) if (collections.Counter(slot_numbers) == collections.Counter( current_slot_numbers)): slot_numbers_exist = True if (mo.raid_level == raid_level and slot_numbers_exist): results['name'] = name results['expected'] = True results['changed'] = False results['present'] = True else: for slot_num in slot_numbers: query_slot = ucs_handle.query_dn( "org-root/disk-group-config-" + name + "/slot-" + slot_num) if (query_slot == None): mo_2 = LstorageLocalDiskConfigRef(parent_mo_or_dn=mo, slot_num=slot_num) ucs_handle.add_mo(mo, True) ucs_handle.commit() mo = LstorageDiskGroupConfigPolicy(parent_mo_or_dn="org-root", raid_level=raid_level, name=name) ucs_handle.add_mo(mo, True) ucs_handle.commit() results['name'] = name results['expected'] = True results['changed'] = True results['present'] = True ###----------if not, create boot policy with desired config ---------------- else: try: mo = LstorageDiskGroupConfigPolicy(parent_mo_or_dn="org-root", raid_level=raid_level, name=name) mo_1 = LstorageVirtualDriveDef( parent_mo_or_dn=mo, read_policy="platform-default", drive_cache="platform-default", strip_size="platform-default", io_policy="platform-default", write_cache_policy="platform-default", access_policy="platform-default") if (len(slot_numbers) > 0): for slot_num in slot_numbers: mo_2 = LstorageLocalDiskConfigRef(parent_mo_or_dn=mo, slot_num=slot_num) ucs_handle.add_mo(mo) ucs_handle.commit() results['name'] = name results['present'] = False results['created'] = True results['changed'] = True except Exception as e: print("disk group configuration policy creation failed " + str(e)) ###------if expected state is "absent"---------------------------- if state == "absent": if mo: try: ucs_handle.remove_mo(mo) results['name'] = name results['present'] = False results['removed'] = True ucs_handle.commit() except: print("Removal Mac-pool mo failed") else: results['name'] = name results['removed'] = False results['present'] = False ucs_handle = pickle.dumps(ucs_handle) ucs_logout.main(ucs_handle) return results
if mo: try: ucs_handle.remove_mo(mo) results['name']=name; results['present'] = False; results['removed'] = True; ucs_handle.commit() except: print("Removal Mac-pool mo failed") else: results['name']=name; results['removed'] = False; results['present'] = False; ucs_handle=pickle.dumps(ucs_handle) ucs_logout.main(ucs_handle) return results def main(): json_input=json.loads(sys.argv[1]) results = vnic_template(json_input) resultsjson=json.dumps(results) print(resultsjson) return resultsjson if __name__ == '__main__': main()
def server_pool(input): name = input['name'] descr = input['descr'] pooled_servers = input['pooled_servers'] state = input['state'] ip = input['ip'] username = input['username'] password = input['password'] mo = "" mo_block = "" results = {} ucs_handle = pickle.loads(str(ucs_login.main(ip, username, password))) ###-------CHECK IF MO EXISTS--------------------------------- try: mo = ucs_handle.query_dn("org-root/compute-pool-" + name) except: results['error'] = "Could not query children of serverpool" ###----if expected state is "present"------------------------ if state == "present": if mo: if (mo.descr == descr): if (len(pooled_servers) != 0): local_mo_exists = True for obj in pooled_servers: mo_exists = pooled_server_exists( ucs_handle, name, obj['chassis_id'], obj['slot_id']) local_mo_exists = (local_mo_exists and mo_exists) if (local_mo_exists): results['name'] = name results['expected'] = True results['changed'] = False results['present'] = True else: for obj in pooled_servers: mo_exists = pooled_server_exists( ucs_handle, name, obj['chassis_id'], obj['slot_id']) if (mo_exists == False): mo_1 = ComputePooledSlot( parent_mo_or_dn=mo, slot_id=obj['slot_id'], chassis_id=obj['chassis_id']) ucs_handle.add_mo(mo, True) ucs_handle.commit() results['name'] = name results['present'] = True results['removed'] = False results['changed'] = True else: results['name'] = name results['expected'] = True results['changed'] = False results['present'] = True else: try: mo = ComputePool(parent_mo_or_dn="org-root", name=name, descr=descr) if (len(pooled_servers) != 0): local_mo_exists = True for obj in pooled_servers: mo_exists = pooled_server_exists( ucs_handle, name, obj['chassis_id'], obj['slot_id']) local_mo_exists = (local_mo_exists and mo_exists) if (local_mo_exists == False): for obj in pooled_servers: mo_exists = pooled_server_exists( ucs_handle, name, obj['chassis_id'], obj['slot_id']) if (mo_exists == False): mo_1 = ComputePooledSlot( parent_mo_or_dn=mo, slot_id=obj['slot_id'], chassis_id=obj['chassis_id']) ucs_handle.add_mo(mo, True) ucs_handle.commit() results['name'] = name results['present'] = True results['removed'] = False results['changed'] = True except Exception as e: results[ 'error'] = "modification of server pool failed " + str( e) ###----------if not, create boot policy with desired config ---------------- else: try: mo = ComputePool(parent_mo_or_dn="org-root", name=name, descr=descr) if (len(pooled_servers) <> 0): for obj in pooled_servers: mo_1 = ComputePooledSlot(parent_mo_or_dn=mo, slot_id=obj['slot_id'], chassis_id=obj['chassis_id']) ucs_handle.add_mo(mo) ucs_handle.commit() results['name'] = name results['present'] = False results['created'] = True results['changed'] = True except Exception as e: results['error'] = "Server pool creation failed " + str(e) ###------if expected state is "absent"---------------------------- if state == "absent": if mo: try: ucs_handle.remove_mo(mo) results['name'] = name results['present'] = False results['removed'] = True ucs_handle.commit() except Exception as e: results['error'] = "Removal server pool 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
def wwn_pool(input): wwn = input results = {} ucs_handle = pickle.loads( str(ucs_login.main(wwn['ip'], wwn['username'], wwn['password']))) # set default params if not wwn.get('descr'): wwn['descr'] = '' if not wwn.get('order'): wwn['order'] = 'default' if not wwn.get('org_dn'): wwn['org_dn'] = 'org-root' if not wwn.get('state'): wwn['state'] = 'present' # append purpose param with suffix used by UCSM purpose_param = wwn['purpose'] + '-wwn-assignment' changed = False if wwn['state'] == 'present': results['expected'] = True else: results['expected'] = False results['name'] = wwn['name'] try: exists = False # dn is <org_dn>/wwn-pool-<name> for WWNN or WWPN dn = wwn['org_dn'] + '/wwn-pool-' + wwn['name'] mo = ucs_handle.query_dn(dn) if mo: obj = {} obj['name'] = mo.name obj['descr'] = mo.descr # check top-level mo props kwargs = {} kwargs['assignment_order'] = wwn['order'] kwargs['descr'] = wwn['descr'] kwargs['purpose'] = purpose_param if (mo.check_prop_match(**kwargs)): # top-level props match, check next level mo/props if 'to' in wwn and 'r_from' in wwn: block_dn = dn + '/block-' + wwn['r_from'].upper( ) + '-' + wwn['to'].upper() mo_1 = ucs_handle.query_dn(block_dn) if mo_1: exists = True else: exists = True if wwn['state'] == 'absent': if exists: if not wwn['check_exists']: ucs_handle.remove_mo(mo) ucs_handle.commit() changed = True results['removed'] = True else: # create/modify for state 'present' results['removed'] = False if not exists: if not wwn['check_exists']: # create if mo does not already exist mo = FcpoolInitiators(parent_mo_or_dn=wwn['org_dn'], name=wwn['name'], assignment_order=wwn['order'], purpose=purpose_param) if 'to' in wwn and 'r_from' in wwn: mo_1 = FcpoolBlock(parent_mo_or_dn=mo, to=wwn['to'], r_from=wwn['r_from']) ucs_handle.add_mo(mo, True) ucs_handle.commit() changed = True results['created'] = True except Exception as e: err = True results['msg'] = "setup error: %s " % str(e) results['changed'] = changed results['present'] = exists ucs_handle = pickle.dumps(ucs_handle) ucs_logout.main(ucs_handle) return results
def san_connectivity(input): san_connectivity = input results = {} ucs_handle = pickle.loads( str( ucs_login.main(san_connectivity['ip'], san_connectivity['username'], san_connectivity['password']))) # set default params if not san_connectivity.get('descr'): san_connectivity['descr'] = '' if not san_connectivity.get('wwnn_pool'): san_connectivity['wwnn_pool'] = 'default' if not san_connectivity.get('org_dn'): san_connectivity['org_dn'] = 'org-root' for vhba in san_connectivity['vhba_list']: if not vhba.get('adapter_policy'): vhba['adapter_policy'] = '' changed = False if san_connectivity['state'] == 'present': results['expected'] = True else: results['expected'] = False results['name'] = san_connectivity['name'] try: exists = False # dn is <org_dn>/san-conn-pol-<name> dn = san_connectivity['org_dn'] + '/san-conn-pol-' + san_connectivity[ 'name'] mo = ucs_handle.query_dn(dn) if mo: # check top-level mo props kwargs = {} kwargs['descr'] = san_connectivity['descr'] if (mo.check_prop_match(**kwargs)): # top-level props match, check next level mo/props # vnicFcNode object child_dn = dn + '/fc-node' mo_1 = ucs_handle.query_dn(child_dn) if mo_1: kwargs = {} kwargs['ident_pool_name'] = san_connectivity['wwnn_pool'] if (mo_1.check_prop_match(**kwargs)): if len(san_connectivity['vhba_list']) == 0: exists = True else: # check vnicFc props for vhba in san_connectivity['vhba_list']: child_dn = dn + '/fc-' + vhba['name'] mo_2 = ucs.login_handle.query_dn(child_dn) kwargs = {} kwargs['adaptor_profile_name'] = vhba[ 'adapter_policy'] kwargs['order'] = vhba['order'] kwargs['nw_templ_name'] = vhba['vhba_template'] if (mo_2.check_prop_match(**kwargs)): exists = True if san_connectivity['state'] == 'absent': if exists: if not san_connectivity['check_exists']: ucs_handle.remove_mo(mo) ucs_handle.commit() changed = True results['removed'] = True else: # create/modify for state 'present' results['removed'] = False if not exists: if not san_connectivity['check_exists']: # create if mo does not already exist mo = VnicSanConnPolicy( parent_mo_or_dn=san_connectivity['org_dn'], name=san_connectivity['name'], descr=san_connectivity['descr']) mo_1 = VnicFcNode( parent_mo_or_dn=mo, ident_pool_name=san_connectivity['wwnn_pool'], addr='pool-derived') for vhba in san_connectivity['vhba_list']: mo_2 = VnicFc( parent_mo_or_dn=mo, name=vhba['name'], adaptor_profile_name=vhba['adapter_policy'], nw_templ_name=vhba['vhba_template'], order=vhba['order']) mo_2_1 = VnicFcIf(parent_mo_or_dn=mo_2, name='default') ucs_handle.add_mo(mo, True) ucs_handle.commit() changed = True results['created'] = True except Exception as e: err = True results['msg'] = "setup error: %s " % str(e) results['changed'] = changed results['present'] = exists ucs_handle = pickle.dumps(ucs_handle) ucs_logout.main(ucs_handle) return results
def vhba_template(input): vhba_template = input results = {} ucs_handle = pickle.loads( str( ucs_login.main(vhba_template['ip'], vhba_template['username'], vhba_template['password']))) # set default params if not vhba_template.get('descr'): vhba_template['descr'] = '' if not vhba_template.get('fabric'): vhba_template['fabric'] = 'A' if not vhba_template.get('redundancy_type'): vhba_template['redundancy_type'] = 'none' if not vhba_template.get('vsan'): vhba_template['vsan'] = 'default' if not vhba_template.get('template_type'): vhba_template['template_type'] = 'initial-template' if not vhba_template.get('max_data'): vhba_template['max_data'] = '2048' if not vhba_template.get('wwpn_pool'): vhba_template['wwpn_pool'] = 'default' if not vhba_template.get('qos_policy'): vhba_template['qos_policy'] = '' if not vhba_template.get('pin_group'): vhba_template['pin_group'] = '' if not vhba_template.get('stats_policy'): vhba_template['stats_policy'] = 'default' if not vhba_template.get('org_dn'): vhba_template['org_dn'] = 'org-root' changed = False if vhba_template['state'] == 'present': results['expected'] = True else: results['expected'] = False results['name'] = vhba_template['name'] try: exists = False # dn is <org_dn>/san-conn-templ-<name> dn = vhba_template['org_dn'] + '/san-conn-templ-' + vhba_template[ 'name'] mo = ucs_handle.query_dn(dn) if mo: # check top-level mo props kwargs = {} kwargs['descr'] = vhba_template['descr'] kwargs['switch_id'] = vhba_template['fabric'] kwargs['redundancy_pair_type'] = vhba_template['redundancy_type'] kwargs['templ_type'] = vhba_template['template_type'] kwargs['max_data_field_size'] = vhba_template['max_data'] kwargs['ident_pool_name'] = vhba_template['wwpn_pool'] kwargs['qos_policy_name'] = vhba_template['qos_policy'] kwargs['pin_to_group_name'] = vhba_template['pin_group'] kwargs['stats_policy_name'] = vhba_template['stats_policy'] if (mo.check_prop_match(**kwargs)): # top-level props match, check next level mo/props child_dn = dn + '/if-default' mo_1 = ucs_handle.query_dn(child_dn) if mo_1: kwargs = {} kwargs['name'] = vhba_template['vsan'] if (mo_1.check_prop_match(**kwargs)): exists = True if vhba_template['state'] == 'absent': if exists: if not vhba_template['check_exists']: ucs_handle.remove_mo(mo) ucs_handle.commit() changed = True results['removed'] = True else: # create/modify for state 'present' results['removed'] = False if not exists: if not vhba_template['check_exists']: # create if mo does not already exist mo = VnicSanConnTempl( parent_mo_or_dn=vhba_template['org_dn'], name=vhba_template['name'], descr=vhba_template['descr'], switch_id=vhba_template['fabric'], redundancy_pair_type=vhba_template['redundancy_type'], templ_type=vhba_template['template_type'], max_data_field_size=vhba_template['max_data'], ident_pool_name=vhba_template['wwpn_pool'], qos_policy_name=vhba_template['qos_policy'], pin_to_group_name=vhba_template['pin_group'], stats_policy_name=vhba_template['stats_policy']) mo_1 = VnicFcIf(parent_mo_or_dn=mo, name=vhba_template['vsan']) ucs_handle.add_mo(mo, True) ucs_handle.commit() changed = True results['created'] = True except Exception as e: err = True results['msg'] = "setup error: %s " % str(e) results['changed'] = changed results['present'] = exists ucs_handle = pickle.dumps(ucs_handle) ucs_logout.main(ucs_handle) return results
def boot_policy(input): name = input['name'] descr = input['descr'] reboot_on_update = input['reboot_on_update'] policy_owner = input['policy_owner'] enforce_vnic_name = input['enforce_vnic_name'] boot_mode = input['boot_mode'] state = input['state'] ip=input['ip'] username=input['username'] password=input['password'] results = {} ucs_handle = pickle.loads(str(ucs_login.main(ip,username,password))) ###-------CHECK IF MO EXISTS--------------------------------- try: mo = ucs_handle.query_dn("org-root/boot-policy-"+name) except: print("Could not query children of org-root") ###----if expected state is "present"------------------------ if state == "present": if mo: if (mo.name == name and mo.descr == descr and mo.boot_mode == boot_mode and mo.reboot_on_update == reboot_on_update and mo.policy_owner == policy_owner and mo.enforce_vnic_name == enforce_vnic_name): results['name']=name; results['expected'] = True; results['changed'] = False; results['present'] = True; #results['mo_bootpolicy'] = json.dumps(json.loads(jsonpickle.encode(mo))); else: try: mo.descr = descr mo.boot_mode = boot_mode mo.reboot_on_update = reboot_on_update mo.policy_owner = policy_owner mo.enforce_vnic_name = enforce_vnic_name results['name']=name; results['expected'] = False; results['changed'] = True; results['present'] = True; ucs_handle.set_mo(mo) ucs_handle.commit() #results['mo_bootpolicy'] = json.dumps(json.loads(jsonpickle.encode(mo))); except: module.fail_json(msg="Modify boot policy mo failed") ###----------if not, create boot policy with desired config ---------------- else: try: #print("inside creator block") mo = LsbootPolicy(parent_mo_or_dn="org-root", name=name, descr=descr, reboot_on_update=reboot_on_update, policy_owner=policy_owner, enforce_vnic_name=enforce_vnic_name, boot_mode=boot_mode) results['name']=name; results['present'] = False; results['created'] = True; results['changed'] = True; #results['mo_bootpolicy'] = json.dumps(json.loads(jsonpickle.encode(mo))); ucs_handle.add_mo(mo) ucs_handle.commit() except: print("Boot Policy creation failed") ###------if expected state is "absent"---------------------------- if state == "absent": if mo: try: ucs_handle.remove_mo(mo) results['name']=name; results['present'] = False; results['removed'] = True; ucs_handle.commit() except: print("Remove boot policy mo failed") else: results['name']=name; results['removed'] = False; results['present'] = False; ucs_handle=pickle.dumps(ucs_handle) ucs_logout.main(ucs_handle) return results