From 9dee912ee1c7ecdc4dd2b633c9d25072fbbceb8f Mon Sep 17 00:00:00 2001 From: "Sk.Lv" Date: Tue, 16 Jan 2024 11:52:23 +0800 Subject: [PATCH 1/4] fix: ssh key diff suppress --- ksyun/resource_ksyun_ssh_key.go | 30 ++++++++++++++++++++++++---- ksyun/resource_ksyun_ssh_key_test.go | 4 +++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/ksyun/resource_ksyun_ssh_key.go b/ksyun/resource_ksyun_ssh_key.go index c2c9c260..a97358f2 100644 --- a/ksyun/resource_ksyun_ssh_key.go +++ b/ksyun/resource_ksyun_ssh_key.go @@ -24,6 +24,8 @@ package ksyun import ( "fmt" + "strings" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) @@ -49,10 +51,30 @@ func resourceKsyunSSHKey() *schema.Resource { Description: "ID of the key.", }, "public_key": { - Type: schema.TypeString, - Optional: true, - Computed: true, - ForceNew: true, + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + // pKey := d.Get("public_key").(string) + if old == "" { + return false + } + old = strings.TrimSpace(old) + new = strings.TrimSpace(new) + oPks := strings.Split(old, " ") + nPks := strings.Split(new, " ") + for i, s := range oPks { + if i > 1 { + return true + } + if s != nPks[i] { + return false + } + } + + return false + }, Description: "public key.", }, "private_key": { diff --git a/ksyun/resource_ksyun_ssh_key_test.go b/ksyun/resource_ksyun_ssh_key_test.go index 49e6f878..a2402bc1 100644 --- a/ksyun/resource_ksyun_ssh_key_test.go +++ b/ksyun/resource_ksyun_ssh_key_test.go @@ -2,9 +2,10 @@ package ksyun import ( "fmt" + "testing" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" - "testing" ) func TestAccKsyunSSHKey_basic(t *testing.T) { @@ -124,6 +125,7 @@ func testAccCheckSSHKeyDestroy(s *terraform.State) error { const testAccSSHKeyConfig = ` resource "ksyun_ssh_key" "foo" { key_name="sshKeyName" + public_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC5MPEC4hjObq7u9uZY+IHwJE71wRMFPmYMo5Qc56Z9TG/5irXh0l8cu1+Qi9VmmJCeYMU8FrVoaBThjRIgKjuuAF9gKuYx8tWEsURO33F+s0u410PPgpOVyHM6yRO9QNM9iEVBQRk2T8cLdfZuKPQRQH+jyVMAFXpomcx7Q0Yt9rFkZIjC3wBw16MziaCPKVSyCn5SQ+mFNCqqn5+lVc5gXhWAJRwfnSFBNJhXNkEuPAFm1UeNW34Zi96SRA2msIuTBmxt+ZtczIw+MGh2/L8wrTUgXg6j9uD80ZwUKRWkmvapPHaqHZ0gPTvbL6lXtfwU4u2MQrvGSSThKugp1QZf+OC8/F9B4K0ehq6NCjaIscSMW33hO96un6kSvz5HWL0mwlJ+ZXvV6mpUY9X8HSJhOxaA4uX5RO/KqcQWhIku7LxvRBsK8O5EUwCYs6tMCQqpdn+edK5NM7PrO3j+IG6NlHD/JPlNh4pjyzK8oPbfTncYhtwBsAaWQLpW/oFxYv8= lvsongke@lvsongkedeMacBook-Air.local" } ` From cfde8809ce050e540072218bb56111ec6d244df7 Mon Sep 17 00:00:00 2001 From: "Sk.Lv" Date: Wed, 17 Jan 2024 14:57:19 +0800 Subject: [PATCH 2/4] fix: ssh key diff suppress test --- ksyun/resource_ksyun_ssh_key_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/ksyun/resource_ksyun_ssh_key_test.go b/ksyun/resource_ksyun_ssh_key_test.go index a2402bc1..794eb494 100644 --- a/ksyun/resource_ksyun_ssh_key_test.go +++ b/ksyun/resource_ksyun_ssh_key_test.go @@ -125,7 +125,6 @@ func testAccCheckSSHKeyDestroy(s *terraform.State) error { const testAccSSHKeyConfig = ` resource "ksyun_ssh_key" "foo" { key_name="sshKeyName" - public_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC5MPEC4hjObq7u9uZY+IHwJE71wRMFPmYMo5Qc56Z9TG/5irXh0l8cu1+Qi9VmmJCeYMU8FrVoaBThjRIgKjuuAF9gKuYx8tWEsURO33F+s0u410PPgpOVyHM6yRO9QNM9iEVBQRk2T8cLdfZuKPQRQH+jyVMAFXpomcx7Q0Yt9rFkZIjC3wBw16MziaCPKVSyCn5SQ+mFNCqqn5+lVc5gXhWAJRwfnSFBNJhXNkEuPAFm1UeNW34Zi96SRA2msIuTBmxt+ZtczIw+MGh2/L8wrTUgXg6j9uD80ZwUKRWkmvapPHaqHZ0gPTvbL6lXtfwU4u2MQrvGSSThKugp1QZf+OC8/F9B4K0ehq6NCjaIscSMW33hO96un6kSvz5HWL0mwlJ+ZXvV6mpUY9X8HSJhOxaA4uX5RO/KqcQWhIku7LxvRBsK8O5EUwCYs6tMCQqpdn+edK5NM7PrO3j+IG6NlHD/JPlNh4pjyzK8oPbfTncYhtwBsAaWQLpW/oFxYv8= lvsongke@lvsongkedeMacBook-Air.local" } ` From 38243673e31da73b002679b1f376a9fed4e5b3a7 Mon Sep 17 00:00:00 2001 From: "Sk.Lv" Date: Wed, 17 Jan 2024 15:28:42 +0800 Subject: [PATCH 3/4] fix: ssh key diff completed --- ksyun/resource_ksyun_ssh_key.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ksyun/resource_ksyun_ssh_key.go b/ksyun/resource_ksyun_ssh_key.go index a97358f2..cc0d5118 100644 --- a/ksyun/resource_ksyun_ssh_key.go +++ b/ksyun/resource_ksyun_ssh_key.go @@ -62,8 +62,18 @@ func resourceKsyunSSHKey() *schema.Resource { } old = strings.TrimSpace(old) new = strings.TrimSpace(new) + + if old == new { + return true + } oPks := strings.Split(old, " ") nPks := strings.Split(new, " ") + + // new public key is incorrect + if len(nPks) < 2 { + return false + } + for i, s := range oPks { if i > 1 { return true From 34f62aca768927c6e1d2f2fedda7c560277aa8ff Mon Sep 17 00:00:00 2001 From: notone <42347556+notone0010@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:43:35 +0800 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index acb2a9b7..0037ba03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 1.14.2 (Jan 17, 2024) + +BUGFIX: + +- `resource_ksyun_ssh_key` 修复public key comments引发的plan diff + ## 1.14.1 (Jan 12, 2024) IMPROVEMENTS: