From 0deaf1801ade7896500c43d4903a69ee5da240a3 Mon Sep 17 00:00:00 2001 From: msdnna Date: Thu, 21 Mar 2024 05:13:57 +0300 Subject: [PATCH] feat(lxc): support hook script for LXC (#1140) * feat(lxc): support hook script for LXC Signed-off-by: msdnna --------- Signed-off-by: msdnna --- .../virtual_environment_container.md | 1 + proxmoxtf/resource/container/container.go | 29 +++++++++++++++++ .../resource/container/container_test.go | 32 ++++++++++--------- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/docs/resources/virtual_environment_container.md b/docs/resources/virtual_environment_container.md index 34b26bfc..ba3ba302 100644 --- a/docs/resources/virtual_environment_container.md +++ b/docs/resources/virtual_environment_container.md @@ -225,6 +225,7 @@ output "ubuntu_container_public_key" { - `keyctl` - (Optional) Whether the container supports `keyctl()` system call (defaults to `false`) - `mount` - (Optional) List of allowed mount types (`cifs` or `nfs`) +- `hook_script_file_id` - (Optional) The identifier for a file containing a hook script (needs to be executable). ## Attribute Reference diff --git a/proxmoxtf/resource/container/container.go b/proxmoxtf/resource/container/container.go index 6eb92375..ac28ee81 100644 --- a/proxmoxtf/resource/container/container.go +++ b/proxmoxtf/resource/container/container.go @@ -49,6 +49,7 @@ const ( dvFeaturesNesting = false dvFeaturesKeyControl = false dvFeaturesFUSE = false + dvHookScript = "" dvMemoryDedicated = 512 dvMemorySwap = 0 dvMountPointACL = false @@ -100,6 +101,7 @@ const ( mkFeaturesKeyControl = "keyctl" mkFeaturesFUSE = "fuse" mkFeaturesMountTypes = "mount" + mkHookScriptFileID = "hook_script_file_id" mkInitialization = "initialization" mkInitializationDNS = "dns" mkInitializationDNSDomain = "domain" @@ -375,6 +377,12 @@ func Container() *schema.Resource { MaxItems: 1, MinItems: 0, }, + mkHookScriptFileID: { + Type: schema.TypeString, + Description: "A hook script", + Optional: true, + Default: dvHookScript, + }, mkInitialization: { Type: schema.TypeList, Description: "The initialization configuration", @@ -996,6 +1004,12 @@ func containerCreateClone(ctx context.Context, d *schema.ResourceData, m interfa updateBody.CPUUnits = &cpuUnits } + hookScript := d.Get(mkHookScriptFileID).(string) + + if hookScript != "" { + updateBody.HookScript = &hookScript + } + var initializationIPConfigIPv4Address []string var initializationIPConfigIPv4Gateway []string @@ -1316,6 +1330,8 @@ func containerCreateCustom(ctx context.Context, d *schema.ResourceData, m interf return diag.FromErr(err) } + hookScript := d.Get(mkHookScriptFileID).(string) + initialization := d.Get(mkInitialization).([]interface{}) initializationDNSDomain := dvInitializationDNSDomain initializationDNSServer := dvInitializationDNSServer @@ -1632,6 +1648,10 @@ func containerCreateCustom(ctx context.Context, d *schema.ResourceData, m interf createBody.Description = &description } + if hookScript != "" { + createBody.HookScript = &hookScript + } + if initializationDNSDomain != "" { createBody.DNSDomain = &initializationDNSDomain } @@ -2597,6 +2617,15 @@ func containerUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) updateBody.Features = features } + if d.HasChange(mkHookScriptFileID) { + hookScript := d.Get(mkHookScriptFileID).(string) + if hookScript != "" { + updateBody.HookScript = &hookScript + } else { + updateBody.Delete = append(updateBody.Delete, "hookscript") + } + } + // Prepare the new initialization configuration. initialization := d.Get(mkInitialization).([]interface{}) initializationDNSDomain := dvInitializationDNSDomain diff --git a/proxmoxtf/resource/container/container_test.go b/proxmoxtf/resource/container/container_test.go index b0014de5..03dc1591 100644 --- a/proxmoxtf/resource/container/container_test.go +++ b/proxmoxtf/resource/container/container_test.go @@ -39,6 +39,7 @@ func TestContainerSchema(t *testing.T) { mkDescription, mkDisk, mkInitialization, + mkHookScriptFileID, mkMemory, mkMountPoint, mkOperatingSystem, @@ -53,21 +54,22 @@ func TestContainerSchema(t *testing.T) { }) test.AssertValueTypes(t, s, map[string]schema.ValueType{ - mkCPU: schema.TypeList, - mkDescription: schema.TypeString, - mkDisk: schema.TypeList, - mkInitialization: schema.TypeList, - mkMemory: schema.TypeList, - mkMountPoint: schema.TypeList, - mkOperatingSystem: schema.TypeList, - mkPoolID: schema.TypeString, - mkStarted: schema.TypeBool, - mkTags: schema.TypeList, - mkTemplate: schema.TypeBool, - mkUnprivileged: schema.TypeBool, - mkStartOnBoot: schema.TypeBool, - mkFeatures: schema.TypeList, - mkVMID: schema.TypeInt, + mkCPU: schema.TypeList, + mkDescription: schema.TypeString, + mkDisk: schema.TypeList, + mkInitialization: schema.TypeList, + mkHookScriptFileID: schema.TypeString, + mkMemory: schema.TypeList, + mkMountPoint: schema.TypeList, + mkOperatingSystem: schema.TypeList, + mkPoolID: schema.TypeString, + mkStarted: schema.TypeBool, + mkTags: schema.TypeList, + mkTemplate: schema.TypeBool, + mkUnprivileged: schema.TypeBool, + mkStartOnBoot: schema.TypeBool, + mkFeatures: schema.TypeList, + mkVMID: schema.TypeInt, }) cloneSchema := test.AssertNestedSchemaExistence(t, s, mkClone)