diff --git a/examples/deployments/main.tf b/examples/deployments/main.tf index 45be905..f54402f 100644 --- a/examples/deployments/main.tf +++ b/examples/deployments/main.tf @@ -8,7 +8,6 @@ terraform { provider "artie" { endpoint = "http://0.0.0.0:8000" - api_key = "api-key" } data "artie_deployments" "example" {} diff --git a/internal/provider/deployments_data_source.go b/internal/provider/deployments_data_source.go index a551254..d13eff2 100644 --- a/internal/provider/deployments_data_source.go +++ b/internal/provider/deployments_data_source.go @@ -9,7 +9,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" - "github.com/hashicorp/terraform-plugin-framework/types/basetypes" "github.com/hashicorp/terraform-plugin-log/tflog" ) @@ -39,8 +38,8 @@ type deploymentsResponse struct { // deploymentsDataSource is the data source implementation. type deploymentsDataSource struct { - endpoint basetypes.StringValue - apiKey basetypes.StringValue + endpoint string + apiKey string } // Metadata returns the data source type name. @@ -56,11 +55,11 @@ func (d *deploymentsDataSource) Configure(_ context.Context, req datasource.Conf return } - providerData, ok := req.ProviderData.(ArtieProviderModel) + providerData, ok := req.ProviderData.(ArtieProviderData) if !ok { resp.Diagnostics.AddError( "Unexpected Data Source Configure Type", - fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), + fmt.Sprintf("Expected ArtieProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData), ) return @@ -93,16 +92,16 @@ func (d *deploymentsDataSource) Schema(_ context.Context, _ datasource.SchemaReq // Read refreshes the Terraform state with the latest data. func (d *deploymentsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { - tflog.Info(ctx, fmt.Sprintf("Endpoint: %s", d.endpoint.ValueString())) + tflog.Info(ctx, fmt.Sprintf("Endpoint: %s", d.endpoint)) var state deploymentsDataSourceModel - apiReq, err := http.NewRequest("GET", fmt.Sprintf("%s/deployments", d.endpoint.ValueString()), nil) + apiReq, err := http.NewRequest("GET", fmt.Sprintf("%s/deployments", d.endpoint), nil) if err != nil { resp.Diagnostics.AddError("Unable to Read Deployments", err.Error()) return } - apiReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", d.apiKey.ValueString())) + apiReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", d.apiKey)) apiResp, err := http.DefaultClient.Do(apiReq) if err != nil { resp.Diagnostics.AddError("Unable to Read Deployments", err.Error()) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 4073d4c..8abcc78 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -5,6 +5,7 @@ package provider import ( "context" + "os" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/function" @@ -32,6 +33,11 @@ type ArtieProviderModel struct { APIKey types.String `tfsdk:"api_key"` } +type ArtieProviderData struct { + Endpoint string + APIKey string +} + func (p *ArtieProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) { resp.TypeName = "artie" resp.Version = p.version @@ -61,11 +67,24 @@ func (p *ArtieProvider) Configure(ctx context.Context, req provider.ConfigureReq return } - // Configuration values are now available. - // if data.Endpoint.IsNull() { /* ... */ } + // Default values to environment variables, but override + // with Terraform configuration value if set. + endpoint := os.Getenv("ARTIE_ENDPOINT") + if !data.Endpoint.IsNull() { + endpoint = data.Endpoint.ValueString() + } + apiKey := os.Getenv("ARTIE_API_KEY") + if !data.APIKey.IsNull() { + apiKey = data.APIKey.ValueString() + } + + providerData := ArtieProviderData{ + Endpoint: endpoint, + APIKey: apiKey, + } - resp.DataSourceData = data - resp.ResourceData = data + resp.DataSourceData = providerData + resp.ResourceData = providerData } func (p *ArtieProvider) Resources(ctx context.Context) []func() resource.Resource {