Skip to content

Commit

Permalink
added create api key (#129)
Browse files Browse the repository at this point in the history
* Add CreateAPIKey method
  • Loading branch information
Ragavi916 authored Sep 26, 2024
1 parent d5dfc0c commit 906dd24
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions client/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 906dd24

Please sign in to comment.