From 46167b2d61d59c8b7185d5bd791ea34945515028 Mon Sep 17 00:00:00 2001 From: manjunath-batakurki Date: Thu, 22 Feb 2024 12:19:10 -0800 Subject: [PATCH 1/6] Ensure that the network integration is refresh after network creation --- go.mod | 2 +- go.sum | 4 ++ internal/cmp/resource_network.go | 70 ++++++++++++++++++++++++++++++-- internal/utils/constants.go | 15 +++---- 4 files changed, 79 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 152c7d7c..7988ac3a 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/HewlettPackard/hpegl-vmaas-terraform-resources go 1.17 require ( - github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.4 + github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.5-0.20240216175630-2d91386aad41 github.com/golang/mock v1.6.0 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/terraform-plugin-docs v0.9.0 diff --git a/go.sum b/go.sum index 20bd90f6..6449cafe 100644 --- a/go.sum +++ b/go.sum @@ -83,6 +83,10 @@ github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.3 h1:UFovE1vu0cj6paebMNZCD github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.3/go.mod h1:gPk3WuH1P3kF3ROxyHERgKMMFHR+W0Fsspg5vsA0+O0= github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.4 h1:nhUgqXKnnBbl/MJNQjHSfyH+Rc4O0quI+TRDYlAnadw= github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.4/go.mod h1:gPk3WuH1P3kF3ROxyHERgKMMFHR+W0Fsspg5vsA0+O0= +github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.5-0.20240216124444-766c22fd0534 h1:nAiMDY6ioq9ygD+lT0SOgw7f1q17Bc9yI1ac8hj7d5Y= +github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.5-0.20240216124444-766c22fd0534/go.mod h1:gPk3WuH1P3kF3ROxyHERgKMMFHR+W0Fsspg5vsA0+O0= +github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.5-0.20240216175630-2d91386aad41 h1:8muuRVWAboWdIASs2Z2zsRXMdNZ7QQmT9StDCh4bdok= +github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.5-0.20240216175630-2d91386aad41/go.mod h1:gPk3WuH1P3kF3ROxyHERgKMMFHR+W0Fsspg5vsA0+O0= github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= diff --git a/internal/cmp/resource_network.go b/internal/cmp/resource_network.go index c1c541d5..354e2240 100644 --- a/internal/cmp/resource_network.go +++ b/internal/cmp/resource_network.go @@ -5,6 +5,8 @@ package cmp import ( "context" "fmt" + "strings" + "time" "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client" "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/models" @@ -105,6 +107,55 @@ func (r *resNetwork) Create(ctx context.Context, d *utils.Data, meta interface{} if err != nil { return err } + // Refresh NSX integration + serverRefreshResp, err := r.rClient.RefreshNetworkServices(ctx, createReq.NetworkServer.ID, nil) + if !serverRefreshResp.Success { + return fmt.Errorf("failed refresh NSX integration post NSX object creation") + } + if err != nil { + return err + } + errCount := 0 + cRetry := utils.CustomRetry{ + Timeout: time.Minute * 5, + RetryDelay: time.Second * 10, + InitialDelay: time.Second * 10, + Cond: func(response interface{}, err error) (bool, error) { + if err != nil { + errCount++ + // return false as condition if same error returns 3 times. + if errCount == 3 { + return false, err + } + + return false, nil + } + + networkResponse, ok := response.(models.GetSpecificNetworkBody) + if !ok { + errCount++ + if errCount == 3 { + return false, fmt.Errorf("%s", "error while getting Network") + } + + return false, nil + } + errCount = 0 + + if strings.Contains(networkResponse.Network.ExternalID, utils.PortGroupPrefix) { + return true, nil + } + + return false, nil + }, + } + + _, err = cRetry.Retry(ctx, meta, func(ctx context.Context) (interface{}, error) { + return r.nClient.GetSpecificNetwork(ctx, createResp.Network.ID) + }) + if err != nil { + return err + } return tftags.Set(d, createResp.Network) } @@ -140,11 +191,22 @@ func (r *resNetwork) Update(ctx context.Context, d *utils.Data, meta interface{} func (r *resNetwork) Delete(ctx context.Context, d *utils.Data, meta interface{}) error { setMeta(meta, r.rClient.Client) networkID := d.GetID() - resp, err := r.nClient.DeleteNetwork(ctx, networkID) - if !resp.Success { - return fmt.Errorf("got success as false on delete network, error: %w", err) + // wait until deleted + retry := &utils.CustomRetry{ + InitialDelay: time.Second * 10, + RetryDelay: time.Second * 10, + Timeout: time.Minute * 2, + } + resp, err := retry.Retry(ctx, meta, func(ctx context.Context) (interface{}, error) { + return r.nClient.DeleteNetwork(ctx, networkID) + }) + response := resp.(models.SuccessOrErrorMessage) + if !response.Success { + return fmt.Errorf("failed to delete the network due to following error: %s", response.Msg) + } + if err != nil { + return err } - return err } diff --git a/internal/utils/constants.go b/internal/utils/constants.go index 41a40fc5..b33dd2a8 100644 --- a/internal/utils/constants.go +++ b/internal/utils/constants.go @@ -27,11 +27,12 @@ const ( defaultTimeout = time.Hour * 24 noRetryCount = -1 // power constants - PowerOn = "poweron" - PowerOff = "poweroff" - Restart = "restart" - Suspend = "suspend" - Deleting = "deleting" - Deleted = "deleted" - Failed = "failed" + PowerOn = "poweron" + PowerOff = "poweroff" + Restart = "restart" + Suspend = "suspend" + Deleting = "deleting" + Deleted = "deleted" + Failed = "failed" + PortGroupPrefix = "dvportgroup-" ) From 56c20c28b6387a2d4e9caabd1b9b83870d137b17 Mon Sep 17 00:00:00 2001 From: manjunath-batakurki Date: Thu, 22 Feb 2024 12:27:55 -0800 Subject: [PATCH 2/6] Ensure that the network integration is refresh after network creation --- internal/cmp/resource_network.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/cmp/resource_network.go b/internal/cmp/resource_network.go index 354e2240..94eee94a 100644 --- a/internal/cmp/resource_network.go +++ b/internal/cmp/resource_network.go @@ -207,7 +207,9 @@ func (r *resNetwork) Delete(ctx context.Context, d *utils.Data, meta interface{} if err != nil { return err } + return err + } func (r *resNetwork) networkRequest(createReq *models.CreateNetwork) error { From c7760cb6ca9114a1971534e0b448d0048b4621e1 Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Fri, 23 Feb 2024 14:23:26 +0530 Subject: [PATCH 3/6] lint fix --- golangci-lint-config.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/golangci-lint-config.yaml b/golangci-lint-config.yaml index c9aceb6b..eab7d0c1 100644 --- a/golangci-lint-config.yaml +++ b/golangci-lint-config.yaml @@ -1,4 +1,4 @@ -#(C) Copyright 2021 Hewlett Packard Enterprise Development LP +#(C) Copyright 2021-2024 Hewlett Packard Enterprise Development LP run: deadline: 240s @@ -12,6 +12,8 @@ linters-settings: threshold: 110 lll: line-length: 128 + gofumpt: + module-path: "github.com/HewlettPackard/hpegl-vmaas-terraform-resources" goimports: local-prefixes: "github.com" godox: From 290744a6143c43dbc3aea9aa3979ef50fe9412ef Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Fri, 23 Feb 2024 14:50:38 +0530 Subject: [PATCH 4/6] lint fix --- internal/cmp/resource_network.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/cmp/resource_network.go b/internal/cmp/resource_network.go index 94eee94a..58533f3e 100644 --- a/internal/cmp/resource_network.go +++ b/internal/cmp/resource_network.go @@ -209,7 +209,6 @@ func (r *resNetwork) Delete(ctx context.Context, d *utils.Data, meta interface{} } return err - } func (r *resNetwork) networkRequest(createReq *models.CreateNetwork) error { From 4cddef7ec7d249964a1d05bbfe5ef2d02b097b8d Mon Sep 17 00:00:00 2001 From: manjunath-batakurki Date: Fri, 23 Feb 2024 01:34:47 -0800 Subject: [PATCH 5/6] address comments --- go.mod | 2 +- go.sum | 18 ++---------------- internal/cmp/resource_network.go | 2 +- 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 7988ac3a..96b46244 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/HewlettPackard/hpegl-vmaas-terraform-resources go 1.17 require ( - github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.5-0.20240216175630-2d91386aad41 + github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.5 github.com/golang/mock v1.6.0 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/terraform-plugin-docs v0.9.0 diff --git a/go.sum b/go.sum index 6449cafe..bfb2f1f6 100644 --- a/go.sum +++ b/go.sum @@ -71,22 +71,8 @@ github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24/go.mod h1:4UJr5HIiMZrwgkSPdsjy2uOQExX/WEILpIrO9UPGuXs= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.1 h1:OG6FsXwEn3Vt9QeJ0T73jzhF+7iXCucDp1jJjOt+kZI= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.1/go.mod h1:gPk3WuH1P3kF3ROxyHERgKMMFHR+W0Fsspg5vsA0+O0= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.2-0.20230206060111-b9ce4e0ba12b h1:XioqpC43aRWkNuzkF9oZ/S+/Ok3C0hUdM4/BGSyUzRU= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.2-0.20230206060111-b9ce4e0ba12b/go.mod h1:gPk3WuH1P3kF3ROxyHERgKMMFHR+W0Fsspg5vsA0+O0= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.3-0.20230607070149-edbaab2cecc5 h1:sVx0x6IJ20fV4s6+kQK58Ur7rW8tpVNkS021qxgknBU= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.3-0.20230607070149-edbaab2cecc5/go.mod h1:gPk3WuH1P3kF3ROxyHERgKMMFHR+W0Fsspg5vsA0+O0= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.3-0.20230608151449-1fae7d0f2b31 h1:kRhW5VrM18kmdWKgbq0GGkdSqE/QxLmDYWHUW5OL7tA= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.3-0.20230608151449-1fae7d0f2b31/go.mod h1:gPk3WuH1P3kF3ROxyHERgKMMFHR+W0Fsspg5vsA0+O0= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.3 h1:UFovE1vu0cj6paebMNZCDZ4v7F654E7OmSOezO7nAog= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.3/go.mod h1:gPk3WuH1P3kF3ROxyHERgKMMFHR+W0Fsspg5vsA0+O0= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.4 h1:nhUgqXKnnBbl/MJNQjHSfyH+Rc4O0quI+TRDYlAnadw= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.4/go.mod h1:gPk3WuH1P3kF3ROxyHERgKMMFHR+W0Fsspg5vsA0+O0= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.5-0.20240216124444-766c22fd0534 h1:nAiMDY6ioq9ygD+lT0SOgw7f1q17Bc9yI1ac8hj7d5Y= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.5-0.20240216124444-766c22fd0534/go.mod h1:gPk3WuH1P3kF3ROxyHERgKMMFHR+W0Fsspg5vsA0+O0= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.5-0.20240216175630-2d91386aad41 h1:8muuRVWAboWdIASs2Z2zsRXMdNZ7QQmT9StDCh4bdok= -github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.5-0.20240216175630-2d91386aad41/go.mod h1:gPk3WuH1P3kF3ROxyHERgKMMFHR+W0Fsspg5vsA0+O0= +github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.5 h1:LUSZRMK0Bphp9CoIqrVY9NJHgoZxyB2ZRZouqi7zQ1U= +github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.5/go.mod h1:gPk3WuH1P3kF3ROxyHERgKMMFHR+W0Fsspg5vsA0+O0= github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= diff --git a/internal/cmp/resource_network.go b/internal/cmp/resource_network.go index 58533f3e..1302b147 100644 --- a/internal/cmp/resource_network.go +++ b/internal/cmp/resource_network.go @@ -195,7 +195,7 @@ func (r *resNetwork) Delete(ctx context.Context, d *utils.Data, meta interface{} retry := &utils.CustomRetry{ InitialDelay: time.Second * 10, RetryDelay: time.Second * 10, - Timeout: time.Minute * 2, + Timeout: time.Minute * 5, } resp, err := retry.Retry(ctx, meta, func(ctx context.Context) (interface{}, error) { return r.nClient.DeleteNetwork(ctx, networkID) From b4b99fc5552bf1f34c697e6883a6ebb1a53d5eb0 Mon Sep 17 00:00:00 2001 From: manjunath-batakurki Date: Fri, 23 Feb 2024 01:43:25 -0800 Subject: [PATCH 6/6] address comments --- internal/cmp/resource_network.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/cmp/resource_network.go b/internal/cmp/resource_network.go index 1302b147..3447b436 100644 --- a/internal/cmp/resource_network.go +++ b/internal/cmp/resource_network.go @@ -117,7 +117,7 @@ func (r *resNetwork) Create(ctx context.Context, d *utils.Data, meta interface{} } errCount := 0 cRetry := utils.CustomRetry{ - Timeout: time.Minute * 5, + Timeout: time.Minute * 10, RetryDelay: time.Second * 10, InitialDelay: time.Second * 10, Cond: func(response interface{}, err error) (bool, error) { @@ -195,7 +195,7 @@ func (r *resNetwork) Delete(ctx context.Context, d *utils.Data, meta interface{} retry := &utils.CustomRetry{ InitialDelay: time.Second * 10, RetryDelay: time.Second * 10, - Timeout: time.Minute * 5, + Timeout: time.Minute * 10, } resp, err := retry.Retry(ctx, meta, func(ctx context.Context) (interface{}, error) { return r.nClient.DeleteNetwork(ctx, networkID)