/
data_source_component_groups.go
100 lines (84 loc) · 2.54 KB
/
data_source_component_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
package statuspage
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func dataSourceComponentGroups() *schema.Resource {
return &schema.Resource{
Description: "",
Read: dataSourceComponentGroupsRead,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"page_id": {
Description: "the ID of the page this component belongs to",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
// Computed values
"component_groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// Required
// Optional
// Computed
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"position": {
Type: schema.TypeInt,
Computed: true,
},
"components": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}
func dataSourceComponentGroupsRead(d *schema.ResourceData, m interface{}) error {
providerConf := m.(*ProviderConfiguration)
statuspageClientV1 := providerConf.StatuspageClientV1
authV1 := providerConf.AuthV1
res, _, err := statuspageClientV1.ComponentGroupsApi.GetPagesPageIdComponentGroups(authV1, d.Get("page_id").(string)).Page(1).PerPage(100).Execute()
if err != nil {
return TranslateClientErrorDiag(err, "error querying component group list")
}
d.SetId(GenerateDataSourceHashID("DataSourceComponentGroups-", dataSourceComponentGroups(), d))
resources := []map[string]interface{}{}
for _, r := range res {
componentGroup := map[string]interface{}{}
if _, ok := r.GetNameOk(); ok {
componentGroup["id"] = r.GetId()
componentGroup["name"] = r.GetName()
componentGroup["description"] = r.GetDescription()
componentGroup["position"] = r.GetPosition()
componentGroup["components"] = r.GetComponents()
}
resources = append(resources, componentGroup)
}
if f, fOk := d.GetOk("filter"); fOk {
resources = ApplyFilters(f.(*schema.Set), resources, dataSourceComponentGroups().Schema["component_groups"].Elem.(*schema.Resource).Schema)
}
if err := d.Set("component_groups", resources); err != nil {
return err
}
return nil
}