mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-07-01 19:12:59 +00:00
a first step to breakdown the vm.go - agent
Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
This commit is contained in:
parent
9e58793f27
commit
f04b729db2
@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource"
|
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource"
|
||||||
clusterfirewall "github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/cluster/firewall"
|
clusterfirewall "github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/cluster/firewall"
|
||||||
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/firewall"
|
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/firewall"
|
||||||
|
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/vm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createResourceMap() map[string]*schema.Resource {
|
func createResourceMap() map[string]*schema.Resource {
|
||||||
@ -33,6 +34,6 @@ func createResourceMap() map[string]*schema.Resource {
|
|||||||
"proxmox_virtual_environment_role": resource.Role(),
|
"proxmox_virtual_environment_role": resource.Role(),
|
||||||
"proxmox_virtual_environment_time": resource.Time(),
|
"proxmox_virtual_environment_time": resource.Time(),
|
||||||
"proxmox_virtual_environment_user": resource.User(),
|
"proxmox_virtual_environment_user": resource.User(),
|
||||||
"proxmox_virtual_environment_vm": resource.VM(),
|
"proxmox_virtual_environment_vm": vm.VM(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/bpg/terraform-provider-proxmox/proxmoxtf"
|
"github.com/bpg/terraform-provider-proxmox/proxmoxtf"
|
||||||
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/validator"
|
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/validator"
|
||||||
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/structure"
|
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/structure"
|
||||||
|
"github.com/bpg/terraform-provider-proxmox/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -63,6 +64,7 @@ const (
|
|||||||
dvResourceVirtualEnvironmentContainerNetworkInterfaceMACAddress = ""
|
dvResourceVirtualEnvironmentContainerNetworkInterfaceMACAddress = ""
|
||||||
dvResourceVirtualEnvironmentContainerNetworkInterfaceRateLimit = 0
|
dvResourceVirtualEnvironmentContainerNetworkInterfaceRateLimit = 0
|
||||||
dvResourceVirtualEnvironmentContainerNetworkInterfaceVLANID = 0
|
dvResourceVirtualEnvironmentContainerNetworkInterfaceVLANID = 0
|
||||||
|
dvResourceVirtualEnvironmentContainerNetworkInterfaceMTU = 0
|
||||||
dvResourceVirtualEnvironmentContainerOperatingSystemType = "unmanaged"
|
dvResourceVirtualEnvironmentContainerOperatingSystemType = "unmanaged"
|
||||||
dvResourceVirtualEnvironmentContainerPoolID = ""
|
dvResourceVirtualEnvironmentContainerPoolID = ""
|
||||||
dvResourceVirtualEnvironmentContainerStarted = true
|
dvResourceVirtualEnvironmentContainerStarted = true
|
||||||
@ -707,11 +709,11 @@ func Container() *schema.Resource {
|
|||||||
Optional: true,
|
Optional: true,
|
||||||
Default: dvResourceVirtualEnvironmentContainerNetworkInterfaceVLANID,
|
Default: dvResourceVirtualEnvironmentContainerNetworkInterfaceVLANID,
|
||||||
},
|
},
|
||||||
mkResourceVirtualEnvironmentVMNetworkDeviceMTU: {
|
mkResourceVirtualEnvironmentContainerNetworkInterfaceMTU: {
|
||||||
Type: schema.TypeInt,
|
Type: schema.TypeInt,
|
||||||
Description: "Maximum transmission unit (MTU)",
|
Description: "Maximum transmission unit (MTU)",
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Default: dvResourceVirtualEnvironmentVMNetworkDeviceMTU,
|
Default: dvResourceVirtualEnvironmentContainerNetworkInterfaceMTU,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -1007,7 +1009,7 @@ func containerCreateClone(ctx context.Context, d *schema.ResourceData, m interfa
|
|||||||
deprecatedServer := initializationDNSBlock[mkResourceVirtualEnvironmentContainerInitializationDNSServer].(string)
|
deprecatedServer := initializationDNSBlock[mkResourceVirtualEnvironmentContainerInitializationDNSServer].(string)
|
||||||
|
|
||||||
if len(servers) > 0 {
|
if len(servers) > 0 {
|
||||||
nameserver := strings.Join(ConvertToStringSlice(servers), " ")
|
nameserver := strings.Join(utils.ConvertToStringSlice(servers), " ")
|
||||||
|
|
||||||
updateBody.DNSServer = &nameserver
|
updateBody.DNSServer = &nameserver
|
||||||
} else {
|
} else {
|
||||||
@ -1134,7 +1136,7 @@ func containerCreateClone(ctx context.Context, d *schema.ResourceData, m interfa
|
|||||||
name := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceName].(string)
|
name := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceName].(string)
|
||||||
rateLimit := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceRateLimit].(float64)
|
rateLimit := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceRateLimit].(float64)
|
||||||
vlanID := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceVLANID].(int)
|
vlanID := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceVLANID].(int)
|
||||||
mtu, _ := networkInterfaceMap[mkResourceVirtualEnvironmentVMNetworkDeviceMTU].(int)
|
mtu, _ := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceMTU].(int)
|
||||||
|
|
||||||
if bridge != "" {
|
if bridge != "" {
|
||||||
networkInterfaceObject.Bridge = &bridge
|
networkInterfaceObject.Bridge = &bridge
|
||||||
@ -1329,7 +1331,7 @@ func containerCreateCustom(ctx context.Context, d *schema.ResourceData, m interf
|
|||||||
deprecatedServer := initializationDNSBlock[mkResourceVirtualEnvironmentContainerInitializationDNSServer].(string)
|
deprecatedServer := initializationDNSBlock[mkResourceVirtualEnvironmentContainerInitializationDNSServer].(string)
|
||||||
|
|
||||||
if len(servers) > 0 {
|
if len(servers) > 0 {
|
||||||
nameserver := strings.Join(ConvertToStringSlice(servers), " ")
|
nameserver := strings.Join(utils.ConvertToStringSlice(servers), " ")
|
||||||
|
|
||||||
initializationDNSServer = nameserver
|
initializationDNSServer = nameserver
|
||||||
} else {
|
} else {
|
||||||
@ -2542,7 +2544,7 @@ func containerUpdate(ctx context.Context, d *schema.ResourceData, m interface{})
|
|||||||
resource := Container()
|
resource := Container()
|
||||||
|
|
||||||
// Retrieve the clone argument as the update logic varies for clones.
|
// Retrieve the clone argument as the update logic varies for clones.
|
||||||
clone := d.Get(mkResourceVirtualEnvironmentVMClone).([]interface{})
|
clone := d.Get(mkResourceVirtualEnvironmentContainerClone).([]interface{})
|
||||||
|
|
||||||
// Prepare the new primitive values.
|
// Prepare the new primitive values.
|
||||||
description := d.Get(mkResourceVirtualEnvironmentContainerDescription).(string)
|
description := d.Get(mkResourceVirtualEnvironmentContainerDescription).(string)
|
||||||
@ -2635,7 +2637,7 @@ func containerUpdate(ctx context.Context, d *schema.ResourceData, m interface{})
|
|||||||
deprecatedServer := initializationDNSBlock[mkResourceVirtualEnvironmentContainerInitializationDNSServer].(string)
|
deprecatedServer := initializationDNSBlock[mkResourceVirtualEnvironmentContainerInitializationDNSServer].(string)
|
||||||
|
|
||||||
if len(servers) > 0 {
|
if len(servers) > 0 {
|
||||||
initializationDNSServer = strings.Join(ConvertToStringSlice(servers), " ")
|
initializationDNSServer = strings.Join(utils.ConvertToStringSlice(servers), " ")
|
||||||
} else {
|
} else {
|
||||||
initializationDNSServer = deprecatedServer
|
initializationDNSServer = deprecatedServer
|
||||||
}
|
}
|
||||||
@ -3012,3 +3014,13 @@ func containerDelete(ctx context.Context, d *schema.ResourceData, m interface{})
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseImportIDWithNodeName(id string) (string, string, error) {
|
||||||
|
nodeName, id, found := strings.Cut(id, "/")
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
return "", "", fmt.Errorf("unexpected format of ID (%s), expected node/id", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodeName, id, nil
|
||||||
|
}
|
||||||
|
116
proxmoxtf/resource/vm/agent.go
Normal file
116
proxmoxtf/resource/vm/agent.go
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
package vm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bpg/terraform-provider-proxmox/proxmox/nodes/vms"
|
||||||
|
"github.com/bpg/terraform-provider-proxmox/proxmox/types"
|
||||||
|
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/structure"
|
||||||
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
func createAgent(d *schema.ResourceData, updateBody *vms.UpdateRequestBody) {
|
||||||
|
agent := d.Get(mkResourceVirtualEnvironmentVMAgent).([]interface{})
|
||||||
|
if len(agent) > 0 {
|
||||||
|
agentBlock := agent[0].(map[string]interface{})
|
||||||
|
|
||||||
|
agentEnabled := types.CustomBool(
|
||||||
|
agentBlock[mkResourceVirtualEnvironmentVMAgentEnabled].(bool),
|
||||||
|
)
|
||||||
|
agentTrim := types.CustomBool(agentBlock[mkResourceVirtualEnvironmentVMAgentTrim].(bool))
|
||||||
|
agentType := agentBlock[mkResourceVirtualEnvironmentVMAgentType].(string)
|
||||||
|
|
||||||
|
updateBody.Agent = &vms.CustomAgent{
|
||||||
|
Enabled: &agentEnabled,
|
||||||
|
TrimClonedDisks: &agentTrim,
|
||||||
|
Type: &agentType,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func customAgent(d *schema.ResourceData, resource *schema.Resource) (*vms.CustomAgent, error) {
|
||||||
|
agentBlock, err := structure.GetSchemaBlock(
|
||||||
|
resource,
|
||||||
|
d,
|
||||||
|
[]string{mkResourceVirtualEnvironmentVMAgent},
|
||||||
|
0,
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
agentEnabled := types.CustomBool(
|
||||||
|
agentBlock[mkResourceVirtualEnvironmentVMAgentEnabled].(bool),
|
||||||
|
)
|
||||||
|
agentTrim := types.CustomBool(agentBlock[mkResourceVirtualEnvironmentVMAgentTrim].(bool))
|
||||||
|
agentType := agentBlock[mkResourceVirtualEnvironmentVMAgentType].(string)
|
||||||
|
|
||||||
|
return &vms.CustomAgent{
|
||||||
|
Enabled: &agentEnabled,
|
||||||
|
TrimClonedDisks: &agentTrim,
|
||||||
|
Type: &agentType,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setAgent(d *schema.ResourceData, clone bool, vmConfig *vms.GetResponseData) error {
|
||||||
|
// Compare the agent configuration to the one stored in the state.
|
||||||
|
currentAgent := d.Get(mkResourceVirtualEnvironmentVMAgent).([]interface{})
|
||||||
|
|
||||||
|
if !clone || len(currentAgent) > 0 {
|
||||||
|
if vmConfig.Agent != nil {
|
||||||
|
agent := map[string]interface{}{}
|
||||||
|
|
||||||
|
if vmConfig.Agent.Enabled != nil {
|
||||||
|
agent[mkResourceVirtualEnvironmentVMAgentEnabled] = bool(*vmConfig.Agent.Enabled)
|
||||||
|
} else {
|
||||||
|
agent[mkResourceVirtualEnvironmentVMAgentEnabled] = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if vmConfig.Agent.TrimClonedDisks != nil {
|
||||||
|
agent[mkResourceVirtualEnvironmentVMAgentTrim] = bool(
|
||||||
|
*vmConfig.Agent.TrimClonedDisks,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
agent[mkResourceVirtualEnvironmentVMAgentTrim] = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(currentAgent) > 0 {
|
||||||
|
currentAgentBlock := currentAgent[0].(map[string]interface{})
|
||||||
|
currentAgentTimeout := currentAgentBlock[mkResourceVirtualEnvironmentVMAgentTimeout].(string)
|
||||||
|
|
||||||
|
if currentAgentTimeout != "" {
|
||||||
|
agent[mkResourceVirtualEnvironmentVMAgentTimeout] = currentAgentTimeout
|
||||||
|
} else {
|
||||||
|
agent[mkResourceVirtualEnvironmentVMAgentTimeout] = dvResourceVirtualEnvironmentVMAgentTimeout
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
agent[mkResourceVirtualEnvironmentVMAgentTimeout] = dvResourceVirtualEnvironmentVMAgentTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
if vmConfig.Agent.Type != nil {
|
||||||
|
agent[mkResourceVirtualEnvironmentVMAgentType] = *vmConfig.Agent.Type
|
||||||
|
} else {
|
||||||
|
agent[mkResourceVirtualEnvironmentVMAgentType] = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if clone {
|
||||||
|
if len(currentAgent) > 0 {
|
||||||
|
return d.Set(mkResourceVirtualEnvironmentVMAgent, []interface{}{agent})
|
||||||
|
}
|
||||||
|
} else if len(currentAgent) > 0 ||
|
||||||
|
agent[mkResourceVirtualEnvironmentVMAgentEnabled] != dvResourceVirtualEnvironmentVMAgentEnabled ||
|
||||||
|
agent[mkResourceVirtualEnvironmentVMAgentTimeout] != dvResourceVirtualEnvironmentVMAgentTimeout ||
|
||||||
|
agent[mkResourceVirtualEnvironmentVMAgentTrim] != dvResourceVirtualEnvironmentVMAgentTrim ||
|
||||||
|
agent[mkResourceVirtualEnvironmentVMAgentType] != dvResourceVirtualEnvironmentVMAgentType {
|
||||||
|
return d.Set(mkResourceVirtualEnvironmentVMAgent, []interface{}{agent})
|
||||||
|
|
||||||
|
}
|
||||||
|
} else if clone {
|
||||||
|
if len(currentAgent) > 0 {
|
||||||
|
return d.Set(mkResourceVirtualEnvironmentVMAgent, []interface{}{})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return d.Set(mkResourceVirtualEnvironmentVMAgent, []interface{}{})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
1648
proxmoxtf/resource/vm/schema.go
Normal file
1648
proxmoxtf/resource/vm/schema.go
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
11
utils/strings.go
Normal file
11
utils/strings.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
// ConvertToStringSlice helps convert interface slice to string slice.
|
||||||
|
func ConvertToStringSlice(interfaceSlice []interface{}) []string {
|
||||||
|
resultSlice := []string{}
|
||||||
|
for _, val := range interfaceSlice {
|
||||||
|
resultSlice = append(resultSlice, val.(string))
|
||||||
|
}
|
||||||
|
|
||||||
|
return resultSlice
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user