From d3851b067e3ca4d97da6779faf50dc58703fd0d5 Mon Sep 17 00:00:00 2001 From: Anurag Shrivastav Date: Wed, 15 Jun 2022 21:51:57 -0500 Subject: [PATCH 1/9] Network HostUse and Purpose support. --- go.mod | 2 +- go.sum | 4 +- .../acceptance_test/resource_network_test.go | 33 ++++++++++++++ .../datasource_available_resources.go | 2 + internal/resources/resource_network.go | 43 +++++++++++++++++++ 5 files changed, 81 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 9354074..74238c3 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/golangci/golangci-lint v1.46.2 github.com/hashicorp/terraform-plugin-docs v0.9.0 github.com/hashicorp/terraform-plugin-sdk/v2 v2.17.0 - github.com/hewlettpackard/hpegl-metal-client v1.4.1 + github.com/hewlettpackard/hpegl-metal-client v1.4.2 github.com/hewlettpackard/hpegl-provider-lib v0.0.12 github.com/stretchr/testify v1.7.2 gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum index d362cb5..5d39014 100644 --- a/go.sum +++ b/go.sum @@ -586,8 +586,8 @@ github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ= github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= -github.com/hewlettpackard/hpegl-metal-client v1.4.1 h1:KIlSmWZgdassRs07CsRGKgEVGsF9oGsEF0VxcSEDXvU= -github.com/hewlettpackard/hpegl-metal-client v1.4.1/go.mod h1:+PNwuL8j80HEhhTV+TBwvmLtxV5jk5xaZt9U4F7F1Po= +github.com/hewlettpackard/hpegl-metal-client v1.4.2 h1:YN/43G+7rWoy/4GnyzMYYsUydkapUCbo3Om7u8W1r4Y= +github.com/hewlettpackard/hpegl-metal-client v1.4.2/go.mod h1:+PNwuL8j80HEhhTV+TBwvmLtxV5jk5xaZt9U4F7F1Po= github.com/hewlettpackard/hpegl-provider-lib v0.0.12 h1:0c9seGoq34yDiQHv/wdLYEjaR9VCxR775dGdAxHo59k= github.com/hewlettpackard/hpegl-provider-lib v0.0.12/go.mod h1:IKqB5hzOz1zrrQloNP8Ux3j/8Kcq0UUcTRtJmJw7YQE= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= diff --git a/internal/acceptance_test/resource_network_test.go b/internal/acceptance_test/resource_network_test.go index 924f44d..7fe4bf5 100644 --- a/internal/acceptance_test/resource_network_test.go +++ b/internal/acceptance_test/resource_network_test.go @@ -25,6 +25,19 @@ func TestAccResourceNetwork_Basic(t *testing.T) { }) } +func TestAccResourceNetwork_OptFields(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: resource.TestCheckFunc(func(s *terraform.State) error { return testAccCheckNetworkDestroy(t, s) }), + Steps: []resource.TestStep{ + { + Config: testAccCheckNetworkOptFields(), + }, + }, + }) +} + func testAccCheckNetworkBasic() string { return ` provider "hpegl" { @@ -43,6 +56,26 @@ resource "hpegl_metal_network" "pnet" { }` } +func testAccCheckNetworkOptFields() string { + return ` +provider "hpegl" { + metal { + } +} + +variable "location" { + default = "USA:Central:V2DCC01" +} + +resource "hpegl_metal_network" "pnet" { + name = "pnet-test" + location = var.location + description = "tf-net description" + host_use = "Default" + purpose = "Storage" +}` +} + func testAccCheckNetworkDestroy(t *testing.T, s *terraform.State) error { t.Helper() diff --git a/internal/resources/datasource_available_resources.go b/internal/resources/datasource_available_resources.go index 273027d..93997e4 100644 --- a/internal/resources/datasource_available_resources.go +++ b/internal/resources/datasource_available_resources.go @@ -34,6 +34,7 @@ const ( nName = "name" nDescription = "description" nHostUse = "host_use" + nPurpose = "purpose" nLocation = "location" nLocationID = "location_id" nIPPoolID = "ip_pool_id" @@ -348,6 +349,7 @@ func addNetworks(p *configuration.Config, d *schema.ResourceData, available rest nName: net.Name, nDescription: net.Description, nHostUse: net.HostUse, + nPurpose: net.Purpose, nLocationID: net.LocationID, nIPPoolID: net.IPPoolID, } diff --git a/internal/resources/resource_network.go b/internal/resources/resource_network.go index 5435902..79d7277 100644 --- a/internal/resources/resource_network.go +++ b/internal/resources/resource_network.go @@ -158,6 +158,11 @@ func networkSchema() map[string]*schema.Schema { Default: rest.NETWORKHOSTUSE_OPTIONAL, Description: "Required, Optional or Default", }, + nPurpose: { + Type: schema.TypeString, + Optional: true, + Description: getSupportedNetworkPurpose(), + }, nIPPoolID: { Type: schema.TypeString, Computed: true, @@ -234,6 +239,16 @@ func resourceMetalNetworkCreate(d *schema.ResourceData, meta interface{}) (err e NewIPPool: ippool, } + hostUseIntf := d.Get(nHostUse) + if hostUseIntf != nil { + newNetwork.HostUse = rest.NetworkHostUse(hostUseIntf.(string)) + } + + purposeIntf := d.Get(nPurpose) + if purposeIntf != nil { + newNetwork.Purpose = rest.NetworkPurpose(purposeIntf.(string)) + } + ctx := p.GetContext() n, _, err := p.Client.NetworksApi.Add(ctx, newNetwork) if err != nil { @@ -350,6 +365,10 @@ func resourceMetalNetworkRead(d *schema.ResourceData, meta interface{}) (err err return err } + if err = d.Set(nPurpose, n.Purpose); err != nil { + return err + } + if err = d.Set(nIPPoolID, n.IPPoolID); err != nil { return err } @@ -388,6 +407,16 @@ func resourceMetalNetworkUpdate(d *schema.ResourceData, meta interface{}) (err e n.Name = d.Get(nName).(string) n.Description = d.Get(nDescription).(string) + hostUse := d.Get(nHostUse) + if hostUse != nil { + n.HostUse = rest.NetworkHostUse(hostUse.(string)) + } + + purpose := d.Get(nPurpose) + if purpose != nil { + n.Purpose = rest.NetworkPurpose(purpose.(string)) + } + _, _, err = p.Client.NetworksApi.Update(ctx, n.ID, n) if err != nil { return err @@ -420,3 +449,17 @@ func resourceMetalNetworkDelete(d *schema.ResourceData, meta interface{}) (err e return p.RefreshAvailableResources() } + +// getSupportedNetworkPurpose returns a string containing supported network purpose values. +func getSupportedNetworkPurpose() string { + return fmt.Sprintf("%v, %v, %v, %v, %v, %v, %v, %v or %v", + rest.NETWORKPURPOSE_BACKUP, + rest.NETWORKPURPOSE_STORAGE, + rest.NETWORKPURPOSE_VM_KERNEL, + rest.NETWORKPURPOSE_VM_NSX_T, + rest.NETWORKPURPOSE_V_MOTION, + rest.NETWORKPURPOSE_V_CHA, + rest.NETWORKPURPOSE_VM_FT, + rest.NETWORKPURPOSE_I_SCSI_A, + rest.NETWORKPURPOSE_I_SCSI_B) +} From b7a3aac3b3639358ca8a3734ea3d660697409e8c Mon Sep 17 00:00:00 2001 From: Anurag Shrivastav Date: Wed, 15 Jun 2022 22:42:17 -0500 Subject: [PATCH 2/9] Fix type assertion lint errors. --- internal/resources/resource_network.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/internal/resources/resource_network.go b/internal/resources/resource_network.go index 79d7277..82f6761 100644 --- a/internal/resources/resource_network.go +++ b/internal/resources/resource_network.go @@ -239,14 +239,12 @@ func resourceMetalNetworkCreate(d *schema.ResourceData, meta interface{}) (err e NewIPPool: ippool, } - hostUseIntf := d.Get(nHostUse) - if hostUseIntf != nil { - newNetwork.HostUse = rest.NetworkHostUse(hostUseIntf.(string)) + if hostUse, ok := d.Get(nHostUse).(string); ok { + newNetwork.HostUse = rest.NetworkHostUse(hostUse) } - purposeIntf := d.Get(nPurpose) - if purposeIntf != nil { - newNetwork.Purpose = rest.NetworkPurpose(purposeIntf.(string)) + if purpose, ok := d.Get(nPurpose).(string); ok { + newNetwork.Purpose = rest.NetworkPurpose(purpose) } ctx := p.GetContext() @@ -407,14 +405,12 @@ func resourceMetalNetworkUpdate(d *schema.ResourceData, meta interface{}) (err e n.Name = d.Get(nName).(string) n.Description = d.Get(nDescription).(string) - hostUse := d.Get(nHostUse) - if hostUse != nil { - n.HostUse = rest.NetworkHostUse(hostUse.(string)) + if hostUse, ok := d.Get(nHostUse).(string); ok { + n.HostUse = rest.NetworkHostUse(hostUse) } - purpose := d.Get(nPurpose) - if purpose != nil { - n.Purpose = rest.NetworkPurpose(purpose.(string)) + if purpose, ok := d.Get(nPurpose).(string); ok { + n.Purpose = rest.NetworkPurpose(purpose) } _, _, err = p.Client.NetworksApi.Update(ctx, n.ID, n) From 3ac709c9df6232f9e52394fe56a885a384a430fe Mon Sep 17 00:00:00 2001 From: Anurag Shrivastav Date: Thu, 16 Jun 2022 12:12:30 -0500 Subject: [PATCH 3/9] Address review comment. --- internal/resources/resource_network.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/resources/resource_network.go b/internal/resources/resource_network.go index 82f6761..430f7d5 100644 --- a/internal/resources/resource_network.go +++ b/internal/resources/resource_network.go @@ -161,7 +161,7 @@ func networkSchema() map[string]*schema.Schema { nPurpose: { Type: schema.TypeString, Optional: true, - Description: getSupportedNetworkPurpose(), + Description: "Purpose of the network. Allowed values: " + getSupportedNetworkPurpose(), }, nIPPoolID: { Type: schema.TypeString, From 7186d3b5392ae2ed1bc1c2843e99703cafad4392 Mon Sep 17 00:00:00 2001 From: Anurag Shrivastav Date: Thu, 16 Jun 2022 12:15:01 -0500 Subject: [PATCH 4/9] Attempt to address wrapcheck lint error. --- internal/resources/resource_network.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/resources/resource_network.go b/internal/resources/resource_network.go index 430f7d5..748aa01 100644 --- a/internal/resources/resource_network.go +++ b/internal/resources/resource_network.go @@ -326,6 +326,8 @@ func resourceMetalNetworkRead(d *schema.ResourceData, meta interface{}) (err err if errors.As(err, &nErr) { err = fmt.Errorf("failed to read network %s: %w", strings.Trim(nErr.Message(), "\n "), err) + } else { + err = fmt.Errorf("failed to read network %w", err) } }() From 9885e972e2edb2380569771bd9301bab493abcdf Mon Sep 17 00:00:00 2001 From: Anurag Shrivastav Date: Thu, 16 Jun 2022 12:23:46 -0500 Subject: [PATCH 5/9] Add nolint:wrapcheck. --- internal/resources/resource_network.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/resources/resource_network.go b/internal/resources/resource_network.go index 748aa01..5ffd7da 100644 --- a/internal/resources/resource_network.go +++ b/internal/resources/resource_network.go @@ -326,8 +326,6 @@ func resourceMetalNetworkRead(d *schema.ResourceData, meta interface{}) (err err if errors.As(err, &nErr) { err = fmt.Errorf("failed to read network %s: %w", strings.Trim(nErr.Message(), "\n "), err) - } else { - err = fmt.Errorf("failed to read network %w", err) } }() @@ -366,6 +364,7 @@ func resourceMetalNetworkRead(d *schema.ResourceData, meta interface{}) (err err } if err = d.Set(nPurpose, n.Purpose); err != nil { + // nolint:wrapcheck // defer func is wrapping the error. return err } From 962d0e9405bd614e4a020c4fcbb15e95c7a7b96c Mon Sep 17 00:00:00 2001 From: Anurag Shrivastav Date: Thu, 16 Jun 2022 13:24:44 -0500 Subject: [PATCH 6/9] Address wrapcheck lint error - part 2. --- internal/resources/resource_network.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/resources/resource_network.go b/internal/resources/resource_network.go index 5ffd7da..2c511a7 100644 --- a/internal/resources/resource_network.go +++ b/internal/resources/resource_network.go @@ -323,9 +323,11 @@ func getIPPool(set *schema.Set) (ipPool *rest.NewIpPool) { func resourceMetalNetworkRead(d *schema.ResourceData, meta interface{}) (err error) { defer func() { var nErr = rest.GenericOpenAPIError{} + if errors.As(err, &nErr) { err = fmt.Errorf("failed to read network %s: %w", strings.Trim(nErr.Message(), "\n "), err) - + } else { + err = fmt.Errorf("failed to read network: %w", err) } }() @@ -364,7 +366,6 @@ func resourceMetalNetworkRead(d *schema.ResourceData, meta interface{}) (err err } if err = d.Set(nPurpose, n.Purpose); err != nil { - // nolint:wrapcheck // defer func is wrapping the error. return err } From 0b0c79bd6966187b029b59830430ede6b82806f3 Mon Sep 17 00:00:00 2001 From: Anurag Shrivastav Date: Thu, 16 Jun 2022 13:56:23 -0500 Subject: [PATCH 7/9] Add back the nolint:wrapcheck. --- internal/resources/resource_network.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/resources/resource_network.go b/internal/resources/resource_network.go index 2c511a7..bc6723a 100644 --- a/internal/resources/resource_network.go +++ b/internal/resources/resource_network.go @@ -366,6 +366,7 @@ func resourceMetalNetworkRead(d *schema.ResourceData, meta interface{}) (err err } if err = d.Set(nPurpose, n.Purpose); err != nil { + // nolint:wrapcheck // defer func is wrapping the error. return err } From 30e6fe60895e9a2b6c385cba611d03659122bf30 Mon Sep 17 00:00:00 2001 From: Anurag Shrivastav Date: Thu, 16 Jun 2022 15:06:45 -0500 Subject: [PATCH 8/9] Set defer err func to handle specific err. --- internal/resources/resource_network.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/resources/resource_network.go b/internal/resources/resource_network.go index bc6723a..681f589 100644 --- a/internal/resources/resource_network.go +++ b/internal/resources/resource_network.go @@ -326,8 +326,6 @@ func resourceMetalNetworkRead(d *schema.ResourceData, meta interface{}) (err err if errors.As(err, &nErr) { err = fmt.Errorf("failed to read network %s: %w", strings.Trim(nErr.Message(), "\n "), err) - } else { - err = fmt.Errorf("failed to read network: %w", err) } }() From 5857312483eb3d947b0decabb48ded23d6cf32eb Mon Sep 17 00:00:00 2001 From: Anurag Shrivastav Date: Thu, 16 Jun 2022 15:50:32 -0500 Subject: [PATCH 9/9] More updates. --- internal/resources/resource_network.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/resources/resource_network.go b/internal/resources/resource_network.go index 681f589..09bfe71 100644 --- a/internal/resources/resource_network.go +++ b/internal/resources/resource_network.go @@ -326,6 +326,8 @@ func resourceMetalNetworkRead(d *schema.ResourceData, meta interface{}) (err err if errors.As(err, &nErr) { err = fmt.Errorf("failed to read network %s: %w", strings.Trim(nErr.Message(), "\n "), err) + } else if err != nil { + err = fmt.Errorf("failed to read network %w", err) } }()