Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reading state versions #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type Run struct {
Plan *Plan `jsonapi:"relation,plan"`
PolicyChecks []*PolicyCheck `jsonapi:"relation,policy-checks"`
Workspace *Workspace `jsonapi:"relation,workspace"`
StateVersions []*StateVersion `jsonapi:"relation,state-versions,omitempty"`
}

// RunCreateOptions represents the options for creating a new run.
Expand Down
2 changes: 2 additions & 0 deletions scalr.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ type Client struct {
ServiceAccountTokens ServiceAccountTokens
ServiceAccounts ServiceAccounts
SlackIntegrations SlackIntegrations
StateVersions StateVersions
Tags Tags
Teams Teams
Users Users
Expand Down Expand Up @@ -239,6 +240,7 @@ func NewClient(cfg *Config) (*Client, error) {
client.ServiceAccountTokens = &serviceAccountTokens{client: client}
client.ServiceAccounts = &serviceAccounts{client: client}
client.SlackIntegrations = &slackIntegrations{client: client}
client.StateVersions = &stateVersions{client: client}
client.Tags = &tags{client: client}
client.Teams = &teams{client: client}
client.Users = &users{client: client}
Expand Down
97 changes: 97 additions & 0 deletions state_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package scalr

import (
"context"
"errors"
"fmt"
"net/url"
"time"
)

// Compile-time proof of interface implementation.
var _ StateVersions = (*stateVersions)(nil)

type StateVersions interface {
// Read gets a state version resource from its ID.
Read(ctx context.Context, stateVersionID string) (*StateVersion, error)

// ReadCurrentForWorkspace gets the current state version for a given
// workspace.
ReadCurrentForWorkspace(ctx context.Context, workspaceID string) (*StateVersion, error)
}

// stateVersions implements StateVersions.
type stateVersions struct {
client *Client
}

// StateVersionOutput describes a particular output of a state version.
type StateVersionOutput struct {
Name string `json:"name"`
Value interface{} `json:"value"`
Sensitive bool `json:"sensitive"`
}

// StateVersionResource describes a resource in a state version.
type StateVersionResource struct {
Type string `json:"type"`
Module string `json:"module,omitempty"`
Address string `json:"address"`
}

// StateVersion is a particular instance of Terraform state.
type StateVersion struct {
ID string `jsonapi:"primary,state-versions"`
Outputs []*StateVersionOutput `jsonapi:"attr,outputs"`
Resources []*StateVersionResource `jsonapi:"attr,resources"`
Force bool `jsonapi:"attr,force"`
Lineage string `jsonapi:"attr,lineage"`
MD5 string `jsonapi:"attr,md5"`
Serial int `jsonapi:"attr,serial"`
Size int `jsonapi:"attr,size"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`

// Relations
Run *Run `jsonapi:"relation,run,omitempty"`
NextStateVersion *StateVersion `jsonapi:"relation,next-state-version,omitempty"`
PreviousStateVersion *StateVersion `jsonapi:"relation,previous-state-version,omitempty"`
Workspace *Workspace `jsonapi:"relation,workspace"`
}

func (s *stateVersions) Read(ctx context.Context, stateVersionID string) (*StateVersion, error) {
if !validStringID(&stateVersionID) {
return nil, errors.New("invalid value for state version ID")
}

u := fmt.Sprintf("state-versions/%s", url.PathEscape(stateVersionID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}

sv := &StateVersion{}
if err := s.client.do(ctx, req, sv); err != nil {
return nil, err
}

return sv, nil
}

func (s *stateVersions) ReadCurrentForWorkspace(ctx context.Context, workspaceID string) (*StateVersion, error) {
if !validStringID(&workspaceID) {
return nil, errors.New("invalid value for workspace ID")
}

u := fmt.Sprintf("workspaces/%s/current-state-version", url.PathEscape(workspaceID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}

sv := &StateVersion{}
if err := s.client.do(ctx, req, sv); err != nil {
return nil, err
}

return sv, nil
}