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()