Skip to content

Commit

Permalink
Supported setCredentials for Azure. (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadkesarwani authored Jan 23, 2025
1 parent 9be19f9 commit 907761a
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/service/setcredentails/providers/azure/create/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"context"
"github.com/spotinst/spotinst-sdk-go/service/account/providers/azure"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"log"

"github.com/spotinst/spotinst-sdk-go/service/account"
"github.com/spotinst/spotinst-sdk-go/spotinst/session"
)

func main() {
sess := session.New()
svc := account.New(sess)
ctx := context.Background()
_, err := svc.CloudProviderAzure().SetCredentials(ctx, &azure.SetCredentialsInput{
Credentials: &azure.Credentials{
AccountId: spotinst.String("accountId"),
ClientId: spotinst.String("clientId"),
ClientSecret: spotinst.String("clientSecret"),
TenantId: spotinst.String("tenantId"),
SubscriptionId: spotinst.String("subscriptionId"),
},
})

if err != nil {
log.Fatalf("spotinst: failed to set credential: %v", err)
}

}
25 changes: 25 additions & 0 deletions examples/service/setcredentails/providers/azure/read/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"context"
"github.com/spotinst/spotinst-sdk-go/service/account/providers/azure"
"log"

"github.com/spotinst/spotinst-sdk-go/service/account"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/spotinst-sdk-go/spotinst/session"
)

func main() {
sess := session.New()
svc := account.New(sess)
ctx := context.Background()
_, err := svc.CloudProviderAzure().ReadCredentials(ctx, &azure.ReadCredentialsInput{
AccountId: spotinst.String("act-123456"),
})

if err != nil {
log.Fatalf("spotinst: failed to fetch credential: %v", err)
}

}
8 changes: 8 additions & 0 deletions service/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"github.com/spotinst/spotinst-sdk-go/service/account/providers/aws"
"github.com/spotinst/spotinst-sdk-go/service/account/providers/azure"
"github.com/spotinst/spotinst-sdk-go/service/account/providers/common"
"github.com/spotinst/spotinst-sdk-go/service/account/providers/gcp"
"github.com/spotinst/spotinst-sdk-go/spotinst"
Expand All @@ -15,6 +16,7 @@ import (
type Service interface {
CloudProviderAWS() aws.Service
CloudProviderGCP() gcp.Service
CloudProviderAzure() azure.Service
CloudProviderCommon() common.Service
}

Expand Down Expand Up @@ -46,6 +48,12 @@ func (s *ServiceOp) CloudProviderGCP() gcp.Service {
}
}

func (s *ServiceOp) CloudProviderAzure() azure.Service {
return &azure.ServiceOp{
Client: s.Client,
}
}

func (s *ServiceOp) CloudProviderCommon() common.Service {
return &common.ServiceOp{
Client: s.Client,
Expand Down
165 changes: 165 additions & 0 deletions service/account/providers/azure/azurecredentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package azure

import (
"context"
"encoding/json"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/spotinst-sdk-go/spotinst/client"
"github.com/spotinst/spotinst-sdk-go/spotinst/util/jsonutil"
"io/ioutil"
"net/http"
)

type Credentials struct {
AccountId *string `json:"accountId,omitempty"`
ClientId *string `json:"clientId,omitempty"`
ClientSecret *string `json:"clientSecret,omitempty"`
TenantId *string `json:"tenantId,omitempty"`
SubscriptionId *string `json:"subscriptionId,omitempty"`

forceSendFields []string
nullFields []string
}

func (o Credentials) MarshalJSON() ([]byte, error) {
type noMethod Credentials
raw := noMethod(o)
return jsonutil.MarshalJSON(raw, o.forceSendFields, o.nullFields)
}

func (o *Credentials) SetClientId(v *string) *Credentials {
if o.ClientId = v; o.ClientId == nil {
o.nullFields = append(o.nullFields, "ClientId")
}
return o
}

func (o *Credentials) SetClientSecret(v *string) *Credentials {
if o.ClientSecret = v; o.ClientSecret == nil {
o.nullFields = append(o.nullFields, "ClientSecret")
}
return o
}

func (o *Credentials) SetTenantId(v *string) *Credentials {
if o.TenantId = v; o.TenantId == nil {
o.nullFields = append(o.nullFields, "TenantId")
}
return o
}

func (o *Credentials) SetSubscriptionId(v *string) *Credentials {
if o.SubscriptionId = v; o.SubscriptionId == nil {
o.nullFields = append(o.nullFields, "SubscriptionId")
}
return o
}

func (o *Credentials) SetAccountId(v *string) *Credentials {
if o.AccountId = v; o.AccountId == nil {
o.nullFields = append(o.nullFields, "AccountId")
}
return o
}

type SetCredentialsInput struct {
Credentials *Credentials `json:"credentials,omitempty"`
}
type SetCredentialsOutput struct {
Credentials *Credentials `json:"Credentials,omitempty"`
}

func (s *ServiceOp) SetCredentials(ctx context.Context, input *SetCredentialsInput) (*SetCredentialsOutput, error) {
r := client.NewRequest(http.MethodPost, "/azure/setup/credentials")

if input != nil {
r.Params.Set("accountId", spotinst.StringValue(input.Credentials.AccountId))
}
input.Credentials.AccountId = nil
r.Obj = input.Credentials

resp, err := client.RequireOK(s.Client.DoOrg(ctx, r))
if err != nil {
return nil, err
}
defer resp.Body.Close()

gs, err := credentialsFromHttpResponse(resp)
if err != nil {
return nil, err
}

output := new(SetCredentialsOutput)
if len(gs) > 0 {
output.Credentials = gs[0]
}

return output, nil
}

type ReadCredentialsInput struct {
AccountId *string `json:"accountId,omitempty"`
}
type ReadCredentialsOutput struct {
Credentials *Credentials `json:"Credentials,omitempty"`
}

func (s *ServiceOp) ReadCredentials(ctx context.Context, input *ReadCredentialsInput) (*ReadCredentialsOutput, error) {
r := client.NewRequest(http.MethodGet, "/azure/setup/credentials")
if input != nil {
r.Params.Set("accountId", spotinst.StringValue(input.AccountId))
}

resp, err := client.RequireOK(s.Client.DoOrg(ctx, r))
if err != nil {
return nil, err
}
defer resp.Body.Close()

gs, err := credentialsFromHttpResponse(resp)
if err != nil {
return nil, err
}

output := new(ReadCredentialsOutput)
if len(gs) > 0 {
output.Credentials = gs[0]
}

return output, nil
}

func credentialsFromHttpResponse(resp *http.Response) ([]*Credentials, error) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return credentialsFromJSON(body)
}

func credentialsFromJSON(in []byte) ([]*Credentials, error) {
var rw client.Response
if err := json.Unmarshal(in, &rw); err != nil {
return nil, err
}
out := make([]*Credentials, len(rw.Response.Items))
if len(out) == 0 {
return out, nil
}
for i, rb := range rw.Response.Items {
b, err := credentialFromJSON(rb)
if err != nil {
return nil, err
}
out[i] = b
}
return out, nil
}

func credentialFromJSON(in []byte) (*Credentials, error) {
b := new(Credentials)
if err := json.Unmarshal(in, b); err != nil {
return nil, err
}
return b, nil
}
35 changes: 35 additions & 0 deletions service/account/providers/azure/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package azure

import (
"context"

"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/spotinst-sdk-go/spotinst/client"
"github.com/spotinst/spotinst-sdk-go/spotinst/session"
)

// Service provides the API operation methods for making requests to endpoints
// of the Spotinst API. See this package's package overview docs for details on
// the service.
type Service interface {
serviceCredentials
}

type serviceCredentials interface {
SetCredentials(context.Context, *SetCredentialsInput) (*SetCredentialsOutput, error)
ReadCredentials(context.Context, *ReadCredentialsInput) (*ReadCredentialsOutput, error)
}

type ServiceOp struct {
Client *client.Client
}

func New(sess *session.Session, cfgs ...*spotinst.Config) *ServiceOp {
cfg := &spotinst.Config{}
cfg.Merge(sess.Config)
cfg.Merge(cfgs...)

return &ServiceOp{
Client: client.New(sess.Config),
}
}

0 comments on commit 907761a

Please sign in to comment.