0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-09 23:35:00 +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,12 +17,10 @@ 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
}
} }
``` ```
@ -46,10 +44,8 @@ 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"
}
} }
``` ```
@ -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,18 +74,16 @@ 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 - `endpoint` - (Required) The endpoint for the Proxmox Virtual Environment
configuration. API (can also be sourced from `PROXMOX_VE_ENDPOINT`). Usually this is
- `endpoint` - (Required) The endpoint for the Proxmox Virtual Environment `https://<your-cluster-endpoint>:8006/`.
API (can also be sourced from `PROXMOX_VE_ENDPOINT`). Usually this is - `insecure` - (Optional) Whether to skip the TLS verification step (can
`https://<your-cluster-endpoint>:8006/`. also be sourced from `PROXMOX_VE_INSECURE`). If omitted, defaults
- `insecure` - (Optional) Whether to skip the TLS verification step (can to `false`.
also be sourced from `PROXMOX_VE_INSECURE`). If omitted, defaults - `otp` - (Optional) The one-time password for the Proxmox Virtual
to `false`. Environment API (can also be sourced from `PROXMOX_VE_OTP`).
- `otp` - (Optional) The one-time password for the Proxmox Virtual - `password` - (Required) The password for the Proxmox Virtual Environment
Environment API (can also be sourced from `PROXMOX_VE_OTP`). API (can also be sourced from `PROXMOX_VE_PASSWORD`).
- `password` - (Required) The password for the Proxmox Virtual Environment - `username` - (Required) The username and realm for the Proxmox Virtual
API (can also be sourced from `PROXMOX_VE_PASSWORD`). Environment API (can also be sourced from `PROXMOX_VE_USERNAME`). For
- `username` - (Required) The username and realm for the Proxmox Virtual example, `root@pam`.
Environment API (can also be sourced from `PROXMOX_VE_USERNAME`). For
example, `root@pam`.

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,15 +48,24 @@ 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),
) )
if err != nil { } else {
return nil, diag.FromErr(err) 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 {
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,123 +7,96 @@
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 {
providerSchema := nestedProviderSchema()
providerSchema[mkProviderVirtualEnvironment] = &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: nestedProviderSchema(),
},
MaxItems: 1,
Deprecated: "Move attributes out of virtual_environment block",
}
return providerSchema
}
func nestedProviderSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{ return map[string]*schema.Schema{
mkProviderVirtualEnvironment: { mkProviderEndpoint: {
Type: schema.TypeList, Type: schema.TypeString,
Optional: true, Optional: true,
Elem: &schema.Resource{ Description: "The endpoint for the Proxmox Virtual Environment API",
Schema: map[string]*schema.Schema{ DefaultFunc: schema.MultiEnvDefaultFunc(
mkProviderVirtualEnvironmentEndpoint: { []string{"PROXMOX_VE_ENDPOINT", "PM_VE_ENDPOINT"},
Type: schema.TypeString, nil,
Optional: true, ),
Description: "The endpoint for the Proxmox Virtual Environment API", AtLeastOneOf: []string{
DefaultFunc: schema.MultiEnvDefaultFunc( mkProviderEndpoint,
[]string{"PROXMOX_VE_ENDPOINT", "PM_VE_ENDPOINT"}, fmt.Sprintf("%s.0.%s", mkProviderVirtualEnvironment, mkProviderEndpoint),
dvProviderVirtualEnvironmentEndpoint,
),
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",
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
},
},
mkProviderVirtualEnvironmentOTP: {
Type: schema.TypeString,
Optional: true,
Description: "The one-time password for the Proxmox Virtual Environment API",
DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"PROXMOX_VE_OTP", "PM_VE_OTP"},
dvProviderVirtualEnvironmentOTP,
),
},
mkProviderVirtualEnvironmentPassword: {
Type: schema.TypeString,
Optional: true,
Description: "The password for the Proxmox Virtual Environment API",
DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"PROXMOX_VE_PASSWORD", "PM_VE_PASSWORD"},
dvProviderVirtualEnvironmentPassword,
),
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,
Optional: true,
Description: "The username for the Proxmox Virtual Environment API",
DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"PROXMOX_VE_USERNAME", "PM_VE_USERNAME"},
dvProviderVirtualEnvironmentUsername,
),
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{}
},
},
},
}, },
MaxItems: 1, ValidateFunc: validation.IsURLWithHTTPorHTTPS,
},
mkProviderInsecure: {
Type: schema.TypeBool,
Optional: true,
Description: "Whether to skip the TLS verification step",
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
},
},
mkProviderOTP: {
Type: schema.TypeString,
Optional: true,
Description: "The one-time password for the Proxmox Virtual Environment API",
DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"PROXMOX_VE_OTP", "PM_VE_OTP"},
dvProviderOTP,
),
},
mkProviderPassword: {
Type: schema.TypeString,
Optional: true,
Description: "The password for the Proxmox Virtual Environment API",
DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"PROXMOX_VE_PASSWORD", "PM_VE_PASSWORD"},
nil,
),
AtLeastOneOf: []string{
mkProviderPassword,
fmt.Sprintf("%s.0.%s", mkProviderVirtualEnvironment, mkProviderPassword),
},
ValidateFunc: validation.StringIsNotEmpty,
},
mkProviderUsername: {
Type: schema.TypeString,
Optional: true,
Description: "The username for the Proxmox Virtual Environment API",
DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"PROXMOX_VE_USERNAME", "PM_VE_USERNAME"},
nil,
),
AtLeastOneOf: []string{
mkProviderUsername,
fmt.Sprintf("%s.0.%s", mkProviderVirtualEnvironment, mkProviderUsername),
},
ValidateFunc: validation.StringIsNotEmpty,
}, },
} }
} }