feat: scaffold terraform provider for fission via kubernetes CRD
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"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"
|
||||
|
||||
"terraform-provider-fission/internal/client"
|
||||
"terraform-provider-fission/internal/resources"
|
||||
)
|
||||
|
||||
var _ provider.Provider = &FissionProvider{}
|
||||
|
||||
type FissionProvider struct {
|
||||
version string
|
||||
}
|
||||
|
||||
type FissionProviderModel struct {
|
||||
KubeconfigPath types.String `tfsdk:"kubeconfig_path"`
|
||||
KubeContext types.String `tfsdk:"kube_context"`
|
||||
Namespace types.String `tfsdk:"namespace"`
|
||||
}
|
||||
|
||||
func New(version string) func() provider.Provider {
|
||||
return func() provider.Provider {
|
||||
return &FissionProvider{version: version}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *FissionProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
|
||||
resp.TypeName = "fission"
|
||||
resp.Version = p.version
|
||||
}
|
||||
|
||||
func (p *FissionProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"kubeconfig_path": schema.StringAttribute{
|
||||
Optional: true,
|
||||
Description: "Путь к kubeconfig файлу.",
|
||||
},
|
||||
"kube_context": schema.StringAttribute{
|
||||
Optional: true,
|
||||
Description: "Имя kubernetes context.",
|
||||
},
|
||||
"namespace": schema.StringAttribute{
|
||||
Optional: true,
|
||||
Description: "Namespace для ресурсов Fission.",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *FissionProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
|
||||
var data FissionProviderModel
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
kubeconfigPath := data.KubeconfigPath.ValueString()
|
||||
if kubeconfigPath == "" {
|
||||
kubeconfigPath = os.Getenv("KUBECONFIG")
|
||||
}
|
||||
|
||||
kubeContext := data.KubeContext.ValueString()
|
||||
if kubeContext == "" {
|
||||
kubeContext = os.Getenv("KUBE_CONTEXT")
|
||||
}
|
||||
|
||||
namespace := data.Namespace.ValueString()
|
||||
if namespace == "" {
|
||||
namespace = os.Getenv("FISSION_NAMESPACE")
|
||||
}
|
||||
if namespace == "" {
|
||||
namespace = "default"
|
||||
}
|
||||
|
||||
fissionClient, err := client.New(kubeconfigPath, kubeContext, namespace)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Ошибка инициализации Kubernetes клиента", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
resp.DataSourceData = fissionClient
|
||||
resp.ResourceData = fissionClient
|
||||
}
|
||||
|
||||
func (p *FissionProvider) Resources(_ context.Context) []func() resource.Resource {
|
||||
return []func() resource.Resource{
|
||||
resources.NewEnvironmentResource,
|
||||
resources.NewPackageResource,
|
||||
resources.NewFunctionResource,
|
||||
resources.NewHTTPTriggerResource,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *FissionProvider) DataSources(_ context.Context) []func() datasource.DataSource {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *FissionProvider) ValidateConfig(ctx context.Context, req provider.ValidateConfigRequest, resp *provider.ValidateConfigResponse) {
|
||||
var data FissionProviderModel
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
if data.Namespace.IsNull() || data.Namespace.IsUnknown() {
|
||||
return
|
||||
}
|
||||
|
||||
if data.Namespace.ValueString() == "" {
|
||||
resp.Diagnostics.AddAttributeError(
|
||||
path.Root("namespace"),
|
||||
"Некорректный namespace",
|
||||
"Значение namespace не может быть пустой строкой.",
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user