From 3da92f750aa6e5a5a204c7070567c0c9352203eb Mon Sep 17 00:00:00 2001 From: pnag90 Date: Sun, 13 Aug 2023 19:40:41 +0100 Subject: [PATCH] Added parameter project for webhook read request (necessary to read webhooks associated by project) and fix response object properties --- sonarqube/resource_sonarqube_webhook.go | 32 ++++++++++++------ sonarqube/resource_sonarqube_webhook_test.go | 35 +++++++------------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/sonarqube/resource_sonarqube_webhook.go b/sonarqube/resource_sonarqube_webhook.go index 9b9f97dd..0ba29b29 100644 --- a/sonarqube/resource_sonarqube_webhook.go +++ b/sonarqube/resource_sonarqube_webhook.go @@ -11,11 +11,10 @@ import ( ) type Webhook struct { - Key string `json:"key"` - Name string `json:"name"` - Url string `json:"url"` - Secret string `json:"secret"` - Project string `json:"project"` // field project added since 7.1 + Key string `json:"key"` + Name string `json:"name"` + Url string `json:"url"` + Secret string `json:"secret"` } type CreateWebhookResponse struct { @@ -57,6 +56,7 @@ func resourceSonarqubeWebhook() *schema.Resource { Type: schema.TypeString, Description: "The key of the project that will own the webhook.", Optional: true, + ForceNew: true, }, }, } @@ -76,6 +76,7 @@ func resourceSonarqubeWebhookCreate(d *schema.ResourceData, m interface{}) error if project, ok := d.GetOk("project"); ok { params.Set("project", project.(string)) } + sonarQubeURL.RawQuery = params.Encode() resp, err := httpRequestHelper( @@ -107,6 +108,13 @@ func resourceSonarqubeWebhookRead(d *schema.ResourceData, m interface{}) error { sonarQubeURL := m.(*ProviderConfiguration).sonarQubeURL sonarQubeURL.Path = strings.TrimSuffix(sonarQubeURL.Path, "/") + "/api/webhooks/list" + if project, ok := d.GetOk("project"); ok { + rawQuery := url.Values{ + "project": []string{string(project.(string))}, + } + sonarQubeURL.RawQuery = rawQuery.Encode() + } + resp, err := httpRequestHelper( m.(*ProviderConfiguration).httpClient, "GET", @@ -129,14 +137,15 @@ func resourceSonarqubeWebhookRead(d *schema.ResourceData, m interface{}) error { if webhook.Key == d.Id() { d.Set("name", webhook.Name) d.Set("url", webhook.Url) + // Field 'project' is not included in the webhook response object, so it is imported from the parameter. + if project, ok := d.GetOk("project"); ok { + d.Set("project", project.(string)) + } // Version 10.1 of sonarqube does not return the secret in the api response anymore. Field 'secret' replaced by flag 'hasSecret' in response // Instead we just set the secret in state to the value being passed in to avoid constant drifts if secret, ok := d.GetOk("secret"); ok { d.Set("secret", secret.(string)) } - if project, ok := d.GetOk("project"); ok { - d.Set("project", project.(string)) - } return nil } } @@ -153,12 +162,13 @@ func resourceSonarqubeWebhookUpdate(d *schema.ResourceData, m interface{}) error "name": []string{d.Get("name").(string)}, "url": []string{d.Get("url").(string)}, } + project := d.Get("project").(string) + if project != "" { + params.Set("project", project) + } if secret, ok := d.GetOk("secret"); ok { params.Set("secret", secret.(string)) } - if project, ok := d.GetOk("project"); ok { - params.Set("project", project.(string)) - } sonarQubeURL.RawQuery = params.Encode() resp, err := httpRequestHelper( diff --git a/sonarqube/resource_sonarqube_webhook_test.go b/sonarqube/resource_sonarqube_webhook_test.go index f1a6f9ed..7a1d8218 100644 --- a/sonarqube/resource_sonarqube_webhook_test.go +++ b/sonarqube/resource_sonarqube_webhook_test.go @@ -92,10 +92,10 @@ resource "sonarqube_webhook" "%s" { func TestAccSonarqubeWebhookProjectBasic(t *testing.T) { rnd := generateRandomResourceName() resourceName := "sonarqube_webhook." + rnd - project := "testAccSonarqubeWebhookProject" name := acctest.RandString(10) url := fmt.Sprintf("https://%s.com", acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + project := "testAccSonarqubeWebhookProject" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -109,32 +109,21 @@ func TestAccSonarqubeWebhookProjectBasic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "project", project), ), }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "name", name), - resource.TestCheckResourceAttr(resourceName, "url", url), - resource.TestCheckResourceAttr(resourceName, "project", project), - ), - }, }, }) } func testAccSonarqubeWebhookProjectBasicConfig(rnd string, name string, url string, project string) string { return fmt.Sprintf(` -resource "sonarqube_project" "%[1]s" { - name = "%[4]s" - project = "%[4]s" - visibility = "public" -} - -resource "sonarqube_webhook" "%[1]s" { - name = "%[2]s" - url = "%[3]s" - project = sonarqube_project.%[1]s.project -} -`, rnd, name, url, project) + resource "sonarqube_project" "%[1]s" { + name = "%[4]s" + project = "%[4]s" + visibility = "public" + } + + resource "sonarqube_webhook" "%[1]s" { + name = "%[2]s" + url = "%[3]s" + project = sonarqube_project.%[1]s.project + }`, rnd, name, url, project) }