Skip to content
Open
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/zclconf/go-cty v1.3.1 // indirect
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 // indirect
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.0.0-20200324201824-1fc30e1f4ccc // indirect
google.golang.org/genproto v0.0.0-20200324203455-a04cca1dde73 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
Expand Down
21 changes: 17 additions & 4 deletions victorops/resource_victorops_routing_key.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package victorops

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/victorops/go-victorops/victorops"
"golang.org/x/time/rate"
)

// Create a global rate limiter instance.
var limiter = rate.NewLimiter(2, 1) // 2 tokens per second, with a burst size of 1

func resourceRoutingKey() *schema.Resource {
return &schema.Resource{
Create: resourceRoutingKeyCreate,
Expand Down Expand Up @@ -47,7 +51,12 @@ func resourceRoutingKeyCreate(d *schema.ResourceData, m interface{}) error {
Targets: targets,
}

// Make the request
// Wait for the rate limiter before making the request
err := limiter.Wait(context.Background())
if err != nil {
return err
}

newRoutingKey, requestDetails, err := config.VictorOpsClient.CreateRoutingKey(routingKey)
if err != nil {
return err
Expand All @@ -64,6 +73,12 @@ func resourceRoutingKeyCreate(d *schema.ResourceData, m interface{}) error {
func resourceRoutingKeyRead(d *schema.ResourceData, m interface{}) error {
config := m.(Config)

// Wait for the rate limiter before making the request
err := limiter.Wait(context.Background())
if err != nil {
return err
}

rk, _, err := config.VictorOpsClient.GetRoutingKey(d.Get("name").(string))
if err != nil {
return err
Expand All @@ -73,8 +88,6 @@ func resourceRoutingKeyRead(d *schema.ResourceData, m interface{}) error {
d.SetId("")
} else {
d.SetId(rk.RoutingKey)

// Convert the response targets to an array of strings that can be compared
targets := []string{}
for _, target := range rk.Targets {
targets = append(targets, target.PolicySlug)
Expand Down