Skip to content

Commit

Permalink
Continue work on container resource
Browse files Browse the repository at this point in the history
  • Loading branch information
danitso-dp committed Jan 2, 2020
1 parent f6c4ad2 commit 6bb5e9f
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 1 deletion.
1 change: 1 addition & 0 deletions proxmox/virtual_environment_container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type VirtualEnvironmentContainerCreateRequestBody struct {
CPUUnits *int `json:"cpuunits,omitempty" url:"cpuunits,omitempty"`
DatastoreID *string `json:"storage,omitempty" url:"storage,omitempty"`
DedicatedMemory *int `json:"memory,omitempty" url:"memory,omitempty"`
Delete []string `json:"delete,omitempty" url:"delete,omitempty"`
Description *string `json:"description,omitempty" url:"description,omitempty"`
DNSDomain *string `json:"searchdomain,omitempty" url:"searchdomain,omitempty"`
DNSServer *string `json:"nameserver,omitempty" url:"nameserver,omitempty"`
Expand Down
147 changes: 146 additions & 1 deletion proxmoxtf/resource_virtual_environment_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,9 @@ func resourceVirtualEnvironmentContainerUpdate(d *schema.ResourceData, m interfa
}

// Prepare the new request object.
body := proxmox.VirtualEnvironmentContainerUpdateRequestBody{}
body := proxmox.VirtualEnvironmentContainerUpdateRequestBody{
Delete: []string{},
}
rebootRequired := false
resource := resourceVirtualEnvironmentContainer()

Expand Down Expand Up @@ -1175,6 +1177,65 @@ func resourceVirtualEnvironmentContainerUpdate(d *schema.ResourceData, m interfa
rebootRequired = true
}

// Prepare the new initialization configuration.
initialization := d.Get(mkResourceVirtualEnvironmentContainerInitialization).([]interface{})
initializationDNSDomain := dvResourceVirtualEnvironmentContainerInitializationDNSDomain
initializationDNSServer := dvResourceVirtualEnvironmentContainerInitializationDNSServer
initializationHostname := dvResourceVirtualEnvironmentContainerInitializationHostname
initializationIPConfigIPv4Address := []string{}
initializationIPConfigIPv4Gateway := []string{}
initializationIPConfigIPv6Address := []string{}
initializationIPConfigIPv6Gateway := []string{}

if len(initialization) > 0 {
initializationBlock := initialization[0].(map[string]interface{})
initializationDNS := initializationBlock[mkResourceVirtualEnvironmentContainerInitializationDNS].([]interface{})

if len(initializationDNS) > 0 {
initializationDNSBlock := initializationDNS[0].(map[string]interface{})
initializationDNSDomain = initializationDNSBlock[mkResourceVirtualEnvironmentContainerInitializationDNSDomain].(string)
initializationDNSServer = initializationDNSBlock[mkResourceVirtualEnvironmentContainerInitializationDNSServer].(string)
}

initializationHostname = initializationBlock[mkResourceVirtualEnvironmentContainerInitializationHostname].(string)
initializationIPConfig := initializationBlock[mkResourceVirtualEnvironmentContainerInitializationIPConfig].([]interface{})

for _, c := range initializationIPConfig {
configBlock := c.(map[string]interface{})
ipv4 := configBlock[mkResourceVirtualEnvironmentContainerInitializationIPConfigIPv4].([]interface{})

if len(ipv4) > 0 {
ipv4Block := ipv4[0].(map[string]interface{})

initializationIPConfigIPv4Address = append(initializationIPConfigIPv4Address, ipv4Block[mkResourceVirtualEnvironmentContainerInitializationIPConfigIPv4Address].(string))
initializationIPConfigIPv4Gateway = append(initializationIPConfigIPv4Gateway, ipv4Block[mkResourceVirtualEnvironmentContainerInitializationIPConfigIPv4Gateway].(string))
} else {
initializationIPConfigIPv4Address = append(initializationIPConfigIPv4Address, "")
initializationIPConfigIPv4Gateway = append(initializationIPConfigIPv4Gateway, "")
}

ipv6 := configBlock[mkResourceVirtualEnvironmentContainerInitializationIPConfigIPv6].([]interface{})

if len(ipv6) > 0 {
ipv6Block := ipv6[0].(map[string]interface{})

initializationIPConfigIPv6Address = append(initializationIPConfigIPv6Address, ipv6Block[mkResourceVirtualEnvironmentContainerInitializationIPConfigIPv6Address].(string))
initializationIPConfigIPv6Gateway = append(initializationIPConfigIPv6Gateway, ipv6Block[mkResourceVirtualEnvironmentContainerInitializationIPConfigIPv6Gateway].(string))
} else {
initializationIPConfigIPv6Address = append(initializationIPConfigIPv6Address, "")
initializationIPConfigIPv6Gateway = append(initializationIPConfigIPv6Gateway, "")
}
}
}

if d.HasChange(mkResourceVirtualEnvironmentContainerInitialization) {
body.DNSDomain = &initializationDNSDomain
body.DNSServer = &initializationDNSServer
body.Hostname = &initializationHostname

rebootRequired = true
}

// Prepare the new memory configuration.
if d.HasChange(mkResourceVirtualEnvironmentContainerMemory) {
memoryBlock, err := getSchemaBlock(resource, d, m, []string{mkResourceVirtualEnvironmentContainerMemory}, 0, true)
Expand All @@ -1192,6 +1253,90 @@ func resourceVirtualEnvironmentContainerUpdate(d *schema.ResourceData, m interfa
rebootRequired = true
}

// Prepare the new network interface configuration.
if d.HasChange(mkResourceVirtualEnvironmentContainerNetworkInterface) {
networkInterface := d.Get(mkResourceVirtualEnvironmentContainerNetworkInterface).([]interface{})
networkInterfaceArray := make(proxmox.VirtualEnvironmentContainerCustomNetworkInterfaceArray, len(networkInterface))

for ni, nv := range networkInterface {
networkInterfaceMap := nv.(map[string]interface{})
networkInterfaceObject := proxmox.VirtualEnvironmentContainerCustomNetworkInterface{}

bridge := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceBridge].(string)
enabled := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceEnabled].(bool)
macAddress := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceMACAddress].(string)
name := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceName].(string)
rateLimit := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceRateLimit].(float64)
vlanID := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceVLANID].(int)

if bridge != "" {
networkInterfaceObject.Bridge = &bridge
}

networkInterfaceObject.Enabled = enabled

if len(initializationIPConfigIPv4Address) > ni {
if initializationIPConfigIPv4Address[ni] != "" {
networkInterfaceObject.IPv4Address = &initializationIPConfigIPv4Address[ni]
}

if initializationIPConfigIPv4Gateway[ni] != "" {
networkInterfaceObject.IPv4Gateway = &initializationIPConfigIPv4Gateway[ni]
}

if initializationIPConfigIPv6Address[ni] != "" {
networkInterfaceObject.IPv6Address = &initializationIPConfigIPv6Address[ni]
}

if initializationIPConfigIPv6Gateway[ni] != "" {
networkInterfaceObject.IPv6Gateway = &initializationIPConfigIPv6Gateway[ni]
}
}

if macAddress != "" {
networkInterfaceObject.MACAddress = &macAddress
}

networkInterfaceObject.Name = name

if rateLimit != 0 {
networkInterfaceObject.RateLimit = &rateLimit
}

if vlanID != 0 {
networkInterfaceObject.Tag = &vlanID
}

networkInterfaceArray[ni] = networkInterfaceObject
}

body.NetworkInterfaces = networkInterfaceArray

index := len(networkInterface)

for index < 8 {
body.Delete = append(body.Delete, fmt.Sprintf("net%d", index))
index++
}

rebootRequired = true
}

// Prepare the new operating system configuration.
if d.HasChange(mkResourceVirtualEnvironmentContainerOperatingSystem) {
operatingSystem, err := getSchemaBlock(resource, d, m, []string{mkResourceVirtualEnvironmentContainerOperatingSystem}, 0, true)

if err != nil {
return err
}

operatingSystemType := operatingSystem[mkResourceVirtualEnvironmentContainerOperatingSystemType].(string)

body.OSType = &operatingSystemType

rebootRequired = true
}

// Update the configuration now that everything has been prepared.
err = veClient.UpdateContainer(nodeName, vmID, &body)

Expand Down

0 comments on commit 6bb5e9f

Please sign in to comment.