Skip to content

Commit

Permalink
F5707/US35357: Network HostUse and Purpose support. (#34)
Browse files Browse the repository at this point in the history
* Network HostUse and Purpose support.

* Fix type assertion lint errors.

* Address review comment.

* Attempt to address wrapcheck lint error.

* Add nolint:wrapcheck.

* Address wrapcheck lint error - part 2.

* Add back the nolint:wrapcheck.

* Set defer err func to handle specific err.

* More updates.
  • Loading branch information
anurag-shrivastav authored Jun 17, 2022
1 parent b0d9033 commit 96dfea8
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
33 changes: 33 additions & 0 deletions internal/acceptance_test/resource_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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()

Expand Down
2 changes: 2 additions & 0 deletions internal/resources/datasource_available_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
nName = "name"
nDescription = "description"
nHostUse = "host_use"
nPurpose = "purpose"
nLocation = "location"
nLocationID = "location_id"
nIPPoolID = "ip_pool_id"
Expand Down Expand Up @@ -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,
}
Expand Down
44 changes: 43 additions & 1 deletion internal/resources/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "Purpose of the network. Allowed values: " + getSupportedNetworkPurpose(),
},
nIPPoolID: {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -234,6 +239,14 @@ func resourceMetalNetworkCreate(d *schema.ResourceData, meta interface{}) (err e
NewIPPool: ippool,
}

if hostUse, ok := d.Get(nHostUse).(string); ok {
newNetwork.HostUse = rest.NetworkHostUse(hostUse)
}

if purpose, ok := d.Get(nPurpose).(string); ok {
newNetwork.Purpose = rest.NetworkPurpose(purpose)
}

ctx := p.GetContext()
n, _, err := p.Client.NetworksApi.Add(ctx, newNetwork)
if err != nil {
Expand Down Expand Up @@ -310,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 if err != nil {
err = fmt.Errorf("failed to read network %w", err)
}
}()

Expand Down Expand Up @@ -350,6 +365,11 @@ func resourceMetalNetworkRead(d *schema.ResourceData, meta interface{}) (err err
return err
}

if err = d.Set(nPurpose, n.Purpose); err != nil {
// nolint:wrapcheck // defer func is wrapping the error.
return err
}

if err = d.Set(nIPPoolID, n.IPPoolID); err != nil {
return err
}
Expand Down Expand Up @@ -388,6 +408,14 @@ func resourceMetalNetworkUpdate(d *schema.ResourceData, meta interface{}) (err e
n.Name = d.Get(nName).(string)
n.Description = d.Get(nDescription).(string)

if hostUse, ok := d.Get(nHostUse).(string); ok {
n.HostUse = rest.NetworkHostUse(hostUse)
}

if purpose, ok := d.Get(nPurpose).(string); ok {
n.Purpose = rest.NetworkPurpose(purpose)
}

_, _, err = p.Client.NetworksApi.Update(ctx, n.ID, n)
if err != nil {
return err
Expand Down Expand Up @@ -420,3 +448,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)
}

0 comments on commit 96dfea8

Please sign in to comment.