-
Notifications
You must be signed in to change notification settings - Fork 219
/
resource_team_external_group.go
143 lines (119 loc) · 3.63 KB
/
resource_team_external_group.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package grafana
import (
"context"
"strconv"
"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceTeamExternalGroup() *schema.Resource {
return &schema.Resource{
Description: `
* [Official documentation](https://grafana.com/docs/grafana/latest/setup-grafana/configure-security/configure-team-sync/)
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/external_group_sync/)
`,
CreateContext: CreateTeamExternalGroup,
UpdateContext: UpdateTeamExternalGroup,
DeleteContext: UpdateTeamExternalGroup,
ReadContext: ReadTeamExternalGroup,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"team_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "The Team ID",
},
"groups": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "The team external groups list",
},
},
}
}
func CreateTeamExternalGroup(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
teamID := d.Get("team_id").(int)
d.SetId(strconv.FormatInt(int64(teamID), 10))
if err := manageTeamExternalGroup(d, meta); err != nil {
return diag.FromErr(err)
}
return diag.Diagnostics{}
}
func ReadTeamExternalGroup(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).GrafanaAPI
teamID, _ := strconv.ParseInt(d.Id(), 10, 64)
teamGroups, err := client.TeamGroups(teamID)
if err != nil {
return diag.FromErr(err)
}
groupIDs := make([]string, 0, len(teamGroups))
for _, teamGroup := range teamGroups {
groupIDs = append(groupIDs, teamGroup.GroupID)
}
d.Set("team_id", teamID)
d.Set("groups", groupIDs)
return diag.Diagnostics{}
}
func UpdateTeamExternalGroup(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if err := manageTeamExternalGroup(d, meta); err != nil {
return diag.FromErr(err)
}
return diag.Diagnostics{}
}
func manageTeamExternalGroup(d *schema.ResourceData, meta interface{}) error {
client := meta.(*common.Client).GrafanaAPI
addGroups, removeGroups := groupChangesTeamExternalGroup(d)
teamID, _ := strconv.ParseInt(d.Id(), 10, 64)
for _, group := range addGroups {
err := client.NewTeamGroup(teamID, group)
if err != nil {
return err
}
}
for _, group := range removeGroups {
err := client.DeleteTeamGroup(teamID, group)
if err != nil {
return err
}
}
return nil
}
func groupChangesTeamExternalGroup(d *schema.ResourceData) ([]string, []string) {
// Get the lists of team groups read in from Grafana state (old) and configured (new)
state, config := d.GetChange("groups")
currentGroups := make([]string, state.(*schema.Set).Len())
for i, v := range state.(*schema.Set).List() {
currentGroups[i] = v.(string)
}
desiredGroups := make([]string, config.(*schema.Set).Len())
for i, v := range config.(*schema.Set).List() {
desiredGroups[i] = v.(string)
}
contains := func(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}
addGroups := []string{}
for _, group := range desiredGroups {
if !contains(currentGroups, group) {
addGroups = append(addGroups, group)
}
}
removeGroups := []string{}
for _, group := range currentGroups {
if !contains(desiredGroups, group) {
removeGroups = append(removeGroups, group)
}
}
return addGroups, removeGroups
}