From c4d9f4a422816b0c513459e6ab631b6b67f6662b Mon Sep 17 00:00:00 2001 From: Anton Sidelnikov Date: Mon, 26 Oct 2020 10:46:24 +0300 Subject: [PATCH 1/9] added suppress schema diff --- opentelekomcloud/diff_suppress_funcs.go | 7 +++++++ .../resource_opentelekomcloud_cce_cluster_v3.go | 9 +++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/opentelekomcloud/diff_suppress_funcs.go b/opentelekomcloud/diff_suppress_funcs.go index cb5474523..33b775111 100644 --- a/opentelekomcloud/diff_suppress_funcs.go +++ b/opentelekomcloud/diff_suppress_funcs.go @@ -139,3 +139,10 @@ func suppressLBWhitelistDiffs(k, old, new string, d *schema.ResourceData) bool { return reflect.DeepEqual(old_array, new_array) } + +func suppressSchemaDiff(k, old, new string, d *schema.ResourceData) bool { + if strings.ToLower(old) == strings.ToLower(new) { + return true + } + return false +} diff --git a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3.go b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3.go index f4df9081d..8c70f3104 100644 --- a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3.go +++ b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3.go @@ -56,10 +56,11 @@ func resourceCCEClusterV3() *schema.Resource { ForceNew: true, }, "cluster_version": { - Type: schema.TypeString, - Optional: true, - Computed: true, - ForceNew: true, + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + DiffSuppressFunc: suppressSchemaDiff, }, "cluster_type": { Type: schema.TypeString, From bad7d5c62f0fc32d738316d332e78017eb9b0a12 Mon Sep 17 00:00:00 2001 From: Anton Sidelnikov Date: Mon, 26 Oct 2020 14:49:52 +0300 Subject: [PATCH 2/9] change suppress func, added test --- opentelekomcloud/diff_suppress_funcs.go | 9 ++++- ...esource_opentelekomcloud_cce_cluster_v3.go | 2 +- ...ce_opentelekomcloud_cce_cluster_v3_test.go | 39 +++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/opentelekomcloud/diff_suppress_funcs.go b/opentelekomcloud/diff_suppress_funcs.go index 33b775111..82a5b8c0a 100644 --- a/opentelekomcloud/diff_suppress_funcs.go +++ b/opentelekomcloud/diff_suppress_funcs.go @@ -6,6 +6,7 @@ import ( "log" "net/url" "reflect" + "regexp" "sort" "strings" @@ -140,8 +141,12 @@ func suppressLBWhitelistDiffs(k, old, new string, d *schema.ResourceData) bool { return reflect.DeepEqual(old_array, new_array) } -func suppressSchemaDiff(k, old, new string, d *schema.ResourceData) bool { - if strings.ToLower(old) == strings.ToLower(new) { +func suppressCceMinorVersionDiff(k, old, new string, d *schema.ResourceData) bool { + compiled_ver := regexp.MustCompile(`v(\d+\.)?(\d+\.)?(\*|\d+)`) + compiled_patch := regexp.MustCompile(`(-\w+)`) + + if strings.ToLower(compiled_ver.FindString(old)) == strings.ToLower(compiled_ver.FindString(new)) && + strings.ToLower(compiled_patch.FindString(old)) == strings.ToLower(compiled_patch.FindString(new)) { return true } return false diff --git a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3.go b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3.go index 8c70f3104..3b7f77710 100644 --- a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3.go +++ b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3.go @@ -60,7 +60,7 @@ func resourceCCEClusterV3() *schema.Resource { Optional: true, Computed: true, ForceNew: true, - DiffSuppressFunc: suppressSchemaDiff, + DiffSuppressFunc: suppressCceMinorVersionDiff, }, "cluster_type": { Type: schema.TypeString, diff --git a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go index d23266ff4..c0c07410a 100644 --- a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go +++ b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go @@ -122,6 +122,33 @@ func testAccCheckCCEClusterV3Exists(n string, cluster *clusters.Clusters) resour } } +func TestAccCCEClusterV3_update_with_version_diff(t *testing.T) { + var cluster clusters.Clusters + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCCEClusterV3Destroy, + Steps: []resource.TestStep{ + { + Config: testAccCCEClusterV3_basic, + Check: resource.ComposeTestCheckFunc( + testAccCheckCCEClusterV3Exists("opentelekomcloud_cce_cluster_v3.cluster_1", &cluster), + resource.TestCheckResourceAttr( + "opentelekomcloud_cce_cluster_v3.cluster_1", "name", "opentelekomcloud-cce"), + ), + }, + { + Config: testAccCCEClusterV3_update_with_invalid_version, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "opentelekomcloud_cce_cluster_v3.cluster_1", "description", "new description"), + ), + }, + }, + }) +} + var testAccCCEClusterV3_basic = fmt.Sprintf(` resource "opentelekomcloud_cce_cluster_v3" "cluster_1" { name = "opentelekomcloud-cce" @@ -166,3 +193,15 @@ resource "opentelekomcloud_cce_cluster_v3" "cluster_1" { multi_az = true } `, OS_VPC_ID, OS_NETWORK_ID) + +var testAccCCEClusterV3_update_with_invalid_version = fmt.Sprintf(` +resource "opentelekomcloud_cce_cluster_v3" "cluster_1" { + name = "opentelekomcloud-cce" + cluster_type="VirtualMachine" + flavor_id="cce.s1.small" + cluster_version = "v1.9.2" + vpc_id="%s" + subnet_id="%s" + container_network_type="overlay_l2" + description="new description" +}`, OS_VPC_ID, OS_NETWORK_ID) From 179b99a084ccdb206301a63c79fb1ca40998c5e1 Mon Sep 17 00:00:00 2001 From: Anton Sidelnikov Date: Mon, 26 Oct 2020 17:22:53 +0300 Subject: [PATCH 3/9] change suppress func --- opentelekomcloud/diff_suppress_funcs.go | 14 +++++--------- .../resource_opentelekomcloud_cce_cluster_v3.go | 2 +- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/opentelekomcloud/diff_suppress_funcs.go b/opentelekomcloud/diff_suppress_funcs.go index 82a5b8c0a..ebac0328a 100644 --- a/opentelekomcloud/diff_suppress_funcs.go +++ b/opentelekomcloud/diff_suppress_funcs.go @@ -141,13 +141,9 @@ func suppressLBWhitelistDiffs(k, old, new string, d *schema.ResourceData) bool { return reflect.DeepEqual(old_array, new_array) } -func suppressCceMinorVersionDiff(k, old, new string, d *schema.ResourceData) bool { - compiled_ver := regexp.MustCompile(`v(\d+\.)?(\d+\.)?(\*|\d+)`) - compiled_patch := regexp.MustCompile(`(-\w+)`) - - if strings.ToLower(compiled_ver.FindString(old)) == strings.ToLower(compiled_ver.FindString(new)) && - strings.ToLower(compiled_patch.FindString(old)) == strings.ToLower(compiled_patch.FindString(new)) { - return true - } - return false +func suppressSmartVersionDiff(k, old, new string, d *schema.ResourceData) bool { + compiled_ver := regexp.MustCompile(`(\d+)\.(\d+)`) + old_array := compiled_ver.FindStringSubmatch(old) + new_array := compiled_ver.FindStringSubmatch(new) + return reflect.DeepEqual(old_array, new_array) } diff --git a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3.go b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3.go index 3b7f77710..bd80d4ac7 100644 --- a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3.go +++ b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3.go @@ -60,7 +60,7 @@ func resourceCCEClusterV3() *schema.Resource { Optional: true, Computed: true, ForceNew: true, - DiffSuppressFunc: suppressCceMinorVersionDiff, + DiffSuppressFunc: suppressSmartVersionDiff, }, "cluster_type": { Type: schema.TypeString, From b5f00eda31febcfbd597fa6c6bdcc8bfe279b31f Mon Sep 17 00:00:00 2001 From: Anton Sidelnikov Date: Tue, 27 Oct 2020 10:29:27 +0300 Subject: [PATCH 4/9] change test --- opentelekomcloud/diff_suppress_funcs.go | 8 ++++---- .../resource_opentelekomcloud_cce_cluster_v3_test.go | 11 ++--------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/opentelekomcloud/diff_suppress_funcs.go b/opentelekomcloud/diff_suppress_funcs.go index ebac0328a..629f9799c 100644 --- a/opentelekomcloud/diff_suppress_funcs.go +++ b/opentelekomcloud/diff_suppress_funcs.go @@ -142,8 +142,8 @@ func suppressLBWhitelistDiffs(k, old, new string, d *schema.ResourceData) bool { } func suppressSmartVersionDiff(k, old, new string, d *schema.ResourceData) bool { - compiled_ver := regexp.MustCompile(`(\d+)\.(\d+)`) - old_array := compiled_ver.FindStringSubmatch(old) - new_array := compiled_ver.FindStringSubmatch(new) - return reflect.DeepEqual(old_array, new_array) + compiledVer := regexp.MustCompile(`(\d+)\.(\d+)`) + oldArray := compiledVer.FindStringSubmatch(old) + newArray := compiledVer.FindStringSubmatch(new) + return reflect.DeepEqual(oldArray, newArray) } diff --git a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go index c0c07410a..4a65f46be 100644 --- a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go +++ b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go @@ -131,20 +131,13 @@ func TestAccCCEClusterV3_update_with_version_diff(t *testing.T) { CheckDestroy: testAccCheckCCEClusterV3Destroy, Steps: []resource.TestStep{ { - Config: testAccCCEClusterV3_basic, + Config: testAccCCEClusterV3_updateWithInvalidVersion, Check: resource.ComposeTestCheckFunc( testAccCheckCCEClusterV3Exists("opentelekomcloud_cce_cluster_v3.cluster_1", &cluster), resource.TestCheckResourceAttr( "opentelekomcloud_cce_cluster_v3.cluster_1", "name", "opentelekomcloud-cce"), ), }, - { - Config: testAccCCEClusterV3_update_with_invalid_version, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "opentelekomcloud_cce_cluster_v3.cluster_1", "description", "new description"), - ), - }, }, }) } @@ -194,7 +187,7 @@ resource "opentelekomcloud_cce_cluster_v3" "cluster_1" { } `, OS_VPC_ID, OS_NETWORK_ID) -var testAccCCEClusterV3_update_with_invalid_version = fmt.Sprintf(` +var testAccCCEClusterV3_updateWithInvalidVersion = fmt.Sprintf(` resource "opentelekomcloud_cce_cluster_v3" "cluster_1" { name = "opentelekomcloud-cce" cluster_type="VirtualMachine" From 462f0c4d057e4d133d5d4a766cdc545579e840f1 Mon Sep 17 00:00:00 2001 From: Anton Sidelnikov Date: Tue, 27 Oct 2020 10:57:25 +0300 Subject: [PATCH 5/9] new regex in suppres func --- opentelekomcloud/diff_suppress_funcs.go | 9 +++++++-- .../resource_opentelekomcloud_cce_cluster_v3_test.go | 8 ++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/opentelekomcloud/diff_suppress_funcs.go b/opentelekomcloud/diff_suppress_funcs.go index 629f9799c..fb2143a94 100644 --- a/opentelekomcloud/diff_suppress_funcs.go +++ b/opentelekomcloud/diff_suppress_funcs.go @@ -142,8 +142,13 @@ func suppressLBWhitelistDiffs(k, old, new string, d *schema.ResourceData) bool { } func suppressSmartVersionDiff(k, old, new string, d *schema.ResourceData) bool { - compiledVer := regexp.MustCompile(`(\d+)\.(\d+)`) + compiledVer := regexp.MustCompile(`v(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-(\w+))?$`) oldArray := compiledVer.FindStringSubmatch(old) newArray := compiledVer.FindStringSubmatch(new) - return reflect.DeepEqual(oldArray, newArray) + for i := 1; i < len(newArray); i++ { + if oldArray[i] != newArray[i] { + return false + } + } + return true } diff --git a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go index 4a65f46be..0e3e8d313 100644 --- a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go +++ b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go @@ -131,11 +131,11 @@ func TestAccCCEClusterV3_update_with_version_diff(t *testing.T) { CheckDestroy: testAccCheckCCEClusterV3Destroy, Steps: []resource.TestStep{ { - Config: testAccCCEClusterV3_updateWithInvalidVersion, + Config: testAccCCEClusterV3_withInvalidVersion, Check: resource.ComposeTestCheckFunc( testAccCheckCCEClusterV3Exists("opentelekomcloud_cce_cluster_v3.cluster_1", &cluster), resource.TestCheckResourceAttr( - "opentelekomcloud_cce_cluster_v3.cluster_1", "name", "opentelekomcloud-cce"), + "opentelekomcloud_cce_cluster_v3.cluster_1", "name", "opentelekomcloud-cce-ver"), ), }, }, @@ -187,9 +187,9 @@ resource "opentelekomcloud_cce_cluster_v3" "cluster_1" { } `, OS_VPC_ID, OS_NETWORK_ID) -var testAccCCEClusterV3_updateWithInvalidVersion = fmt.Sprintf(` +var testAccCCEClusterV3_withInvalidVersion = fmt.Sprintf(` resource "opentelekomcloud_cce_cluster_v3" "cluster_1" { - name = "opentelekomcloud-cce" + name = "opentelekomcloud-cce-ver" cluster_type="VirtualMachine" flavor_id="cce.s1.small" cluster_version = "v1.9.2" From 662e813687e2f8de641d345f4530eff4094f6f97 Mon Sep 17 00:00:00 2001 From: Anton Sidelnikov Date: Tue, 27 Oct 2020 11:47:34 +0300 Subject: [PATCH 6/9] nil check --- opentelekomcloud/diff_suppress_funcs.go | 7 +++++-- .../resource_opentelekomcloud_cce_cluster_v3_test.go | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/opentelekomcloud/diff_suppress_funcs.go b/opentelekomcloud/diff_suppress_funcs.go index 0a33c1d97..a6087bc35 100644 --- a/opentelekomcloud/diff_suppress_funcs.go +++ b/opentelekomcloud/diff_suppress_funcs.go @@ -144,10 +144,13 @@ func suppressSmartVersionDiff(k, old, new string, d *schema.ResourceData) bool { compiledVer := regexp.MustCompile(`v(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-(\w+))?$`) oldArray := compiledVer.FindStringSubmatch(old) newArray := compiledVer.FindStringSubmatch(new) + if oldArray == nil { + return false + } for i := 1; i < len(newArray); i++ { if oldArray[i] != newArray[i] { - return false + return true } } - return true + return false } diff --git a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go index 1b73619fb..ffa68e97a 100644 --- a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go +++ b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go @@ -122,7 +122,7 @@ func testAccCheckCCEClusterV3Exists(n string, cluster *clusters.Clusters) resour } } -func TestAccCCEClusterV3_update_with_version_diff(t *testing.T) { +func TestAccCCEClusterV3_withVersionDiff(t *testing.T) { var cluster clusters.Clusters resource.Test(t, resource.TestCase{ @@ -135,7 +135,7 @@ func TestAccCCEClusterV3_update_with_version_diff(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckCCEClusterV3Exists("opentelekomcloud_cce_cluster_v3.cluster_1", &cluster), resource.TestCheckResourceAttr( - "opentelekomcloud_cce_cluster_v3.cluster_1", "name", "opentelekomcloud-cce-ver"), + "opentelekomcloud_cce_cluster_v3.cluster_1", "name", "opentelekomcloud-cce"), ), }, }, @@ -189,7 +189,7 @@ resource "opentelekomcloud_cce_cluster_v3" "cluster_1" { var testAccCCEClusterV3_withInvalidVersion = fmt.Sprintf(` resource "opentelekomcloud_cce_cluster_v3" "cluster_1" { - name = "opentelekomcloud-cce-ver" + name = "opentelekomcloud-cce" cluster_type="VirtualMachine" flavor_id="cce.s1.small" cluster_version = "v1.9.2" From 13585f700f1ddf7176ed53826d6d1ff24176a3b2 Mon Sep 17 00:00:00 2001 From: Anton Sidelnikov Date: Tue, 27 Oct 2020 17:51:50 +0300 Subject: [PATCH 7/9] check on not full version string --- opentelekomcloud/diff_suppress_funcs.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/opentelekomcloud/diff_suppress_funcs.go b/opentelekomcloud/diff_suppress_funcs.go index a6087bc35..7172ed770 100644 --- a/opentelekomcloud/diff_suppress_funcs.go +++ b/opentelekomcloud/diff_suppress_funcs.go @@ -148,9 +148,12 @@ func suppressSmartVersionDiff(k, old, new string, d *schema.ResourceData) bool { return false } for i := 1; i < len(newArray); i++ { - if oldArray[i] != newArray[i] { + if oldArray[i] == "" || newArray[i] == "" { return true } + if oldArray[i] != newArray[i] { + return false + } } return false } From 5ad794c918fa9a700ec9dff26af18cbf2f09d7d5 Mon Sep 17 00:00:00 2001 From: Anton Sidelnikov Date: Wed, 28 Oct 2020 10:52:16 +0300 Subject: [PATCH 8/9] only empty check --- opentelekomcloud/diff_suppress_funcs.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/opentelekomcloud/diff_suppress_funcs.go b/opentelekomcloud/diff_suppress_funcs.go index 7172ed770..4062d3054 100644 --- a/opentelekomcloud/diff_suppress_funcs.go +++ b/opentelekomcloud/diff_suppress_funcs.go @@ -151,9 +151,6 @@ func suppressSmartVersionDiff(k, old, new string, d *schema.ResourceData) bool { if oldArray[i] == "" || newArray[i] == "" { return true } - if oldArray[i] != newArray[i] { - return false - } } return false } From 6036e26c76536e4eb7a03a228b849133555de5ad Mon Sep 17 00:00:00 2001 From: Anton Sidelnikov Date: Wed, 28 Oct 2020 10:58:44 +0300 Subject: [PATCH 9/9] Revert "check on not full version string" This reverts commit 13585f70 --- opentelekomcloud/diff_suppress_funcs.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/opentelekomcloud/diff_suppress_funcs.go b/opentelekomcloud/diff_suppress_funcs.go index 4062d3054..7172ed770 100644 --- a/opentelekomcloud/diff_suppress_funcs.go +++ b/opentelekomcloud/diff_suppress_funcs.go @@ -151,6 +151,9 @@ func suppressSmartVersionDiff(k, old, new string, d *schema.ResourceData) bool { if oldArray[i] == "" || newArray[i] == "" { return true } + if oldArray[i] != newArray[i] { + return false + } } return false }