Edits history of script submission #24037 for ' Create CNAME record (infoblox_nios)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    function wapiUrl(auth: RT.InfobloxNios, path: string) {
      const host = auth.host
        .trim()
        .replace(/^https?:\/\//, "")
        .replace(/\/+$/, "")
      const version = auth.wapi_version.trim().replace(/^v/, "")
      return new URL(`https://${host}/wapi/v${version}/${path}`)
    }
    
    function basicAuth(auth: RT.InfobloxNios) {
      const bytes = new TextEncoder().encode(`${auth.username}:${auth.password}`)
      return `Basic ${btoa(String.fromCharCode(...bytes))}`
    }
    
    /**
     * Create CNAME record
     * Create a DNS CNAME record (record:cname) pointing the alias name to a canonical name. extattrs uses the WAPI shape {"Site": {"value": "East"}}. Returns the created record with its _ref.
     */
    export async function main(
      auth: RT.InfobloxNios,
      name: string,
      canonical: string,
      view: string | undefined,
      ttl: number | undefined,
      comment: string | undefined,
      extattrs: { [key: string]: any } | undefined
    ) {
      const url = wapiUrl(auth, "record:cname")
      url.searchParams.append("_return_fields+", "name,canonical,view,ttl,comment")
      url.searchParams.append("_return_as_object", "1")
    
      const body: { [key: string]: any } = { name, canonical }
      if (view) body.view = view
      if (ttl != null) {
        body.ttl = ttl
        body.use_ttl = true
      }
      if (comment) body.comment = comment
      if (extattrs && Object.keys(extattrs).length > 0) body.extattrs = extattrs
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: basicAuth(auth),
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(body),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 7 hours ago