diff --git a/internal/service/platform/file_store/resource_file.go b/internal/service/platform/file_store/resource_file.go index faec98958..6c2cc586a 100644 --- a/internal/service/platform/file_store/resource_file.go +++ b/internal/service/platform/file_store/resource_file.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "net/http" + "os" "strings" "github.com/antihax/optional" @@ -23,6 +24,7 @@ func ResourceFileStoreNodeFile() *schema.Resource { UpdateContext: resourceFileStoreNodeFileCreateOrUpdate, CreateContext: resourceFileStoreNodeFileCreateOrUpdate, DeleteContext: resourceFileStoreNodeFileDelete, + CustomizeDiff: resourceFileStoreNodeFileCustomizeDiff, Importer: helpers.MultiLevelResourceImporter, Schema: map[string]*schema.Schema{ @@ -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", @@ -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", @@ -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)