codify saved plan artifact contents (#33185)

This commit is contained in:
Lauren 2023-05-24 12:58:43 -05:00 committed by Sebastian Rivera
parent b3f254e08d
commit 717a36036d
3 changed files with 85 additions and 0 deletions

View 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
}

View 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)
}

View File

@ -0,0 +1,5 @@
{
"remote_plan_format": 1,
"run_id": "run-GXfuHMkbyHccAGUg",
"hostname": "app.terraform.io"
}