Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate LKE and MySQL Client Functions to Utilize Request Helpers #540

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions lke_cluster_pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ type LKEClusterPoolCreateOptions = LKENodePoolCreateOptions
// Deprecated: LKEClusterPoolUpdateOptions fields are those accepted by UpdateLKEClusterPool
type LKEClusterPoolUpdateOptions = LKENodePoolUpdateOptions

// Deprecated: LKEClusterPoolsPagedResponse represents a paginated LKEClusterPool API response
type LKEClusterPoolsPagedResponse = LKENodePoolsPagedResponse

// Deprecated: ListLKEClusterPools lists LKEClusterPools
func (c *Client) ListLKEClusterPools(ctx context.Context, clusterID int, opts *ListOptions) ([]LKEClusterPool, error) {
return c.ListLKENodePools(ctx, clusterID, opts)
Expand Down
166 changes: 40 additions & 126 deletions lke_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package linodego
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"

"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)

Expand Down Expand Up @@ -133,32 +130,9 @@ func (i LKECluster) GetUpdateOptions() (o LKEClusterUpdateOptions) {
return
}

// LKEVersionsPagedResponse represents a paginated LKEVersion API response
type LKEVersionsPagedResponse struct {
*PageOptions
Data []LKEVersion `json:"data"`
}

// endpoint gets the endpoint URL for LKEVersion
func (LKEVersionsPagedResponse) endpoint(_ ...any) string {
return "lke/versions"
}

func (resp *LKEVersionsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(LKEVersionsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*LKEVersionsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// ListLKEVersions lists the Kubernetes versions available through LKE. This endpoint is cached by default.
func (c *Client) ListLKEVersions(ctx context.Context, opts *ListOptions) ([]LKEVersion, error) {
response := LKEVersionsPagedResponse{}

endpoint, err := generateListCacheURL(response.endpoint(), opts)
endpoint, err := generateListCacheURL("lke/versions", opts)
if err != nil {
return nil, err
}
Expand All @@ -167,198 +141,138 @@ func (c *Client) ListLKEVersions(ctx context.Context, opts *ListOptions) ([]LKEV
return result.([]LKEVersion), nil
}

err = c.listHelper(ctx, &response, opts)
response, err := getPaginatedResults[LKEVersion](ctx, c, "lke/versions", opts)
ezilber-akamai marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

c.addCachedResponse(endpoint, response.Data, &cacheExpiryTime)
c.addCachedResponse(endpoint, response, &cacheExpiryTime)

return response.Data, nil
return response, nil
}

// GetLKEVersion gets details about a specific LKE Version. This endpoint is cached by default.
func (c *Client) GetLKEVersion(ctx context.Context, version string) (*LKEVersion, error) {
version = url.PathEscape(version)
e := fmt.Sprintf("lke/versions/%s", version)
e := formatAPIPath("lke/versions/%s", version)

if result := c.getCachedResponse(e); result != nil {
result := result.(LKEVersion)
return &result, nil
}

req := c.R(ctx).SetResult(&LKEVersion{})
r, err := coupleAPIErrors(req.Get(e))
response, err := doGETRequest[LKEVersion](ctx, c, e)
if err != nil {
return nil, err
}

c.addCachedResponse(e, r.Result(), &cacheExpiryTime)

return r.Result().(*LKEVersion), nil
}

// LKEClusterAPIEndpointsPagedResponse represents a paginated LKEClusterAPIEndpoints API response
type LKEClusterAPIEndpointsPagedResponse struct {
*PageOptions
Data []LKEClusterAPIEndpoint `json:"data"`
}

// endpoint gets the endpoint URL for LKEClusterAPIEndpointsPagedResponse
func (LKEClusterAPIEndpointsPagedResponse) endpoint(ids ...any) string {
id := ids[0].(int)
return fmt.Sprintf("lke/clusters/%d/api-endpoints", id)
}
c.addCachedResponse(e, response, &cacheExpiryTime)

func (resp *LKEClusterAPIEndpointsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(LKEClusterAPIEndpointsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*LKEClusterAPIEndpointsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
return response, nil
}

// ListLKEClusterAPIEndpoints gets the API Endpoint for the LKE Cluster specified
func (c *Client) ListLKEClusterAPIEndpoints(ctx context.Context, clusterID int, opts *ListOptions) ([]LKEClusterAPIEndpoint, error) {
response := LKEClusterAPIEndpointsPagedResponse{}
err := c.listHelper(ctx, &response, opts, clusterID)
response, err := getPaginatedResults[LKEClusterAPIEndpoint](ctx, c, formatAPIPath("lke/clusters/%d/api-endpoints", clusterID), opts)
if err != nil {
return nil, err
}
return response.Data, nil
}

// LKEClustersPagedResponse represents a paginated LKECluster API response
type LKEClustersPagedResponse struct {
*PageOptions
Data []LKECluster `json:"data"`
}

// endpoint gets the endpoint URL for LKECluster
func (LKEClustersPagedResponse) endpoint(_ ...any) string {
return "lke/clusters"
}

func (resp *LKEClustersPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(LKEClustersPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*LKEClustersPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
return response, nil
}

// ListLKEClusters lists LKEClusters
func (c *Client) ListLKEClusters(ctx context.Context, opts *ListOptions) ([]LKECluster, error) {
response := LKEClustersPagedResponse{}
err := c.listHelper(ctx, &response, opts)
response, err := getPaginatedResults[LKECluster](ctx, c, "lke/clusters", opts)
if err != nil {
return nil, err
}
return response.Data, nil

return response, nil
}

// GetLKECluster gets the lkeCluster with the provided ID
func (c *Client) GetLKECluster(ctx context.Context, clusterID int) (*LKECluster, error) {
e := fmt.Sprintf("lke/clusters/%d", clusterID)
req := c.R(ctx).SetResult(&LKECluster{})
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("lke/clusters/%d", clusterID)
response, err := doGETRequest[LKECluster](ctx, c, e)
if err != nil {
return nil, err
}
return r.Result().(*LKECluster), nil

return response, nil
Comment on lines 195 to +199
Copy link
Contributor

@lgarber-akamai lgarber-akamai Jul 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: This logic can be simplified down to return response, err 👍

}

// CreateLKECluster creates a LKECluster
func (c *Client) CreateLKECluster(ctx context.Context, opts LKEClusterCreateOptions) (*LKECluster, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

e := "lke/clusters"
req := c.R(ctx).SetResult(&LKECluster{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
response, err := doPOSTRequest[LKECluster](ctx, c, e, opts)
if err != nil {
return nil, err
}
return r.Result().(*LKECluster), nil

return response, nil
}

// UpdateLKECluster updates the LKECluster with the specified id
func (c *Client) UpdateLKECluster(ctx context.Context, clusterID int, opts LKEClusterUpdateOptions) (*LKECluster, error) {
body, err := json.Marshal(opts)
e := formatAPIPath("lke/clusters/%d", clusterID)
response, err := doPUTRequest[LKECluster](ctx, c, e, opts)
if err != nil {
return nil, err
}

e := fmt.Sprintf("lke/clusters/%d", clusterID)
req := c.R(ctx).SetResult(&LKECluster{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
if err != nil {
return nil, err
}
return r.Result().(*LKECluster), nil
return response, nil
}

// DeleteLKECluster deletes the LKECluster with the specified id
func (c *Client) DeleteLKECluster(ctx context.Context, clusterID int) error {
e := fmt.Sprintf("lke/clusters/%d", clusterID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
e := formatAPIPath("lke/clusters/%d", clusterID)
err := doDELETERequest(ctx, c, e)
return err
}

// GetLKEClusterKubeconfig gets the Kubeconfig for the LKE Cluster specified
func (c *Client) GetLKEClusterKubeconfig(ctx context.Context, clusterID int) (*LKEClusterKubeconfig, error) {
e := fmt.Sprintf("lke/clusters/%d/kubeconfig", clusterID)
req := c.R(ctx).SetResult(&LKEClusterKubeconfig{})
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("lke/clusters/%d/kubeconfig", clusterID)
response, err := doGETRequest[LKEClusterKubeconfig](ctx, c, e)
if err != nil {
return nil, err
}
return r.Result().(*LKEClusterKubeconfig), nil

return response, nil
}

// GetLKEClusterDashboard gets information about the dashboard for an LKE cluster
func (c *Client) GetLKEClusterDashboard(ctx context.Context, clusterID int) (*LKEClusterDashboard, error) {
e := fmt.Sprintf("lke/clusters/%d/dashboard", clusterID)
req := c.R(ctx).SetResult(&LKEClusterDashboard{})
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("lke/clusters/%d/dashboard", clusterID)
response, err := doGETRequest[LKEClusterDashboard](ctx, c, e)
if err != nil {
return nil, err
}
return r.Result().(*LKEClusterDashboard), nil

return response, nil
}

// RecycleLKEClusterNodes recycles all nodes in all pools of the specified LKE Cluster.
func (c *Client) RecycleLKEClusterNodes(ctx context.Context, clusterID int) error {
e := fmt.Sprintf("lke/clusters/%d/recycle", clusterID)
_, err := coupleAPIErrors(c.R(ctx).Post(e))
e := formatAPIPath("lke/clusters/%d/recycle", clusterID)
_, err := doPOSTRequest[LKECluster, any](ctx, c, e)
return err
}

// RegenerateLKECluster regenerates the Kubeconfig file and/or the service account token for the specified LKE Cluster.
func (c *Client) RegenerateLKECluster(ctx context.Context, clusterID int, opts LKEClusterRegenerateOptions) (*LKECluster, error) {
body, err := json.Marshal(opts)
e := formatAPIPath("lke/clusters/%d/regenerate", clusterID)
response, err := doPOSTRequest[LKECluster](ctx, c, e, opts)
if err != nil {
return nil, err
}

e := fmt.Sprintf("lke/clusters/%d/regenerate", clusterID)
req := c.R(ctx).SetResult(&LKECluster{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}
return r.Result().(*LKECluster), nil
return response, nil
}

// DeleteLKEClusterServiceToken deletes and regenerate the service account token for a Cluster.
func (c *Client) DeleteLKEClusterServiceToken(ctx context.Context, clusterID int) error {
e := fmt.Sprintf("lke/clusters/%d/servicetoken", clusterID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
e := formatAPIPath("lke/clusters/%d/servicetoken", clusterID)
err := doDELETERequest(ctx, c, e)
return err
}
Loading