mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-07-01 11:02:59 +00:00
fix(file): handle missing file on state refresh (#649)
* fix(file): handle missing file on state refresh --------- Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
This commit is contained in:
parent
a30f96c348
commit
2a56c23f52
@ -17,6 +17,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/brianvoe/gofakeit/v6"
|
"github.com/brianvoe/gofakeit/v6"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
@ -26,6 +27,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
accTestFileRawName = "proxmox_virtual_environment_file.test_raw"
|
||||||
accTestFileName = "proxmox_virtual_environment_file.test"
|
accTestFileName = "proxmox_virtual_environment_file.test"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -94,8 +96,11 @@ func TestAccResourceFile(t *testing.T) {
|
|||||||
},
|
},
|
||||||
// Update testing
|
// Update testing
|
||||||
{
|
{
|
||||||
Config: testAccResourceFileSnippetRawUpdatedConfig(snippetRaw),
|
PreConfig: func() {
|
||||||
Check: testAccResourceFileSnippetUpdatedCheck(snippetRaw),
|
deleteSnippet(t, filepath.Base(snippetFile1.Name()))
|
||||||
|
},
|
||||||
|
Config: testAccResourceFileSnippetUpdateConfig(snippetFile1.Name()),
|
||||||
|
Check: testAccResourceFileSnippetUpdatedCheck(snippetFile1.Name()),
|
||||||
},
|
},
|
||||||
// ImportState testing
|
// ImportState testing
|
||||||
{
|
{
|
||||||
@ -165,9 +170,16 @@ func createFile(t *testing.T, namePattern string, content string) *os.File {
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func deleteSnippet(t *testing.T, fname string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
err := getNodesClient().DeleteDatastoreFile(context.Background(), "local", fmt.Sprintf("snippets/%s", fname))
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func testAccResourceFileSnippetRawCreatedConfig(fname string) string {
|
func testAccResourceFileSnippetRawCreatedConfig(fname string) string {
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
resource "proxmox_virtual_environment_file" "test" {
|
resource "proxmox_virtual_environment_file" "test_raw" {
|
||||||
content_type = "snippets"
|
content_type = "snippets"
|
||||||
datastore_id = "local"
|
datastore_id = "local"
|
||||||
node_name = "%s"
|
node_name = "%s"
|
||||||
@ -223,11 +235,11 @@ resource "proxmox_virtual_environment_file" "test" {
|
|||||||
|
|
||||||
func testAccResourceFileSnippetRawCreatedCheck(fname string) resource.TestCheckFunc {
|
func testAccResourceFileSnippetRawCreatedCheck(fname string) resource.TestCheckFunc {
|
||||||
return resource.ComposeTestCheckFunc(
|
return resource.ComposeTestCheckFunc(
|
||||||
resource.TestCheckResourceAttr(accTestFileName, "content_type", "snippets"),
|
resource.TestCheckResourceAttr(accTestFileRawName, "content_type", "snippets"),
|
||||||
resource.TestCheckResourceAttr(accTestFileName, "file_name", fname),
|
resource.TestCheckResourceAttr(accTestFileRawName, "file_name", fname),
|
||||||
resource.TestCheckResourceAttr(accTestFileName, "source_raw.0.file_name", fname),
|
resource.TestCheckResourceAttr(accTestFileRawName, "source_raw.0.file_name", fname),
|
||||||
resource.TestCheckResourceAttr(accTestFileName, "source_raw.0.data", "test snippet\n"),
|
resource.TestCheckResourceAttr(accTestFileRawName, "source_raw.0.data", "test snippet\n"),
|
||||||
resource.TestCheckResourceAttr(accTestFileName, "id", fmt.Sprintf("local:snippets/%s", fname)),
|
resource.TestCheckResourceAttr(accTestFileRawName, "id", fmt.Sprintf("local:snippets/%s", fname)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -239,19 +251,13 @@ func testAccResourceFileCreatedCheck(ctype string, fname string) resource.TestCh
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAccResourceFileSnippetRawUpdatedConfig(fname string) string {
|
func testAccResourceFileSnippetUpdateConfig(fname string) string {
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
resource "proxmox_virtual_environment_file" "test" {
|
resource "proxmox_virtual_environment_file" "test" {
|
||||||
content_type = "snippets"
|
|
||||||
datastore_id = "local"
|
datastore_id = "local"
|
||||||
node_name = "%s"
|
node_name = "%s"
|
||||||
|
source_file {
|
||||||
source_raw {
|
path = "%s"
|
||||||
data = <<EOF
|
|
||||||
test snippet - updated
|
|
||||||
EOF
|
|
||||||
|
|
||||||
file_name = "%s"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`, accTestNodeName, fname)
|
`, accTestNodeName, fname)
|
||||||
@ -260,9 +266,7 @@ test snippet - updated
|
|||||||
func testAccResourceFileSnippetUpdatedCheck(fname string) resource.TestCheckFunc {
|
func testAccResourceFileSnippetUpdatedCheck(fname string) resource.TestCheckFunc {
|
||||||
return resource.ComposeTestCheckFunc(
|
return resource.ComposeTestCheckFunc(
|
||||||
resource.TestCheckResourceAttr(accTestFileName, "content_type", "snippets"),
|
resource.TestCheckResourceAttr(accTestFileName, "content_type", "snippets"),
|
||||||
resource.TestCheckResourceAttr(accTestFileName, "file_name", fname),
|
resource.TestCheckResourceAttr(accTestFileName, "file_name", filepath.Base(fname)),
|
||||||
resource.TestCheckResourceAttr(accTestFileName, "source_raw.0.file_name", fname),
|
resource.TestCheckResourceAttr(accTestFileName, "id", fmt.Sprintf("local:%s/%s", "snippets", filepath.Base(fname))),
|
||||||
resource.TestCheckResourceAttr(accTestFileName, "source_raw.0.data", "test snippet - updated\n"),
|
|
||||||
resource.TestCheckResourceAttr(accTestFileName, "id", fmt.Sprintf("local:snippets/%s", fname)),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -780,8 +780,9 @@ func fileRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
diags = append(diags, diag.Errorf("no such file: %q", d.Id())...)
|
// an empty ID is used to signal that the resource does not exist when provider reads the state
|
||||||
return diags
|
// back after creation, or on the state refresh.
|
||||||
|
d.SetId("")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user