From d5dfc0c45e81ba41a8a4f61162dba8119c2842ed Mon Sep 17 00:00:00 2001 From: will <30413278+wcrum@users.noreply.github.com> Date: Mon, 9 Sep 2024 15:05:23 -0600 Subject: [PATCH] feat: api key and standardization of uid action (#128) * feat: api key and standardization of uid action * fix: method comments --- client/api_key.go | 42 ++++++++++++++++++++++++++++++++++++++++++ client/project.go | 4 ++-- client/ssh_key.go | 4 ++-- client/user.go | 6 +++--- 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/client/api_key.go b/client/api_key.go index 5aac8829..85a7f4f7 100644 --- a/client/api_key.go +++ b/client/api_key.go @@ -2,3 +2,45 @@ 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" +) + +// GetAPIKeys retrieves all API Keys. +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 +} + +// DeleteAPIKeyByName deletes an existing API Key by name. +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) +} + +// DeleteAPIKey deletes an existing API Key by UID. +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..a19f2b40 100644 --- a/client/project.go +++ b/client/project.go @@ -38,8 +38,8 @@ func (h *V1Client) GetProjectUID(projectName string) (string, error) { return "", fmt.Errorf("project '%s' not found", projectName) } -// GetProjectByUID retrieves an existing project by UID. -func (h *V1Client) GetProjectByUID(uid string) (*models.V1Project, error) { +// GetProject retrieves an existing project by UID. +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..1adf2c97 100644 --- a/client/ssh_key.go +++ b/client/ssh_key.go @@ -42,8 +42,8 @@ func (h *V1Client) GetSSHKeyByName(name string) (*models.V1UserAssetSSH, error) return nil, fmt.Errorf("ssh key '%s' not found", name) } -// GetSSHKeyByUID retrieves an existing SSH key by UID. -func (h *V1Client) GetSSHKeyByUID(uid string) (*models.V1UserAssetSSH, error) { +// GetSSHKey retrieves an existing SSH key by UID. +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..8369d3a5 100644 --- a/client/user.go +++ b/client/user.go @@ -84,8 +84,8 @@ func (h *V1Client) GetUserByEmail(email string) (*models.V1User, error) { return nil, fmt.Errorf("user with email '%s' not found", email) } -// DeleteUserByUID deletes an existing user by UID. -func (h *V1Client) DeleteUserByUID(uid string) error { +// DeleteUser deletes an existing user by UID. +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)