Edits history of script submission #24011 for ' Get interfaces (cisco_iosxe)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Get interfaces
     * List the interfaces of a Cisco IOS XE device with their admin and operational status, or get a single interface by name (e.g. GigabitEthernet1/0/1). Reads the Cisco-IOS-XE-interfaces-oper model.
     */
    export async function main(
      auth: RT.CiscoIosxe,
      name: string | undefined,
      full_details: boolean | undefined
    ) {
      const base = `${auth.base_url.replace(/\/+$/, "")}/restconf/data/Cisco-IOS-XE-interfaces-oper:interfaces`
      // List keys go in the path, percent-encoded: GigabitEthernet1/0/1 -> GigabitEthernet1%2F0%2F1.
      const url = new URL(
        name ? `${base}/interface=${encodeURIComponent(name)}` : base
      )
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
          Accept: "application/yang-data+json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const data = response.status === 204 ? {} : await response.json()
      const interfaces: any[] = name
        ? (data["Cisco-IOS-XE-interfaces-oper:interface"] ?? [])
        : (data["Cisco-IOS-XE-interfaces-oper:interfaces"]?.interface ?? [])
    
      if (full_details) {
        return interfaces
      }
      return interfaces.map((i) => ({
        name: i.name,
        interface_type: i["interface-type"],
        description: i.description,
        admin_status: i["admin-status"],
        oper_status: i["oper-status"],
        ipv4: i.ipv4,
        phys_address: i["phys-address"],
        speed: i.speed,
        mtu: i.mtu,
        last_change: i["last-change"],
      }))
    }
    

    Submitted by hugo989 2 hours ago