0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-12 08:45:01 +00:00

fix(provider): Deprecate virtual_environment block (#288)

refactor(provider): Allow specifying attributes outside of virtual_environment block

Also deprecate virtual_environment block, update docs and examples.

Fixes #117
Apparently CDKTF skips schemas without attributes, it has been fixed but it is available only in prerelease currently (https://github.com/hashicorp/terraform-cdk/pull/2736)

Release-As: 0.17.0
This commit is contained in:
Szczepan Wiśniowski 2023-04-05 01:55:48 +02:00 committed by GitHub
parent e8d926218e
commit ed3dfeae99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 150 additions and 171 deletions

View File

@ -17,13 +17,11 @@ Use the navigation to the left to read about the available resources.
```terraform ```terraform
provider "proxmox" { provider "proxmox" {
virtual_environment {
endpoint = "https://10.0.0.2:8006/" endpoint = "https://10.0.0.2:8006/"
username = "root@pam" username = "root@pam"
password = "the-password-set-during-installation-of-proxmox-ve" password = "the-password-set-during-installation-of-proxmox-ve"
insecure = true insecure = true
} }
}
``` ```
## Authentication ## Authentication
@ -46,11 +44,9 @@ in the Proxmox provider block:
```terraform ```terraform
provider "proxmox" { provider "proxmox" {
virtual_environment {
username = "username@realm" username = "username@realm"
password = "a-strong-password" password = "a-strong-password"
} }
}
``` ```
### Environment variables ### Environment variables
@ -60,9 +56,7 @@ and `PROXMOX_VE_PASSWORD`, environment variables, representing your Proxmox
username, realm and password, respectively: username, realm and password, respectively:
```terraform ```terraform
provider "proxmox" { provider "proxmox" {}
virtual_environment {}
}
``` ```
Usage: Usage:
@ -80,8 +74,6 @@ to [generic provider arguments](https://www.terraform.io/docs/configuration/prov
e.g. `alias` and `version`), the following arguments are supported in the e.g. `alias` and `version`), the following arguments are supported in the
Proxmox `provider` block: Proxmox `provider` block:
- `virtual_environment` - (Optional) The Proxmox Virtual Environment
configuration.
- `endpoint` - (Required) The endpoint for the Proxmox Virtual Environment - `endpoint` - (Required) The endpoint for the Proxmox Virtual Environment
API (can also be sourced from `PROXMOX_VE_ENDPOINT`). Usually this is API (can also be sourced from `PROXMOX_VE_ENDPOINT`). Usually this is
`https://<your-cluster-endpoint>:8006/`. `https://<your-cluster-endpoint>:8006/`.

View File

@ -1,8 +1,6 @@
provider "proxmox" { provider "proxmox" {
virtual_environment {
endpoint = var.virtual_environment_endpoint endpoint = var.virtual_environment_endpoint
username = var.virtual_environment_username username = var.virtual_environment_username
password = var.virtual_environment_password password = var.virtual_environment_password
insecure = true insecure = true
} }
}

View File

@ -17,17 +17,14 @@ import (
) )
const ( const (
dvProviderVirtualEnvironmentEndpoint = "" dvProviderOTP = ""
dvProviderVirtualEnvironmentOTP = ""
dvProviderVirtualEnvironmentPassword = ""
dvProviderVirtualEnvironmentUsername = ""
mkProviderVirtualEnvironment = "virtual_environment" mkProviderVirtualEnvironment = "virtual_environment"
mkProviderVirtualEnvironmentEndpoint = "endpoint" mkProviderEndpoint = "endpoint"
mkProviderVirtualEnvironmentInsecure = "insecure" mkProviderInsecure = "insecure"
mkProviderVirtualEnvironmentOTP = "otp" mkProviderOTP = "otp"
mkProviderVirtualEnvironmentPassword = "password" mkProviderPassword = "password"
mkProviderVirtualEnvironmentUsername = "username" mkProviderUsername = "username"
) )
// ProxmoxVirtualEnvironment returns the object for this provider. // ProxmoxVirtualEnvironment returns the object for this provider.
@ -51,16 +48,25 @@ func providerConfigure(_ context.Context, d *schema.ResourceData) (interface{},
veConfig := veConfigBlock[0].(map[string]interface{}) veConfig := veConfigBlock[0].(map[string]interface{})
veClient, err = proxmox.NewVirtualEnvironmentClient( veClient, err = proxmox.NewVirtualEnvironmentClient(
veConfig[mkProviderVirtualEnvironmentEndpoint].(string), veConfig[mkProviderEndpoint].(string),
veConfig[mkProviderVirtualEnvironmentUsername].(string), veConfig[mkProviderUsername].(string),
veConfig[mkProviderVirtualEnvironmentPassword].(string), veConfig[mkProviderPassword].(string),
veConfig[mkProviderVirtualEnvironmentOTP].(string), veConfig[mkProviderOTP].(string),
veConfig[mkProviderVirtualEnvironmentInsecure].(bool), veConfig[mkProviderInsecure].(bool),
) )
} else {
veClient, err = proxmox.NewVirtualEnvironmentClient(
d.Get(mkProviderEndpoint).(string),
d.Get(mkProviderUsername).(string),
d.Get(mkProviderPassword).(string),
d.Get(mkProviderOTP).(string),
d.Get(mkProviderInsecure).(bool),
)
}
if err != nil { if err != nil {
return nil, diag.FromErr(err) return nil, diag.FromErr(err)
} }
}
config := proxmoxtf.NewProviderConfiguration(veClient) config := proxmoxtf.NewProviderConfiguration(veClient)

View File

@ -33,27 +33,37 @@ func TestProviderSchema(t *testing.T) {
test.AssertOptionalArguments(t, s, []string{ test.AssertOptionalArguments(t, s, []string{
mkProviderVirtualEnvironment, mkProviderVirtualEnvironment,
mkProviderUsername,
mkProviderPassword,
mkProviderEndpoint,
mkProviderInsecure,
mkProviderOTP,
}) })
test.AssertValueTypes(t, s, map[string]schema.ValueType{ test.AssertValueTypes(t, s, map[string]schema.ValueType{
mkProviderVirtualEnvironment: schema.TypeList, mkProviderVirtualEnvironment: schema.TypeList,
mkProviderUsername: schema.TypeString,
mkProviderPassword: schema.TypeString,
mkProviderEndpoint: schema.TypeString,
mkProviderInsecure: schema.TypeBool,
mkProviderOTP: schema.TypeString,
}) })
veSchema := test.AssertNestedSchemaExistence(t, s, mkProviderVirtualEnvironment) veSchema := test.AssertNestedSchemaExistence(t, s, mkProviderVirtualEnvironment)
test.AssertOptionalArguments(t, veSchema, []string{ test.AssertOptionalArguments(t, veSchema, []string{
mkProviderVirtualEnvironmentEndpoint, mkProviderEndpoint,
mkProviderVirtualEnvironmentInsecure, mkProviderInsecure,
mkProviderVirtualEnvironmentOTP, mkProviderOTP,
mkProviderVirtualEnvironmentPassword, mkProviderPassword,
mkProviderVirtualEnvironmentUsername, mkProviderUsername,
}) })
test.AssertValueTypes(t, veSchema, map[string]schema.ValueType{ test.AssertValueTypes(t, veSchema, map[string]schema.ValueType{
mkProviderVirtualEnvironmentEndpoint: schema.TypeString, mkProviderEndpoint: schema.TypeString,
mkProviderVirtualEnvironmentInsecure: schema.TypeBool, mkProviderInsecure: schema.TypeBool,
mkProviderVirtualEnvironmentOTP: schema.TypeString, mkProviderOTP: schema.TypeString,
mkProviderVirtualEnvironmentPassword: schema.TypeString, mkProviderPassword: schema.TypeString,
mkProviderVirtualEnvironmentUsername: schema.TypeString, mkProviderUsername: schema.TypeString,
}) })
} }

View File

@ -7,52 +7,45 @@
package provider package provider
import ( import (
"errors" "fmt"
"net/url"
"os" "os"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
) )
func createSchema() map[string]*schema.Schema { func createSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{ providerSchema := nestedProviderSchema()
mkProviderVirtualEnvironment: { providerSchema[mkProviderVirtualEnvironment] = &schema.Schema{
Type: schema.TypeList, Type: schema.TypeList,
Optional: true, Optional: true,
Elem: &schema.Resource{ Elem: &schema.Resource{
Schema: map[string]*schema.Schema{ Schema: nestedProviderSchema(),
mkProviderVirtualEnvironmentEndpoint: { },
MaxItems: 1,
Deprecated: "Move attributes out of virtual_environment block",
}
return providerSchema
}
func nestedProviderSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
mkProviderEndpoint: {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Description: "The endpoint for the Proxmox Virtual Environment API", Description: "The endpoint for the Proxmox Virtual Environment API",
DefaultFunc: schema.MultiEnvDefaultFunc( DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"PROXMOX_VE_ENDPOINT", "PM_VE_ENDPOINT"}, []string{"PROXMOX_VE_ENDPOINT", "PM_VE_ENDPOINT"},
dvProviderVirtualEnvironmentEndpoint, nil,
), ),
ValidateFunc: func(v interface{}, k string) (warns []string, errs []error) { AtLeastOneOf: []string{
value := v.(string) mkProviderEndpoint,
fmt.Sprintf("%s.0.%s", mkProviderVirtualEnvironment, mkProviderEndpoint),
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{}
}, },
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
}, },
mkProviderVirtualEnvironmentInsecure: { mkProviderInsecure: {
Type: schema.TypeBool, Type: schema.TypeBool,
Optional: true, Optional: true,
Description: "Whether to skip the TLS verification step", Description: "Whether to skip the TLS verification step",
@ -68,62 +61,42 @@ func createSchema() map[string]*schema.Schema {
return false, nil return false, nil
}, },
}, },
mkProviderVirtualEnvironmentOTP: { mkProviderOTP: {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Description: "The one-time password for the Proxmox Virtual Environment API", Description: "The one-time password for the Proxmox Virtual Environment API",
DefaultFunc: schema.MultiEnvDefaultFunc( DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"PROXMOX_VE_OTP", "PM_VE_OTP"}, []string{"PROXMOX_VE_OTP", "PM_VE_OTP"},
dvProviderVirtualEnvironmentOTP, dvProviderOTP,
), ),
}, },
mkProviderVirtualEnvironmentPassword: { mkProviderPassword: {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Description: "The password for the Proxmox Virtual Environment API", Description: "The password for the Proxmox Virtual Environment API",
DefaultFunc: schema.MultiEnvDefaultFunc( DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"PROXMOX_VE_PASSWORD", "PM_VE_PASSWORD"}, []string{"PROXMOX_VE_PASSWORD", "PM_VE_PASSWORD"},
dvProviderVirtualEnvironmentPassword, nil,
), ),
ValidateFunc: func(v interface{}, k string) (warns []string, errs []error) { AtLeastOneOf: []string{
value := v.(string) mkProviderPassword,
fmt.Sprintf("%s.0.%s", mkProviderVirtualEnvironment, mkProviderPassword),
if value == "" {
return []string{}, []error{
errors.New(
"you must specify a password for the Proxmox Virtual Environment API",
),
}
}
return []string{}, []error{}
}, },
ValidateFunc: validation.StringIsNotEmpty,
}, },
mkProviderVirtualEnvironmentUsername: { mkProviderUsername: {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Description: "The username for the Proxmox Virtual Environment API", Description: "The username for the Proxmox Virtual Environment API",
DefaultFunc: schema.MultiEnvDefaultFunc( DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"PROXMOX_VE_USERNAME", "PM_VE_USERNAME"}, []string{"PROXMOX_VE_USERNAME", "PM_VE_USERNAME"},
dvProviderVirtualEnvironmentUsername, nil,
), ),
ValidateFunc: func(v interface{}, k string) (warns []string, errs []error) { AtLeastOneOf: []string{
value := v.(string) mkProviderUsername,
fmt.Sprintf("%s.0.%s", mkProviderVirtualEnvironment, mkProviderUsername),
if value == "" {
return []string{}, []error{
errors.New(
"you must specify a username for the Proxmox Virtual Environment API (valid: username@realm)",
),
}
}
return []string{}, []error{}
}, },
}, ValidateFunc: validation.StringIsNotEmpty,
},
},
MaxItems: 1,
}, },
} }
} }