def get_ip_mac_arp_list(auth, url, devid=None, devip=None): """ function takes devid of specific device and issues a RESTFUL call to get the IP/MAC/ARP list from the target device. :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param devid: int or str value of the target device. :param devip: str of ipv4 address of the target device :return: list of dictionaries containing the IP/MAC/ARP list of the target device. :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.termaccess import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> ip_mac_list = get_ip_mac_arp_list( auth.creds, auth.url, devid='10') >>> ip_mac_list = get_ip_mac_arp_list( auth.creds, auth.url, devip='10.101.0.221') >>> assert type(ip_mac_list) is list >>> assert 'deviceId' in ip_mac_list[0] """ if devip is not None: dev_details = get_dev_details(devip, auth, url) if isinstance(dev_details, str): print("Device not found") return 403 else: devid = get_dev_details(devip, auth, url)['id'] f_url = url + "/imcrs/res/access/ipMacArp/" + str(devid) response = requests.get(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 200: ipmacarplist = (json.loads(response.text)) if 'ipMacArp' in ipmacarplist: return ipmacarplist['ipMacArp'] else: return ['this function is unsupported'] except requests.exceptions.RequestException as error: return "Error:\n" + str(error) + " get_ip_mac_arp_list: An Error has occured"
def test_add_devs_custom_views_Content(self): dev_list = [get_dev_details(CW5_Switch, auth.creds, auth.url)['id']] add_devs = add_devs_custom_views('L1 View', dev_list, auth.creds, auth.url) view_details = get_custom_view_details('L1 View', auth.creds, auth.url) view_dev_list = [] for dev in view_details: print (dev['ip']) view_dev_list.append(dev['ip']) self.assertEqual(get_dev_details(CW5_Switch, auth.creds, auth.url)['ip'], view_dev_list[0])
def test_add_devs_custom_views_Content(self): dev_list = [get_dev_details(CW5_Switch, auth.creds, auth.url)['id']] add_devs = add_devs_custom_views('L1 View', dev_list, auth.creds, auth.url) view_details = get_custom_view_details('L1 View', auth.creds, auth.url) view_dev_list = [] for dev in view_details: print((dev['ip'])) view_dev_list.append(dev['ip']) self.assertEqual( get_dev_details(CW5_Switch, auth.creds, auth.url)['ip'], view_dev_list[0])
def get_ip_mac_arp_list(auth, url, devid=None, devip=None): """ function takes devid of specific device and issues a RESTFUL call to get the IP/MAC/ARP list from the target device. :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param devid: int or str value of the target device. :param devip: str of ipv4 address of the target device :return: list of dictionaries containing the IP/MAC/ARP list of the target device. :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.termaccess import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> ip_mac_list = get_ip_mac_arp_list( auth.creds, auth.url, devid='10') >>> ip_mac_list = get_ip_mac_arp_list( auth.creds, auth.url, devip='10.101.0.221') >>> assert type(ip_mac_list) is list >>> assert 'deviceId' in ip_mac_list[0] """ if devip is not None: dev_details = get_dev_details(devip, auth, url) if isinstance(dev_details, str): print("Device not found") return 403 else: devid = get_dev_details(devip, auth, url)['id'] f_url = url + "/imcrs/res/access/ipMacArp/" + str(devid) response = requests.get(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 200: ipmacarplist = (json.loads(response.text)) if 'ipMacArp' in ipmacarplist: return ipmacarplist['ipMacArp'] else: return ['this function is unsupported'] except requests.exceptions.RequestException as error: return "Error:\n" + str( error) + " get_ip_mac_arp_list: An Error has occured"
def get_vm_host_info(hostip, auth, url): """ function takes hostId as input to RESTFUL call to HP IMC :param hostip: int or string of hostip of Hypervisor host :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: Dictionary contraining the information for the target VM host :rtype: dict >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vrm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> host_info = get_vm_host_info('10.101.0.6', auth.creds, auth.url) >>> assert type(host_info) is dict >>> assert len(host_info) == 10 >>> assert 'cpuFeg' in host_info >>> assert 'cpuNum' in host_info >>> assert 'devId' in host_info >>> assert 'devIp' in host_info >>> assert 'diskSize' in host_info >>> assert 'memory' in host_info >>> assert 'parentDevId' in host_info >>> assert 'porductFlag' in host_info >>> assert 'serverName' in host_info >>> assert 'vendor' in host_info """ hostid = get_dev_details(hostip, auth, url)['id'] f_url = url + "/imcrs/vrm/host?hostId=" + str(hostid) response = requests.get(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 200: if len(response.text) > 0: return json.loads(response.text) elif response.status_code == 204: print("Device is not a supported Hypervisor") return "Device is not a supported Hypervisor" except requests.exceptions.RequestException as error: return "Error:\n" + str( error) + " get_vm_host_info: An Error has occured"
def set_access_interface_pvid(ifindex, pvid, auth, url, devip=None, devid=None): """ Function takes devip ( ipv4 address ), ifIndex and pvid (vlanid) of specific device and 802.1q VLAN tag and issues a RESTFUL call to remove the specified VLAN from the target device. :param ifindex: str value of ifIndex for a specific interface on the device :param pvid: str value of dot1q VLAN desired to apply to the device :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param devid: str requires devid of the target device :param devip: str of ipv4 address of the target device :return: int of 204 if successful or 409 if not succesful :rtype: int >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vlanm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> set_access_int_vlan = set_access_interface_pvid('9', '1', auth.creds, auth.url, devip='10.101.0.221') >>> set_access_int_vlan = set_access_interface_pvid('9', '10', auth.creds, auth.url, devip='10.101.0.221') >>> assert type(set_access_int_vlan) is int >>> assert set_access_int_vlan == 204 >>> set_access_int_vlan = set_access_interface_pvid('9', '1', auth.creds, auth.url, devip='10.101.0.221') """ if devip is not None: devid = get_dev_details(devip, auth, url)['id'] set_access_interface_pvid_url = "/imcrs/vlan/access?devId=" + devid + "&destVlanId=" + pvid \ + "&ifIndex=" + str(ifindex) f_url = url + set_access_interface_pvid_url response = requests.put(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 204: return 204 if response.status_code == 409: return 409 except requests.exceptions.RequestException as error: return "Error:\n" + str( error) + " set_access_interface_pvid: An Error has occured"
def modify_hybrid_interface(ifindex, pvid, taggedvlans, untaggedvlans, auth, url, devip=None, devid=None): """ Function takes ifindex, pvid, tagged vlans untagged vlans as input values to modify a hybrid port to a HPE Comware based switch. These functions only apply to HPE Comware based devices. :param ifindex: str ifIndex value of target interface :param pvid: str 802.1q value (1-4094) of target VLAN :param taggedvlans: str 802.1q value, seperated by commas, of target tagged VLANs :param untaggedvlans: str 802.1q value, seperated by commas, of target untagged VLANs :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param devid: str requires devid of the target device :param devip: str of ipv4 address of the target device :return int of http response code :rtype int """ if devip is not None: devid = get_dev_details(devip, auth, url)['id'] f_url = url + "/imcrs/vlan/hybrid?devId=" + str(devid) + "&start=1&size=500&total=false" payload = '''{"ifIndex": "''' + ifindex + '''", "pvid": "''' + pvid + '''", "taggedVlans": "''' + taggedvlans + '''", "untagVlanFlag": "true", "untaggedVlans": "''' + untaggedvlans + '''" }''' response = requests.put(f_url, auth=auth, data=payload, headers=HEADERS) try: if response.status_code == 204: return 204 if response.status_code == 409: return 409 except requests.exceptions.RequestException as error: return "Error:\n" + str(error) + " get_device_hybrid_interfaces: An Error has occured"
def get_vm_host_info(hostip, auth, url): """ function takes hostId as input to RESTFUL call to HP IMC :param hostip: int or string of hostip of Hypervisor host :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: Dictionary contraining the information for the target VM host :rtype: dict >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vrm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> host_info = get_vm_host_info('10.101.0.6', auth.creds, auth.url) >>> assert type(host_info) is dict >>> assert len(host_info) == 10 >>> assert 'cpuFeg' in host_info >>> assert 'cpuNum' in host_info >>> assert 'devId' in host_info >>> assert 'devIp' in host_info >>> assert 'diskSize' in host_info >>> assert 'memory' in host_info >>> assert 'parentDevId' in host_info >>> assert 'porductFlag' in host_info >>> assert 'serverName' in host_info >>> assert 'vendor' in host_info """ hostid = get_dev_details(hostip, auth, url)['id'] f_url = url + "/imcrs/vrm/host?hostId=" + str(hostid) response = requests.get(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 200: if len(response.text) > 0: return json.loads(response.text) elif response.status_code == 204: print("Device is not a supported Hypervisor") return "Device is not a supported Hypervisor" except requests.exceptions.RequestException as error: return "Error:\n" + str(error) + " get_vm_host_info: An Error has occured"
def get_trunk_interfaces(auth, url, devid=None, devip=None): """Function takes devId as input to RESTFULL call to HP IMC platform :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param devid: str requires devid of the target device :param devip: str of ipv4 address of the target device :return: list of dictionaries where each element of the list represents an interface which has been configured as a VLAN trunk port :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vlanm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> trunk_interfaces = get_trunk_interfaces('10', auth.creds, auth.url) >>> assert type(trunk_interfaces) is list >>> assert len(trunk_interfaces[0]) == 3 >>> assert 'allowedVlans' in trunk_interfaces[0] >>> assert 'ifIndex' in trunk_interfaces[0] >>> assert 'pvid' in trunk_interfaces[0] >>> get_trunk_interfaces('350', auth.creds, auth.url) ['No trunk inteface'] """ if devip is not None: devid = get_dev_details(devip, auth, url)['id'] get_trunk_interfaces_url = "/imcrs/vlan/trunk?devId=" + str(devid) + \ "&start=1&size=5000&total=false" f_url = url + get_trunk_interfaces_url response = requests.get(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 200: dev_trunk_interfaces = (json.loads(response.text)) if len(dev_trunk_interfaces) == 2: if isinstance(dev_trunk_interfaces['trunkIf'], list): return dev_trunk_interfaces['trunkIf'] elif isinstance(dev_trunk_interfaces['trunkIf'], dict): return [dev_trunk_interfaces['trunkIf']] else: dev_trunk_interfaces['trunkIf'] = ["No trunk inteface"] return dev_trunk_interfaces['trunkIf'] except requests.exceptions.RequestException as error: return "Error:\n" + str( error) + ' get_trunk_interfaces: An Error has occured'
def get_trunk_interfaces(auth, url, devid=None, devip=None): """Function takes devId as input to RESTFULL call to HP IMC platform :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param devid: str requires devid of the target device :param devip: str of ipv4 address of the target device :return: list of dictionaries where each element of the list represents an interface which has been configured as a VLAN trunk port :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vlanm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> trunk_interfaces = get_trunk_interfaces('10', auth.creds, auth.url) >>> assert type(trunk_interfaces) is list >>> assert len(trunk_interfaces[0]) == 3 >>> assert 'allowedVlans' in trunk_interfaces[0] >>> assert 'ifIndex' in trunk_interfaces[0] >>> assert 'pvid' in trunk_interfaces[0] >>> get_trunk_interfaces('350', auth.creds, auth.url) ['No trunk inteface'] """ if devip is not None: devid = get_dev_details(devip, auth, url)['id'] get_trunk_interfaces_url = "/imcrs/vlan/trunk?devId=" + str(devid) + \ "&start=1&size=5000&total=false" f_url = url + get_trunk_interfaces_url response = requests.get(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 200: dev_trunk_interfaces = (json.loads(response.text)) if len(dev_trunk_interfaces) == 2: if isinstance(dev_trunk_interfaces['trunkIf'], list): return dev_trunk_interfaces['trunkIf'] elif isinstance(dev_trunk_interfaces['trunkIf'], dict): return [dev_trunk_interfaces['trunkIf']] else: dev_trunk_interfaces['trunkIf'] = ["No trunk inteface"] return dev_trunk_interfaces['trunkIf'] except requests.exceptions.RequestException as error: return "Error:\n" + str(error) + ' get_trunk_interfaces: An Error has occured'
def get_vm_host_vnic(hostip, auth, url): """ function takes hostId as input to RESTFUL call to HP IMC :param hostip: int or string of hostip of Hypervisor host :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: list of dictionaries where each element of the list represents a single NIC on the target host :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vrm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> host_vnic = get_vm_host_vnic('10.101.0.6', auth.creds, auth.url) >>> assert type(host_vnic) is list >>> assert (len(host_vnic[0])) is 6 >>> assert 'ip' in host_vnic[0] >>> assert 'mask' in host_vnic[0] >>> assert 'nicName' in host_vnic[0] >>> assert 'serverDevId' in host_vnic[0] >>> assert 'vSwitchName' in host_vnic[0] >>> assert 'vSwtichKey' in host_vnic[0] """ hostid = get_dev_details(hostip, auth, url)['id'] f_url = url + "/imcrs/vrm/host/vnic?hostDevId=" + str(hostid) response = requests.get(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 200: if len(response.text) > 0: return json.loads(response.text)['Nic'] elif response.status_code == 204: print("Device is not a supported Hypervisor") return "Device is not a supported Hypervisor" except requests.exceptions.RequestException as error: return "Error:\n" + str( error) + " get_vm_host_info: An Error has occured"
def set_access_interface_pvid(ifindex, pvid, auth, url, devip=None, devid=None): """ Function takes devip ( ipv4 address ), ifIndex and pvid (vlanid) of specific device and 802.1q VLAN tag and issues a RESTFUL call to remove the specified VLAN from the target device. :param ifindex: str value of ifIndex for a specific interface on the device :param pvid: str value of dot1q VLAN desired to apply to the device :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param devid: str requires devid of the target device :param devip: str of ipv4 address of the target device :return: int of 204 if successful or 409 if not succesful :rtype: int >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vlanm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> set_access_int_vlan = set_access_interface_pvid('9', '1', auth.creds, auth.url, devip='10.101.0.221') >>> set_access_int_vlan = set_access_interface_pvid('9', '10', auth.creds, auth.url, devip='10.101.0.221') >>> assert type(set_access_int_vlan) is int >>> assert set_access_int_vlan == 204 >>> set_access_int_vlan = set_access_interface_pvid('9', '1', auth.creds, auth.url, devip='10.101.0.221') """ if devip is not None: devid = get_dev_details(devip, auth, url)['id'] set_access_interface_pvid_url = "/imcrs/vlan/access?devId=" + devid + "&destVlanId=" + pvid \ + "&ifIndex=" + str(ifindex) f_url = url + set_access_interface_pvid_url response = requests.put(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 204: return 204 if response.status_code == 409: return 409 except requests.exceptions.RequestException as error: return "Error:\n" + str(error) + " set_access_interface_pvid: An Error has occured"
def get_vm_host_vnic(hostip, auth, url): """ function takes hostId as input to RESTFUL call to HP IMC :param hostip: int or string of hostip of Hypervisor host :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: list of dictionaries where each element of the list represents a single NIC on the target host :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vrm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> host_vnic = get_vm_host_vnic('10.101.0.6', auth.creds, auth.url) >>> assert type(host_vnic) is list >>> assert (len(host_vnic[0])) is 6 >>> assert 'ip' in host_vnic[0] >>> assert 'mask' in host_vnic[0] >>> assert 'nicName' in host_vnic[0] >>> assert 'serverDevId' in host_vnic[0] >>> assert 'vSwitchName' in host_vnic[0] >>> assert 'vSwtichKey' in host_vnic[0] """ hostid = get_dev_details(hostip, auth, url)['id'] f_url = url + "/imcrs/vrm/host/vnic?hostDevId=" + str(hostid) response = requests.get(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 200: if len(response.text) > 0: return json.loads(response.text)['Nic'] elif response.status_code == 204: print("Device is not a supported Hypervisor") return "Device is not a supported Hypervisor" except requests.exceptions.RequestException as error: return "Error:\n" + str(error) + " get_vm_host_info: An Error has occured"
def get_dev_asset_details(ipaddress, auth, url): """Takes in ipaddress as input to fetch device assett details from HP IMC RESTFUL API :param ipaddress: IP address of the device you wish to gather the asset details :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: object of type list containing the device asset details, with each asset contained in a dictionary :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.netassets import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> single_asset = get_dev_asset_details('10.101.0.1', auth.creds, auth.url) >>> assert type(single_asset) is list >>> assert 'name' in single_asset[0] """ ipaddress = get_dev_details(ipaddress, auth, url) if isinstance(ipaddress, dict): ipaddress = ipaddress['ip'] else: print("Asset Doesn't Exist") return 403 f_url = url + "/imcrs/netasset/asset?assetDevice.ip=" + str(ipaddress) response = requests.get(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 200: dev_asset_info = (json.loads(response.text)) if len(dev_asset_info) > 0: dev_asset_info = dev_asset_info['netAsset'] if isinstance(dev_asset_info, dict): dev_asset_info = [dev_asset_info] if isinstance(dev_asset_info, list): dev_asset_info[:] = [ dev for dev in dev_asset_info if dev.get('deviceIp') == ipaddress ] return dev_asset_info except requests.exceptions.RequestException as error: return "Error:\n" + str( error) + ' get_dev_asset_details: An Error has occured'
def get_device_hybrid_interfaces(auth, url, devId=None, devip = None): """Function takes devId as input to RESTFUL call to HP IMC platform :param devid: str requires devId as the only input parameter :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: list of dictionaries where each element of the list represents an interface which has been configured as a VLAN access port :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vlanm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> hybrid_interfaces = get_device_hybrid_interfaces('10', auth.creds, auth.url) >>> assert type(access_interfaces) is list >>> assert (len(access_interfaces[0])) is 2 >>> assert 'ifIndex' in access_interfaces[0] >>> assert 'pvid' in access_interfaces[0] """ if devip is not None: devId=get_dev_details(devip, auth, url)['id'] get_hybrid_interface_vlan_url = "/imcrs/vlan/hybrid?devId=" + str(devId) + "&start=1&size=500&total=false" f_url = url + get_hybrid_interface_vlan_url payload = None # creates the URL using the payload variable as the contents r = requests.get(f_url, auth=auth, headers=HEADERS) # r.status_code try: if r.status_code == 200: dev_hybrid_interfaces = (json.loads(r.text)) if len(dev_hybrid_interfaces) == 2: dev_hybrid = dev_hybrid_interfaces['hybridIf'] if type(dev_hybrid) == dict: dev_hybrid = [dev_hybrid] return dev_hybrid else: dev_hybrid_interfaces['hybridIf'] = ["No hybrid inteface"] return dev_hybrid_interfaces['hybridIf'] except requests.exceptions.RequestException as e: return "Error:\n" + str(e) + " get_device_hybrid_interfaces: An Error has occured"
def create_dev_vlan(vlanid, vlan_name, auth, url, devid=None, devip=None): """ function takes devid and vlanid vlan_name of specific device and 802.1q VLAN tag and issues a RESTFUL call to add the specified VLAN from the target device. VLAN Name MUST be valid on target device. :param vlanid:int or str value of target 802.1q VLAN :param vlan_name: str value of the target 802.1q VLAN name. MUST be valid name on target device. :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param devid: str requires devid of the target device :param devip: str of ipv4 address of the target device :return: str HTTP Response code. Should be 201 if successfully created :rtype: str >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vlanm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> create_dev_vlan = create_dev_vlan('350', '200', 'test vlan', auth.creds, auth.url) """ if devip is not None: devid = get_dev_details(devip, auth, url)['id'] create_dev_vlan_url = "/imcrs/vlan?devId=" + str(devid) f_url = url + create_dev_vlan_url payload = '''{"vlanId":"%s", "vlanName":"%s"}''' % (str(vlanid), vlan_name) response = requests.post(f_url, data=payload, auth=auth, headers=HEADERS) try: if response.status_code == 201: print('Vlan Created') return 201 elif response.status_code == 409: print( '''Unable to create VLAN.\nVLAN Already Exists\nDevice does not support VLAN function''') return 409 except requests.exceptions.RequestException as error: return "Error:\n" + str( error) + " create_dev_vlan: An Error has occured"
def get_dev_asset_details(ipaddress, auth, url): """Takes in ipaddress as input to fetch device assett details from HP IMC RESTFUL API :param ipaddress: IP address of the device you wish to gather the asset details :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: object of type list containing the device asset details, with each asset contained in a dictionary :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.netassets import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> single_asset = get_dev_asset_details('10.101.0.1', auth.creds, auth.url) >>> assert type(single_asset) is list >>> assert 'name' in single_asset[0] """ ipaddress = get_dev_details(ipaddress, auth,url) if type(ipaddress) is dict: ipaddress = ipaddress['ip'] else: print ("Asset Doens't Exist") return 403 get_dev_asset_url = "/imcrs/netasset/asset?assetDevice.ip=" + str(ipaddress) f_url = url + get_dev_asset_url # creates the URL using the payload variable as the contents r = requests.get(f_url, auth=auth, headers=HEADERS) # r.status_code try: if r.status_code == 200: dev_asset_info = (json.loads(r.text)) if len(dev_asset_info) > 0: dev_asset_info = dev_asset_info['netAsset'] if type(dev_asset_info) == dict: dev_asset_info = [dev_asset_info] if type(dev_asset_info) == list: dev_asset_info[:] = [dev for dev in dev_asset_info if dev.get('deviceIp') == ipaddress] return dev_asset_info except requests.exceptions.RequestException as e: return "Error:\n" + str(e) + ' get_dev_asset_details: An Error has occured'
def create_dev_vlan(vlanid, vlan_name, auth, url, devid=None, devip=None): """ function takes devid and vlanid vlan_name of specific device and 802.1q VLAN tag and issues a RESTFUL call to add the specified VLAN from the target device. VLAN Name MUST be valid on target device. :param vlanid:int or str value of target 802.1q VLAN :param vlan_name: str value of the target 802.1q VLAN name. MUST be valid name on target device. :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param devid: str requires devid of the target device :param devip: str of ipv4 address of the target device :return: str HTTP Response code. Should be 201 if successfully created :rtype: str >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vlanm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> create_dev_vlan = create_dev_vlan('350', '200', 'test vlan', auth.creds, auth.url) """ if devip is not None: devid = get_dev_details(devip, auth, url)['id'] create_dev_vlan_url = "/imcrs/vlan?devId=" + str(devid) f_url = url + create_dev_vlan_url payload = '''{"vlanId":"%s", "vlanName":"%s"}''' % (str(vlanid), vlan_name) response = requests.post(f_url, data=payload, auth=auth, headers=HEADERS) try: if response.status_code == 201: print('Vlan Created') return 201 elif response.status_code == 409: print('''Unable to create VLAN.\nVLAN Already Exists\nDevice does not support VLAN function''') return 409 except requests.exceptions.RequestException as error: return "Error:\n" + str(error) + " create_dev_vlan: An Error has occured"
def delete_dev_vlans(vlanid, auth, url, devid=None, devip=None): """ function takes devid and vlanid of specific device and 802.1q VLAN tag and issues a RESTFUL call to remove the specified VLAN from the target device. :param vlanid:int or str value of target 802.1q VLAN :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: HTTP response object from requests library. Status code should be 204 if Successful :param devid: str requires devid of the target device :param devip: str of ipv4 address of the target device :rtype: requests.models.Response >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vlanm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> create_dev_vlan = create_dev_vlan('350', '200', 'test vlan', auth.creds, auth.url) """ if devip is not None: devid = get_dev_details(devip, auth, url)['id'] remove_dev_vlan_url = "/imcrs/vlan/delvlan?devId=" + str( devid) + "&vlanId=" + str(vlanid) f_url = url + remove_dev_vlan_url response = requests.delete(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 204: print('Vlan deleted') return response.status_code elif response.status_code == 409: print( 'Unable to delete VLAN.\nVLAN does not Exist\nDevice does not support VLAN ' 'function') return response.status_code except requests.exceptions.RequestException as error: return "Error:\n" + str( error) + " delete_dev_vlans: An Error has occured"
def get_dev_alarms(auth, url, devid=None, devip=None): """ function takes the devId of a specific device and issues a RESTFUL call to get the current alarms for the target device. :param devid: int or str value of the target device :param devip: str of ipv4 address of the target device :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return:list of dictionaries containing the alarms for this device :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.alarms import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> dev_alarms = get_dev_alarms(auth.creds, auth.url, devip='10.101.0.221') >>> assert 'ackStatus' in dev_alarms[0] """ # checks to see if the imc credentials are already available if devip is not None: devid = get_dev_details(devip, auth, url)['id'] f_url = url + "/imcrs/fault/alarm?operatorName=admin&deviceId=" + \ str(devid) + "&desc=false" response = requests.get(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 200: dev_alarm = (json.loads(response.text)) if 'alarm' in dev_alarm: return dev_alarm['alarm'] else: return "Device has no alarms" except requests.exceptions.RequestException as error: return "Error:\n" + str( error) + ' get_dev_alarms: An Error has occured'
def get_dev_vlans(auth, url, devid=None, devip=None): """Function takes input of devID to issue RESTUL call to HP IMC :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param devid: str requires devId as the only input parameter :param devip: str of ipv4 address of the target device :return: list of dictionaries where each element of the list represents one vlan on the target device :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vlanm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> vlans = get_dev_vlans('350', auth.creds, auth.url) >>> assert type(vlans) is list >>> assert 'vlanId' in vlans[0] """ if devip is not None: devid = get_dev_details(devip, auth, url)['id'] get_dev_vlans_url = "/imcrs/vlan?devId=" + str( devid) + "&start=0&size=5000&total=false" f_url = url + get_dev_vlans_url response = requests.get(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 200: dev_vlans = (json.loads(response.text)) return dev_vlans['vlan'] elif response.status_code == 409: return {'vlan': 'no vlans'} except requests.exceptions.RequestException as error: return "Error:\n" + str(error) + ' get_dev_vlans: An Error has occured'
def get_dev_vlans(auth, url, devid = None, devip= None): """Function takes input of devID to issue RESTUL call to HP IMC :param devid: str requires devId as the only input parameter :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: list of dictionaries where each element of the list represents one vlan on the target device :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vlanm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> vlans = get_dev_vlans('350', auth.creds, auth.url) >>> assert type(vlans) is list >>> assert 'vlanId' in vlans[0] """ if devip is not None: devid=get_dev_details(devip, auth, url)['id'] # checks to see if the imc credentials are already available get_dev_vlans_url = "/imcrs/vlan?devId=" + str(devid) + "&start=0&size=5000&total=false" f_url = url + get_dev_vlans_url # creates the URL using the payload variable as the contents r = requests.get(f_url, auth=auth, headers=HEADERS) # r.status_code try: if r.status_code == 200: dev_vlans = (json.loads(r.text)) return dev_vlans['vlan'] elif r.status_code == 409: return {'vlan': 'no vlans'} except requests.exceptions.RequestException as e: return "Error:\n" + str(e) + ' get_dev_vlans: An Error has occured'
def get_dev_alarms(auth, url, devid=None, devip=None): """ function takes the devId of a specific device and issues a RESTFUL call to get the current alarms for the target device. :param devid: int or str value of the target device :param devip: str of ipv4 address of the target device :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return:list of dictionaries containing the alarms for this device :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.alarms import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> dev_alarms = get_dev_alarms(auth.creds, auth.url, devip='10.101.0.221') >>> assert 'ackStatus' in dev_alarms[0] """ # checks to see if the imc credentials are already available if devip is not None: devid = get_dev_details(devip, auth, url)['id'] f_url = url + "/imcrs/fault/alarm?operatorName=admin&deviceId=" + \ str(devid) + "&desc=false" response = requests.get(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 200: dev_alarm = (json.loads(response.text)) if 'alarm' in dev_alarm: return dev_alarm['alarm'] else: return "Device has no alarms" except requests.exceptions.RequestException as error: return "Error:\n" + str(error) + ' get_dev_alarms: An Error has occured'
def modify_hybrid_interface(ifindex, pvid, taggedVlans, untaggedVlans, auth, url, devip=None): if devip is not None: devId=get_dev_details(devip, auth, url)['id'] modify_hybrid_interface_vlan_url = "/imcrs/vlan/hybrid?devId=" + str(devId) + "&start=1&size=500&total=false" f_url = url + modify_hybrid_interface_vlan_url payload = '''{"ifIndex": "'''+ifindex+'''", "pvid": "'''+pvid+'''", "taggedVlans": "'''+taggedVlans+'''", "untagVlanFlag": "true", "untaggedVlans": "'''+untaggedVlans+'''" }''' # creates the URL using the payload variable as the contents r = requests.put(f_url, auth=auth, data=payload, headers=HEADERS) try: if r.status_code == 204: return 204 if r.status_code == 409: return 409 except requests.exceptions.RequestException as e: return "Error:\n" + str(e) + " get_device_hybrid_interfaces: An Error has occured"
def delete_dev_vlans(vlanid, auth, url, devid=None, devip=None): """ function takes devid and vlanid of specific device and 802.1q VLAN tag and issues a RESTFUL call to remove the specified VLAN from the target device. :param vlanid:int or str value of target 802.1q VLAN :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: HTTP response object from requests library. Status code should be 204 if Successful :param devid: str requires devid of the target device :param devip: str of ipv4 address of the target device :rtype: requests.models.Response >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vlanm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> create_dev_vlan = create_dev_vlan('350', '200', 'test vlan', auth.creds, auth.url) """ if devip is not None: devid = get_dev_details(devip, auth, url)['id'] remove_dev_vlan_url = "/imcrs/vlan/delvlan?devId=" + str(devid) + "&vlanId=" + str(vlanid) f_url = url + remove_dev_vlan_url response = requests.delete(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 204: print('Vlan deleted') return response.status_code elif response.status_code == 409: print('Unable to delete VLAN.\nVLAN does not Exist\nDevice does not support VLAN ' 'function') return response.status_code except requests.exceptions.RequestException as error: return "Error:\n" + str(error) + " delete_dev_vlans: An Error has occured"
def delete_hybrid_interface(ifindex, auth, url, devip=None): """ Function takes devip ( ipv4 address ), ifIndex and pvid (vlanid) of specific device and 802.1q VLAN tag and issues a RESTFUL call to remove the specified VLAN from the target device. :param ifIndex: str value of ifIndex for a specific interface on the device :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param devip: str Ipv4 address of target device :return: int of 204 if successful or 409 if not succesful :rtype: int >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vlanm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> delete_hybrid_interface('9', auth.creds, auth.url, devip='10.101.0.221') 409 >>> add_hybrid = add_hybrid_interface('9', '1', '10', '1', auth.creds, auth.url, devip='10.101.0.221') >>> delete_hybrid = delete_hybrid_interface('9', auth.creds, auth.url, devip='10.101.0.221') >>> assert type(delete_hybrid) is int >>> assert delete_hybrid == 204 """ if devip is not None: devId=get_dev_details(devip, auth, url)['id'] delete_hybrid_interface_vlan_url = "/imcrs/vlan/hybrid?devId="+devId+"&ifIndex="+ifindex f_url = url + delete_hybrid_interface_vlan_url r = requests.delete(f_url, auth=auth, headers=HEADERS) # r.status_code try: if r.status_code == 204: return 204 if r.status_code == 409: return 409 except requests.exceptions.RequestException as e: return "Error:\n" + str(e) + " get_device_hybrid_interfaces: An Error has occured"
def get_host_vms(hostip, auth, url): """ function takes hostId as input to RESTFUL call to HP IMC :param hostip: string of ipv4 address of Hypervisor host :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: list of dictionaries where each element of the list represents a single virtual machine which is currently located on the target host. :rtype: list >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vrm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> host_vms = get_host_vms('10.101.0.6', auth.creds, auth.url) >>> assert type(host_vms) is list >>> assert len(host_vms[0]) == 12 >>> assert 'coresPerCpu' in host_vms[0] >>> assert 'cpu' in host_vms[0] >>> assert 'memory' in host_vms[0] >>> assert 'osDesc' in host_vms[0] >>> assert 'parentServerId' in host_vms[0] >>> assert 'porductFlag' in host_vms[0] >>> assert 'vmDevId' in host_vms[0] >>> assert 'vmIP' in host_vms[0] >>> assert 'vmMask' in host_vms[0] >>> assert 'vmName' in host_vms[0] >>> assert 'vmTools' in host_vms[0] """ hostid = get_dev_details(hostip, auth, url)['id'] f_url = url + "/imcrs/vrm/host/vm?hostId=" + str(hostid) response = requests.get(f_url, auth=auth, headers=HEADERS) try: if response.status_code == 200: if len(json.loads(response.text)) > 1: return json.loads(response.text)['vmDevice'] else: return "Device is not a supported Hypervisor" except requests.exceptions.RequestException as error: return "Error:\n" + str(error) + " get_host_vms: An Error has occured"
def test_add_devs_custom_viewsType(self): dev_list = [get_dev_details(CW5_Switch, auth.creds, auth.url)['id']] add_devs = add_devs_custom_views('L1 View', dev_list, auth.creds, auth.url) self.assertIs(add_devs, 204) self.assertIs(type(add_devs), int)
def set_interface_pvid(ifindex, ifType, pvid, auth, url, devip=None, devid=None, allowedVlans=None): """ Function takes devip ( ipv4 address ), ifIndex, ifType and pvid (vlanid) of specific interface and issues a RESTFUL call to remove the specified VLAN from the target device. :param ifindex: str value of ifIndex for a specific interface on the device :param pvid: str value of dot1q VLAN desired to apply to the device :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :param devid: str requires devid of the target device :param devip: str of ipv4 address of the target device :return: int of 204 if successful or 409 if not succesful :rtype: int # TODO: add a test case for trunk interfaces >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.vlanm import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> set_int_vlan = set_interface_pvid('9', 'access', '1', auth.creds, auth.url, devip='10.101.0.221') >>> set_int_vlan = set_interface_pvid('9', 'access', '10', auth.creds, auth.url, devip='10.101.0.221') >>> assert type(set_int_vlan) is int >>> assert set_int_vlan == 204 >>> set_int_vlan = set_interface_pvid('9', 'access', '1', auth.creds, auth.url, devip='10.101.0.221') """ if devip is not None: devid = get_dev_details(devip, auth, url)['id'] if ifType == 'access': set_interface_pvid_url = "/imcrs/vlan/access?devId=" + devid + "&destVlanId=" + pvid \ + "&ifIndex=" + str(ifindex) f_url = url + set_interface_pvid_url response = requests.put(f_url, auth=auth, headers=HEADERS) elif ifType == 'trunk': set_interface_pvid_url = "/imcrs/vlan/trunk?devId=" + devid f_url = url + set_interface_pvid_url if allowedVlans is not None: payload = '''{"ifIndex": "''' + str( ifindex) + '''", "pvid": "''' + pvid + '''", "allowedVlans": "''' + allowedVlans + '''"}''' else: payload = '''{"ifIndex": "''' + str( ifindex) + '''", "pvid": "''' + pvid + '''"}''' response = requests.put(f_url, data=payload, auth=auth, headers=HEADERS) else: raise ValueError( 'Incorrect ifType: \'%s\'. Must be either \'access\' or \'trunk\'' % ifType) try: if response.status_code == 204: return 204 if response.status_code == 409: return 409 except requests.exceptions.RequestException as error: return "Error:\n" + str( error) + " set_access_interface_pvid: An Error has occurred"