Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
fix: webhook import
Browse files Browse the repository at this point in the history
  • Loading branch information
reinoudk committed Dec 3, 2022
1 parent 0f2b7de commit 046d687
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
4 changes: 2 additions & 2 deletions sonarcloud/resource_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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])...)
}
Expand Down
67 changes: 42 additions & 25 deletions sonarcloud/resource_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
})
Expand Down Expand Up @@ -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,
}
}

0 comments on commit 046d687

Please sign in to comment.