-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supported setCredentials for Azure. (#341)
- Loading branch information
1 parent
9be19f9
commit 907761a
Showing
5 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
examples/service/setcredentails/providers/azure/create/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
examples/service/setcredentails/providers/azure/read/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |