-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
79 lines (66 loc) · 1.73 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
69
70
71
72
73
74
75
76
77
78
79
package github
import (
"fmt"
"strings"
"github.com/hashicorp/terraform/helper/schema"
)
const (
// https://developer.github.com/guides/traversing-with-pagination/#basics-of-pagination
maxPerPage = 100
)
func validateValueFunc(values []string) schema.SchemaValidateFunc {
return func(v interface{}, k string) (we []string, errors []error) {
value := v.(string)
valid := false
for _, role := range values {
if value == role {
valid = true
break
}
}
if !valid {
errors = append(errors, fmt.Errorf("%s is an invalid value for argument %s", value, k))
}
return
}
}
// return the pieces of id `a:b` as a, b
func parseTwoPartID(id string) (string, string, error) {
parts := strings.SplitN(id, ":", 2)
if len(parts) != 2 {
return "", "", fmt.Errorf("Unexpected ID format (%q). Expected organization:name", id)
}
return parts[0], parts[1], nil
}
// format the strings into an id `a:b`
func buildTwoPartID(a, b *string) string {
return fmt.Sprintf("%s:%s", *a, *b)
}
func expandStringList(configured []interface{}) []string {
vs := make([]string, 0, len(configured))
for _, v := range configured {
val, ok := v.(string)
if ok && val != "" {
vs = append(vs, val)
}
}
return vs
}
func flattenStringList(v []string) []interface{} {
c := make([]interface{}, 0, len(v))
for _, s := range v {
c = append(c, s)
}
return c
}
func unconvertibleIdErr(id string, err error) *unconvertibleIdError {
return &unconvertibleIdError{OriginalId: id, OriginalError: err}
}
type unconvertibleIdError struct {
OriginalId string
OriginalError error
}
func (e *unconvertibleIdError) Error() string {
return fmt.Sprintf("Unexpected ID format (%q), expected numerical ID. %s",
e.OriginalId, e.OriginalError.Error())
}