Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions docs/resources/service.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ Optional:

- **config_name** (String) Name of the config that this references, but this is just provided for lookup/display purposes. The config in the reference will be identified by its ID
- **file_gid** (String) Represents the file GID. Defaults to `0`.
- **file_mode** (Number) Represents represents the FileMode of the file. Defaults to `0o444`.
- **file_mode** (String) Represents represents the FileMode of the file. Defaults to `0o444`.
- **file_uid** (String) Represents the file UID. Defaults to `0`.


Expand Down Expand Up @@ -515,7 +515,7 @@ Required:
Optional:

- **file_gid** (String) Represents the file GID. Defaults to `0`
- **file_mode** (Number) Represents represents the FileMode of the file. Defaults to `0o444`
- **file_mode** (String) Represents represents the FileMode of the file. Defaults to `0o444`
- **file_uid** (String) Represents the file UID. Defaults to `0`
- **secret_name** (String) Name of the secret that this references, but this is just provided for lookup/display purposes. The config in the reference will be identified by its ID

Expand Down
56 changes: 56 additions & 0 deletions internal/provider/file_mode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package provider

import (
"fmt"
"log"
"os"
"strconv"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

const defaultFileMode = "0444"

func convertStrToFileMode(s string) (os.FileMode, error) {
if s == "" {
s = defaultFileMode
}
a, err := strconv.ParseInt(s, 8, 32)
if err != nil {
return 0, err
}
if a < 0 {
return 0, fmt.Errorf("file_mode must be greater equal than 0: %d", a)
}
if a > 0o4777 {
return 0, fmt.Errorf("file_mode must be less equal than 0o4777: %d", a)
}
return os.FileMode(a), nil
}

func fileModeDiffSuppressFunc(k, oldV, newV string, d *schema.ResourceData) bool {
log.Printf("[DEBUG] DiffSuppressFunc(key: %s, old value: %s, new value: %s)", k, oldV, newV)
if oldV == newV {
return true
}
a, err := convertStrToFileMode(oldV)
if err != nil {
log.Printf("[DEBUG] DiffSuppressFunc(key: %s, old value: %s, new value: %s): old value is invalid: %s", k, oldV, newV, err.Error())
return false
}
b, err := convertStrToFileMode(newV)
if err != nil {
log.Printf("[DEBUG] DiffSuppressFunc(key: %s, old value: %s, new value: %s): new value is invalid: %s", k, oldV, newV, err.Error())
return false
}
return a == b
}

func fileModeSchemaValidateDiagFunc(value interface{}, ctyPath cty.Path) diag.Diagnostics {
if _, err := convertStrToFileMode(value.(string)); err != nil {
return diag.FromErr(err)
}
return nil
}
21 changes: 21 additions & 0 deletions internal/provider/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package provider

import "hash/crc32"

// hashString hashes a string to a unique hashcode.
//
// crc32 returns a uint32, but for our use we need
// and non negative integer. Here we cast to an integer
// and invert it if the result is negative.
// https://www.terraform.io/docs/extend/guides/v2-upgrade-guide.html#removal-of-helper-hashcode-package
func hashString(s string) int {
v := int(crc32.ChecksumIEEE([]byte(s)))
if v >= 0 {
return v
}
if -v >= 0 {
return -v
}
// v == MinInt
return 0
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

17 changes: 11 additions & 6 deletions internal/provider/resource_docker_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,12 @@ func resourceDockerService() *schema.Resource {
Optional: true,
},
"file_mode": {
Type: schema.TypeInt,
Type: schema.TypeString,
Description: "Represents represents the FileMode of the file. Defaults to `0o444`",
Default: 0o444,
Default: defaultFileMode,
Optional: true,
ValidateDiagFunc: validateIntegerGeqThan(0),
ValidateDiagFunc: fileModeSchemaValidateDiagFunc,
DiffSuppressFunc: fileModeDiffSuppressFunc,
},
},
},
Expand All @@ -452,6 +453,9 @@ func resourceDockerService() *schema.Resource {
Type: schema.TypeSet,
Description: "References to zero or more configs that will be exposed to the service",
Optional: true,
Set: func(v interface{}) int {
return hashString(v.(map[string]interface{})["config_id"].(string))
},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"config_id": {
Expand Down Expand Up @@ -482,11 +486,12 @@ func resourceDockerService() *schema.Resource {
Optional: true,
},
"file_mode": {
Type: schema.TypeInt,
Type: schema.TypeString,
Description: "Represents represents the FileMode of the file. Defaults to `0o444`.",
Default: 0o444,
Default: defaultFileMode,
Optional: true,
ValidateDiagFunc: validateIntegerGeqThan(0),
ValidateDiagFunc: fileModeSchemaValidateDiagFunc,
DiffSuppressFunc: fileModeDiffSuppressFunc,
},
},
},
Expand Down