Skip to content

Commit

Permalink
Merge pull request #353 from equinix/metal-go-setup
Browse files Browse the repository at this point in the history
feat: introduce metal-go client
  • Loading branch information
displague authored Aug 16, 2023
2 parents d672cd5 + eeedd08 commit 24cb78c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
53 changes: 45 additions & 8 deletions equinix/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

v4 "github.com/equinix-labs/fabric-go/fabric/v4"
metalv1 "github.com/equinix-labs/metal-go/metal/v1"
"github.com/equinix/ecx-go/v2"
"github.com/equinix/ne-go"
"github.com/equinix/oauth2-go"
Expand Down Expand Up @@ -80,13 +81,15 @@ type Config struct {
PageSize int
Token string

ecx ecx.Client
ne ne.Client
metal *packngo.Client
ecx ecx.Client
ne ne.Client
metal *packngo.Client
metalgo *metalv1.APIClient

ecxUserAgent string
neUserAgent string
metalUserAgent string
ecxUserAgent string
neUserAgent string
metalUserAgent string
metalGoUserAgent string

terraformVersion string
fabricClient *v4.APIClient
Expand Down Expand Up @@ -158,6 +161,7 @@ func (c *Config) Load(ctx context.Context) error {
c.ecx = ecxClient
c.ne = neClient
c.metal = c.NewMetalClient()
c.metalgo = c.NewMetalGoClient()
c.fabricClient = c.NewFabricClient()
return nil
}
Expand All @@ -183,11 +187,11 @@ func (c *Config) NewFabricClient() *v4.APIClient {
return client
}

// NewMetalClient returns a new client for accessing Equinix Metal's API.
// NewMetalClient returns a new packngo client for accessing Equinix Metal's API.
func (c *Config) NewMetalClient() *packngo.Client {
transport := http.DefaultTransport
// transport = &DumpTransport{http.DefaultTransport} // Debug only
transport = logging.NewTransport("Equinix Metal", transport)
transport = logging.NewTransport("Equinix Metal (packngo)", transport)
retryClient := retryablehttp.NewClient()
retryClient.HTTPClient.Transport = transport
retryClient.RetryMax = c.MaxRetries
Expand All @@ -203,6 +207,35 @@ func (c *Config) NewMetalClient() *packngo.Client {
return client
}

// NewMetalGoClient returns a new metal-go client for accessing Equinix Metal's API.
func (c *Config) NewMetalGoClient() *metalv1.APIClient {
transport := http.DefaultTransport
transport = logging.NewSubsystemLoggingHTTPTransport("Equinix Metal (metal-go)", transport)
retryClient := retryablehttp.NewClient()
retryClient.HTTPClient.Transport = transport
retryClient.RetryMax = c.MaxRetries
retryClient.RetryWaitMin = time.Second
retryClient.RetryWaitMax = c.MaxRetryWait
retryClient.CheckRetry = MetalRetryPolicy
standardClient := retryClient.StandardClient()

baseURL, _ := url.Parse(c.BaseURL)
baseURL.Path = path.Join(baseURL.Path, metalBasePath) + "/"

configuration := metalv1.NewConfiguration()
configuration.Servers = metalv1.ServerConfigurations{
metalv1.ServerConfiguration{
URL: baseURL.String(),
},
}
configuration.HTTPClient = standardClient
configuration.AddDefaultHeader("X-Auth-Token", os.Getenv("METAL_AUTH_TOKEN"))
configuration.UserAgent = c.fullUserAgent(configuration.UserAgent)
client := metalv1.NewAPIClient(configuration)
c.metalGoUserAgent = client.GetConfig().UserAgent
return client
}

func (c *Config) requestTimeout() time.Duration {
if c.RequestTimeout == 0 {
return 5 * time.Second
Expand Down Expand Up @@ -271,6 +304,10 @@ func (c *Config) addModuleToMetalUserAgent(d *schema.ResourceData) {
c.metal.UserAgent = generateModuleUserAgentString(d, c.metalUserAgent)
}

func (c *Config) addModuleToMetalGoUserAgent(d *schema.ResourceData) {
c.metalgo.GetConfig().UserAgent = generateModuleUserAgentString(d, c.metalUserAgent)
}

func generateModuleUserAgentString(d *schema.ResourceData, baseUserAgent string) string {
var m providerMeta
err := d.GetProviderMeta(&m)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/antihax/optional v1.0.0
github.com/equinix-labs/fabric-go v0.4.0
github.com/equinix-labs/metal-go v0.16.0
github.com/equinix/ecx-go/v2 v2.3.1
github.com/equinix/ne-go v1.10.0
github.com/equinix/oauth2-go v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/equinix-labs/fabric-go v0.4.0 h1:YM6jkdPlYJrgUEfCqt1WpXe2gACM5EavRL26ocIMM9c=
github.com/equinix-labs/fabric-go v0.4.0/go.mod h1:/0uePNYbhu/1qWrxhD011AjU6yjf7r0sZgTCn8TyitI=
github.com/equinix-labs/metal-go v0.16.0 h1:4YmGx9SRFkDtHiEqRsSjlgJDztV6NHqH1eeaOZcK7d4=
github.com/equinix-labs/metal-go v0.16.0/go.mod h1:SmxCklxW+KjmBLVMdEXgtFO5gD5/b4N0VxcNgUYbOH4=
github.com/equinix/ecx-go/v2 v2.3.1 h1:gFcAIeyaEUw7S8ebqApmT7E/S7pC7Ac3wgScp89fkPU=
github.com/equinix/ecx-go/v2 v2.3.1/go.mod h1:FvCdZ3jXU8Z4CPKig2DT+4J2HdwgRK17pIcznM7RXyk=
github.com/equinix/ne-go v1.10.0 h1:hy1umXQFPi1b3z/maZ8kqLRFlD1PXD4qp0cV8rnyL8k=
Expand Down

0 comments on commit 24cb78c

Please sign in to comment.