From 046d68748f7a253c248ae355b69a097d656cc3a8 Mon Sep 17 00:00:00 2001 From: Reinoud Kruithof <2184455+reinoudk@users.noreply.github.com> Date: Sat, 3 Dec 2022 21:25:26 +0100 Subject: [PATCH] fix: webhook import --- sonarcloud/resource_webhook.go | 4 +- sonarcloud/resource_webhook_test.go | 67 ++++++++++++++++++----------- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/sonarcloud/resource_webhook.go b/sonarcloud/resource_webhook.go index 60294f6..db4d8f0 100644 --- a/sonarcloud/resource_webhook.go +++ b/sonarcloud/resource_webhook.go @@ -230,7 +230,7 @@ func (r resourceWebhook) Delete(ctx context.Context, req tfsdk.DeleteResourceReq resp.State.RemoveResource(ctx) } -func (r Webhook) ImportState(ctx context.Context, req tfsdk.ImportResourceStateRequest, resp *tfsdk.ImportResourceStateResponse) { +func (r resourceWebhook) ImportState(ctx context.Context, req tfsdk.ImportResourceStateRequest, resp *tfsdk.ImportResourceStateResponse) { idParts := strings.Split(req.ID, ",") if len(idParts) < 1 || len(idParts) > 2 || idParts[0] == "" { resp.Diagnostics.AddError( @@ -240,7 +240,7 @@ func (r Webhook) ImportState(ctx context.Context, req tfsdk.ImportResourceStateR return } - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), idParts[0])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), idParts[0])...) if len(idParts) == 2 { resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project"), idParts[1])...) } diff --git a/sonarcloud/resource_webhook_test.go b/sonarcloud/resource_webhook_test.go index 15ea764..b9a4fc1 100644 --- a/sonarcloud/resource_webhook_test.go +++ b/sonarcloud/resource_webhook_test.go @@ -4,33 +4,36 @@ import ( "fmt" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "os" "testing" ) func TestAccWebhook(t *testing.T) { - //project := os.Getenv("SONARCLOUD_PROJECT_KEY") + project := os.Getenv("SONARCLOUD_PROJECT_KEY") secret := "ThisIsNotAVeryGoodSecret..." resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProviderFactories, Steps: []resource.TestStep{ - //{ - // Config: testAccProjectWebhookConfig("test", project, secret, "https://www.example.com"), - // Check: resource.ComposeTestCheckFunc( - // resource.TestCheckResourceAttr("sonarcloud_webhook.test", "name", "test"), - // resource.TestCheckResourceAttr("sonarcloud_webhook.test", "url", "https://www.example.com"), - // resource.TestCheckResourceAttr("sonarcloud_webhook.test", "secret", secret), - // ), - //}, - //{ - // Config: testAccProjectWebhookConfig("test-two", project, "", "https://www.example.com/test"), - // Check: resource.ComposeTestCheckFunc( - // resource.TestCheckResourceAttr("sonarcloud_webhook.test", "name", "test-two"), - // resource.TestCheckResourceAttr("sonarcloud_webhook.test", "url", "https://www.example.com/test"), - // resource.TestCheckResourceAttr("sonarcloud_webhook.test", "secret", ""), - // ), - //}, + { + Config: testAccProjectWebhookConfig("test", project, secret, "https://www.example.com"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("sonarcloud_webhook.test", "name", "test"), + resource.TestCheckResourceAttr("sonarcloud_webhook.test", "url", "https://www.example.com"), + resource.TestCheckResourceAttr("sonarcloud_webhook.test", "secret", secret), + ), + }, + webhookImportCheck("sonarcloud_webhook.test", project), + { + Config: testAccProjectWebhookConfig("test-two", project, "", "https://www.example.com/test"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("sonarcloud_webhook.test", "name", "test-two"), + resource.TestCheckResourceAttr("sonarcloud_webhook.test", "url", "https://www.example.com/test"), + resource.TestCheckResourceAttr("sonarcloud_webhook.test", "secret", ""), + ), + }, + webhookImportCheck("sonarcloud_webhook.test", project), { Config: testAccOrganizationWebhookConfig("test", secret, "https://www.example.com"), Check: resource.ComposeTestCheckFunc( @@ -39,14 +42,16 @@ func TestAccWebhook(t *testing.T) { resource.TestCheckResourceAttr("sonarcloud_webhook.test", "secret", secret), ), }, - //{ - // Config: testAccOrganizationWebhookConfig("test-two", "", "https://www.example.com/test"), - // Check: resource.ComposeTestCheckFunc( - // resource.TestCheckResourceAttr("sonarcloud_webhook.test", "name", "test-two"), - // resource.TestCheckResourceAttr("sonarcloud_webhook.test", "url", "https://www.example.com/test"), - // resource.TestCheckResourceAttr("sonarcloud_webhook.test", "secret", ""), - // ), - //}, + webhookImportCheck("sonarcloud_webhook.test", ""), + { + Config: testAccOrganizationWebhookConfig("test-two", "", "https://www.example.com/test"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("sonarcloud_webhook.test", "name", "test-two"), + resource.TestCheckResourceAttr("sonarcloud_webhook.test", "url", "https://www.example.com/test"), + resource.TestCheckResourceAttr("sonarcloud_webhook.test", "secret", ""), + ), + }, + webhookImportCheck("sonarcloud_webhook.test", ""), }, CheckDestroy: testAccWebhookDestroy, }) @@ -78,3 +83,15 @@ resource "sonarcloud_webhook" "test" { `, name, secret, url) return result } + +func webhookImportCheck(resourceName, project string) resource.TestStep { + return resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateIdFunc: func(state *terraform.State) (string, error) { + id := state.RootModule().Resources[resourceName].Primary.ID + return fmt.Sprintf("%s,%s", id, project), nil + }, + ImportStateVerify: true, + } +}