Skip to content

Commit

Permalink
fix: [CDS-100878]: File store object returns changes even if there ar…
Browse files Browse the repository at this point in the history
…e no changes in the script (#1100)

Co-authored-by: Ritek <[email protected]>
  • Loading branch information
harness-abedonik and ritek01 authored Oct 23, 2024
1 parent 9045385 commit ebaf3af
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions internal/service/platform/file_store/resource_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/antihax/optional"
Expand All @@ -23,6 +24,7 @@ func ResourceFileStoreNodeFile() *schema.Resource {
UpdateContext: resourceFileStoreNodeFileCreateOrUpdate,
CreateContext: resourceFileStoreNodeFileCreateOrUpdate,
DeleteContext: resourceFileStoreNodeFileDelete,
CustomizeDiff: resourceFileStoreNodeFileCustomizeDiff,
Importer: helpers.MultiLevelResourceImporter,

Schema: map[string]*schema.Schema{
Expand All @@ -35,6 +37,7 @@ func ResourceFileStoreNodeFile() *schema.Resource {
Description: "File content path to be upladed on Harness File Store",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"mime_type": {
Description: "File mime type",
Expand All @@ -52,6 +55,7 @@ func ResourceFileStoreNodeFile() *schema.Resource {
Description: "File content stored on Harness File Store",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"path": {
Description: "Harness File Store file path",
Expand Down Expand Up @@ -117,6 +121,28 @@ func ResourceFileStoreNodeFile() *schema.Resource {
return resource
}

func resourceFileStoreNodeFileCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error {
if contentFilePath, ok := diff.GetOk(fileContentPath); ok {
fileContent, err := os.ReadFile(contentFilePath.(string))
if err != nil {
return fmt.Errorf("error reading content file at path '%s': %w", contentFilePath, err)
}

if diff.Id() != "" {
remoteContent := diff.Get("content").(string)

if string(fileContent) != remoteContent {
err = diff.SetNew("content", string(fileContent))
if err != nil {
return fmt.Errorf("error setting content: %w", err)
}
}
}
}

return nil
}

func resourceFileStoreNodeFileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
id := d.Get(identifier).(string)
Expand Down

0 comments on commit ebaf3af

Please sign in to comment.