Skip to content

Commit

Permalink
Fix xray_webhook headers not being imported correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhung committed Dec 18, 2024
1 parent 728d55c commit 442da39
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ attach:

acceptance: fmt
export TF_ACC=true && \
go test -cover -coverprofile=coverage.txt -ldflags="-X '${PKG_VERSION_PATH}/provider.Version=${NEXT_VERSION}-test'" -v -p 1 -parallel 20 -timeout 45m ./pkg/...
go test -cover -coverprofile=coverage.txt -ldflags="-X '${PKG_VERSION_PATH}/provider.Version=${NEXT_VERSION}-test'" -v -p 1 -parallel 20 -timeout 1h ./pkg/...

# To generate coverage.txt run `make acceptance` first
coverage:
Expand Down
21 changes: 12 additions & 9 deletions pkg/xray/resource/resource_xray_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,28 +202,31 @@ func (r *WebhookResource) Read(ctx context.Context, req resource.ReadRequest, re

state.Name = types.StringValue(webhook.Name)

if state.Description.IsNull() {
state.Description = types.StringValue("")
}
description := types.StringNull()
if len(webhook.Description) > 0 {
state.Description = types.StringValue(webhook.Description)
description = types.StringValue(webhook.Description)
}
state.Description = description

state.URL = types.StringValue(webhook.URL)
state.UseProxy = types.BoolValue(webhook.UseProxy)

if !state.UserName.IsNull() {
state.UserName = types.StringValue(webhook.UserName)
username := types.StringNull()
if len(webhook.UserName) > 0 {
username = types.StringValue(webhook.UserName)
}
state.UserName = username

if !state.Headers.IsNull() {
headers, ds := types.MapValueFrom(ctx, types.StringType, webhook.Headers)
headers := types.MapNull(types.StringType)
if len(webhook.Headers) > 0 {
hs, ds := types.MapValueFrom(ctx, types.StringType, webhook.Headers)
if ds.HasError() {
resp.Diagnostics.Append(ds...)
return
}
state.Headers = headers
headers = hs
}
state.Headers = headers

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
Expand Down
52 changes: 26 additions & 26 deletions pkg/xray/resource/resource_xray_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ func TestAccWebhook_full(t *testing.T) {
url := fmt.Sprintf("https://tempurl%d.org", testutil.RandomInt())

const template = `
resource "xray_webhook" "{{ .name }}" {
name = "{{ .name }}"
description = "{{ .description }}"
url = "{{ .url }}"
use_proxy = "{{ .use_proxy }}"
}
`
testData := map[string]string{
"name": resourceName,
"description": "test description 2",
"url": url,
"use_proxy": "false",
}

config := util.ExecuteTemplate("TestAccWebhook_full", template, testData)

const updateTemplate = `
resource "xray_webhook" "{{ .name }}" {
name = "{{ .name }}"
description = "{{ .description }}"
Expand All @@ -96,7 +113,7 @@ func TestAccWebhook_full(t *testing.T) {
}
}
`
testData := map[string]string{
updatedTestData := map[string]string{
"name": resourceName,
"description": "test description",
"url": url,
Expand All @@ -108,23 +125,6 @@ func TestAccWebhook_full(t *testing.T) {
"header2_name": "header2_name",
"header2_value": "header2_value",
}

config := util.ExecuteTemplate("TestAccWebhook_full", template, testData)

const updateTemplate = `
resource "xray_webhook" "{{ .name }}" {
name = "{{ .name }}"
description = "{{ .description }}"
url = "{{ .url }}"
use_proxy = "{{ .use_proxy }}"
}
`
updatedTestData := map[string]string{
"name": resourceName,
"description": "test description 2",
"url": url,
"use_proxy": "false",
}
updatedConfig := util.ExecuteTemplate("TestAccWebhook_full", updateTemplate, updatedTestData)

resource.Test(t, resource.TestCase{
Expand All @@ -138,11 +138,9 @@ func TestAccWebhook_full(t *testing.T) {
resource.TestCheckResourceAttr(fqrn, "description", testData["description"]),
resource.TestCheckResourceAttr(fqrn, "url", testData["url"]),
resource.TestCheckResourceAttr(fqrn, "use_proxy", testData["use_proxy"]),
resource.TestCheckResourceAttr(fqrn, "user_name", testData["user_name"]),
resource.TestCheckResourceAttr(fqrn, "password", testData["password"]),
resource.TestCheckResourceAttr(fqrn, "headers.%", "2"),
resource.TestCheckResourceAttr(fqrn, "headers.header1_name", testData["header1_value"]),
resource.TestCheckResourceAttr(fqrn, "headers.header2_name", testData["header2_value"]),
resource.TestCheckNoResourceAttr(fqrn, "user_name"),
resource.TestCheckNoResourceAttr(fqrn, "password"),
resource.TestCheckNoResourceAttr(fqrn, "headers.%"),
),
},
{
Expand All @@ -152,9 +150,11 @@ func TestAccWebhook_full(t *testing.T) {
resource.TestCheckResourceAttr(fqrn, "description", updatedTestData["description"]),
resource.TestCheckResourceAttr(fqrn, "url", updatedTestData["url"]),
resource.TestCheckResourceAttr(fqrn, "use_proxy", updatedTestData["use_proxy"]),
resource.TestCheckNoResourceAttr(fqrn, "user_name"),
resource.TestCheckNoResourceAttr(fqrn, "password"),
resource.TestCheckNoResourceAttr(fqrn, "headers.%"),
resource.TestCheckResourceAttr(fqrn, "user_name", updatedTestData["user_name"]),
resource.TestCheckResourceAttr(fqrn, "password", updatedTestData["password"]),
resource.TestCheckResourceAttr(fqrn, "headers.%", "2"),
resource.TestCheckResourceAttr(fqrn, "headers.header1_name", updatedTestData["header1_value"]),
resource.TestCheckResourceAttr(fqrn, "headers.header2_name", updatedTestData["header2_value"]),
),
ConfigPlanChecks: testutil.ConfigPlanChecks(""),
},
Expand Down

0 comments on commit 442da39

Please sign in to comment.