This repository has been archived by the owner on Jan 8, 2021. It is now read-only.
forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.go
112 lines (93 loc) · 2.65 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package github
import (
"fmt"
"strconv"
"strings"
"github.com/hashicorp/terraform/helper/schema"
)
const (
// https://developer.github.com/guides/traversing-with-pagination/#basics-of-pagination
maxPerPage = 100
)
func checkOrganization(meta interface{}) error {
if meta.(*Organization).name == "" {
return fmt.Errorf("This resource requires GitHub organization to be set on the provider.")
}
return nil
}
func caseInsensitive() schema.SchemaDiffSuppressFunc {
return func(k, old, new string, d *schema.ResourceData) bool {
return strings.EqualFold(old, new)
}
}
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 `left:right` as left, right
func parseTwoPartID(id, left, right string) (string, string, error) {
parts := strings.SplitN(id, ":", 2)
if len(parts) != 2 {
return "", "", fmt.Errorf("Unexpected ID format (%q). Expected %s:%s", id, left, right)
}
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())
}
func validateTeamIDFunc(v interface{}, keyName string) (we []string, errors []error) {
teamIDString, ok := v.(string)
if !ok {
return nil, []error{fmt.Errorf("expected type of %s to be string", keyName)}
}
// Check that the team ID can be converted to an int
if _, err := strconv.ParseInt(teamIDString, 10, 64); err != nil {
return nil, []error{unconvertibleIdErr(teamIDString, err)}
}
return
}
func splitRepoFilePath(path string) (string, string) {
parts := strings.Split(path, "/")
return parts[0], strings.Join(parts[1:], "/")
}