:chore: added OSC API Client Connection

This commit is contained in:
PacoDw 2020-08-17 10:02:00 -05:00
parent 74f868da37
commit 1055007cba
1 changed files with 47 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi" "github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
) )
// AccessConfig is for common configuration related to Outscale API access // AccessConfig is for common configuration related to Outscale API access
@ -24,6 +25,7 @@ type AccessConfig struct {
SkipMetadataApiCheck bool `mapstructure:"skip_metadata_api_check"` SkipMetadataApiCheck bool `mapstructure:"skip_metadata_api_check"`
Token string `mapstructure:"token"` Token string `mapstructure:"token"`
clientConfig *oapi.Config clientConfig *oapi.Config
API string `mapstructure:"api"`
getOAPIConnection func() oapi.OAPIClient getOAPIConnection func() oapi.OAPIClient
} }
@ -66,7 +68,44 @@ func (c *AccessConfig) Config() (*oapi.Config, error) {
} }
return config, nil return config, nil
}
// NewOSCClient retrieves the Outscale OSC-SDK client
func (c *AccessConfig) NewOSCClient() *osc.APIClient {
if c.AccessKey == "" {
c.AccessKey = os.Getenv("OUTSCALE_ACCESSKEYID")
}
if c.SecretKey == "" {
c.SecretKey = os.Getenv("OUTSCALE_SECRETKEYID")
}
if c.RawRegion == "" {
c.RawRegion = os.Getenv("OUTSCALE_REGION")
}
if c.CustomEndpointOAPI == "" {
c.CustomEndpointOAPI = os.Getenv("OUTSCALE_OAPI_URL")
}
if c.CustomEndpointOAPI == "" {
c.CustomEndpointOAPI = "outscale.com/oapi/latest"
}
skipClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
skipClient.Transport = NewTransport(c.AccessKey, c.SecretKey, c.RawRegion, skipClient.Transport)
return osc.NewAPIClient(&osc.Configuration{
BasePath: fmt.Sprintf("https://api.%s.%s", c.RawRegion, c.CustomEndpointOAPI),
DefaultHeader: make(map[string]string),
UserAgent: "packer-osc",
HTTPClient: skipClient,
})
} }
func (c *AccessConfig) NewOAPIConnection() (oapi.OAPIClient, error) { func (c *AccessConfig) NewOAPIConnection() (oapi.OAPIClient, error) {
@ -102,5 +141,13 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
fmt.Errorf("`access_key` and `secret_key` must both be either set or not set.")) fmt.Errorf("`access_key` and `secret_key` must both be either set or not set."))
} }
if c.API != "" {
if c.API != "osc" && c.API != "oapi" {
c.API = "oapi"
}
} else {
c.API = "oapi"
}
return errs return errs
} }