Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add module_name provider metadata #255

Merged
merged 4 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions equinix/data_source_metal_project_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func TestAccDataSourceMetalProject_basic(t *testing.T) {

func testAccDataSourceMetalProject_basic(r string) string {
return fmt.Sprintf(`
terraform {
ocobles marked this conversation as resolved.
Show resolved Hide resolved
provider_meta "equinix" {
module_name = "test"
}
}

resource "equinix_metal_project" "foobar" {
name = "tfacc-project-%s"
bgp_config {
Expand Down
14 changes: 13 additions & 1 deletion equinix/helpers_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ func waitForDeviceAttribute(d *schema.ResourceData, targets []string, pending []
Target: targets,
Refresh: func() (interface{}, string, error) {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return "error", "error", err
}
client.UserAgent = userAgent
device, _, err := client.Devices.Get(d.Id(), &packngo.GetOptions{Includes: []string{"project"}})
if err == nil {
retAttrVal := device.State
Expand Down Expand Up @@ -216,7 +222,13 @@ func waitForDeviceAttribute(d *schema.ResourceData, targets []string, pending []
// powerOnAndWait Powers on the device and waits for it to be active.
func powerOnAndWait(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal
_, err := client.Devices.PowerOn(d.Id())

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent
_, err = client.Devices.PowerOn(d.Id())
if err != nil {
return friendlyError(err)
}
Expand Down
6 changes: 6 additions & 0 deletions equinix/port_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type ClientPortResource struct {
func getClientPortResource(d *schema.ResourceData, meta interface{}) (*ClientPortResource, *packngo.Response, error) {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return nil, nil, err
}
client.UserAgent = userAgent

port_id := d.Get("port_id").(string)

getOpts := &packngo.GetOptions{Includes: []string{
Expand Down
29 changes: 29 additions & 0 deletions equinix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ func Provider() *schema.Provider {
"equinix_metal_port_vlan_attachment": resourceMetalPortVlanAttachment(),
"equinix_metal_gateway": resourceMetalGateway(),
},
ProviderMetaSchema: map[string]*schema.Schema{
"module_name": {
Type: schema.TypeString,
Optional: true,
},
},
}

provider.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
Expand All @@ -170,6 +176,10 @@ func Provider() *schema.Provider {
return provider
}

type providerMeta struct {
ModuleName string `cty:"module_name"`
}

func configureProvider(ctx context.Context, d *schema.ResourceData, p *schema.Provider) (interface{}, diag.Diagnostics) {
mrws := d.Get("max_retry_wait_seconds").(int)
rt := d.Get("request_timeout").(int)
Expand All @@ -185,7 +195,11 @@ func configureProvider(ctx context.Context, d *schema.ResourceData, p *schema.Pr
MaxRetries: d.Get("max_retries").(int),
MaxRetryWait: time.Duration(mrws) * time.Second,
}
meta := providerMeta{}

if err := d.GetProviderMeta(&meta); err != nil {
return nil, diag.FromErr(err)
}
config.terraformVersion = p.TerraformVersion
if config.terraformVersion == "" {
// Terraform 0.12 introduced this field to the protocol
Expand Down Expand Up @@ -403,3 +417,18 @@ func schemaSetToMap(set *schema.Set) map[int]interface{} {
}
return transformed
}

func generateUserAgentString(d *schema.ResourceData, currentUserAgent string) (string, error) {
var m providerMeta

err := d.GetProviderMeta(&m)
if err != nil {
return currentUserAgent, err
}

if m.ModuleName != "" {
return strings.Join([]string{m.ModuleName, currentUserAgent}, " "), nil
}

return currentUserAgent, nil
}
19 changes: 19 additions & 0 deletions equinix/resource_metal_bgp_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ func resourceMetalBGPSession() *schema.Resource {

func resourceMetalBGPSessionCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent

dID := d.Get("device_id").(string)
addressFamily := d.Get("address_family").(string)
defaultRoute := d.Get("default_route").(bool)
Expand All @@ -69,6 +76,12 @@ func resourceMetalBGPSessionCreate(d *schema.ResourceData, meta interface{}) err

func resourceMetalBGPSessionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent
bgpSession, _, err := client.BGPSessions.Get(d.Id(),
&packngo.GetOptions{Includes: []string{"device"}})
if err != nil {
Expand Down Expand Up @@ -97,6 +110,12 @@ func resourceMetalBGPSessionRead(d *schema.ResourceData, meta interface{}) error

func resourceMetalBGPSessionDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent
resp, err := client.BGPSessions.Delete(d.Id())
return ignoreResponseErrors(httpForbidden, httpNotFound)(resp, err)
}
24 changes: 24 additions & 0 deletions equinix/resource_metal_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ func resourceMetalConnection() *schema.Resource {
func resourceMetalConnectionCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent

facility, facOk := d.GetOk("facility")
metro, metOk := d.GetOk("metro")

Expand Down Expand Up @@ -290,6 +296,12 @@ func resourceMetalConnectionCreate(d *schema.ResourceData, meta interface{}) err
func resourceMetalConnectionUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent

if d.HasChange("locked") {
var action func(string) (*packngo.Response, error)
if d.Get("locked").(bool) {
Expand Down Expand Up @@ -343,6 +355,12 @@ func resourceMetalConnectionUpdate(d *schema.ResourceData, meta interface{}) err

func resourceMetalConnectionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent
connId := d.Id()

conn, _, err := client.Connections.Get(
Expand Down Expand Up @@ -402,6 +420,12 @@ func resourceMetalConnectionRead(d *schema.ResourceData, meta interface{}) error

func resourceMetalConnectionDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent
resp, err := client.Connections.Delete(d.Id(), true)
if ignoreResponseErrors(httpForbidden, httpNotFound)(resp, err) != nil {
return friendlyError(err)
Expand Down
24 changes: 24 additions & 0 deletions equinix/resource_metal_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,12 @@ func shouldReinstall(_ context.Context, d *schema.ResourceDiff, meta interface{}
func resourceMetalDeviceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent

var addressTypesSlice []packngo.IPAddressCreateRequest
_, ok := d.GetOk("ip_address")
if ok {
Expand Down Expand Up @@ -579,6 +585,12 @@ func resourceMetalDeviceCreate(d *schema.ResourceData, meta interface{}) error {
func resourceMetalDeviceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent

device, _, err := client.Devices.Get(d.Id(), deviceReadOptions)
if err != nil {
err = friendlyError(err)
Expand Down Expand Up @@ -681,6 +693,12 @@ func resourceMetalDeviceRead(d *schema.ResourceData, meta interface{}) error {
func resourceMetalDeviceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent

if d.HasChange("locked") {
var action func(string) (*packngo.Response, error)
if d.Get("locked").(bool) {
Expand Down Expand Up @@ -783,6 +801,12 @@ func getReinstallOptions(d *schema.ResourceData) (packngo.DeviceReinstallFields,
func resourceMetalDeviceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent

fdvIf, fdvOk := d.GetOk("force_detach_volumes")
fdv := false
if fdvOk && fdvIf.(bool) {
Expand Down
20 changes: 19 additions & 1 deletion equinix/resource_metal_device_network_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ func getAndPossiblySetNetworkType(d *schema.ResourceData, c *packngo.Client, tar

func resourceMetalDeviceNetworkTypeCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent
ntype := d.Get("type").(string)

err := getAndPossiblySetNetworkType(d, client, ntype)
err = getAndPossiblySetNetworkType(d, client, ntype)
if err != nil {
return err
}
Expand All @@ -85,6 +91,12 @@ func resourceMetalDeviceNetworkTypeCreate(d *schema.ResourceData, meta interface
func resourceMetalDeviceNetworkTypeRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent

_, devNType, err := getDevIDandNetworkType(d, client)
if err != nil {
err = friendlyError(err)
Expand Down Expand Up @@ -112,6 +124,12 @@ func resourceMetalDeviceNetworkTypeRead(d *schema.ResourceData, meta interface{}

func resourceMetalDeviceNetworkTypeUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent
ntype := d.Get("type").(string)
if d.HasChange("type") {
err := getAndPossiblySetNetworkType(d, client, ntype)
Expand Down
18 changes: 18 additions & 0 deletions equinix/resource_metal_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ func resourceMetalGateway() *schema.Resource {
func resourceMetalGatewayCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent

_, hasIPReservation := d.GetOk("ip_reservation_id")
_, hasSubnetSize := d.GetOk("private_ipv4_subnet_size")
if !(hasIPReservation || hasSubnetSize) {
Expand All @@ -120,6 +126,12 @@ func resourceMetalGatewayCreate(d *schema.ResourceData, meta interface{}) error

func resourceMetalGatewayRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent
mgId := d.Id()

includes := &packngo.GetOptions{Includes: []string{"project", "ip_reservation", "virtual_network", "vrf"}}
Expand Down Expand Up @@ -152,6 +164,12 @@ func resourceMetalGatewayRead(d *schema.ResourceData, meta interface{}) error {

func resourceMetalGatewayDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent
resp, err := client.MetalGateways.Delete(d.Id())
if ignoreResponseErrors(httpForbidden, httpNotFound)(resp, err) != nil {
return friendlyError(err)
Expand Down
18 changes: 18 additions & 0 deletions equinix/resource_metal_ip_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func resourceMetalIPAttachment() *schema.Resource {

func resourceMetalIPAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent
deviceID := d.Get("device_id").(string)
ipa := d.Get("cidr_notation").(string)

Expand All @@ -52,6 +58,12 @@ func resourceMetalIPAttachmentCreate(d *schema.ResourceData, meta interface{}) e

func resourceMetalIPAttachmentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent
assignment, _, err := client.DeviceIPs.Get(d.Id(), nil)
if err != nil {
err = friendlyError(err)
Expand Down Expand Up @@ -88,6 +100,12 @@ func resourceMetalIPAttachmentRead(d *schema.ResourceData, meta interface{}) err
func resourceMetalIPAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).metal

userAgent, err := generateUserAgentString(d, client.UserAgent)
if err != nil {
return err
}
client.UserAgent = userAgent

resp, err := client.DeviceIPs.Unassign(d.Id())
if ignoreResponseErrors(httpForbidden, httpNotFound)(resp, err) != nil {
return friendlyError(err)
Expand Down
Loading