-
Notifications
You must be signed in to change notification settings - Fork 247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modify alert_channel to use maps for headers, payload #81
Closed
aaronshaver
wants to merge
1
commit into
newrelic:master
from
newrelic-forks:alert-channel-webhook-improvements
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,9 +50,7 @@ var alertChannelTypes = map[string][]string{ | |
"auth_type", | ||
"auth_username", | ||
"base_url", | ||
"headers", | ||
"payload_type", | ||
"payload", | ||
}, | ||
} | ||
|
||
|
@@ -89,17 +87,51 @@ func resourceNewRelicAlertChannel() *schema.Resource { | |
//TODO: ValidateFunc: (use list of keys from map above) | ||
Sensitive: true, | ||
}, | ||
"headers": { | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
ForceNew: true, | ||
Sensitive: true, | ||
}, | ||
"payload": { | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
ForceNew: true, | ||
Sensitive: true, | ||
ValidateFunc: configBlockLengthGreaterThan(0), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// This function verifies that the number of keys within a configuration | ||
// map is greater than the provided parameter. | ||
func configBlockLengthGreaterThan(minLength int) schema.SchemaValidateFunc { | ||
return func(i interface{}, st string) (s []string, errorSlice []error) { | ||
length := len(i.(map[string]interface{})) | ||
if length > minLength { | ||
return | ||
} | ||
errorSlice = append(errorSlice, fmt.Errorf("expected %s not to be empty", st)) | ||
return | ||
} | ||
} | ||
|
||
func buildAlertChannelStruct(d *schema.ResourceData) *newrelic.AlertChannel { | ||
channel := newrelic.AlertChannel{ | ||
Name: d.Get("name").(string), | ||
Type: d.Get("type").(string), | ||
Configuration: d.Get("configuration").(map[string]interface{}), | ||
} | ||
|
||
if headerMap, ok := d.GetOk("headers"); ok { | ||
channel.Configuration["headers"] = headerMap.(map[string]interface{}) | ||
} | ||
|
||
if payload, ok := d.GetOk("payload"); ok { | ||
channel.Configuration["payload"] = payload.(map[string]interface{}) | ||
} | ||
|
||
return &channel | ||
} | ||
|
||
|
@@ -141,6 +173,19 @@ func resourceNewRelicAlertChannelRead(d *schema.ResourceData, meta interface{}) | |
|
||
d.Set("name", channel.Name) | ||
d.Set("type", channel.Type) | ||
|
||
// extract headers from Configuration before we try and set it in the resource | ||
if headers, ok := channel.Configuration["headers"]; ok { | ||
d.Set("headers", headers) | ||
delete(channel.Configuration, "headers") | ||
} | ||
|
||
// extract payload from Configuration before we try and set it in the resource | ||
if payload, ok := channel.Configuration["payload"]; ok { | ||
d.Set("payload", payload) | ||
delete(channel.Configuration, "payload") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little hesitant to do this |
||
} | ||
|
||
if err := d.Set("configuration", channel.Configuration); err != nil { | ||
return fmt.Errorf("[DEBUG] Error setting Alert Channel Configuration: %#v", err) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package newrelic | |
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"testing" | ||
|
||
|
@@ -29,6 +30,8 @@ func TestAccNewRelicAlertChannel_Basic(t *testing.T) { | |
"newrelic_alert_channel.foo", "configuration.recipients", "[email protected]"), | ||
resource.TestCheckResourceAttr( | ||
"newrelic_alert_channel.foo", "configuration.include_json_attachment", "1"), | ||
resource.TestCheckNoResourceAttr( | ||
"newrelic_alert_channel.foo", "headers"), | ||
), | ||
}, | ||
{ | ||
|
@@ -43,6 +46,8 @@ func TestAccNewRelicAlertChannel_Basic(t *testing.T) { | |
"newrelic_alert_channel.foo", "configuration.recipients", "[email protected]"), | ||
resource.TestCheckResourceAttr( | ||
"newrelic_alert_channel.foo", "configuration.include_json_attachment", "0"), | ||
resource.TestCheckNoResourceAttr( | ||
"newrelic_alert_channel.foo", "headers"), | ||
), | ||
}, | ||
}, | ||
|
@@ -70,6 +75,60 @@ func TestAccNewRelicAlertChannel_import(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestAccNewRelicAlertChannel_Webhook_withoutHeaders(t *testing.T) { | ||
rName := acctest.RandString(5) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckNewRelicAlertChannelDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckNewRelicAlertChannelConfigWebhook_withoutHeaders(rName), | ||
|
||
ExpectNonEmptyPlan: true, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckNewRelicAlertChannelExists("newrelic_alert_channel.channel_without_headers"), | ||
resource.TestCheckResourceAttr( | ||
"newrelic_alert_channel.channel_without_headers", "name", fmt.Sprintf("tf-test-webhook-%s", rName)), | ||
resource.TestCheckResourceAttr( | ||
"newrelic_alert_channel.channel_without_headers", "type", "webhook"), | ||
resource.TestCheckResourceAttr( | ||
"newrelic_alert_channel.channel_without_headers", "configuration.base_url", "http://test.com"), | ||
resource.TestCheckNoResourceAttr( | ||
"newrelic_alert_channel.channel_without_headers", "headers"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccNewRelicAlertChannel_Webhook_withHeaders(t *testing.T) { | ||
rName := acctest.RandString(5) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckNewRelicAlertChannelDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckNewRelicAlertChannelConfigWebhook_withHeaders(rName), | ||
|
||
ExpectNonEmptyPlan: true, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckNewRelicAlertChannelExists("newrelic_alert_channel.channel_with_headers"), | ||
resource.TestCheckResourceAttr( | ||
"newrelic_alert_channel.channel_with_headers", "name", fmt.Sprintf("tf-test-webhook-%s", rName)), | ||
resource.TestCheckResourceAttr( | ||
"newrelic_alert_channel.channel_with_headers", "type", "webhook"), | ||
resource.TestCheckResourceAttr( | ||
"newrelic_alert_channel.channel_with_headers", "configuration.base_url", "http://test.com"), | ||
resource.TestCheckResourceAttr( | ||
"newrelic_alert_channel.channel_with_headers", "headers.header1", "test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckNewRelicAlertChannelDestroy(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*ProviderConfig).Client | ||
for _, r := range s.RootModule().Resources { | ||
|
@@ -149,3 +208,121 @@ resource "newrelic_alert_channel" "foo" { | |
} | ||
`, rName) | ||
} | ||
|
||
func testAccCheckNewRelicAlertChannelConfigWebhook_withoutHeaders(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "newrelic_alert_channel" "channel_without_headers" { | ||
name = "tf-test-webhook-%s" | ||
type = "webhook" | ||
|
||
configuration = { | ||
base_url = "http://test.com", | ||
auth_username = "username", | ||
auth_password = "password", | ||
payload_type = "application/json", | ||
} | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccCheckNewRelicAlertChannelConfigWebhook_withHeaders(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "newrelic_alert_channel" "channel_with_headers" { | ||
name = "tf-test-webhook-%s" | ||
type = "webhook" | ||
|
||
configuration = { | ||
base_url = "http://test.com", | ||
auth_username = "username", | ||
auth_password = "password", | ||
payload_type = "application/json", | ||
} | ||
|
||
headers { | ||
header1 = "test" | ||
header2 = "test2" | ||
} | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccCheckNewRelicAlertChannelConfigWebhook_withEmptyPayload(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "newrelic_alert_channel" "webhook_with_empty_payload" { | ||
name = "tf-test-webhook-%s" | ||
type = "webhook" | ||
|
||
configuration = { | ||
base_url = "http://test.com", | ||
auth_username = "username", | ||
auth_password = "password", | ||
payload_type = "application/json", | ||
} | ||
|
||
payload = { | ||
} | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccCheckNewRelicAlertChannelConfigWebhook_withPayload(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "newrelic_alert_channel" "webhook_with_payload" { | ||
name = "tf-test-webhook-%s" | ||
type = "webhook" | ||
|
||
configuration = { | ||
base_url = "http://test.com", | ||
auth_username = "username", | ||
auth_password = "password", | ||
payload_type = "application/json", | ||
} | ||
|
||
payload = { | ||
account_id = "test" | ||
} | ||
} | ||
`, rName) | ||
} | ||
|
||
func TestAccNewRelicAlertChannel_Webhook_withPayload(t *testing.T) { | ||
rName := acctest.RandString(5) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckNewRelicAlertChannelDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckNewRelicAlertChannelConfigWebhook_withPayload(rName), | ||
ExpectNonEmptyPlan: true, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckNewRelicAlertChannelExists("newrelic_alert_channel.webhook_with_payload"), | ||
resource.TestCheckResourceAttr( | ||
"newrelic_alert_channel.webhook_with_payload", "name", fmt.Sprintf("tf-test-webhook-%s", rName)), | ||
resource.TestCheckResourceAttr( | ||
"newrelic_alert_channel.webhook_with_payload", "type", "webhook"), | ||
resource.TestCheckResourceAttr( | ||
"newrelic_alert_channel.webhook_with_payload", "configuration.base_url", "http://test.com"), | ||
resource.TestCheckResourceAttr( | ||
"newrelic_alert_channel.webhook_with_payload", "payload.account_id", "test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccNewRelicAlertChannel_Webhook_withEmptyPayloadReturnsError(t *testing.T) { | ||
expectedErrorMsg, _ := regexp.Compile("expected payload not to be empty") | ||
rName := acctest.RandString(5) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckNewRelicAlertChannelDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckNewRelicAlertChannelConfigWebhook_withEmptyPayload(rName), | ||
ExpectError: expectedErrorMsg, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little hesitant to do this
delete
as its a breaking change for anyone already using this behavior.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not believe that the
delete
could break anything because, as far as I can tell setting the headers or payload is broken currentlyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think nathanael-ness is right, I don't see how either
headers
orpayload
could be working currently with the type mismatch.We're having to use a null-resource to deploy a webhook alert channel, can't use the resource as it is.