From 089b53a47d5c94058075ab1270ee97c937fb852c Mon Sep 17 00:00:00 2001 From: c10l Date: Sun, 17 Oct 2021 16:33:13 +0000 Subject: [PATCH] Fix panic === RUN TestAccContainerFile_content 67 panic: mode: '' expected type 'string', got unconvertible type 'int' 68 69 goroutine 16536 [running]: 70 github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*ResourceData).Set(0xc00108a400, 0x1af4fe9, 0x4, 0x18dbfc0, 0x25716a0, 0x0, 0x0) 71 /home/runner/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.4.3/helper/schema/resource_data.go:230 +0x574 72 github.com/terraform-lxd/terraform-provider-lxd/lxd.resourceLxdContainerFileRead(0xc00108a400, 0x1aad460, 0xc0011ffa00, 0x7facf43a8858, 0x68) 73 /home/runner/work/terraform-provider-lxd/terraform-provider-lxd/lxd/resource_lxd_container_file.go:146 +0x776 ... --- lxd/resource_lxd_container.go | 4 ++-- lxd/resource_lxd_container_file.go | 4 ++-- lxd/shared.go | 18 +++++------------- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/lxd/resource_lxd_container.go b/lxd/resource_lxd_container.go index 7330564c..f585bfb2 100644 --- a/lxd/resource_lxd_container.go +++ b/lxd/resource_lxd_container.go @@ -328,7 +328,7 @@ func resourceLxdContainerCreate(d *schema.ResourceData, meta interface{}) error Source: f["source"].(string), UID: f["uid"].(int), GID: f["gid"].(int), - Mode: f["mode"].(string), + Mode: f["mode"].(int), CreateDirectories: f["create_directories"].(bool), } @@ -652,7 +652,7 @@ func resourceLxdContainerUpdate(d *schema.ResourceData, meta interface{}) error Source: f["source"].(string), UID: f["uid"].(int), GID: f["gid"].(int), - Mode: f["mode"].(string), + Mode: f["mode"].(int), CreateDirectories: f["create_directories"].(bool), } diff --git a/lxd/resource_lxd_container_file.go b/lxd/resource_lxd_container_file.go index d05f9176..1d4c6437 100644 --- a/lxd/resource_lxd_container_file.go +++ b/lxd/resource_lxd_container_file.go @@ -54,7 +54,7 @@ func resourceLxdContainerFile() *schema.Resource { }, "mode": { - Type: schema.TypeString, + Type: schema.TypeInt, ForceNew: true, Optional: true, }, @@ -102,7 +102,7 @@ func resourceLxdContainerFileCreate(d *schema.ResourceData, meta interface{}) er Source: d.Get("source").(string), UID: d.Get("uid").(int), GID: d.Get("gid").(int), - Mode: d.Get("mode").(string), + Mode: d.Get("mode").(int), CreateDirectories: d.Get("create_directories").(bool), Append: d.Get("append").(bool), } diff --git a/lxd/shared.go b/lxd/shared.go index 589ca73d..8ec4831a 100644 --- a/lxd/shared.go +++ b/lxd/shared.go @@ -6,7 +6,6 @@ import ( "os" "path" "path/filepath" - "strconv" "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -66,7 +65,7 @@ type File struct { Source string UID int GID int - Mode string + Mode int CreateDirectories bool Append bool } @@ -229,23 +228,16 @@ func containerUploadFile(server lxd.ContainerServer, container string, file File return fmt.Errorf("Target must be an absolute path with filename") } - mode := os.FileMode(0755) - if len(file.Mode) != 3 { - file.Mode = "0" + file.Mode + mode := os.FileMode(file.Mode) + if mode == 0 { + mode = os.FileMode(0755) } - m, err := strconv.ParseInt(file.Mode, 0, 0) - if err != nil { - return fmt.Errorf("Could not determine file mode %s", file.Mode) - } - - mode = os.FileMode(m) - // Build the file creation request, without the content. uid := int64(file.UID) gid := int64(file.GID) args := lxd.ContainerFileArgs{ - Mode: int(mode.Perm()), + Mode: int(mode), UID: int64(uid), GID: int64(gid), Type: "file",