mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-27 01:11:10 -06:00
codify saved plan artifact contents (#33185)
This commit is contained in:
parent
b3f254e08d
commit
717a36036d
38
internal/cloud/backend_saved_plan.go
Normal file
38
internal/cloud/backend_saved_plan.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// Copyright (c) HashiCorp, Inc.
|
||||||
|
// SPDX-License-Identifier: MPL-2.0
|
||||||
|
|
||||||
|
package cloud
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SavedPlanBookmark struct {
|
||||||
|
RemotePlanFormat int `json:"remote_plan_format"`
|
||||||
|
RunID string `json:"run_id"`
|
||||||
|
Hostname string `json:"hostname"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadSavedPlanBookmark(filepath string) (SavedPlanBookmark, error) {
|
||||||
|
bookmark := SavedPlanBookmark{}
|
||||||
|
data, err := os.ReadFile(filepath)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return bookmark, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal([]byte(data), &bookmark)
|
||||||
|
return bookmark, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SavedPlanBookmark) Save(filepath string) error {
|
||||||
|
data, _ := json.Marshal(s)
|
||||||
|
|
||||||
|
err := os.WriteFile(filepath, data, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
42
internal/cloud/backend_saved_plan_test.go
Normal file
42
internal/cloud/backend_saved_plan_test.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
// Copyright (c) HashiCorp, Inc.
|
||||||
|
// SPDX-License-Identifier: MPL-2.0
|
||||||
|
|
||||||
|
package cloud
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"github.com/zclconf/go-cty/cty"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCloud_loadBasic(t *testing.T) {
|
||||||
|
bookmark := SavedPlanBookmark{
|
||||||
|
RemotePlanFormat: 1,
|
||||||
|
RunID: "run-GXfuHMkbyHccAGUg",
|
||||||
|
Hostname: "app.terraform.io",
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := LoadSavedPlanBookmark("./testdata/plan-bookmark/bookmark.json")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if diff := cmp.Diff(bookmark, result, cmp.Comparer(cty.Value.RawEquals)); diff != "" {
|
||||||
|
t.Errorf("wrong result\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCloud_saveBasic(t *testing.T) {
|
||||||
|
tmp := t.TempDir()
|
||||||
|
bookmarkPath := filepath.Join(tmp, "saved-bookmark.json")
|
||||||
|
|
||||||
|
b := &SavedPlanBookmark{
|
||||||
|
RemotePlanFormat: 1,
|
||||||
|
RunID: "run-GXfuHMkbyHccAGUg",
|
||||||
|
Hostname: "app.terraform.io",
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Save(bookmarkPath)
|
||||||
|
}
|
5
internal/cloud/testdata/plan-bookmark/bookmark.json
vendored
Normal file
5
internal/cloud/testdata/plan-bookmark/bookmark.json
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"remote_plan_format": 1,
|
||||||
|
"run_id": "run-GXfuHMkbyHccAGUg",
|
||||||
|
"hostname": "app.terraform.io"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user