-
Notifications
You must be signed in to change notification settings - Fork 216
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
Manage extension configuration as plain JSON #84
Conversation
…nsion-config branch: govendor fetch github.com/heimweh/go-pagerduty/pagerduty::github.com/pdecat/go-pagerduty/pagerduty@=f-extension-config
Hi @heimweh, what do you think about this change? If it sounds good to you, I'll proceed with the remaining work. |
Depends on heimweh/go-pagerduty#22 |
Was just looking for a way to do this! If this change is accepted I'd be keen to make a similar one for |
@samm-git thanks for this info! I did not expect this would work for |
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.
Hi @pdecat, thank you so much for the PR and sorry for the delay. I added some comments, nothing super critical and overall it looks good but would you be able to update the documentation as well?
Many thanks!
Best regards,
Alexander
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.ValidateJsonString, | ||
DiffSuppressFunc: jsonExtensionConfigDiffSuppress, |
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.
Would it be possible to use https://github.com/hashicorp/terraform/blob/master/helper/structure/suppress_json_diff.go#L9 here?
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.
Good catch, done.
@@ -58,6 +67,7 @@ func buildExtensionStruct(d *schema.ResourceData) *pagerduty.Extension { | |||
ID: d.Get("extension_schema").(string), | |||
}, | |||
ExtensionObjects: expandServiceObjects(d.Get("extension_objects")), | |||
Config: expandExtensionConfig(d.Get("config")), |
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.
Since this is an optional field I believe you must check if the field is in the configuration using GetOk
.
Example:
if v, ok := d.GetOk("config"); ok {
Extension.Config = v
}
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.
Indeed, this will avoid misleading unmarshalling errors.
Done.
@@ -174,3 +188,35 @@ func flattenExtensionObjects(serviceList []*pagerduty.ServiceReference) interfac | |||
} | |||
return services | |||
} | |||
|
|||
func expandExtensionConfig(v interface{}) interface{} { |
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.
Would it be possible to just use the generic https://github.com/hashicorp/terraform/blob/master/helper/structure/expand_json.go#L5 instead?
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.
The generic ExpandJsonFromString()
function provided by terraform unmarshals JSON-encoded data into map[string]interface{}
instead of interface{}
Same for flattenExtensionConfig()
that marshals a map[string]interface{}
value into JSON-encoded data.
I believe using those would require to change the type of the terraform configuration field config
from TypeString
to TypeMap
and specify a schema for this map.
That last part is what I'm trying to avoid here as this would require to enumerate all possible configuration options.
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.
Ah makes sense 👍 let's keep it as-is then.
Hi @heimweh , thanks for your feedback. |
Hi @pdecat, nice work! I agree, let's leave the flatten/expand funcs as they are. I'll do a final review when the documentation is in place. Thank you so much for this! Best regards, Alexander |
Great, documentation added. |
@@ -98,6 +111,10 @@ func resourcePagerDutyExtensionRead(d *schema.ResourceData, meta interface{}) er | |||
} | |||
d.Set("extension_schema", extension.ExtensionSchema) | |||
|
|||
if err := d.Set("config", flattenExtensionConfig(extension.Config)); err != nil { |
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.
The schema expects a string
here but is actually a []byte
so this won't work.
Think you must wrap a string()
here or have the flatten func return a string.
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.
Well, it does work as is as the acceptance tests pass.
Also, I'm using it with success on our own stack.
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 sure it works correctly but the problem here is that we're failing to set config
as returned by the API (it's never updated).
Note: I'm seeing error setting extension config
when I'm running the acctests.
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.
That makes sense to me now, thanks!
"acknowledge": false, | ||
"assignments": false | ||
}, | ||
"access_token": "XXX" |
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.
No biggie but the indentation here looks a bit off :)
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.
Fixed.
@@ -68,10 +68,12 @@ func TestAccPagerDutyExtension_Basic(t *testing.T) { | |||
"pagerduty_extension.foo", "extension_schema", "PJFWPEP"), | |||
resource.TestCheckResourceAttr( | |||
"pagerduty_extension.foo", "endpoint_url", url), | |||
resource.TestCheckResourceAttr( | |||
"pagerduty_extension.foo", "config", "{\n\t\"restrict\": \"any\",\n\t\"notify_types\": {\n\t\t\t\"resolve\": false,\n\t\t\t\"acknowledge\": false,\n\t\t\t\"assignments\": false\n\t}\n}\n"), |
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 believe this should be:
"pagerduty_extension.foo", "config", "{\"notify_types\":{\"acknowledge\":false,\"assignments\":false,\"resolve\":false},\"restrict\":\"any\"}"),
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.
Changing this makes the acceptance to fail:
# make testacc TEST=./pagerduty/ TESTARGS='-run=TestAccPagerDutyExtension_Basic'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./pagerduty/ -v -run=TestAccPagerDutyExtension_Basic -timeout 120m
=== RUN TestAccPagerDutyExtension_Basic
--- FAIL: TestAccPagerDutyExtension_Basic (10.67s)
testing.go:459: Step 0 error: Check failed: Check 5/5 error: pagerduty_extension.foo: Attribute 'config' expected "{\"restrict\": \"any\",\"notify_types\": {\"resolve\": false,\"acknowledge\": false,\"assignments\": false}}", got "{\n\t\"restrict\": \"any\",\n\t\"notify_types\": {\n\t\t\t\"resolve\": false,\n\t\t\t\"acknowledge\": false,\n\t\t\t\"assignments\": false\n\t}\n}\n"
FAIL
FAIL github.com/terraform-providers/terraform-provider-pagerduty/pagerduty 10.674s
GNUmakefile:17: recipe for target 'testacc' failed
make: *** [testacc] Error 1
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 think that's due to the d.Set
issue above,
because we're not actually reading back the config from the API.
@@ -80,6 +82,8 @@ func TestAccPagerDutyExtension_Basic(t *testing.T) { | |||
"pagerduty_extension.foo", "extension_schema", "PJFWPEP"), | |||
resource.TestCheckResourceAttr( | |||
"pagerduty_extension.foo", "endpoint_url", url_updated), | |||
resource.TestCheckResourceAttr( | |||
"pagerduty_extension.foo", "config", "{\n\t\"restrict\": \"pd-users\",\n\t\"notify_types\": {\n\t\t\t\"resolve\": true,\n\t\t\t\"acknowledge\": true,\n\t\t\t\"assignments\": true\n\t}\n}\n"), |
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 believe this should be:
"pagerduty_extension.foo", "config", "{\"notify_types\":{\"acknowledge\":true,\"assignments\":true,\"resolve\":true},\"restrict\":\"pd-users\"}"),
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.
Same as above.
Acceptance tests results
|
I got it to work using the following patch: diff --git a/pagerduty/resource_pagerduty_extension.go b/pagerduty/resource_pagerduty_extension.go
index 7a7da32..13c9894 100644
--- a/pagerduty/resource_pagerduty_extension.go
+++ b/pagerduty/resource_pagerduty_extension.go
@@ -207,5 +207,5 @@ func flattenExtensionConfig(config interface{}) interface{} {
log.Printf("[ERROR] Could not marshal extension config %s: %v", config.(string), err)
return nil
}
- return json
+ return string(json)
}
diff --git a/pagerduty/resource_pagerduty_extension_test.go b/pagerduty/resource_pagerduty_extension_test.go
index 7650404..876582b 100644
--- a/pagerduty/resource_pagerduty_extension_test.go
+++ b/pagerduty/resource_pagerduty_extension_test.go
@@ -69,7 +69,7 @@ func TestAccPagerDutyExtension_Basic(t *testing.T) {
resource.TestCheckResourceAttr(
"pagerduty_extension.foo", "endpoint_url", url),
resource.TestCheckResourceAttr(
- "pagerduty_extension.foo", "config", "{\n\t\"restrict\": \"any\",\n\t\"notify_types\": {\n\t\t\t\"resolve\": false,\n\t\t\t\"acknowledge\": false,\n\t\t\t\"assignments\": false\n\t}\n}\n"),
+ "pagerduty_extension.foo", "config", "{\"notify_types\":{\"acknowledge\":false,\"assignments\":false,\"resolve\":false},\"restrict\":\"any\"}"),
),
},
{
@@ -83,7 +83,7 @@ func TestAccPagerDutyExtension_Basic(t *testing.T) {
resource.TestCheckResourceAttr(
"pagerduty_extension.foo", "endpoint_url", url_updated),
resource.TestCheckResourceAttr(
- "pagerduty_extension.foo", "config", "{\n\t\"restrict\": \"pd-users\",\n\t\"notify_types\": {\n\t\t\t\"resolve\": true,\n\t\t\t\"acknowledge\": true,\n\t\t\t\"assignments\": true\n\t}\n}\n"),
+ "pagerduty_extension.foo", "config", "{\"notify_types\":{\"acknowledge\":true,\"assignments\":true,\"resolve\":true},\"restrict\":\"pd-users\"}"),
),
},
},
TF_ACC=1 go test ./pagerduty -v -run=TestAccPagerDutyExtension -timeout 120m
=== RUN TestAccPagerDutyExtension_import
--- PASS: TestAccPagerDutyExtension_import (16.27s)
=== RUN TestAccPagerDutyExtension_Basic
--- PASS: TestAccPagerDutyExtension_Basic (19.89s)
PASS
ok github.com/terraform-providers/terraform-provider-pagerduty/pagerduty 36.175s |
Ok, got it, thanks! |
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.
Thank you so much for this contribution @pdecat! this will be a great addition to the provider 🙏
This LGTM 👍
Is this only using slack ? looking to automation adding extentions for MS Teams, is there an example ? |
Hi @anirban1c, the You can get the extension schema for Microsoft Teams (id Currently, the API documentation states it supports only one setting:
You may get additional fields from actual API responses once you create |
thanks @pdecat, just to clarify this will work with the new integration ? and not the legacy MS Teams V1 integration ? |
I only see one MS Teams extension in the API and the UI. |
this is whats confusing me : The PagerDuty (Legacy) connector sends notifications about triggered incidents. To use this connector, you'll need to create certain settings on the PagerDuty website by following a few easy steps. If you don't already have an account, create one on the PagerDuty (Legacy) website. |
MS teams integration works just fine from the UI and notifications flowing btw |
this is what I get, let me try this in TF and wondering if anyone has got this working ? In [10]: json.loads(r.content) In [11]: |
This PR adds support to manage the configuration of extensions.
It is done as plain JSON as the configuration part differs from vendor to vendor: https://v2.developer.pagerduty.com/v2/page/api-reference#!/Extension_Schemas/get_extension_schemas
TODO:
Note: upgrading terraform dependencies as done in https://github.com/pdecat/terraform-provider-pagerduty/tree/upgrade-terraform-deps would allow for more readable terraform configuration snippets using structured
locals
and advancedjsonencode()
: