Skip to content

Commit

Permalink
fix: [CDS-78671]: Make file content optional (#681)
Browse files Browse the repository at this point in the history
* fix: [CDS-78671]: Make file content optional

* Update changelog

* Update change log

* Update changelog
  • Loading branch information
sarthakkasat committed Sep 11, 2023
1 parent eb56f07 commit ce46bc5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .changelog/681.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
harness_platform_file_store_file - Make file content optional, if the file content is provided use it directly else get the content from file path.
```
13 changes: 8 additions & 5 deletions internal/service/platform/file_store/resource_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ func ResourceFileStoreNodeFile() *schema.Resource {
"content": {
Description: "File content stored on Harness File Store",
Type: schema.TypeString,
Optional: false,
Computed: true,
Optional: true,
},
"path": {
Description: "Harness File Store file path",
Expand Down Expand Up @@ -201,7 +200,7 @@ func resourceFileStoreNodeFileDelete(ctx context.Context, d *schema.ResourceData
}

func buildFileStoreApiFileCreateRequest(d *schema.ResourceData) (*nextgen.FileStoreApiCreateOpts, error) {
fileContent, err := getFileContent(d.Get(fileContentPath))
fileContent, err := getFileContent(d.Get(fileContentPath), d.Get(content))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -230,7 +229,7 @@ func buildFileStoreApiFileCreateRequest(d *schema.ResourceData) (*nextgen.FileSt
}

func buildFileStoreApiFileUpdateRequest(d *schema.ResourceData) (*nextgen.FileStoreApiUpdateOpts, error) {
fileContent, err := getFileContent(d.Get(fileContentPath))
fileContent, err := getFileContent(d.Get(fileContentPath), d.Get(content))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -293,7 +292,11 @@ func readFileNode(d *schema.ResourceData, file *nextgen.File, fileContentOpt opt
d.Set(content, fileContent)
}

func getFileContent(filePath interface{}) (optional.Interface, error) {
func getFileContent(filePath interface{}, fileContent interface{}) (optional.Interface, error) {
if fileContentStr, ok := fileContent.(string); ok && len(fileContentStr) > 0 {
// If fileContent is a non-empty string, return it directly
return optional.NewInterface([]byte(fileContentStr)), nil
}
filePathStr, ok := filePath.(string)
if !ok {
return optional.Interface{}, nil
Expand Down
50 changes: 49 additions & 1 deletion internal/service/platform/file_store/resource_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,39 @@ func TestAccResourceFileStoreFile(t *testing.T) {
})
}

func TestAccResourceFileStoreFileWithContent(t *testing.T) {

id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5))
name := id
resourceName := "harness_platform_file_store_file.test"

resource.UnitTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccFileStoreDestroy(resourceName),
Steps: []resource.TestStep{
{
Config: testAccResourceFileStore_FileContent(id, name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", id),
resource.TestCheckResourceAttr(resourceName, "identifier", id),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "description", "test file"),
resource.TestCheckResourceAttr(resourceName, "content", "file content"),
resource.TestCheckResourceAttr(resourceName, "tags.#", "2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"file_content_path"},
ImportStateIdFunc: acctest.AccountLevelResourceImportStateIdFunc(resourceName),
},
},
})
}

func TestAccResourceFileStoreFileOrgLevel(t *testing.T) {

id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5))
Expand Down Expand Up @@ -165,7 +198,7 @@ func TestAccResourceFileStoreFile_DeleteUnderlyingResource(t *testing.T) {
ExternalProviders: map[string]resource.ExternalProvider{
"time": {},
},
CheckDestroy: testAccFileStoreDestroy(resourceName),
CheckDestroy: testAccFileStoreDestroy(resourceName),
Steps: []resource.TestStep{
{
Config: testAccResourceFileStore_FileProjectLevel(id, name),
Expand Down Expand Up @@ -267,6 +300,21 @@ func testAccResourceFileStore_File(id string, name string) string {
`, id, name, getAbsFilePath("../../../acctest/file_store_files/file.txt"))
}

func testAccResourceFileStore_FileContent(id string, name string) string {
return fmt.Sprintf(`
resource "harness_platform_file_store_file" "test" {
identifier = "%[1]s"
name = "%[2]s"
description = "test file"
tags = ["foo:bar", "bar:foo"]
parent_identifier = "Root"
mime_type = "text"
file_usage = "SCRIPT"
content = file("%[3]s")
}
`, id, name, getAbsFilePath("../../../acctest/file_store_files/file.txt"))
}

// common methods for file and folder
func buildField(r *terraform.ResourceState, field string) optional.String {
if attr, ok := r.Primary.Attributes[field]; ok {
Expand Down

0 comments on commit ce46bc5

Please sign in to comment.