Skip to content

Commit

Permalink
Add a new function to the Plans API to give users ability to retrieve…
Browse files Browse the repository at this point in the history
… a list of the resource changes done by a specific Plan
  • Loading branch information
AhmedRaafat14 authored and ahmedRaafat2112 committed Feb 14, 2024
1 parent 7e3754a commit 128e04f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
56 changes: 56 additions & 0 deletions plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package tfe
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/url"
Expand All @@ -23,6 +24,9 @@ type Plans interface {
// Read a plan by its ID.
Read(ctx context.Context, planID string) (*Plan, error)

// ReadResourceChanges fetch plan changed resources
ReadResourceChanges(ctx context.Context, planID string) (*PlanResourceChanges, error)

// Logs retrieves the logs of a plan.
Logs(ctx context.Context, planID string) (io.Reader, error)

Expand Down Expand Up @@ -78,6 +82,32 @@ type PlanStatusTimestamps struct {
StartedAt time.Time `jsonapi:"attr,started-at,rfc3339"`
}

// ResourceChange details changes made to a specific resource within a plan.
type ResourceChange struct {
Address string `json:"address"`
Change Change `json:"change"`
Index interface{} `json:"index"`
Mode string `json:"mode"`
Name string `json:"name"`
ProviderName string `json:"provider_name"`
Type string `json:"type"`
}

// Change captures the before and after states of a resource, including actions taken.
type Change struct {
Actions []string `json:"actions"`
After interface{} `json:"after"`
AfterSensitive interface{} `json:"after_sensitive"`
AfterUnknown interface{} `json:"after_unknown"`
Before interface{} `json:"before"`
BeforeSensitive interface{} `json:"before_sensitive"`
}

// PlanResourceChanges encapsulates all resource changes within a plan.
type PlanResourceChanges struct {
ResourceChanges []ResourceChange `json:"resource_changes"`
}

// Read a plan by its ID.
func (s *plans) Read(ctx context.Context, planID string) (*Plan, error) {
if !validStringID(&planID) {
Expand Down Expand Up @@ -163,3 +193,29 @@ func (s *plans) ReadJSONOutput(ctx context.Context, planID string) ([]byte, erro

return buf.Bytes(), nil
}

// ReadResourceChanges fetch plan resource changes.
func (s *plans) ReadResourceChanges(ctx context.Context, planID string) (*PlanResourceChanges, error) {
if !validStringID(&planID) {
return nil, ErrInvalidPlanID
}

u := fmt.Sprintf("plans/%s/json-output-redacted", url.QueryEscape(planID))
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}

var buf bytes.Buffer
err = req.Do(ctx, &buf)
if err != nil {
return nil, err
}

var resourceChanges PlanResourceChanges
if err := json.Unmarshal(buf.Bytes(), &resourceChanges); err != nil {
return nil, err
}

return &resourceChanges, nil
}
19 changes: 19 additions & 0 deletions plan_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,22 @@ func TestPlansJSONOutput(t *testing.T) {
assert.Error(t, err)
})
}

func TestPlansReadResourceChanges(t *testing.T) {
client := testClient(t)
ctx := context.Background()
rTest, rTestCleanup := createPlannedRun(t, client, nil)
defer rTestCleanup()

t.Run("when resource changes exist for the plan", func(t *testing.T) {
resourceChanges, err := client.Plans.ReadResourceChanges(ctx, rTest.Plan.ID)
require.NoError(t, err)
assert.NotEmpty(t, resourceChanges.ResourceChanges)
})

t.Run("when the plan does not exist", func(t *testing.T) {
resourceChanges, err := client.Plans.ReadResourceChanges(ctx, "nonexisting")
assert.Nil(t, resourceChanges)
assert.Error(t, err)
})
}

0 comments on commit 128e04f

Please sign in to comment.