cloudflare / cloudflare-go

The official Go library for the Cloudflare API
https://developers.cloudflare.com/api
BSD 3-Clause "New" or "Revised" License
1.49k stars 594 forks source link

Zerotrust / Access User endpoints. #1425

Closed gavinelder closed 1 year ago

gavinelder commented 1 year ago

Current cloudflare-go version

v0.80.0

Description

Addition of the User methods documented at https://developers.cloudflare.com/api/operations/zero-trust-users-get-users

Use cases

The following API endpoint would be used as part of JML associated with the licence management and incident response processes related to reviewing & revoking cloudflare access / zero trust usage.

Potential cloudflare-go usage

# Example useage

api := yourpackage.NewAPI("API-KEY")

// Define a resource container for the organization level.
orgResource := &yourpackage.ResourceContainer{
    Level:      "organizations",
    Identifier: "ORG-ID",
}

// Define parameters for listing access groups.
listParams := yourpackage.ListAccessGroupsParams{
    PerPage: 50, // Number of results per page.
    Page:    1,  // Page number.
}

// Retrieve a list of access users for the access organization.
accessUsers, _, err := api.GetAccessUsers(context.Background(), orgResource, listParams)
if err != nil {
    fmt.Printf("Error fetching access users: %v\n", err)
    return
}

# Example implementation based on ListAccessGroups

// GetAccessUsers returns all users access within a Cloudflare Access Organisation.
//
// Access API Reference: https://developers.cloudflare.com/api/operations/zero-trust-users-get-users
func (api *API) GetAccessUsers(ctx context.Context, rc *ResourceContainer, params ListAccessGroupsParams) ([]AccessGroup, *ResultInfo, error) {
    baseURL := fmt.Sprintf("/%s/%s/access/users", rc.Level, rc.Identifier)

    autoPaginate := true
    if params.PerPage >= 1 || params.Page >= 1 {
        autoPaginate = false
    }

    if params.PerPage < 1 {
        params.PerPage = 25
    }

    if params.Page < 1 {
        params.Page = 1
    }

    var acccessUsers []AccessUser
    var r AccessGroupListResponse

    for {
        uri := buildURI(baseURL, params)
        res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
        if err != nil {
            return []AccessGroup{}, &ResultInfo{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
        }

        err = json.Unmarshal(res, &r)
        if err != nil {
            return []AccessGroup{}, &ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
        }
        acccessUsers = append(acccessUsers, r.Result...)
        params.ResultInfo = r.ResultInfo.Next()
        if params.ResultInfo.Done() || !autoPaginate {
            break
        }
    }

    return acccessUsers, &r.ResultInfo, nil
}

References

No response

jacobbednarz commented 1 year ago

thanks! you're welcome to send over a PR for this if you'd like to contribute.

gavinelder commented 1 year ago

thanks! you're welcome to send over a PR for this if you'd like to contribute.

Happily.