From 5c67d02bfb0387d4b66ad8a65eb40cabf66020f9 Mon Sep 17 00:00:00 2001 From: Charles Treatman Date: Mon, 7 Aug 2023 13:28:47 -0500 Subject: [PATCH] feat: introduce metal-go client This adds a metal-go client to the terraform provider. The metal-go client can be gradually adopted by Metal resources and data sources in this provider to enable the eventual deprecation of packngo. An example of using this client to interact with the Metal API can be found in #291. --- equinix/config.go | 53 ++++++++++++++++++++++++++++++++++++++++------- go.mod | 1 + go.sum | 2 ++ 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/equinix/config.go b/equinix/config.go index 56e55c55f..226237739 100644 --- a/equinix/config.go +++ b/equinix/config.go @@ -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" @@ -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 @@ -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 } @@ -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 @@ -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 @@ -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) diff --git a/go.mod b/go.mod index 8923ec48e..1d498e03e 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 631474437..f5db03ee3 100644 --- a/go.sum +++ b/go.sum @@ -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=