mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-06-30 02:31:10 +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"
|
||||
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/vm"
|
||||
)
|
||||
|
||||
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_time": resource.Time(),
|
||||
"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/resource/validator"
|
||||
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/structure"
|
||||
"github.com/bpg/terraform-provider-proxmox/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -63,6 +64,7 @@ const (
|
||||
dvResourceVirtualEnvironmentContainerNetworkInterfaceMACAddress = ""
|
||||
dvResourceVirtualEnvironmentContainerNetworkInterfaceRateLimit = 0
|
||||
dvResourceVirtualEnvironmentContainerNetworkInterfaceVLANID = 0
|
||||
dvResourceVirtualEnvironmentContainerNetworkInterfaceMTU = 0
|
||||
dvResourceVirtualEnvironmentContainerOperatingSystemType = "unmanaged"
|
||||
dvResourceVirtualEnvironmentContainerPoolID = ""
|
||||
dvResourceVirtualEnvironmentContainerStarted = true
|
||||
@ -707,11 +709,11 @@ func Container() *schema.Resource {
|
||||
Optional: true,
|
||||
Default: dvResourceVirtualEnvironmentContainerNetworkInterfaceVLANID,
|
||||
},
|
||||
mkResourceVirtualEnvironmentVMNetworkDeviceMTU: {
|
||||
mkResourceVirtualEnvironmentContainerNetworkInterfaceMTU: {
|
||||
Type: schema.TypeInt,
|
||||
Description: "Maximum transmission unit (MTU)",
|
||||
Optional: true,
|
||||
Default: dvResourceVirtualEnvironmentVMNetworkDeviceMTU,
|
||||
Default: dvResourceVirtualEnvironmentContainerNetworkInterfaceMTU,
|
||||
},
|
||||
},
|
||||
},
|
||||
@ -1007,7 +1009,7 @@ func containerCreateClone(ctx context.Context, d *schema.ResourceData, m interfa
|
||||
deprecatedServer := initializationDNSBlock[mkResourceVirtualEnvironmentContainerInitializationDNSServer].(string)
|
||||
|
||||
if len(servers) > 0 {
|
||||
nameserver := strings.Join(ConvertToStringSlice(servers), " ")
|
||||
nameserver := strings.Join(utils.ConvertToStringSlice(servers), " ")
|
||||
|
||||
updateBody.DNSServer = &nameserver
|
||||
} else {
|
||||
@ -1134,7 +1136,7 @@ func containerCreateClone(ctx context.Context, d *schema.ResourceData, m interfa
|
||||
name := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceName].(string)
|
||||
rateLimit := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceRateLimit].(float64)
|
||||
vlanID := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceVLANID].(int)
|
||||
mtu, _ := networkInterfaceMap[mkResourceVirtualEnvironmentVMNetworkDeviceMTU].(int)
|
||||
mtu, _ := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceMTU].(int)
|
||||
|
||||
if bridge != "" {
|
||||
networkInterfaceObject.Bridge = &bridge
|
||||
@ -1329,7 +1331,7 @@ func containerCreateCustom(ctx context.Context, d *schema.ResourceData, m interf
|
||||
deprecatedServer := initializationDNSBlock[mkResourceVirtualEnvironmentContainerInitializationDNSServer].(string)
|
||||
|
||||
if len(servers) > 0 {
|
||||
nameserver := strings.Join(ConvertToStringSlice(servers), " ")
|
||||
nameserver := strings.Join(utils.ConvertToStringSlice(servers), " ")
|
||||
|
||||
initializationDNSServer = nameserver
|
||||
} else {
|
||||
@ -2542,7 +2544,7 @@ func containerUpdate(ctx context.Context, d *schema.ResourceData, m interface{})
|
||||
resource := Container()
|
||||
|
||||
// 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.
|
||||
description := d.Get(mkResourceVirtualEnvironmentContainerDescription).(string)
|
||||
@ -2635,7 +2637,7 @@ func containerUpdate(ctx context.Context, d *schema.ResourceData, m interface{})
|
||||
deprecatedServer := initializationDNSBlock[mkResourceVirtualEnvironmentContainerInitializationDNSServer].(string)
|
||||
|
||||
if len(servers) > 0 {
|
||||
initializationDNSServer = strings.Join(ConvertToStringSlice(servers), " ")
|
||||
initializationDNSServer = strings.Join(utils.ConvertToStringSlice(servers), " ")
|
||||
} else {
|
||||
initializationDNSServer = deprecatedServer
|
||||
}
|
||||
@ -3012,3 +3014,13 @@ func containerDelete(ctx context.Context, d *schema.ResourceData, m interface{})
|
||||
|
||||
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