/
data_property_akamai_groups.go
97 lines (83 loc) · 2.41 KB
/
data_property_akamai_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
package property
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v5/pkg/session"
"github.com/akamai/terraform-provider-akamai/v3/pkg/akamai"
"github.com/akamai/terraform-provider-akamai/v3/pkg/tools"
)
func dataSourcePropertyMultipleGroups() *schema.Resource {
return &schema.Resource{
ReadContext: dataPropertyMultipleGroupsRead,
Schema: map[string]*schema.Schema{
"groups": {
Type: schema.TypeList,
Computed: true,
Description: "List of groups",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"group_id": {
Type: schema.TypeString,
Computed: true,
},
"group_name": {
Type: schema.TypeString,
Computed: true,
},
"parent_group_id": {
Type: schema.TypeString,
Computed: true,
},
"contract_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}
func dataPropertyMultipleGroupsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
meta := akamai.Meta(m)
logger := meta.Log("PAPI", "dataPropertyMultipleGroupsRead")
// create a context with logging for api calls
ctx = session.ContextWithOptions(
ctx,
session.WithContextLog(logger),
)
logger.Debugf("[Akamai Property Groups] Start Searching for group records")
groups, err := getGroups(ctx, meta)
if err != nil {
return diag.FromErr(err)
}
grpList := make([]map[string]interface{}, 0, len(groups.Groups.Items))
for _, g := range groups.Groups.Items {
contractIDs := make([]string, 0, len(g.ContractIDs))
for _, contractID := range g.ContractIDs {
contractIDs = append(contractIDs, contractID)
}
grpList = append(grpList, map[string]interface{}{
"group_id": g.GroupID,
"group_name": g.GroupName,
"parent_group_id": g.ParentGroupID,
"contract_ids": contractIDs,
})
}
if err := d.Set("groups", grpList); err != nil {
return diag.FromErr(fmt.Errorf("%w:%q", tools.ErrValueSet, err.Error()))
}
jsonBody, err := json.Marshal(grpList)
if err != nil {
return diag.FromErr(err)
}
d.SetId(tools.GetSHAString(string(jsonBody)))
logger.Debugf("[Akamai Property Groups] Done")
return nil
}