0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-01 11:02:59 +00:00
terraform-provider-proxmox/proxmoxtf/provider/schema.go
zoop 9fa92423b5
feat: SSH-Agent Support (#306)
* chore: add agent configuration bool

* feat: add ssh-agent authentication mechanism for linux

* chore: make sure ssh-agent auth is only executed on linux

* chore: add ssh user override

* chore: add ssh configuration block, check ssh config during VirtualEnvironmentClient creation

* fix: handle case of empty ssh config block

* chore: add ssh password auth fallback logic

* fix: remove not needed runtime

* fix linter errors & re-format

* allow ssh agent on all POSIX systems

* add `agent_socket` parameter

* update docs and examples

---------

Co-authored-by: zoop <zoop@zoop.li>
Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
2023-05-22 13:34:24 -04:00

162 lines
4.8 KiB
Go

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package provider
import (
"fmt"
"os"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
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{
mkProviderEndpoint: {
Type: schema.TypeString,
Optional: true,
Description: "The endpoint for the Proxmox Virtual Environment API",
DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"PROXMOX_VE_ENDPOINT", "PM_VE_ENDPOINT"},
nil,
),
AtLeastOneOf: []string{
mkProviderEndpoint,
fmt.Sprintf("%s.0.%s", mkProviderVirtualEnvironment, mkProviderEndpoint),
},
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,
},
mkProviderSSH: {
Type: schema.TypeSet,
Optional: true,
MaxItems: 1,
Description: "The SSH connection configuration to a Proxmox node",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
mkProviderSSHUsername: {
Type: schema.TypeString,
Optional: true,
Description: fmt.Sprintf("The username used for the SSH connection, "+
"defaults to the user specified in '%s'", mkProviderUsername),
DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"PROXMOX_VE_SSH_USERNAME", "PM_VE_SSH_USERNAME"},
nil,
),
ValidateFunc: validation.StringIsNotEmpty,
},
mkProviderSSHPassword: {
Type: schema.TypeString,
Optional: true,
Description: fmt.Sprintf("The password used for the SSH connection, "+
"defaults to the password specified in '%s'", mkProviderPassword),
DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"PROXMOX_VE_SSH_PASSWORD", "PM_VE_SSH_PASSWORD"},
nil,
),
ValidateFunc: validation.StringIsNotEmpty,
},
mkProviderSSHAgent: {
Type: schema.TypeBool,
Optional: true,
Description: "Whether to use the SSH agent for the SSH authentication. Defaults to false",
DefaultFunc: func() (interface{}, error) {
for _, k := range []string{"PROXMOX_VE_SSH_AGENT", "PM_VE_SSH_AGENT"} {
v := os.Getenv(k)
if v == "true" || v == "1" {
return true, nil
}
}
return false, nil
},
},
mkProviderSSHAgentSocket: {
Type: schema.TypeString,
Optional: true,
Description: "The path to the SSH agent socket. Defaults to the value of the `SSH_AUTH_SOCK` " +
"environment variable",
DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"SSH_AUTH_SOCK", "PROXMOX_VE_SSH_AUTH_SOCK", "PM_VE_SSH_AUTH_SOCK"},
nil,
),
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},
}
}