Skip to content

Commit

Permalink
feat(vm): Add cloud-init network-config support (#197)
Browse files Browse the repository at this point in the history
* feat: Add cloud-init network-config support

* add documentation entry

* fix re-apply use case

Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
  • Loading branch information
groggemans and bpg authored Jan 1, 2023
1 parent 60a6818 commit 79a2101
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/resources/virtual_environment_vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ output "ubuntu_vm_public_key" {
* `keys` - (Optional) The SSH keys.
* `password` - (Optional) The SSH password.
* `username` - (Optional) The SSH username.
* `network_data_file_id` - (Optional) The identifier for a file containing network configuration data passed to the
VM via cloud-init (conflicts with `ip_config`).
* `user_data_file_id` - (Optional) The identifier for a file containing custom user data (conflicts
with `user_account`).
* `vendor_data_file_id` - (Optional) The identifier for a file containing all vendor data passed to the VM via cloud-init.
Expand Down
2 changes: 1 addition & 1 deletion proxmox/virtual_environment_vm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ func (r *CustomCloudInitFiles) UnmarshalJSON(b []byte) error {
case "meta":
r.MetaVolume = &v[1]
case "network":
r.MetaVolume = &v[1]
r.NetworkVolume = &v[1]
case "user":
r.UserVolume = &v[1]
case "vendor":
Expand Down
25 changes: 25 additions & 0 deletions proxmoxtf/resource_virtual_environment_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const (
dvResourceVirtualEnvironmentVMInitializationUserAccountPassword = ""
dvResourceVirtualEnvironmentVMInitializationUserDataFileID = ""
dvResourceVirtualEnvironmentVMInitializationVendorDataFileID = ""
dvResourceVirtualEnvironmentVMInitializationNetworkDataFileID = ""
dvResourceVirtualEnvironmentVMInitializationType = ""
dvResourceVirtualEnvironmentVMKeyboardLayout = "en-us"
dvResourceVirtualEnvironmentVMMachineType = ""
Expand Down Expand Up @@ -183,6 +184,7 @@ const (
mkResourceVirtualEnvironmentVMInitializationUserAccountUsername = "username"
mkResourceVirtualEnvironmentVMInitializationUserDataFileID = "user_data_file_id"
mkResourceVirtualEnvironmentVMInitializationVendorDataFileID = "vendor_data_file_id"
mkResourceVirtualEnvironmentVMInitializationNetworkDataFileID = "network_data_file_id"
mkResourceVirtualEnvironmentVMIPv4Addresses = "ipv4_addresses"
mkResourceVirtualEnvironmentVMIPv6Addresses = "ipv6_addresses"
mkResourceVirtualEnvironmentVMKeyboardLayout = "keyboard_layout"
Expand Down Expand Up @@ -782,6 +784,14 @@ func resourceVirtualEnvironmentVM() *schema.Resource {
Default: dvResourceVirtualEnvironmentVMInitializationVendorDataFileID,
ValidateDiagFunc: getFileIDValidator(),
},
mkResourceVirtualEnvironmentVMInitializationNetworkDataFileID: {
Type: schema.TypeString,
Description: "The ID of a file containing network config",
Optional: true,
ForceNew: true,
Default: dvResourceVirtualEnvironmentVMInitializationNetworkDataFileID,
ValidateDiagFunc: getFileIDValidator(),
},
mkResourceVirtualEnvironmentVMInitializationType: {
Type: schema.TypeString,
Description: "The cloud-init configuration format",
Expand Down Expand Up @@ -2315,6 +2325,15 @@ func resourceVirtualEnvironmentVMGetCloudInitConfig(d *schema.ResourceData) (*pr
initializationConfig.Files.VendorVolume = &initializationVendorDataFileID
}

initializationNetworkDataFileID := initializationBlock[mkResourceVirtualEnvironmentVMInitializationNetworkDataFileID].(string)

if initializationNetworkDataFileID != "" {
if initializationConfig.Files == nil {
initializationConfig.Files = &proxmox.CustomCloudInitFiles{}
}
initializationConfig.Files.NetworkVolume = &initializationNetworkDataFileID
}

initializationType := initializationBlock[mkResourceVirtualEnvironmentVMInitializationType].(string)

if initializationType != "" {
Expand Down Expand Up @@ -3183,9 +3202,15 @@ func resourceVirtualEnvironmentVMReadCustom(ctx context.Context, d *schema.Resou
} else {
initialization[mkResourceVirtualEnvironmentVMInitializationVendorDataFileID] = ""
}
if vmConfig.CloudInitFiles.NetworkVolume != nil {
initialization[mkResourceVirtualEnvironmentVMInitializationNetworkDataFileID] = *vmConfig.CloudInitFiles.NetworkVolume
} else {
initialization[mkResourceVirtualEnvironmentVMInitializationNetworkDataFileID] = ""
}
} else if len(initialization) > 0 {
initialization[mkResourceVirtualEnvironmentVMInitializationUserDataFileID] = ""
initialization[mkResourceVirtualEnvironmentVMInitializationVendorDataFileID] = ""
initialization[mkResourceVirtualEnvironmentVMInitializationNetworkDataFileID] = ""
}

if vmConfig.CloudInitType != nil {
Expand Down

0 comments on commit 79a2101

Please sign in to comment.