From 906dd2488f4bdec8b081c1896ceacbe81c104ec6 Mon Sep 17 00:00:00 2001 From: Ragavi916 Date: Thu, 26 Sep 2024 18:49:03 +0530 Subject: [PATCH] added create api key (#129) * Add CreateAPIKey method --- client/api_key.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/client/api_key.go b/client/api_key.go index 85a7f4f7..fe08bc6c 100644 --- a/client/api_key.go +++ b/client/api_key.go @@ -5,11 +5,36 @@ package client import ( "fmt" + "time" clientv1 "github.com/spectrocloud/palette-sdk-go/api/client/v1" "github.com/spectrocloud/palette-sdk-go/api/models" ) +// CreateAPIKey creates a new API key for the tenant +func (h *V1Client) CreateAPIKey(name string, annotations map[string]string, expiry time.Duration) (string, error) { + tenantUID, err := h.GetTenantUID() + if err != nil { + return "", fmt.Errorf("failed to get tenant UID: %w", err) + } + body := &models.V1APIKeyEntity{ + Metadata: &models.V1ObjectMeta{ + Name: name, + Annotations: annotations, + }, + Spec: &models.V1APIKeySpecEntity{ + UserUID: tenantUID, + Expiry: models.V1Time(time.Now().Add(expiry)), + }, + } + params := clientv1.NewV1APIKeysCreateParams().WithBody(body) + resp, err := h.Client.V1APIKeysCreate(params) + if err != nil { + return "", fmt.Errorf("failed to create API key: %w", err) + } + return resp.Payload.APIKey, nil +} + // GetAPIKeys retrieves all API Keys. func (h *V1Client) GetAPIKeys() (*models.V1APIKeys, error) { params := clientv1.NewV1APIKeysListParams()