From 0632e8db3ea36e4e722167bfcb849e45883b9c04 Mon Sep 17 00:00:00 2001 From: Ragavi916 Date: Wed, 25 Sep 2024 12:34:56 +0530 Subject: [PATCH 1/6] added create api key --- client/api_key.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/client/api_key.go b/client/api_key.go index 85a7f4f..c623c15 100644 --- a/client/api_key.go +++ b/client/api_key.go @@ -5,11 +5,45 @@ package client import ( "fmt" + "github.com/go-openapi/strfmt" + log "github.com/sirupsen/logrus" + "time" clientv1 "github.com/spectrocloud/palette-sdk-go/api/client/v1" "github.com/spectrocloud/palette-sdk-go/api/models" ) +func (h *V1Client) CreateAPIKey(name, TenantUID, emailID, org, password string) (string, error) { + annotations := map[string]string{ + "Description": "Automation-Test"} + body := &models.V1APIKeyEntity{ + Metadata: &models.V1ObjectMeta{ + Name: name, + Annotations: annotations, + }, + Spec: &models.V1APIKeySpecEntity{ + UserUID: TenantUID, + Expiry: models.V1Time(time.Now().Add(5 * time.Hour)), // Set expiry time + }, + } + if _, err := h.AuthLogin(emailID, org, password); err != nil { + return "", fmt.Errorf("failed to login as sysadmin: %w", err) + } + // Check if the JWT is set + if h.jwt == "" { + return "", fmt.Errorf("JWT is not set after login") + } + log.Infof("JWT before creating API key: %s", h.jwt) + h.Client = clientv1.New(h.getTransport(), strfmt.Default) + + 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.UID, nil +} + // GetAPIKeys retrieves all API Keys. func (h *V1Client) GetAPIKeys() (*models.V1APIKeys, error) { params := clientv1.NewV1APIKeysListParams() @@ -44,3 +78,18 @@ func (h *V1Client) DeleteAPIKey(uid string) error { } return nil } + +func (h *V1Client) AuthLogin(emailID, org, password string) (string, error) { + loginRequest := &models.V1AuthLogin{ + EmailID: emailID, + Org: org, + Password: strfmt.Password(password), + } + loginParams := clientv1.NewV1AuthenticateParams().WithBody(loginRequest) + resp, err := h.Client.V1Authenticate(loginParams) + if err != nil { + return "", fmt.Errorf("failed to login: %w", err) + } + h.jwt = resp.Payload.Authorization + return h.jwt, nil +} From f69b6e2ac29d442c57a8e84ac36b84c6be61387f Mon Sep 17 00:00:00 2001 From: Ragavi916 Date: Wed, 25 Sep 2024 13:42:40 +0530 Subject: [PATCH 2/6] added comments --- client/api_key.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/api_key.go b/client/api_key.go index c623c15..11427e5 100644 --- a/client/api_key.go +++ b/client/api_key.go @@ -13,6 +13,7 @@ import ( "github.com/spectrocloud/palette-sdk-go/api/models" ) +// CreateAPIKey creates a new API key for the specified tenant. func (h *V1Client) CreateAPIKey(name, TenantUID, emailID, org, password string) (string, error) { annotations := map[string]string{ "Description": "Automation-Test"} @@ -41,7 +42,7 @@ func (h *V1Client) CreateAPIKey(name, TenantUID, emailID, org, password string) if err != nil { return "", fmt.Errorf("failed to create API key: %w", err) } - return resp.Payload.UID, nil + return resp.Payload.APIKey, nil } // GetAPIKeys retrieves all API Keys. @@ -79,6 +80,7 @@ func (h *V1Client) DeleteAPIKey(uid string) error { return nil } +// AuthLogin authenticates a user and returns a session token. func (h *V1Client) AuthLogin(emailID, org, password string) (string, error) { loginRequest := &models.V1AuthLogin{ EmailID: emailID, From 170fc142f8a200229b7312bcb69a3f6604b4d1be Mon Sep 17 00:00:00 2001 From: Ragavi916 Date: Wed, 25 Sep 2024 19:50:44 +0530 Subject: [PATCH 3/6] addressed review comments --- client/tenant.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/tenant.go b/client/tenant.go index 82551aa..784a7af 100644 --- a/client/tenant.go +++ b/client/tenant.go @@ -1,6 +1,8 @@ package client -import "errors" +import ( + "errors" +) // GetTenantUID retrieves the tenant UID of the authenticated user. func (h *V1Client) GetTenantUID() (string, error) { From e4626d07a84f4e51d3a54304ab8d45ddc257d9c2 Mon Sep 17 00:00:00 2001 From: Ragavi916 Date: Wed, 25 Sep 2024 20:04:32 +0530 Subject: [PATCH 4/6] reverted --- client/tenant.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/client/tenant.go b/client/tenant.go index 784a7af..82551aa 100644 --- a/client/tenant.go +++ b/client/tenant.go @@ -1,8 +1,6 @@ package client -import ( - "errors" -) +import "errors" // GetTenantUID retrieves the tenant UID of the authenticated user. func (h *V1Client) GetTenantUID() (string, error) { From 22368e52ecfc417fc3c0f5f4d9fa78b2c35ff48b Mon Sep 17 00:00:00 2001 From: Ragavi916 Date: Thu, 26 Sep 2024 12:49:51 +0530 Subject: [PATCH 5/6] addressed review comments --- client/api_key.go | 41 +++++++---------------------------------- 1 file changed, 7 insertions(+), 34 deletions(-) diff --git a/client/api_key.go b/client/api_key.go index 11427e5..7154487 100644 --- a/client/api_key.go +++ b/client/api_key.go @@ -5,38 +5,27 @@ package client import ( "fmt" - "github.com/go-openapi/strfmt" - log "github.com/sirupsen/logrus" "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 specified tenant. -func (h *V1Client) CreateAPIKey(name, TenantUID, emailID, org, password string) (string, error) { - annotations := map[string]string{ - "Description": "Automation-Test"} +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(5 * time.Hour)), // Set expiry time + UserUID: tenantUID, + Expiry: models.V1Time(time.Now().Add(expiry)), }, } - if _, err := h.AuthLogin(emailID, org, password); err != nil { - return "", fmt.Errorf("failed to login as sysadmin: %w", err) - } - // Check if the JWT is set - if h.jwt == "" { - return "", fmt.Errorf("JWT is not set after login") - } - log.Infof("JWT before creating API key: %s", h.jwt) - h.Client = clientv1.New(h.getTransport(), strfmt.Default) - params := clientv1.NewV1APIKeysCreateParams().WithBody(body) resp, err := h.Client.V1APIKeysCreate(params) if err != nil { @@ -79,19 +68,3 @@ func (h *V1Client) DeleteAPIKey(uid string) error { } return nil } - -// AuthLogin authenticates a user and returns a session token. -func (h *V1Client) AuthLogin(emailID, org, password string) (string, error) { - loginRequest := &models.V1AuthLogin{ - EmailID: emailID, - Org: org, - Password: strfmt.Password(password), - } - loginParams := clientv1.NewV1AuthenticateParams().WithBody(loginRequest) - resp, err := h.Client.V1Authenticate(loginParams) - if err != nil { - return "", fmt.Errorf("failed to login: %w", err) - } - h.jwt = resp.Payload.Authorization - return h.jwt, nil -} From d0a3aaab1cecefd6f62ac6ef6f398fb6035bbcc9 Mon Sep 17 00:00:00 2001 From: Ragavi916 Date: Thu, 26 Sep 2024 18:43:05 +0530 Subject: [PATCH 6/6] added doc strings --- client/api_key.go | 1 + 1 file changed, 1 insertion(+) diff --git a/client/api_key.go b/client/api_key.go index 7154487..fe08bc6 100644 --- a/client/api_key.go +++ b/client/api_key.go @@ -11,6 +11,7 @@ import ( "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 {