Skip to content

Commit

Permalink
Merge pull request #309 from ezilber-akamai/TPT-1775
Browse files Browse the repository at this point in the history
Add support for new LKE service token endpoints
  • Loading branch information
ezilber-akamai-zz authored Mar 10, 2023
2 parents 22d99f5 + fc362f3 commit 0a77553
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lke_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ type LKEVersion struct {
ID string `json:"id"`
}

// LKEClusterRegenerateOptions fields are those accepted by RegenerateLKECluster
type LKEClusterRegenerateOptions struct {
KubeConfig bool `json:"kubeconfig"`
ServiceToken bool `json:"servicetoken"`
}

// UnmarshalJSON implements the json.Unmarshaler interface
func (i *LKECluster) UnmarshalJSON(b []byte) error {
type Mask LKECluster
Expand Down Expand Up @@ -322,3 +328,26 @@ func (c *Client) RecycleLKEClusterNodes(ctx context.Context, clusterID int) erro
_, err := coupleAPIErrors(c.R(ctx).Post(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)
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
}

// 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))
return err
}
26 changes: 26 additions & 0 deletions test/integration/lke_clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"testing"

"github.com/jarcoal/httpmock"
"github.com/linode/linodego"
k8scondition "github.com/linode/linodego/k8s/pkg/condition"
)
Expand Down Expand Up @@ -218,6 +219,31 @@ func TestLKEVersion_GetMissing(t *testing.T) {
}
}

func TestLKECluster_Regenerate(t *testing.T) {
client := createMockClient(t)

requestData := linodego.LKEClusterRegenerateOptions{
KubeConfig: true,
ServiceToken: false,
}

httpmock.RegisterRegexpResponder("POST", mockRequestURL(t, "clusters/1234/regenerate"),
mockRequestBodyValidate(t, requestData, nil))

if _, err := client.RegenerateLKECluster(context.Background(), 1234, requestData); err != nil {
t.Fatal(err)
}
}

func TestLKECluster_DeleteServiceToken(t *testing.T) {
client := createMockClient(t)

httpmock.RegisterRegexpResponder("DELETE", mockRequestURL(t, "clusters/1234/servicetoken"), httpmock.NewStringResponder(200, "{}"))

if err := client.DeleteLKEClusterServiceToken(context.Background(), 1234); err != nil {
t.Fatal(err)
}
}
func TestLKEVersion_GetFound(t *testing.T) {
client, teardown := createTestClient(t, "fixtures/TestLKEVersion_GetFound")
defer teardown()
Expand Down

0 comments on commit 0a77553

Please sign in to comment.