Skip to content

Commit ebaf3af

Browse files
fix: [CDS-100878]: File store object returns changes even if there are no changes in the script (#1100)
Co-authored-by: Ritek <[email protected]>
1 parent 9045385 commit ebaf3af

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

internal/service/platform/file_store/resource_file.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io/ioutil"
77
"net/http"
8+
"os"
89
"strings"
910

1011
"github.com/antihax/optional"
@@ -23,6 +24,7 @@ func ResourceFileStoreNodeFile() *schema.Resource {
2324
UpdateContext: resourceFileStoreNodeFileCreateOrUpdate,
2425
CreateContext: resourceFileStoreNodeFileCreateOrUpdate,
2526
DeleteContext: resourceFileStoreNodeFileDelete,
27+
CustomizeDiff: resourceFileStoreNodeFileCustomizeDiff,
2628
Importer: helpers.MultiLevelResourceImporter,
2729

2830
Schema: map[string]*schema.Schema{
@@ -35,6 +37,7 @@ func ResourceFileStoreNodeFile() *schema.Resource {
3537
Description: "File content path to be upladed on Harness File Store",
3638
Type: schema.TypeString,
3739
Optional: true,
40+
Computed: true,
3841
},
3942
"mime_type": {
4043
Description: "File mime type",
@@ -52,6 +55,7 @@ func ResourceFileStoreNodeFile() *schema.Resource {
5255
Description: "File content stored on Harness File Store",
5356
Type: schema.TypeString,
5457
Optional: true,
58+
Computed: true,
5559
},
5660
"path": {
5761
Description: "Harness File Store file path",
@@ -117,6 +121,28 @@ func ResourceFileStoreNodeFile() *schema.Resource {
117121
return resource
118122
}
119123

124+
func resourceFileStoreNodeFileCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error {
125+
if contentFilePath, ok := diff.GetOk(fileContentPath); ok {
126+
fileContent, err := os.ReadFile(contentFilePath.(string))
127+
if err != nil {
128+
return fmt.Errorf("error reading content file at path '%s': %w", contentFilePath, err)
129+
}
130+
131+
if diff.Id() != "" {
132+
remoteContent := diff.Get("content").(string)
133+
134+
if string(fileContent) != remoteContent {
135+
err = diff.SetNew("content", string(fileContent))
136+
if err != nil {
137+
return fmt.Errorf("error setting content: %w", err)
138+
}
139+
}
140+
}
141+
}
142+
143+
return nil
144+
}
145+
120146
func resourceFileStoreNodeFileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
121147
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
122148
id := d.Get(identifier).(string)

0 commit comments

Comments
 (0)