/
data_source_endpoint_verification.go
104 lines (90 loc) · 3.47 KB
/
data_source_endpoint_verification.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
package vercel
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/vercel/terraform-provider-vercel/client"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &endpointVerificationDataSource{}
)
func newEndpointVerificationDataSource() datasource.DataSource {
return &endpointVerificationDataSource{}
}
type endpointVerificationDataSource struct {
client *client.Client
}
func (d *endpointVerificationDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_endpoint_verification"
}
func (d *endpointVerificationDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*client.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *client.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
// Schema returns the schema information for a file data source
func (d *endpointVerificationDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Provides a verification code that can be used to prove ownership over an API.",
Attributes: map[string]schema.Attribute{
"verification_code": schema.StringAttribute{
Description: "A verification code that should be set in the `x-vercel-verify` response header for your API. This is used to verify that the endpoint belongs to you.",
Computed: true,
},
"id": schema.StringAttribute{
Computed: true,
},
"team_id": schema.StringAttribute{
Optional: true,
Computed: true,
Description: "The ID of the team the Edge Config should exist under. Required when configuring a team resource if a default team has not been set in the provider.",
},
},
}
}
// FileData represents the information terraform knows about a File data source
type EndpointVerification struct {
ID types.String `tfsdk:"id"`
TeamID types.String `tfsdk:"team_id"`
VerificationCode types.String `tfsdk:"verification_code"`
}
// Read will read a file from the filesytem and provide terraform with information about it.
// It is called by the provider whenever data source values should be read to update state.
func (d *endpointVerificationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config EndpointVerification
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
code, err := d.client.GetEndpointVerificationCode(ctx, config.TeamID.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Failed to get verification code",
fmt.Sprintf("Failed to get verification code, unexpected error: %s", err),
)
return
}
diags = resp.State.Set(ctx, EndpointVerification{
TeamID: config.TeamID,
ID: types.StringValue(code),
VerificationCode: types.StringValue(code),
})
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}