Edits history of script submission #24012 for ' Get running config (cisco_iosxe)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Get running config
     * Read the running configuration as JSON, whole (Cisco-IOS-XE-native:native) or for a given RESTCONF data path such as Cisco-IOS-XE-native:native/interface/GigabitEthernet=1%2F0%2F1.
     */
    export async function main(
      auth: RT.CiscoIosxe,
      path: string | undefined,
      depth: number | undefined,
      fields: string | undefined
    ) {
      const dataPath = (path || "Cisco-IOS-XE-native:native").replace(/^\/+/, "")
      const url = new URL(
        `${auth.base_url.replace(/\/+$/, "")}/restconf/data/${dataPath}`
      )
      if (depth) {
        url.searchParams.append("depth", String(depth))
      }
      if (fields != null && fields !== "") {
        url.searchParams.append("fields", fields)
      }
    
      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()}`)
      }
      // 204: the path exists but holds no data.
      if (response.status === 204) return {}
    
      return await response.json()
    }
    

    Submitted by hugo989 2 hours ago