-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathdata_source_cloud_ips.go
98 lines (91 loc) · 2.82 KB
/
data_source_cloud_ips.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
package cloud
import (
"context"
"io"
"net/http"
"strings"
"github.com/grafana/terraform-provider-grafana/v3/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func datasourceIPs() *common.DataSource {
schema := &schema.Resource{
Description: "Data source for retrieving sets of cloud IPs. See https://grafana.com/docs/grafana-cloud/reference/allow-list/ for more info",
ReadContext: datasourceIPsRead,
Schema: map[string]*schema.Schema{
"hosted_alerts": {
Description: "Set of IP addresses that are used for hosted alerts.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"hosted_grafana": {
Description: "Set of IP addresses that are used for hosted Grafana.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"hosted_metrics": {
Description: "Set of IP addresses that are used for hosted metrics.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"hosted_traces": {
Description: "Set of IP addresses that are used for hosted traces.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"hosted_logs": {
Description: "Set of IP addresses that are used for hosted logs.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
return common.NewLegacySDKDataSource("grafana_cloud_ips", schema)
}
func datasourceIPsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
d.SetId("cloud_ips")
for attr, dataURL := range map[string]string{
"hosted_alerts": "https://grafana.com/api/hosted-alerts/source-ips.txt",
"hosted_grafana": "https://grafana.com/api/hosted-grafana/source-ips.txt",
"hosted_metrics": "https://grafana.com/api/hosted-metrics/source-ips.txt",
"hosted_traces": "https://grafana.com/api/hosted-traces/source-ips.txt",
"hosted_logs": "https://grafana.com/api/hosted-logs/source-ips.txt",
} {
// nolint: gosec
resp, err := http.Get(dataURL)
if err != nil {
return diag.Errorf("error querying IPs for %s (%s): %v", attr, dataURL, err)
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return diag.Errorf("error reading response body for %s (%s): %v", attr, dataURL, err)
}
var ipStr []string
for _, ip := range strings.Split(string(b), "\n") {
ip = strings.TrimSpace(ip)
if ip != "" {
ipStr = append(ipStr, ip)
}
}
if err := d.Set(attr, ipStr); err != nil {
return diag.Errorf("error setting %s: %v", attr, err)
}
}
return nil
}