Edits history of script submission #24080 for ' Delete session (f5_bigip)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    async function connect(auth: RT.F5Bigip): Promise<{
      base: string
      headers: { [key: string]: string }
      release: () => Promise<void>
    }> {
      const base = auth.base_url.replace(/\/+$/, "")
      if (auth.auth_method === "basic") {
        return {
          base,
          headers: {
            Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
          },
          release: async () => {},
        }
      }
      const login = await fetch(`${base}/mgmt/shared/authn/login`, {
        method: "POST",
        headers: { "Content-Type": "application/json", Accept: "application/json" },
        body: JSON.stringify({
          username: auth.username,
          password: auth.password,
          loginProviderName: auth.login_provider_name || "tmos",
        }),
      })
      if (!login.ok) {
        throw new Error(`${login.status} ${await login.text()}`)
      }
      const token: string = (await login.json()).token.token
      return {
        base,
        headers: { "X-F5-Auth-Token": token },
        release: async () => {
          await fetch(
            `${base}/mgmt/shared/authz/tokens/${encodeURIComponent(token)}`,
            {
              method: "DELETE",
              headers: { "X-F5-Auth-Token": token },
            }
          )
        },
      }
    }
    
    /**
     * Delete session
     * Terminate an active APM access session by its session id (as returned by List active sessions). The user loses access to APM resources immediately.
     */
    export async function main(auth: RT.F5Bigip, session_id: string) {
      const f5 = await connect(auth)
      try {
        const response = await fetch(
          `${f5.base}/mgmt/tm/apm/session/${encodeURIComponent(session_id)}`,
          {
            method: "DELETE",
            headers: { ...f5.headers, Accept: "application/json" },
          }
        )
    
        if (!response.ok) {
          throw new Error(`${response.status} ${await response.text()}`)
        }
    
        return { success: true, session_id }
      } finally {
        await f5.release()
      }
    }
    

    Submitted by hugo989 6 hours ago