/
warehouse_metadata_data_source.go
186 lines (161 loc) · 5.43 KB
/
warehouse_metadata_data_source.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package provider
import (
"context"
"fmt"
"github.com/segmentio/terraform-provider-segment/internal/provider/models"
"github.com/segmentio/public-api-sdk-go/api"
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &warehouseMetadataDataSource{}
_ datasource.DataSourceWithConfigure = &warehouseMetadataDataSource{}
)
// NewWarehouseMetadataDataSource is a helper function to simplify the provider implementation.
func NewWarehouseMetadataDataSource() datasource.DataSource {
return &warehouseMetadataDataSource{}
}
// warehouseMetadataDataSource is the data source implementation.
type warehouseMetadataDataSource struct {
client *api.APIClient
authContext context.Context
}
func warehouseMetadataSchema() map[string]schema.Attribute {
return map[string]schema.Attribute{
"id": schema.StringAttribute{
Required: true,
Description: "The id of this object.",
},
"name": schema.StringAttribute{
Computed: true,
Description: "The name of this object.",
},
"slug": schema.StringAttribute{
Computed: true,
Description: "A human-readable, unique identifier for object.",
},
"description": schema.StringAttribute{
Computed: true,
Description: "A description, in English, of this object.",
},
"logos": schema.SingleNestedAttribute{
Computed: true,
Description: "Logo information for this object.",
Attributes: map[string]schema.Attribute{
"default": schema.StringAttribute{
Computed: true,
Description: "The default URL for this logo.",
},
"mark": schema.StringAttribute{
Computed: true,
Description: "The logo mark.",
},
"alt": schema.StringAttribute{
Computed: true,
Description: "The alternative text for this logo.",
},
},
},
"options": schema.ListNestedAttribute{
Computed: true,
Description: "The Integration options for this object.",
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Computed: true,
Description: "The name identifying this option in the context of a Segment Integration.",
},
"type": schema.StringAttribute{
Computed: true,
Description: "Defines the type for this option in the schema.",
},
"required": schema.BoolAttribute{
Computed: true,
Description: "Whether this is a required option when setting up the Integration.",
},
"description": schema.StringAttribute{
Computed: true,
Description: "An optional short text description of the field.",
},
"default_value": schema.StringAttribute{
CustomType: jsontypes.NormalizedType{},
Computed: true,
Description: "An optional default value for the field.",
},
"label": schema.StringAttribute{
Computed: true,
Description: "An optional label for this field.",
},
},
},
},
}
}
// Metadata returns the data source type name.
func (d *warehouseMetadataDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_warehouse_metadata"
}
// Read refreshes the Terraform state with the latest data.
func (d *warehouseMetadataDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state models.WarehouseMetadataState
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
id := state.ID.ValueString()
if id == "" {
resp.Diagnostics.AddError("Unable to read Warehouse metadata", "ID is empty")
return
}
response, body, err := d.client.CatalogAPI.GetWarehouseMetadata(d.authContext, state.ID.ValueString()).Execute()
if body != nil {
defer body.Body.Close()
}
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Warehouse metadata",
getError(err, body),
)
return
}
warehouseMetadata := response.Data.WarehouseMetadata
err = state.Fill(warehouseMetadata)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Warehouse metadata",
err.Error(),
)
return
}
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Schema defines the schema for the data source.
func (d *warehouseMetadataDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Reads a Warehouse metadata. For more information, visit the [Segment docs](https://segment.com/docs/connections/storage/catalog/).",
Attributes: warehouseMetadataSchema(),
}
}
// Configure adds the provider configured client to the data source.
func (d *warehouseMetadataDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
clientInfo, ok := req.ProviderData.(*ClientInfo)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *ClientInfo, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = clientInfo.client
d.authContext = clientInfo.authContext
}