-
Notifications
You must be signed in to change notification settings - Fork 8
/
datasource_sink.go
117 lines (104 loc) · 2.93 KB
/
datasource_sink.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
115
116
117
package datasources
import (
"context"
"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 Sink() *schema.Resource {
return &schema.Resource{
ReadContext: sinkRead,
Schema: map[string]*schema.Schema{
"database_name": {
Type: schema.TypeString,
Optional: true,
Description: "Limit sinks to a specific database",
},
"schema_name": {
Type: schema.TypeString,
Optional: true,
Description: "Limit sinks to a specific schema within a specific database",
RequiredWith: []string{"database_name"},
},
"sinks": {
Type: schema.TypeList,
Computed: true,
Description: "The sinks in the account",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"schema_name": {
Type: schema.TypeString,
Computed: true,
},
"database_name": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"size": {
Type: schema.TypeString,
Computed: true,
},
"envelope_type": {
Type: schema.TypeString,
Computed: true,
},
"connection_name": {
Type: schema.TypeString,
Computed: true,
},
"cluster_name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"region": RegionSchema(),
},
}
}
func sinkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
schemaName := d.Get("schema_name").(string)
databaseName := d.Get("database_name").(string)
var diags diag.Diagnostics
metaDb, region, err := utils.GetDBClientFromMeta(meta, d)
if err != nil {
return diag.FromErr(err)
}
dataSource, err := materialize.ListSinks(metaDb, schemaName, databaseName)
if err != nil {
return diag.FromErr(err)
}
sinkFormats := []map[string]interface{}{}
for _, p := range dataSource {
sinkMap := map[string]interface{}{}
sinkMap["id"] = p.SinkId.String
sinkMap["name"] = p.SinkName.String
sinkMap["schema_name"] = p.SchemaName.String
sinkMap["database_name"] = p.DatabaseName.String
sinkMap["type"] = p.SinkType.String
sinkMap["size"] = p.Size.String
sinkMap["envelope_type"] = p.EnvelopeType.String
sinkMap["connection_name"] = p.ConnectionName.String
sinkMap["cluster_name"] = p.ClusterName.String
sinkFormats = append(sinkFormats, sinkMap)
}
if err := d.Set("sinks", sinkFormats); err != nil {
return diag.FromErr(err)
}
SetId(string(region), "sinks", databaseName, schemaName, d)
return diags
}