Skip to content

Commit 42c1f63

Browse files
fix: parse file_mode as 8 based value
1 parent bc55425 commit 42c1f63

File tree

3 files changed

+94
-7
lines changed

3 files changed

+94
-7
lines changed

internal/provider/file_mode.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package provider
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"strconv"
8+
9+
"github.com/hashicorp/go-cty/cty"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
)
13+
14+
const defaultFileMode = "0444"
15+
16+
func convertStrToFileMode(s string) (os.FileMode, error) {
17+
if s == "" {
18+
s = defaultFileMode
19+
}
20+
a, err := strconv.ParseInt(s, 8, 32)
21+
if err != nil {
22+
return 0, err
23+
}
24+
if a < 0 {
25+
return 0, fmt.Errorf("file_mode must be greater equal than 0: %d", a)
26+
}
27+
if a > 0o777 {
28+
return 0, fmt.Errorf("file_mode must be less equal than 0o777: %d", a)
29+
}
30+
return os.FileMode(a), nil
31+
}
32+
33+
func convertFileModeToStr(fileMode os.FileMode) string {
34+
// e.g. 288 (10) => "0440" (8)
35+
return "0" + strconv.FormatInt(int64(fileMode), 8)
36+
}
37+
38+
func fileModeDiffSuppressFunc(k, oldV, newV string, d *schema.ResourceData) bool {
39+
log.Printf("[DEBUG] DiffSuppressFunc(key: %s, old value: %s, new value: %s)", k, oldV, newV)
40+
if oldV == newV {
41+
return true
42+
}
43+
a, err := convertStrToFileMode(oldV)
44+
if err != nil {
45+
log.Printf("[DEBUG] DiffSuppressFunc(key: %s, old value: %s, new value: %s): old value is invalid: %s", k, oldV, newV, err.Error())
46+
return false
47+
}
48+
b, err := convertStrToFileMode(newV)
49+
if err != nil {
50+
log.Printf("[DEBUG] DiffSuppressFunc(key: %s, old value: %s, new value: %s): new value is invalid: %s", k, oldV, newV, err.Error())
51+
return false
52+
}
53+
return a == b
54+
}
55+
56+
func fileModeSchemaValidateDiagFunc(value interface{}, ctyPath cty.Path) diag.Diagnostics {
57+
if _, err := convertStrToFileMode(value.(string)); err != nil {
58+
return diag.FromErr(err)
59+
}
60+
return nil
61+
}

internal/provider/helper.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package provider
2+
3+
import "hash/crc32"
4+
5+
// hashString hashes a string to a unique hashcode.
6+
//
7+
// crc32 returns a uint32, but for our use we need
8+
// and non negative integer. Here we cast to an integer
9+
// and invert it if the result is negative.
10+
// https://www.terraform.io/docs/extend/guides/v2-upgrade-guide.html#removal-of-helper-hashcode-package
11+
func hashString(s string) int {
12+
v := int(crc32.ChecksumIEEE([]byte(s)))
13+
if v >= 0 {
14+
return v
15+
}
16+
if -v >= 0 {
17+
return -v
18+
}
19+
// v == MinInt
20+
return 0
21+
}

internal/provider/resource_docker_service.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,12 @@ func resourceDockerService() *schema.Resource {
439439
Optional: true,
440440
},
441441
"file_mode": {
442-
Type: schema.TypeInt,
443-
Description: "Represents represents the FileMode of the file. Defaults to `0o444`",
444-
Default: 0o444,
442+
Type: schema.TypeString,
445443
Optional: true,
446-
ValidateDiagFunc: validateIntegerGeqThan(0),
444+
Description: "Represents represents the FileMode of the file. Defaults to `0o444`",
445+
Default: defaultFileMode,
446+
DiffSuppressFunc: fileModeDiffSuppressFunc,
447+
ValidateDiagFunc: fileModeSchemaValidateDiagFunc,
447448
},
448449
},
449450
},
@@ -452,6 +453,9 @@ func resourceDockerService() *schema.Resource {
452453
Type: schema.TypeSet,
453454
Description: "References to zero or more configs that will be exposed to the service",
454455
Optional: true,
456+
Set: func(v interface{}) int {
457+
return hashString(v.(map[string]interface{})["config_id"].(string))
458+
},
455459
Elem: &schema.Resource{
456460
Schema: map[string]*schema.Schema{
457461
"config_id": {
@@ -482,11 +486,12 @@ func resourceDockerService() *schema.Resource {
482486
Optional: true,
483487
},
484488
"file_mode": {
485-
Type: schema.TypeInt,
489+
Type: schema.TypeString,
486490
Description: "Represents represents the FileMode of the file. Defaults to `0o444`.",
487-
Default: 0o444,
488491
Optional: true,
489-
ValidateDiagFunc: validateIntegerGeqThan(0),
492+
Default: defaultFileMode,
493+
DiffSuppressFunc: fileModeDiffSuppressFunc,
494+
ValidateDiagFunc: fileModeSchemaValidateDiagFunc,
490495
},
491496
},
492497
},

0 commit comments

Comments
 (0)