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

Add support for new LKE service token endpoints #309

Merged
merged 1 commit into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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