Skip to content
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

Adding Event Rule Resource #150

Merged
merged 9 commits into from
Aug 23, 2019
Merged

Conversation

stmcallister
Copy link
Contributor

This PR adds support for the Event Rules endpoint in the PagerDuty API. This relates to Issue 114

Acceptance Test Results

TF_ACC=1 go test -run "TestAccPagerDutyEventRule_Basic" ./pagerduty -v -timeout 120m
=== RUN   TestAccPagerDutyEventRule_Basic
--- PASS: TestAccPagerDutyEventRule_Basic (9.95s)
PASS
ok  	github.com/terraform-providers/terraform-provider-pagerduty/pagerduty	10.906s


TF_ACC=1 go test -run "TestAccPagerDutyEventRule_import" ./pagerduty -v -timeout 120m
=== RUN   TestAccPagerDutyEventRule_import
--- PASS: TestAccPagerDutyEventRule_import (6.06s)
PASS
ok  	github.com/terraform-providers/terraform-provider-pagerduty/pagerduty	6.144s

Copy link
Collaborator

@heimweh heimweh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @stmcallister 👌 I left a few comments but overall this looks great 👍

err := json.Unmarshal([]byte(v), &obj)

if err != nil {
log.Printf(string(err.Error()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about returning the error here if JSON unmarshal fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, I've adopted the same strategy used in the extension resource. Would that work?

func flattenSlice(v []interface{}) string {
b, err := json.Marshal(v)
if err != nil {
log.Printf(string(err.Error()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, do we want to return the error here?

if err != nil {
return err
}
for _, rule := range resp.EventRules {
Copy link
Collaborator

@heimweh heimweh Aug 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about doing something like (not tested):

var found *pagerduty.EventRule

for _, rule := range resp.EventRules {
  if rule.ID == d.Id() {
    found = rule
    break
  }
}

if found == nil {
  d.SetId("")
}
...
d.Set("catch_all", rule.CatchAll)
...

That way you can set the attributes outside the loop 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense. I've adopted this idea in my next commit.

}
}
// check if eventRule not found
if _, ok := d.GetOk("action_json"); !ok {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work? This calls d.SetId("") if the response code was a 404.
I added an example above on how to clear it from the state if the event rule is missing from the list of event rules 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appeared to have worked. 🤷‍♂ But, I've changed that logic to match what you were saying in the above comment.

Copy link
Collaborator

@heimweh heimweh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @stmcallister! 👌
Tests are passing and this LGTM 👍

@stmcallister stmcallister merged commit e428d10 into PagerDuty:master Aug 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants