/
provider.go
122 lines (104 loc) · 3.94 KB
/
provider.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
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/timeplus-io/terraform-provider-timeplus/internal/timeplus"
myValidator "github.com/timeplus-io/terraform-provider-timeplus/internal/validator"
)
// Ensure ScaffoldingProvider satisfies various provider interfaces.
var _ provider.Provider = &TimeplusProvider{}
// TimeplusProvider defines the provider implementation.
type TimeplusProvider struct {
// version is set to the provider version on release, "dev" when the
// provider is built and ran locally, and "test" when running acceptance
// testing.
version string
}
// TimeplusProviderModel describes the provider data model.
type TimeplusProviderModel struct {
Endpoint types.String `tfsdk:"endpoint"`
Workspace types.String `tfsdk:"workspace"`
ApiKey types.String `tfsdk:"api_key"`
}
func (p *TimeplusProvider) Metadata(ctx context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "timeplus"
resp.Version = p.version
}
func (p *TimeplusProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: `The Timeplus provider is used to interact with the resources supported by [Timeplus](https://www.timeplus.com/) in a workspace. The provider needs to be configured with an API key before it can be used.
Use the navigation to the left to read about the available resources.`,
Attributes: map[string]schema.Attribute{
"endpoint": schema.StringAttribute{
MarkdownDescription: "The base URL endpoint for connecting to the Timeplus workspace. When it's not set, `https://us.timeplus.cloud` will be used.",
Optional: true,
Validators: []validator.String{myValidator.URL()},
},
"workspace": schema.StringAttribute{
MarkdownDescription: "The ID of the workspace in which the provider manages resources.",
Required: true,
},
"api_key": schema.StringAttribute{
MarkdownDescription: "The API key to be used to call Timeplus API.",
Required: true,
Sensitive: true,
},
},
}
}
func (p *TimeplusProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var data TimeplusProviderModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// Configuration values are now available.
client, err := timeplus.NewClient(data.Workspace.ValueString(), data.ApiKey.ValueString(), timeplus.ClientOptions{
BaseURL: data.Endpoint.ValueString(),
})
if err != nil {
resp.Diagnostics.AddError("failed to create Timeplus client", err.Error())
return
}
resp.DataSourceData = client
resp.ResourceData = client
}
func (p *TimeplusProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewStreamResource,
NewViewResource,
NewMaterializedViewResource,
NewSinkResource,
NewAlertResource,
NewSourceResource,
NewRemoteFunctionResource,
NewJavascriptFunctionResource,
NewDashboardResource,
}
}
func (p *TimeplusProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewStreamDataSource,
NewViewDataSource,
NewMaterializedViewDataSource,
NewSinkDataSource,
NewAlertDataSource,
NewSourceDataSource,
NewRemoteFunctionDataSource,
NewJavascriptFunctionDataSource,
NewDashboardDataSource,
}
}
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &TimeplusProvider{
version: version,
}
}
}