|
@@ -5,11 +5,15 @@ package provider
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "os"
|
|
|
|
|
|
+ "github.com/hashicorp-demoapp/hashicups-client-go"
|
|
|
"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"
|
|
|
)
|
|
|
|
|
|
|
|
@@ -38,20 +42,166 @@ func (p *hashicupsProvider) Metadata(_ context.Context, _ provider.MetadataReque
|
|
|
}
|
|
|
|
|
|
func (p *hashicupsProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
|
|
|
- resp.Schema = schema.Schema{}
|
|
|
+ resp.Schema = schema.Schema{
|
|
|
+ Attributes: map[string]schema.Attribute{
|
|
|
+ "host": schema.StringAttribute{
|
|
|
+ Optional: true,
|
|
|
+ },
|
|
|
+ "username": schema.StringAttribute{
|
|
|
+ Optional: true,
|
|
|
+ },
|
|
|
+ "password": schema.StringAttribute{
|
|
|
+ Optional: true,
|
|
|
+ Sensitive: true,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-func (p *hashicupsProvider) Configure(_ context.Context, _ provider.ConfigureRequest, _ *provider.ConfigureResponse) {
|
|
|
+func (p *hashicupsProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
|
|
|
+
|
|
|
+
|
|
|
+ var config hashicupsProviderModel
|
|
|
+ diags := req.Config.Get(ctx, &config)
|
|
|
+ resp.Diagnostics.Append(diags...)
|
|
|
+ if resp.Diagnostics.HasError() {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if config.Host.IsUnknown() {
|
|
|
+ resp.Diagnostics.AddAttributeError(
|
|
|
+ path.Root("host"),
|
|
|
+ "Unknown HashiCups API Host",
|
|
|
+ "The provider cannot create the HashiCups API client as there is an unknown configuration value for the HashiCups API host. "+
|
|
|
+ "Either target apply the source of the value first, set the value statically in the configuration, or use the HASHICUPS_HOST environment variable.",
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ if config.Username.IsUnknown() {
|
|
|
+ resp.Diagnostics.AddAttributeError(
|
|
|
+ path.Root("username"),
|
|
|
+ "Unknown HashiCups API Username",
|
|
|
+ "The provider cannot create the HashiCups API client as there is an unknown configuration value for the HashiCups API username. "+
|
|
|
+ "Either target apply the source of the value first, set the value statically in the configuration, or use the HASHICUPS_USERNAME environment variable.",
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ if config.Password.IsUnknown() {
|
|
|
+ resp.Diagnostics.AddAttributeError(
|
|
|
+ path.Root("password"),
|
|
|
+ "Unknown HashiCups API Password",
|
|
|
+ "The provider cannot create the HashiCups API client as there is an unknown configuration value for the HashiCups API password. "+
|
|
|
+ "Either target apply the source of the value first, set the value statically in the configuration, or use the HASHICUPS_PASSWORD environment variable.",
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ if resp.Diagnostics.HasError() {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ host := os.Getenv("HASHICUPS_HOST")
|
|
|
+ username := os.Getenv("HASHICUPS_USERNAME")
|
|
|
+ password := os.Getenv("HASHICUPS_PASSWORD")
|
|
|
+
|
|
|
+
|
|
|
+ if !config.Host.IsNull() {
|
|
|
+ host = config.Host.ValueString()
|
|
|
+ }
|
|
|
+ if !config.Username.IsNull() {
|
|
|
+ username = config.Username.ValueString()
|
|
|
+ }
|
|
|
+
|
|
|
+ if !config.Password.IsNull() {
|
|
|
+ password = config.Password.ValueString()
|
|
|
+ }
|
|
|
+
|
|
|
+ if host == "" {
|
|
|
+ resp.Diagnostics.AddAttributeError(
|
|
|
+ path.Root("host"),
|
|
|
+ "Missing HashiCups API Host",
|
|
|
+ "The provider cannot create the HashiCups API client as there is a missing or empty value for the HashiCups API host. "+
|
|
|
+ "Set the host value in the configuration or use the HASHICUPS_HOST environment variable. "+
|
|
|
+ "If either is already set, ensure the value is not empty.",
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ if username == "" {
|
|
|
+ resp.Diagnostics.AddAttributeError(
|
|
|
+ path.Root("username"),
|
|
|
+ "Missing HashiCups API Username",
|
|
|
+ "The provider cannot create the HashiCups API client as there is a missing or empty value for the HashiCups API username. "+
|
|
|
+ "Set the username value in the configuration or use the HASHICUPS_USERNAME environment variable. "+
|
|
|
+ "If either is already set, ensure the value is not empty.",
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ if password == "" {
|
|
|
+ resp.Diagnostics.AddAttributeError(
|
|
|
+ path.Root("password"),
|
|
|
+ "Missing HashiCups API Password",
|
|
|
+ "The provider cannot create the HashiCups API client as there is a missing or empty value for the HashiCups API password. "+
|
|
|
+ "Set the password value in the configuration or use the HASHICUPS_PASSWORD environment variable. "+
|
|
|
+ "If either is already set, ensure the value is not empty.",
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ if resp.Diagnostics.HasError() {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ client, err := hashicups.NewClient(&host, &username, &password)
|
|
|
+ if err != nil {
|
|
|
+ resp.Diagnostics.AddError(
|
|
|
+ "Unable to Create HashiCups API Client",
|
|
|
+ "An unexpected error occurred when creating the HashiCups API client. "+
|
|
|
+ "If the error is not clear, please contact the provider developers.\n\n"+
|
|
|
+ "HashiCups Client Error: "+err.Error(),
|
|
|
+ )
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ resp.DataSourceData = client
|
|
|
+ resp.ResourceData = client
|
|
|
}
|
|
|
|
|
|
func (p *hashicupsProvider) DataSources(_ context.Context) []func() datasource.DataSource {
|
|
|
return nil
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
func (p *hashicupsProvider) Resources(_ context.Context) []func() resource.Resource {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+type hashicupsProviderModel struct {
|
|
|
+ Host types.String `tfsdk:"host"`
|
|
|
+ Username types.String `tfsdk:"username"`
|
|
|
+ Password types.String `tfsdk:"password"`
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
|
|
|
|