-
Notifications
You must be signed in to change notification settings - Fork 7
/
datasource_egress_ips.go
75 lines (62 loc) · 1.79 KB
/
datasource_egress_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
package datasources
import (
"context"
"database/sql"
"errors"
"log"
"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"
"github.com/MaterializeInc/terraform-provider-materialize/pkg/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func EgressIps() *schema.Resource {
return &schema.Resource{
ReadContext: EgressIpsRead,
Schema: map[string]*schema.Schema{
"egress_ips": {
Type: schema.TypeList,
Computed: true,
Description: "The egress IPs in the account",
Elem: &schema.Schema{Type: schema.TypeString},
},
"region": RegionSchema(),
},
}
}
func EgressIpsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
metaDb, region, err := utils.GetDBClientFromMeta(meta, d)
if err != nil {
return diag.FromErr(err)
}
conn := metaDb
q := materialize.ReadEgressIpsDatasource()
rows, err := conn.Query(q)
if errors.Is(err, sql.ErrNoRows) {
log.Printf("[DEBUG] no egress IPs found in account")
return diag.FromErr(err)
} else if err != nil {
log.Println("[DEBUG] failed to list egress IPs")
return diag.FromErr(err)
}
egressIps := []string{}
for rows.Next() {
var egressIp string
err := rows.Scan(&egressIp)
if err != nil {
log.Println("[DEBUG] failed to scan egress IP")
return diag.FromErr(err)
}
egressIps = append(egressIps, egressIp)
}
if err := rows.Err(); err != nil {
log.Println("[DEBUG] failed to list egress IPs")
return diag.FromErr(err)
}
if err := d.Set("egress_ips", egressIps); err != nil {
log.Println("[DEBUG] failed to set egress_ips")
return diag.FromErr(err)
}
d.SetId(utils.TransformIdWithRegion(string(region), "egress_ips"))
return diags
}