From c382bf4f093f8a5e7cd3f912db433c8e32aad431 Mon Sep 17 00:00:00 2001 From: Will Date: Thu, 5 Sep 2024 15:15:18 -0600 Subject: [PATCH] feat: api key and standardization of uid action --- client/api_key.go | 39 +++++++++++++++++++++++++++++++++++++++ client/project.go | 2 +- client/ssh_key.go | 2 +- client/user.go | 4 ++-- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/client/api_key.go b/client/api_key.go index 5aac8829..227b11f6 100644 --- a/client/api_key.go +++ b/client/api_key.go @@ -2,3 +2,42 @@ package client // CRUDL operations on API keys are all tenant scoped. // See: hubble/services/service/user/internal/service/apikey/apikey_acl.go + +import ( + "fmt" + + clientv1 "github.com/spectrocloud/palette-sdk-go/api/client/v1" + "github.com/spectrocloud/palette-sdk-go/api/models" +) + +func (h *V1Client) GetAPIKeys() (*models.V1APIKeys, error) { + params := clientv1.NewV1APIKeysListParams() + resp, err := h.Client.V1APIKeysList(params) + if err != nil { + return nil, err + } + + return resp.Payload, nil +} + +func (h *V1Client) DeleteAPIKeyByName(name string) error { + keys, err := h.GetAPIKeys() + if err != nil { + return err + } + for _, key := range keys.Items { + if key.Metadata.Name == name { + return h.DeleteAPIKey(key.Metadata.UID) + } + } + return fmt.Errorf("api key with name '%s' not found", name) +} + +func (h *V1Client) DeleteAPIKey(uid string) error { + params := clientv1.NewV1APIKeysUIDDeleteParams().WithUID(uid) + _, err := h.Client.V1APIKeysUIDDelete(params) + if err != nil { + return err + } + return nil +} diff --git a/client/project.go b/client/project.go index 3cb2fa83..5c6dd947 100644 --- a/client/project.go +++ b/client/project.go @@ -39,7 +39,7 @@ func (h *V1Client) GetProjectUID(projectName string) (string, error) { } // GetProjectByUID retrieves an existing project by UID. -func (h *V1Client) GetProjectByUID(uid string) (*models.V1Project, error) { +func (h *V1Client) GetProject(uid string) (*models.V1Project, error) { // ACL scoped to tenant only params := clientv1.NewV1ProjectsUIDGetParams(). WithUID(uid) diff --git a/client/ssh_key.go b/client/ssh_key.go index 8987fb8b..42dc50ba 100644 --- a/client/ssh_key.go +++ b/client/ssh_key.go @@ -43,7 +43,7 @@ func (h *V1Client) GetSSHKeyByName(name string) (*models.V1UserAssetSSH, error) } // GetSSHKeyByUID retrieves an existing SSH key by UID. -func (h *V1Client) GetSSHKeyByUID(uid string) (*models.V1UserAssetSSH, error) { +func (h *V1Client) GetSSHKey(uid string) (*models.V1UserAssetSSH, error) { params := clientv1.NewV1UsersAssetSSHGetUIDParamsWithContext(h.ctx). WithUID(uid) resp, err := h.Client.V1UsersAssetSSHGetUID(params) diff --git a/client/user.go b/client/user.go index 89c1f66d..ee6ebd86 100644 --- a/client/user.go +++ b/client/user.go @@ -85,7 +85,7 @@ func (h *V1Client) GetUserByEmail(email string) (*models.V1User, error) { } // DeleteUserByUID deletes an existing user by UID. -func (h *V1Client) DeleteUserByUID(uid string) error { +func (h *V1Client) DeleteUser(uid string) error { params := clientv1.NewV1UsersUIDDeleteParams().WithUID(uid) _, err := h.Client.V1UsersUIDDelete(params) if err != nil { @@ -102,7 +102,7 @@ func (h *V1Client) DeleteUserByName(name string) error { } for _, user := range users.Items { if user.Metadata.Name == name { - return h.DeleteUserByUID(user.Metadata.UID) + return h.DeleteUser(user.Metadata.UID) } } return fmt.Errorf("user with name '%s' not found", name)