Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for custom cloud-init vendor data file #162

Merged
merged 6 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/auto-label.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ staleness:
path:
pullrequest: true
paths:
.: 'docs'
docs: 'documentation'
1 change: 1 addition & 0 deletions docs/resources/virtual_environment_vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ output "ubuntu_vm_public_key" {
* `username` - (Optional) The SSH username.
* `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.
* `keyboard_layout` - (Optional) The keyboard layout (defaults to `en-us`).
* `da` - Danish.
* `de` - German.
Expand Down
9 changes: 7 additions & 2 deletions proxmox/virtual_environment_vm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type CustomCloudInitFiles struct {
MetaVolume *string `json:"meta,omitempty" url:"meta,omitempty"`
NetworkVolume *string `json:"network,omitempty" url:"network,omitempty"`
UserVolume *string `json:"user,omitempty" url:"user,omitempty"`
VendorVolume *string `json:"vendor,omitempty" url:"vendor,omitempty"`
}

// CustomCloudInitIPConfig handles QEMU cloud-init IP configuration parameters.
Expand Down Expand Up @@ -221,7 +222,7 @@ type VirtualEnvironmentVMCloneRequestBody struct {
VMIDNew int `json:"newid" url:"newid"`
}

// VirtualEnvironmentVMCreateRequestBody contains the data for an virtual machine create request.
// VirtualEnvironmentVMCreateRequestBody contains the data for a virtual machine create request.
type VirtualEnvironmentVMCreateRequestBody struct {
ACPI *CustomBool `json:"acpi,omitempty" url:"acpi,omitempty,int"`
Agent *CustomAgent `json:"agent,omitempty" url:"agent,omitempty"`
Expand Down Expand Up @@ -623,7 +624,7 @@ func (r CustomAudioDevices) EncodeValues(key string, v *url.Values) error {
return nil
}

// EncodeValues converts a CustomCloudInitConfig struct to multiple URL vlaues.
// EncodeValues converts a CustomCloudInitConfig struct to multiple URL values.
func (r CustomCloudInitConfig) EncodeValues(_ string, v *url.Values) error {
if r.Files != nil {
var volumes []string
Expand All @@ -640,6 +641,10 @@ func (r CustomCloudInitConfig) EncodeValues(_ string, v *url.Values) error {
volumes = append(volumes, fmt.Sprintf("user=%s", *r.Files.UserVolume))
}

if r.Files.VendorVolume != nil {
volumes = append(volumes, fmt.Sprintf("vendor=%s", *r.Files.VendorVolume))
}

if len(volumes) > 0 {
v.Add("cicustom", strings.Join(volumes, ","))
}
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 @@ -65,6 +65,7 @@ const (
dvResourceVirtualEnvironmentVMInitializationIPConfigIPv6Gateway = ""
dvResourceVirtualEnvironmentVMInitializationUserAccountPassword = ""
dvResourceVirtualEnvironmentVMInitializationUserDataFileID = ""
dvResourceVirtualEnvironmentVMInitializationVendorDataFileID = ""
dvResourceVirtualEnvironmentVMInitializationType = ""
dvResourceVirtualEnvironmentVMKeyboardLayout = "en-us"
dvResourceVirtualEnvironmentVMMemoryDedicated = 512
Expand Down Expand Up @@ -160,6 +161,7 @@ const (
mkResourceVirtualEnvironmentVMInitializationUserAccountPassword = "password"
mkResourceVirtualEnvironmentVMInitializationUserAccountUsername = "username"
mkResourceVirtualEnvironmentVMInitializationUserDataFileID = "user_data_file_id"
mkResourceVirtualEnvironmentVMInitializationVendorDataFileID = "vendor_data_file_id"
mkResourceVirtualEnvironmentVMIPv4Addresses = "ipv4_addresses"
mkResourceVirtualEnvironmentVMIPv6Addresses = "ipv6_addresses"
mkResourceVirtualEnvironmentVMKeyboardLayout = "keyboard_layout"
Expand Down Expand Up @@ -741,6 +743,14 @@ func resourceVirtualEnvironmentVM() *schema.Resource {
Default: dvResourceVirtualEnvironmentVMInitializationUserDataFileID,
ValidateDiagFunc: getFileIDValidator(),
},
mkResourceVirtualEnvironmentVMInitializationVendorDataFileID: {
Type: schema.TypeString,
Description: "The ID of a file containing vendor data",
Optional: true,
ForceNew: true,
Default: dvResourceVirtualEnvironmentVMInitializationVendorDataFileID,
ValidateDiagFunc: getFileIDValidator(),
},
mkResourceVirtualEnvironmentVMInitializationType: {
Type: schema.TypeString,
Description: "The cloud-init configuration format",
Expand Down Expand Up @@ -2087,6 +2097,15 @@ func resourceVirtualEnvironmentVMGetCloudInitConfig(d *schema.ResourceData) (*pr
}
}

initializationVendorDataFileID := initializationBlock[mkResourceVirtualEnvironmentVMInitializationVendorDataFileID].(string)

if initializationVendorDataFileID != "" {
if initializationConfig.Files == nil {
initializationConfig.Files = &proxmox.CustomCloudInitFiles{}
}
initializationConfig.Files.VendorVolume = &initializationVendorDataFileID
}

initializationType := initializationBlock[mkResourceVirtualEnvironmentVMInitializationType].(string)

if initializationType != "" {
Expand Down Expand Up @@ -2817,8 +2836,14 @@ func resourceVirtualEnvironmentVMReadCustom(ctx context.Context, d *schema.Resou
} else {
initialization[mkResourceVirtualEnvironmentVMInitializationUserDataFileID] = ""
}
if vmConfig.CloudInitFiles.VendorVolume != nil {
initialization[mkResourceVirtualEnvironmentVMInitializationVendorDataFileID] = *vmConfig.CloudInitFiles.VendorVolume
} else {
initialization[mkResourceVirtualEnvironmentVMInitializationVendorDataFileID] = ""
}
} else if len(initialization) > 0 {
initialization[mkResourceVirtualEnvironmentVMInitializationUserDataFileID] = ""
initialization[mkResourceVirtualEnvironmentVMInitializationVendorDataFileID] = ""
}

if vmConfig.CloudInitType != nil {
Expand Down