Skip to content

Commit

Permalink
Merge branch 'main' into OPS-4460-enable-github-actions-for-palette-s…
Browse files Browse the repository at this point in the history
…dk-go-repos
  • Loading branch information
afayasa authored May 24, 2024
2 parents 8a70757 + 6d5477c commit 807402b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
57 changes: 49 additions & 8 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/spectrocloud/palette-api-go/apiutil/transport"
clientV1 "github.com/spectrocloud/palette-api-go/client/v1"
"github.com/spectrocloud/palette-api-go/models"
)

type V1Client struct {
Expand All @@ -18,6 +19,8 @@ type V1Client struct {
ctx context.Context
apikey string
jwt string
username string
password string
hubbleUri string
projectUid string
schemes []string
Expand Down Expand Up @@ -45,12 +48,24 @@ func WithAPIKey(apiKey string) func(*V1Client) {
}
}

func WithJWTToken(jwt string) func(*V1Client) {
func WithJWT(jwt string) func(*V1Client) {
return func(v *V1Client) {
v.jwt = jwt
}
}

func WithUsername(username string) func(*V1Client) {
return func(v *V1Client) {
v.username = username
}
}

func WithPassword(password string) func(*V1Client) {
return func(v *V1Client) {
v.password = password
}
}

func WithHubbleURI(hubbleUri string) func(*V1Client) {
return func(v *V1Client) {
v.hubbleUri = hubbleUri
Expand Down Expand Up @@ -118,7 +133,10 @@ func (h *V1Client) Clone() *V1Client {
opts = append(opts, WithAPIKey(h.apikey))
}
if h.jwt != "" {
opts = append(opts, WithJWTToken(h.jwt))
opts = append(opts, WithJWT(h.jwt))
}
if h.username != "" && h.password != "" {
opts = append(opts, WithUsername(h.username), WithPassword(h.password))
}
if h.projectUid != "" {
opts = append(opts, WithScopeProject(h.projectUid))
Expand All @@ -129,16 +147,20 @@ func (h *V1Client) Clone() *V1Client {
return New(opts...)
}

func (h *V1Client) getTransport() *transport.Runtime {
var httpTransport *transport.Runtime
func (h *V1Client) getTransport() (t *transport.Runtime) {
if h.username != "" && h.password != "" {
if err := h.authenticate(); err != nil {
return nil
}
}
if h.apikey != "" {
httpTransport = h.apiKeyTransport()
t = h.apiKeyTransport()
} else if h.jwt != "" {
httpTransport = h.jwtTransport()
t = h.jwtTransport()
} else {
httpTransport = h.baseTransport()
t = h.baseTransport()
}
return httpTransport
return
}

func (h *V1Client) apiKeyTransport() *transport.Runtime {
Expand All @@ -153,6 +175,25 @@ func (h *V1Client) jwtTransport() *transport.Runtime {
return httpTransport
}

func (h *V1Client) authenticate() error {
httpTransport := h.baseTransport()
c := clientV1.New(httpTransport, strfmt.Default)

params := &clientV1.V1AuthenticateParams{
Body: &models.V1AuthLogin{
EmailID: h.username,
Password: strfmt.Password(h.password),
},
}
resp, err := c.V1Authenticate(params)
if err != nil {
return err
}
h.jwt = resp.Payload.Authorization

return nil
}

func (h *V1Client) baseTransport() *transport.Runtime {
httpTransport := transport.NewWithClient(h.hubbleUri, "", h.schemes, h.httpClient())
httpTransport.RetryAttempts = h.retryAttempts
Expand Down
10 changes: 10 additions & 0 deletions client/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import (
"github.com/spectrocloud/palette-sdk-go/client/apiutil"
)

func (h *V1Client) Authenticate(body *models.V1AuthLogin) (*models.V1UserToken, error) {
params := clientV1.NewV1AuthenticateParams().
WithBody(body)
resp, err := h.Client.V1Authenticate(params)
if err != nil {
return nil, err
}
return resp.Payload, nil
}

func (h *V1Client) GetMe() (*models.V1UserMe, error) {
params := clientV1.NewV1UsersMeGetParamsWithContext(h.ctx)
resp, err := h.Client.V1UsersMeGet(params)
Expand Down

0 comments on commit 807402b

Please sign in to comment.