From 4e1b8602f55bde639564e56a15beccef72a81a44 Mon Sep 17 00:00:00 2001
From: Simon Walker <github@stwalkerster.co.uk>
Date: Fri, 29 Nov 2024 00:00:28 +0000
Subject: [PATCH] Allow configuring the provider by environment variables

Fixes T321250
---
 internal/provider/provider.go | 49 ++++++++++++++++++++++++++++++++---
 1 file changed, 45 insertions(+), 4 deletions(-)

diff --git a/internal/provider/provider.go b/internal/provider/provider.go
index ddf1275..814b426 100644
--- a/internal/provider/provider.go
+++ b/internal/provider/provider.go
@@ -3,6 +3,7 @@ package provider
 import (
 	"context"
 	"fmt"
+	"os"
 
 	"github.com/gophercloud/gophercloud"
 	"github.com/gophercloud/gophercloud/openstack"
@@ -48,6 +49,46 @@ func (p *CloudVpsProvider) Configure(ctx context.Context, req provider.Configure
 	diags := req.Config.Get(ctx, &data)
 	resp.Diagnostics.Append(diags...)
 
+	if data.OsAuthUrl.IsNull() {
+		envVar := os.Getenv("OS_AUTH_URL")
+
+		if envVar != "" {
+			data.OsAuthUrl = types.StringValue(envVar)
+		} else {
+			resp.Diagnostics.AddError("OpenStack Auth URL not set", "The provider is unable to determine the OpenStack Auth URL. Please configure this setting in the provider block or by using the OS_AUTH_URL environment variable.")
+		}
+	}
+
+	if data.OsProjectId.IsNull() {
+		envVar := os.Getenv("OS_PROJECT_ID")
+
+		if envVar != "" {
+			data.OsProjectId = types.StringValue(envVar)
+		} else {
+			resp.Diagnostics.AddError("OpenStack Project ID not set", "The provider is unable to determine the OpenStack Project ID. Please configure this setting in the provider block or by using the OS_PROJECT_ID environment variable.")
+		}
+	}
+
+	if data.OsApplicationCredentialId.IsNull() {
+		envVar := os.Getenv("OS_APPLICATION_CREDENTIAL_ID")
+
+		if envVar != "" {
+			data.OsApplicationCredentialId = types.StringValue(envVar)
+		} else {
+			resp.Diagnostics.AddError("OpenStack Application Credential ID not set", "The provider is unable to determine the OpenStack Application Credential ID. Please configure this setting in the provider block or by using the OS_APPLICATION_CREDENTIAL_ID environment variable.")
+		}
+	}
+
+	if data.OsApplicationCredentialSecret.IsNull() {
+		envVar := os.Getenv("OS_APPLICATION_CREDENTIAL_SECRET")
+
+		if envVar != "" {
+			data.OsApplicationCredentialSecret = types.StringValue(envVar)
+		} else {
+			resp.Diagnostics.AddError("OpenStack Application Credential Secret not set", "The provider is unable to determine the OpenStack Application Credential Secret. Please configure this setting in the provider block or by using the OS_APPLICATION_CREDENTIAL_SECRET environment variable.")
+		}
+	}
+
 	if resp.Diagnostics.HasError() {
 		return
 	}
@@ -93,20 +134,20 @@ func (p *CloudVpsProvider) Schema(ctx context.Context, req provider.SchemaReques
 		Attributes: map[string]schema.Attribute{
 			"os_auth_url": schema.StringAttribute{
 				MarkdownDescription: "OpenStack authentication URL",
-				Required:            true,
+				Optional:            true,
 			},
 			"os_application_credential_id": schema.StringAttribute{
 				MarkdownDescription: "OpenStack application credential ID",
-				Required:            true,
+				Optional:            true,
 			},
 			"os_application_credential_secret": schema.StringAttribute{
 				MarkdownDescription: "OpenStack application credential secret",
-				Required:            true,
+				Optional:            true,
 				Sensitive:           true,
 			},
 			"os_project_id": schema.StringAttribute{
 				MarkdownDescription: "OpenStack project ID",
-				Required:            true,
+				Optional:            true,
 			},
 		},
 	}
-- 
2.47.0

