forked from hashicorp/terraform-provider-vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_github_team.go
118 lines (99 loc) · 2.79 KB
/
resource_github_team.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
113
114
115
116
117
118
package vault
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/vault/api"
)
func githubTeamResource() *schema.Resource {
return &schema.Resource{
Create: githubTeamCreate,
Read: githubTeamRead,
Update: githubTeamUpdate,
Delete: githubTeamDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"backend": {
Type: schema.TypeString,
Optional: true,
Description: "Auth backend to which team mapping will be congigured.",
ForceNew: true,
Default: "github",
// standardise on no beginning or trailing slashes
StateFunc: func(v interface{}) string {
return strings.Trim(v.(string), "/")
},
},
"policies": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Policies to be assigned to this team.",
},
"team": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "GitHub team name in \"slugified\" format.",
ValidateFunc: validateStringSlug,
},
},
}
}
func githubTeamCreate(d *schema.ResourceData, meta interface{}) error {
id := githubMapId(d.Get("backend").(string), d.Get("team").(string), "teams")
d.SetId(id)
d.MarkNewResource()
log.Printf("[INFO] Creating new github team map at '%v'", id)
return githubTeamUpdate(d, meta)
}
func githubTeamUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
path := d.Id()
data := map[string]interface{}{}
data["key"] = d.Get("team").(string)
if v, ok := d.GetOk("policies"); ok {
vs := expandStringSlice(v.([]interface{}))
data["value"] = strings.Join(vs, ",")
}
_, err := client.Logical().Write(path, data)
if err != nil {
d.SetId("")
return err
}
log.Printf("[INFO] Saved github team map at '%v'", path)
return githubTeamRead(d, meta)
}
func githubTeamRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
path := d.Id()
dt, err := client.Logical().Read(path)
if err != nil {
log.Printf("[ERROR] error when reading github team mapping from '%s'", path)
return err
}
if v, ok := dt.Data["key"]; ok {
d.Set("team", v.(string))
} else {
return fmt.Errorf("github team information not found at path: '%v'", d.Id())
}
if v, ok := dt.Data["value"]; ok {
policies := flattenCommaSeparatedStringSlice(v.(string))
if err := d.Set("policies", policies); err != nil {
return err
}
}
d.Set("backend", githubMappingPath(d.Id(), "teams"))
return nil
}
func githubTeamDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
_, err := client.Logical().Delete(d.Id())
if err != nil {
return err
}
return nil
}