From 5302ce0362ea1edc241177c2093c0b8415efaac9 Mon Sep 17 00:00:00 2001 From: Anton Sidelnikov <53078276+anton-sidelnikov@users.noreply.github.com> Date: Wed, 28 Oct 2020 11:24:21 +0300 Subject: [PATCH] suppress schema diff in CCE version (#666) suppress schema diff in CCE version Reviewed-by: Anton Kachurin https://github.com/outcatcher --- opentelekomcloud/diff_suppress_funcs.go | 19 +++++++++++ ...esource_opentelekomcloud_cce_cluster_v3.go | 9 +++--- ...ce_opentelekomcloud_cce_cluster_v3_test.go | 32 +++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/opentelekomcloud/diff_suppress_funcs.go b/opentelekomcloud/diff_suppress_funcs.go index b42554164..7172ed770 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" @@ -138,3 +139,21 @@ func suppressLBWhitelistDiffs(k, old, new string, d *schema.ResourceData) bool { return reflect.DeepEqual(old_array, new_array) } + +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 true + } + if oldArray[i] != newArray[i] { + return false + } + } + return false +} diff --git a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3.go b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3.go index 92153017c..155cff9e4 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: suppressSmartVersionDiff, }, "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 bc637961e..ffa68e97a 100644 --- a/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go +++ b/opentelekomcloud/resource_opentelekomcloud_cce_cluster_v3_test.go @@ -122,6 +122,26 @@ func testAccCheckCCEClusterV3Exists(n string, cluster *clusters.Clusters) resour } } +func TestAccCCEClusterV3_withVersionDiff(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_withInvalidVersion, + Check: resource.ComposeTestCheckFunc( + testAccCheckCCEClusterV3Exists("opentelekomcloud_cce_cluster_v3.cluster_1", &cluster), + resource.TestCheckResourceAttr( + "opentelekomcloud_cce_cluster_v3.cluster_1", "name", "opentelekomcloud-cce"), + ), + }, + }, + }) +} + var testAccCCEClusterV3_basic = fmt.Sprintf(` resource "opentelekomcloud_cce_cluster_v3" "cluster_1" { name = "opentelekomcloud-cce" @@ -166,3 +186,15 @@ resource "opentelekomcloud_cce_cluster_v3" "cluster_1" { multi_az = true } `, OS_VPC_ID, OS_NETWORK_ID) + +var testAccCCEClusterV3_withInvalidVersion = 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)