Skip to content

Commit 0a77553

Browse files
Merge pull request #309 from ezilber-akamai/TPT-1775
Add support for new LKE service token endpoints
2 parents 22d99f5 + fc362f3 commit 0a77553

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lke_clusters.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ type LKEVersion struct {
7575
ID string `json:"id"`
7676
}
7777

78+
// LKEClusterRegenerateOptions fields are those accepted by RegenerateLKECluster
79+
type LKEClusterRegenerateOptions struct {
80+
KubeConfig bool `json:"kubeconfig"`
81+
ServiceToken bool `json:"servicetoken"`
82+
}
83+
7884
// UnmarshalJSON implements the json.Unmarshaler interface
7985
func (i *LKECluster) UnmarshalJSON(b []byte) error {
8086
type Mask LKECluster
@@ -322,3 +328,26 @@ func (c *Client) RecycleLKEClusterNodes(ctx context.Context, clusterID int) erro
322328
_, err := coupleAPIErrors(c.R(ctx).Post(e))
323329
return err
324330
}
331+
332+
// RegenerateLKECluster regenerates the Kubeconfig file and/or the service account token for the specified LKE Cluster.
333+
func (c *Client) RegenerateLKECluster(ctx context.Context, clusterID int, opts LKEClusterRegenerateOptions) (*LKECluster, error) {
334+
body, err := json.Marshal(opts)
335+
if err != nil {
336+
return nil, err
337+
}
338+
339+
e := fmt.Sprintf("lke/clusters/%d/regenerate", clusterID)
340+
req := c.R(ctx).SetResult(&LKECluster{}).SetBody(string(body))
341+
r, err := coupleAPIErrors(req.Post(e))
342+
if err != nil {
343+
return nil, err
344+
}
345+
return r.Result().(*LKECluster), nil
346+
}
347+
348+
// DeleteLKEClusterServiceToken deletes and regenerate the service account token for a Cluster.
349+
func (c *Client) DeleteLKEClusterServiceToken(ctx context.Context, clusterID int) error {
350+
e := fmt.Sprintf("lke/clusters/%d/servicetoken", clusterID)
351+
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
352+
return err
353+
}

test/integration/lke_clusters_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"reflect"
77
"testing"
88

9+
"github.com/jarcoal/httpmock"
910
"github.com/linode/linodego"
1011
k8scondition "github.com/linode/linodego/k8s/pkg/condition"
1112
)
@@ -218,6 +219,31 @@ func TestLKEVersion_GetMissing(t *testing.T) {
218219
}
219220
}
220221

222+
func TestLKECluster_Regenerate(t *testing.T) {
223+
client := createMockClient(t)
224+
225+
requestData := linodego.LKEClusterRegenerateOptions{
226+
KubeConfig: true,
227+
ServiceToken: false,
228+
}
229+
230+
httpmock.RegisterRegexpResponder("POST", mockRequestURL(t, "clusters/1234/regenerate"),
231+
mockRequestBodyValidate(t, requestData, nil))
232+
233+
if _, err := client.RegenerateLKECluster(context.Background(), 1234, requestData); err != nil {
234+
t.Fatal(err)
235+
}
236+
}
237+
238+
func TestLKECluster_DeleteServiceToken(t *testing.T) {
239+
client := createMockClient(t)
240+
241+
httpmock.RegisterRegexpResponder("DELETE", mockRequestURL(t, "clusters/1234/servicetoken"), httpmock.NewStringResponder(200, "{}"))
242+
243+
if err := client.DeleteLKEClusterServiceToken(context.Background(), 1234); err != nil {
244+
t.Fatal(err)
245+
}
246+
}
221247
func TestLKEVersion_GetFound(t *testing.T) {
222248
client, teardown := createTestClient(t, "fixtures/TestLKEVersion_GetFound")
223249
defer teardown()

0 commit comments

Comments
 (0)