/
destination_metadata_data_source.go
447 lines (425 loc) · 15 KB
/
destination_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package provider
import (
"context"
"fmt"
"github.com/segmentio/terraform-provider-segment/internal/provider/models"
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/segmentio/public-api-sdk-go/api"
"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 = &destinationMetadataDataSource{}
_ datasource.DataSourceWithConfigure = &destinationMetadataDataSource{}
)
// NewDestinationMetadataDataSource is a helper function to simplify the provider implementation.
func NewDestinationMetadataDataSource() datasource.DataSource {
return &destinationMetadataDataSource{}
}
// destinationMetadataDataSource is the data source implementation.
type destinationMetadataDataSource struct {
client *api.APIClient
authContext context.Context
}
func destinationMetadataSchema() map[string]schema.Attribute {
return map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "The id of the Destination metadata. Config API note: analogous to `name`.",
Required: true,
},
"name": schema.StringAttribute{
Description: "The user-friendly name of the Destination. Config API note: equal to `displayName`.",
Computed: true,
},
"description": schema.StringAttribute{
Description: "The description of the Destination.",
Computed: true,
},
"slug": schema.StringAttribute{
Description: "The slug used to identify the Destination in the Segment app.",
Computed: true,
},
"logos": schema.SingleNestedAttribute{
Description: "The Destination's logos.",
Computed: true,
Attributes: map[string]schema.Attribute{
"default": schema.StringAttribute{
Computed: true,
},
"mark": schema.StringAttribute{
Description: "The logo mark.",
Computed: true,
},
"alt": schema.StringAttribute{
Description: "The alternative text for this logo.",
Computed: true,
},
},
},
"options": schema.ListNestedAttribute{
Description: "Options configured for the Destination.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "The name identifying this option in the context of a Segment Integration.",
Computed: true,
},
"type": schema.StringAttribute{
Description: "Defines the type for this option in the schema.",
Computed: true,
},
"required": schema.BoolAttribute{
Description: "Whether this is a required option when setting up the Integration.",
Computed: true,
},
"description": schema.StringAttribute{
Description: "An optional short text description of the field.",
Computed: true,
},
"default_value": schema.StringAttribute{
Description: "An optional default value for the field.",
Computed: true,
CustomType: jsontypes.NormalizedType{},
},
"label": schema.StringAttribute{
Description: "An optional label for this field.",
Computed: true,
},
},
},
},
"status": schema.StringAttribute{
Description: "Support status of the Destination.",
Computed: true,
},
"previous_names": schema.ListAttribute{
ElementType: types.StringType,
Description: "A list of names previously used by the Destination.",
Computed: true,
},
"categories": schema.ListAttribute{
ElementType: types.StringType,
Description: "A list of categories with which the Destination is associated.",
Computed: true,
},
"website": schema.StringAttribute{
Description: "A website URL for this Destination.",
Computed: true,
},
"components": schema.ListNestedAttribute{
Description: "A list of components this Destination provides.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"type": schema.StringAttribute{
Description: "The component type.",
Computed: true,
},
"code": schema.StringAttribute{
Description: "Link to the repository hosting the code for this component.",
Computed: true,
},
"owner": schema.StringAttribute{
Description: "The owner of this component. Either 'SEGMENT' or 'PARTNER'.",
Computed: true,
},
},
},
},
"supported_features": schema.SingleNestedAttribute{
Description: "Features that this Destination supports.",
Computed: true,
Attributes: map[string]schema.Attribute{
"cloud_mode_instances": schema.StringAttribute{
Description: "This Destination's support level for cloud mode instances.",
Computed: true,
},
"device_mode_instances": schema.StringAttribute{
Description: "This Destination's support level for device mode instances.",
Computed: true,
},
"replay": schema.BoolAttribute{
Description: "Whether this Destination supports replays.",
Computed: true,
},
"browser_unbundling": schema.BoolAttribute{
Description: "Whether this Destination supports browser unbundling.",
Computed: true,
},
"browser_unbundling_public": schema.BoolAttribute{
Description: "Whether this Destination supports public browser unbundling.",
Computed: true,
},
},
},
"supported_methods": schema.SingleNestedAttribute{
Description: "Methods that this Destination supports.",
Computed: true,
Attributes: map[string]schema.Attribute{
"pageview": schema.BoolAttribute{
Description: "Identifies if the Destination supports the `pageview` method.",
Computed: true,
},
"identify": schema.BoolAttribute{
Description: "Identifies if the Destination supports the `identify` method.",
Computed: true,
},
"alias": schema.BoolAttribute{
Description: "Identifies if the Destination supports the `alias` method.",
Computed: true,
},
"track": schema.BoolAttribute{
Description: "Identifies if the Destination supports the `track` method.",
Computed: true,
},
"group": schema.BoolAttribute{
Description: "Identifies if the Destination supports the `group` method.",
Computed: true,
},
},
},
"supported_platforms": schema.SingleNestedAttribute{
Description: "Platforms from which the Destination receives events.",
Computed: true,
Attributes: map[string]schema.Attribute{
"browser": schema.BoolAttribute{
Description: "Whether this Destination supports browser events.",
Computed: true,
},
"server": schema.BoolAttribute{
Description: "Whether this Destination supports server events.",
Computed: true,
},
"mobile": schema.BoolAttribute{
Description: "Whether this Destination supports mobile events.",
Computed: true,
},
},
},
"actions": schema.ListNestedAttribute{
Description: "Actions available for the Destination.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "The primary key of the action.",
Computed: true,
},
"slug": schema.StringAttribute{
Description: "A machine-readable key unique to the action definition.",
Computed: true,
},
"name": schema.StringAttribute{
Description: "A human-readable name for the action.",
Computed: true,
},
"description": schema.StringAttribute{
Description: "A human-readable description of the action. May include Markdown.",
Computed: true,
},
"platform": schema.StringAttribute{
Description: "The platform on which this action runs.",
Computed: true,
},
"hidden": schema.BoolAttribute{
Description: "Whether the action should be hidden.",
Computed: true,
},
"default_trigger": schema.StringAttribute{
Description: "The default value used as the trigger when connecting this action.",
Optional: true,
},
"fields": schema.ListNestedAttribute{
Description: "The fields expected in order to perform the action.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "The primary key of the field.",
Computed: true,
},
"sort_order": schema.Float64Attribute{
Description: "The order this particular field is (used in the UI for displaying the fields in a specified order).",
Computed: true,
},
"field_key": schema.StringAttribute{
Description: "A unique machine-readable key for the field. Should ideally match the expected key in the action's API request.",
Computed: true,
},
"label": schema.StringAttribute{
Description: "A human-readable label for this value.",
Computed: true,
},
"type": schema.StringAttribute{
Description: "The data type for this value.",
Computed: true,
},
"description": schema.StringAttribute{
Description: "A human-readable description of this value. You can use Markdown.",
Computed: true,
},
"placeholder": schema.StringAttribute{
Description: "An example value displayed but not saved.",
Computed: true,
},
"default_value": schema.StringAttribute{
Description: "A default value that is saved the first time an action is created.",
Computed: true,
CustomType: jsontypes.NormalizedType{},
},
"required": schema.BoolAttribute{
Description: "Whether this field is required.",
Computed: true,
},
"multiple": schema.BoolAttribute{
Description: "Whether a user can provide multiples of this field.",
Computed: true,
},
"choices": schema.StringAttribute{
Description: "A list of machine-readable value/label pairs to populate a static dropdown.",
Computed: true,
CustomType: jsontypes.NormalizedType{},
},
"dynamic": schema.BoolAttribute{
Description: "Whether this field should execute a dynamic request to fetch choices to populate a dropdown. When true, `choices` is ignored.",
Computed: true,
},
"allow_null": schema.BoolAttribute{
Description: "Whether this field allows null values.",
Computed: true,
},
},
},
},
},
},
},
"presets": schema.ListNestedAttribute{
Description: "Predefined Destination subscriptions that can optionally be applied when connecting a new instance of the Destination.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"action_id": schema.StringAttribute{
Description: "The unique identifier for the Destination Action to trigger.",
Computed: true,
},
"name": schema.StringAttribute{
Description: "The name of the subscription.",
Computed: true,
},
"fields": schema.StringAttribute{
Computed: true,
Description: "The default settings for action fields.",
CustomType: jsontypes.NormalizedType{},
},
"trigger": schema.StringAttribute{
Description: "FQL string that describes what events should trigger an action. See https://segment.com/docs/config-api/fql/ for more information regarding Segment's Filter Query Language (FQL).",
Computed: true,
},
},
},
},
"contacts": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "Name of this contact.",
Computed: true,
},
"email": schema.StringAttribute{
Description: "Email of this contact.",
Computed: true,
},
"role": schema.StringAttribute{
Description: "Role of this contact.",
Computed: true,
},
"is_primary": schema.BoolAttribute{
Description: "Whether this is a primary contact.",
Computed: true,
},
},
},
Description: "Contact info for Integration Owners.",
Computed: true,
},
"partner_owned": schema.BoolAttribute{
Description: "Partner Owned flag.",
Computed: true,
},
"supported_regions": schema.ListAttribute{
ElementType: types.StringType,
Description: "A list of supported regions for this Destination.",
Computed: true,
},
"region_endpoints": schema.ListAttribute{
ElementType: types.StringType,
Description: "The list of regional endpoints for this Destination.",
Computed: true,
},
}
}
// Metadata returns the data source type name.
func (d *destinationMetadataDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_destination_metadata"
}
// Schema defines the schema for the data source.
func (d *destinationMetadataDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Reads a Destination metadata. For more information, visit the [Segment docs](https://segment.com/docs/connections/destinations/catalog/).",
Attributes: destinationMetadataSchema(),
}
}
// Read refreshes the Terraform state with the latest data.
func (d *destinationMetadataDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state models.DestinationMetadataState
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
response, body, err := d.client.CatalogAPI.GetDestinationMetadata(d.authContext, state.ID.ValueString()).Execute()
if body != nil {
defer body.Body.Close()
}
if err != nil {
resp.Diagnostics.AddError(
"Unable to read Source metadata",
getError(err, body),
)
return
}
destinationMetadata := response.Data.DestinationMetadata
err = state.Fill(destinationMetadata)
if err != nil {
resp.Diagnostics.AddError(
"Unable to read Source metadata",
err.Error(),
)
return
}
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Configure adds the provider configured client to the data source.
func (d *destinationMetadataDataSource) 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
}