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 Ruleset and Ruleset Rule Resources #195

Merged
merged 9 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/terraform-providers/terraform-provider-pagerduty
require (
github.com/google/go-querystring v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/heimweh/go-pagerduty v0.0.0-20200303231559-1936667f26f9
github.com/heimweh/go-pagerduty v0.0.0-20200403175011-aa0df2b03565
)

go 1.13
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ github.com/heimweh/go-pagerduty v0.0.0-20190807171021-2a6540956dc5 h1:UZQ03lpxS/
github.com/heimweh/go-pagerduty v0.0.0-20190807171021-2a6540956dc5/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/heimweh/go-pagerduty v0.0.0-20200303231559-1936667f26f9 h1:C3Wdm8ZIXNM9oP2lWdAjtP3b1J41lb4IJ9ib71QzzZ8=
github.com/heimweh/go-pagerduty v0.0.0-20200303231559-1936667f26f9/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/heimweh/go-pagerduty v0.0.0-20200331225917-7fbbcd73f188 h1:oh+QxBGkrMPDUJ2R0u4ik6anVybX2byrACqdbeLveLc=
github.com/heimweh/go-pagerduty v0.0.0-20200331225917-7fbbcd73f188/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/heimweh/go-pagerduty v0.0.0-20200402235138-d0c1080900c6 h1:dXou0ExlaLRGOlmgOTek1Et8NjPb4IPlrPjCF9MHvPo=
github.com/heimweh/go-pagerduty v0.0.0-20200402235138-d0c1080900c6/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/heimweh/go-pagerduty v0.0.0-20200403175011-aa0df2b03565 h1:9GEmT44tcHOX3yOjVxzsQz4+Ug8LOP8FA+mD7Wp54Tk=
github.com/heimweh/go-pagerduty v0.0.0-20200403175011-aa0df2b03565/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
2 changes: 2 additions & 0 deletions pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func Provider() terraform.ResourceProvider {
"pagerduty_user_notification_rule": resourcePagerDutyUserNotificationRule(),
"pagerduty_extension": resourcePagerDutyExtension(),
"pagerduty_event_rule": resourcePagerDutyEventRule(),
"pagerduty_ruleset": resourcePagerDutyRuleset(),
"pagerduty_ruleset_rule": resourcePagerDutyRulesetRule(),
},
}

Expand Down
163 changes: 163 additions & 0 deletions pagerduty/resource_pagerduty_ruleset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package pagerduty

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
)

func resourcePagerDutyRuleset() *schema.Resource {
return &schema.Resource{
Create: resourcePagerDutyRulesetCreate,
Read: resourcePagerDutyRulesetRead,
Update: resourcePagerDutyRulesetUpdate,
Delete: resourcePagerDutyRulesetDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"team": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"routing_keys": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func buildRulesetStruct(d *schema.ResourceData) *pagerduty.Ruleset {
ruleset := &pagerduty.Ruleset{
Name: d.Get("name").(string),
}

if attr, ok := d.GetOk("team"); ok {
ruleset.Team = expandTeam(attr)
}

if attr, ok := d.GetOk("routing_keys"); ok {
ruleset.RoutingKeys = expandKeys(attr.([]interface{}))
}

return ruleset
}

func expandKeys(v []interface{}) []string {
keys := make([]string, len(v))

for i, k := range v {
keys[i] = fmt.Sprintf("%v", k)
}

return keys
}

func expandTeam(v interface{}) *pagerduty.RulesetObject {
var team *pagerduty.RulesetObject
t := v.([]interface{})[0].(map[string]interface{})
team = &pagerduty.RulesetObject{
ID: t["id"].(string),
}

return team
}

func flattenTeam(v *pagerduty.RulesetObject) map[string]interface{} {
team := map[string]interface{}{
"id": v.ID,
}
return team
}
func resourcePagerDutyRulesetCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

ruleset := buildRulesetStruct(d)

log.Printf("[INFO] Creating PagerDuty ruleset: %s", ruleset.Name)

retryErr := resource.Retry(10*time.Second, func() *resource.RetryError {
if ruleset, _, err := client.Rulesets.Create(ruleset); err != nil {
if isErrCode(err, 400) || isErrCode(err, 429) {
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
} else if ruleset != nil {
d.SetId(ruleset.ID)
}
return nil
})

if retryErr != nil {
return retryErr
}

return resourcePagerDutyRulesetRead(d, meta)
}

func resourcePagerDutyRulesetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

log.Printf("[INFO] Reading PagerDuty ruleset: %s", d.Id())

ruleset, _, err := client.Rulesets.Get(d.Id())
if err != nil {
return handleNotFoundError(err, d)
}

// if ruleset is found set to ResourceData
if ruleset.Team != nil {
d.Set("team", flattenTeam(ruleset.Team))
}
d.Set("routing_keys", ruleset.RoutingKeys)
return nil
}
func resourcePagerDutyRulesetUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

ruleset := buildRulesetStruct(d)

log.Printf("[INFO] Updating PagerDuty ruleset: %s", d.Id())

if _, _, err := client.Rulesets.Update(d.Id(), ruleset); err != nil {
return err
}

return nil
}

func resourcePagerDutyRulesetDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

log.Printf("[INFO] Deleting PagerDuty ruleset: %s", d.Id())

if _, err := client.Rulesets.Delete(d.Id()); err != nil {
return err
}

d.SetId("")

return nil
}
Loading