114 lines
3.2 KiB
Go
114 lines
3.2 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
|
"github.com/hashicorp/terraform-plugin-framework/path"
|
|
"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/types"
|
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
|
|
|
edgeclient "edp.buildth.ing/DevFW-CICD/edge-connect-client/v2/sdk/edgeconnect"
|
|
)
|
|
|
|
var _ provider.Provider = &EdgeConnectProvider{}
|
|
|
|
type EdgeConnectProvider struct {
|
|
version string
|
|
}
|
|
|
|
type EdgeConnectProviderModel struct {
|
|
Endpoint types.String `tfsdk:"endpoint"`
|
|
Token types.String `tfsdk:"token"`
|
|
}
|
|
|
|
func (p *EdgeConnectProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
|
|
resp.TypeName = "edge-connect"
|
|
resp.Version = p.version
|
|
}
|
|
|
|
func (p *EdgeConnectProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
Attributes: map[string]schema.Attribute{
|
|
"endpoint": schema.StringAttribute{
|
|
MarkdownDescription: "Edge Connect API endpoint",
|
|
Required: true,
|
|
},
|
|
"token": schema.StringAttribute{
|
|
MarkdownDescription: "Edge Connect API token",
|
|
Required: true,
|
|
Sensitive: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (p *EdgeConnectProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
|
|
var data EdgeConnectProviderModel
|
|
|
|
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
endpoint := data.Endpoint.ValueString()
|
|
token := data.Token.ValueString()
|
|
|
|
if endpoint == "" {
|
|
resp.Diagnostics.AddAttributeError(
|
|
path.Root("endpoint"),
|
|
"Missing Edge Connect API Endpoint",
|
|
"The provider cannot create the Edge Connect API client as there is a missing or empty value for the Edge Connect API endpoint.",
|
|
)
|
|
}
|
|
|
|
if token == "" {
|
|
resp.Diagnostics.AddAttributeError(
|
|
path.Root("token"),
|
|
"Missing Edge Connect API Token",
|
|
"The provider cannot create the Edge Connect API client as there is a missing or empty value for the Edge Connect API token.",
|
|
)
|
|
}
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
ctx = tflog.SetField(ctx, "edge_connect_endpoint", endpoint)
|
|
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "edge_connect_token")
|
|
|
|
tflog.Debug(ctx, "Creating Edge Connect client")
|
|
|
|
client := edgeclient.NewClient(endpoint)
|
|
|
|
resp.DataSourceData = client
|
|
resp.ResourceData = client
|
|
|
|
tflog.Info(ctx, "Configured Edge Connect client", map[string]any{"success": true})
|
|
}
|
|
|
|
func (p *EdgeConnectProvider) Resources(ctx context.Context) []func() resource.Resource {
|
|
return []func() resource.Resource{
|
|
NewAppResource,
|
|
NewAppInstanceResource,
|
|
}
|
|
}
|
|
|
|
func (p *EdgeConnectProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
|
|
return []func() datasource.DataSource{
|
|
NewAppDataSource,
|
|
NewAppInstanceDataSource,
|
|
}
|
|
}
|
|
|
|
func New(version string) func() provider.Provider {
|
|
return func() provider.Provider {
|
|
return &EdgeConnectProvider{
|
|
version: version,
|
|
}
|
|
}
|
|
}
|