From d08416630b249baa4c5df6964ca92b674d363e45 Mon Sep 17 00:00:00 2001 From: Dan Petersen Date: Sun, 15 Dec 2019 17:34:46 +0100 Subject: [PATCH] Initial support for environment variables --- README.md | 26 +++++++++++++ proxmoxtf/provider.go | 75 +++++++++++++++++++++++++++++++++++--- proxmoxtf/provider_test.go | 7 +--- 3 files changed, 98 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 48906f87..be9fbc2b 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,32 @@ If you're building the provider, follow the instructions to [install it as a plu * `password` - (Required) The password for the Proxmox Virtual Environment API * `username` - (Required) The username for the Proxmox Virtual Environment API +#### Environment variables +You can set up the provider by passing environment variables instead of specifying arguments. + +* `PROXMOX_VE_ENDPOINT` or `PM_VE_ENDPOINT` - The endpoint for the Proxmox Virtual Environment API +* `PROXMOX_VE_INSECURE` or `PM_VE_INSECURE` - Whether to skip the TLS verification step +* `PROXMOX_VE_PASSWORD` or `PM_VE_PASSWORD` - The password for the Proxmox Virtual Environment API +* `PROXMOX_VE_USERNAME` or `PM_VE_USERNAME` - The username for the Proxmox Virtual Environment API + +```hcl +provider "proxmox" { + virtual_environment {} +} +``` + +##### Usage + +``` +export PROXMOX_VE_ENDPOINT="https://hostname:8006" +export PROXMOX_VE_INSECURE="true" +export PROXMOX_VE_PASSWORD="a-strong-password" +export PROXMOX_VE_USERNAME="username@realm" +terraform plan +``` + +You can omit `PROXMOX_VE_INSECURE`, if the Proxmox Virtual Environment API is exposing a certificate trusted by your operating system. + ### Data Sources #### Virtual Environment diff --git a/proxmoxtf/provider.go b/proxmoxtf/provider.go index b8aac8f0..16868b6a 100644 --- a/proxmoxtf/provider.go +++ b/proxmoxtf/provider.go @@ -6,6 +6,8 @@ package proxmoxtf import ( "errors" + "net/url" + "os" "github.com/danitso/terraform-provider-proxmox/proxmox" "github.com/hashicorp/terraform/helper/schema" @@ -55,24 +57,87 @@ func Provider() *schema.Provider { Schema: map[string]*schema.Schema{ mkProviderVirtualEnvironmentEndpoint: { Type: schema.TypeString, - Required: true, + Optional: true, Description: "The endpoint for the Proxmox Virtual Environment API", + DefaultFunc: schema.MultiEnvDefaultFunc( + []string{"PROXMOX_VE_ENDPOINT", "PM_VE_ENDPOINT"}, + "", + ), + ValidateFunc: func(v interface{}, k string) (warns []string, errs []error) { + value := v.(string) + + if value == "" { + return []string{}, []error{ + errors.New("You must specify an endpoint for the Proxmox Virtual Environment API (valid: https://host:port)"), + } + } + + _, err := url.ParseRequestURI(value) + + if err != nil { + return []string{}, []error{ + errors.New("You must specify a valid endpoint for the Proxmox Virtual Environment API (valid: https://host:port)"), + } + } + + return []string{}, []error{} + }, }, mkProviderVirtualEnvironmentInsecure: { Type: schema.TypeBool, Optional: true, Description: "Whether to skip the TLS verification step", - Default: false, + DefaultFunc: func() (interface{}, error) { + for _, k := range []string{"PROXMOX_VE_INSECURE", "PM_VE_INSECURE"} { + v := os.Getenv(k) + + if v == "true" || v == "1" { + return true, nil + } + } + + return false, nil + }, }, mkProviderVirtualEnvironmentPassword: { Type: schema.TypeString, - Required: true, + Optional: true, Description: "The password for the Proxmox Virtual Environment API", + DefaultFunc: schema.MultiEnvDefaultFunc( + []string{"PROXMOX_VE_PASSWORD", "PM_VE_PASSWORD"}, + "", + ), + ValidateFunc: func(v interface{}, k string) (warns []string, errs []error) { + value := v.(string) + + if value == "" { + return []string{}, []error{ + errors.New("You must specify a password for the Proxmox Virtual Environment API"), + } + } + + return []string{}, []error{} + }, }, mkProviderVirtualEnvironmentUsername: { Type: schema.TypeString, - Required: true, + Optional: true, Description: "The username for the Proxmox Virtual Environment API", + DefaultFunc: schema.MultiEnvDefaultFunc( + []string{"PROXMOX_VE_USERNAME", "PM_VE_USERNAME"}, + "", + ), + ValidateFunc: func(v interface{}, k string) (warns []string, errs []error) { + value := v.(string) + + if value == "" { + return []string{}, []error{ + errors.New("You must specify a username for the Proxmox Virtual Environment API (valid: username@realm)"), + } + } + + return []string{}, []error{} + }, }, }, }, @@ -113,7 +178,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) { func (c *providerConfiguration) GetVEClient() (*proxmox.VirtualEnvironmentClient, error) { if c.veClient == nil { - return nil, errors.New("You must specify the virtual environment details in the provider configuration to use this data source") + return nil, errors.New("You must specify the virtual environment details in the provider configuration") } return c.veClient, nil diff --git a/proxmoxtf/provider_test.go b/proxmoxtf/provider_test.go index 2080f67b..49b7142b 100644 --- a/proxmoxtf/provider_test.go +++ b/proxmoxtf/provider_test.go @@ -37,16 +37,13 @@ func TestProviderSchema(t *testing.T) { veSchema := testNestedSchemaExistence(t, s, mkProviderVirtualEnvironment) - testRequiredArguments(t, veSchema, []string{ + testOptionalArguments(t, veSchema, []string{ mkProviderVirtualEnvironmentEndpoint, + mkProviderVirtualEnvironmentInsecure, mkProviderVirtualEnvironmentPassword, mkProviderVirtualEnvironmentUsername, }) - testOptionalArguments(t, veSchema, []string{ - mkProviderVirtualEnvironmentInsecure, - }) - testSchemaValueTypes(t, veSchema, []string{ mkProviderVirtualEnvironmentEndpoint, mkProviderVirtualEnvironmentInsecure,