forked from DrFaust92/terraform-provider-bitbucket
/
data_groups.go
114 lines (92 loc) · 2.35 KB
/
data_groups.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
package bitbucket
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataGroups() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataReadGroups,
Schema: map[string]*schema.Schema{
"workspace": {
Type: schema.TypeString,
Required: true,
},
"groups": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"slug": {
Type: schema.TypeString,
Computed: true,
},
"auto_add": {
Type: schema.TypeBool,
Computed: true,
},
"permission": {
Type: schema.TypeString,
Computed: true,
},
"email_forwarding_disabled": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
}
}
func dataReadGroups(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(Clients).httpClient
workspace := d.Get("workspace").(string)
groupsReq, _ := client.Get(fmt.Sprintf("1.0/groups/%s", workspace))
if groupsReq.Body == nil {
return diag.Errorf("error reading Groups (%s): empty response", d.Id())
}
var grps []*UserGroup
body, readerr := io.ReadAll(groupsReq.Body)
if readerr != nil {
return diag.FromErr(readerr)
}
log.Printf("[DEBUG] Groups Response JSON: %v", string(body))
decodeerr := json.Unmarshal(body, &grps)
if decodeerr != nil {
return diag.FromErr(decodeerr)
}
log.Printf("[DEBUG] Groups Response Decoded: %#v", grps)
d.SetId(workspace)
d.Set("groups", flattenUserGroups(grps))
return nil
}
func flattenUserGroups(groups []*UserGroup) []interface{} {
if len(groups) == 0 {
return nil
}
var tfList []interface{}
for _, btRaw := range groups {
log.Printf("[DEBUG] User Group Response Decoded: %#v", btRaw)
if btRaw == nil {
continue
}
group := map[string]interface{}{
"name": btRaw.Name,
"permission": btRaw.Permission,
"slug": btRaw.Slug,
"auto_add": btRaw.AutoAdd,
"email_forwarding_disabled": btRaw.EmailForwardingDisabled,
}
tfList = append(tfList, group)
}
return tfList
}