forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
68 lines (55 loc) · 1.37 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package pagerduty
import (
"fmt"
pagerduty "github.com/PagerDuty/go-pagerduty"
"github.com/hashicorp/terraform/helper/schema"
)
// Validate a value against a set of possible values
func validateValueFunc(values []string) schema.SchemaValidateFunc {
return func(v interface{}, k string) (we []string, errors []error) {
value := v.(string)
valid := false
for _, val := range values {
if value == val {
valid = true
break
}
}
if !valid {
errors = append(errors, fmt.Errorf("%#v is an invalid value for argument %s. Must be one of %#v", value, k, values))
}
return
}
}
// getVendors retrieves all PagerDuty vendors and returns a list of []pagerduty.Vendor
func getVendors(client *pagerduty.Client) ([]pagerduty.Vendor, error) {
var offset uint
var totalCount int
var vendors []pagerduty.Vendor
for {
o := &pagerduty.ListVendorOptions{
APIListObject: pagerduty.APIListObject{
Limit: 100,
Total: 1,
Offset: offset,
},
}
resp, err := client.ListVendors(*o)
if err != nil {
return nil, err
}
for _, v := range resp.Vendors {
totalCount++
vendors = append(vendors, v)
}
rOffset := uint(resp.Offset)
returnedCount := uint(len(resp.Vendors))
rTotal := uint(resp.Total)
if resp.More && uint(totalCount) != uint(rTotal) {
offset = returnedCount + rOffset
continue
}
break
}
return vendors, nil
}