Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 227f284

Browse files
committedSep 6, 2023
Add support for reading state versions
1 parent 6aa9155 commit 227f284

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
 

‎run.go

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type Run struct {
7979
Plan *Plan `jsonapi:"relation,plan"`
8080
PolicyChecks []*PolicyCheck `jsonapi:"relation,policy-checks"`
8181
Workspace *Workspace `jsonapi:"relation,workspace"`
82+
StateVersions []*StateVersion `jsonapi:"relation,state-versions,omitempty"`
8283
}
8384

8485
// RunCreateOptions represents the options for creating a new run.

‎scalr.go

+2
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ type Client struct {
140140
ServiceAccountTokens ServiceAccountTokens
141141
ServiceAccounts ServiceAccounts
142142
SlackIntegrations SlackIntegrations
143+
StateVersions StateVersions
143144
Tags Tags
144145
Teams Teams
145146
Users Users
@@ -239,6 +240,7 @@ func NewClient(cfg *Config) (*Client, error) {
239240
client.ServiceAccountTokens = &serviceAccountTokens{client: client}
240241
client.ServiceAccounts = &serviceAccounts{client: client}
241242
client.SlackIntegrations = &slackIntegrations{client: client}
243+
client.StateVersions = &stateVersions{client: client}
242244
client.Tags = &tags{client: client}
243245
client.Teams = &teams{client: client}
244246
client.Users = &users{client: client}

‎state_version.go

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package scalr
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"net/url"
8+
"time"
9+
)
10+
11+
// Compile-time proof of interface implementation.
12+
var _ StateVersions = (*stateVersions)(nil)
13+
14+
type StateVersions interface {
15+
// Read gets a state version resource from its ID.
16+
Read(ctx context.Context, stateVersionID string) (*StateVersion, error)
17+
18+
// ReadCurrentForWorkspace gets the current state version for a given
19+
// workspace.
20+
ReadCurrentForWorkspace(ctx context.Context, workspaceID string) (*StateVersion, error)
21+
}
22+
23+
// stateVersions implements StateVersions.
24+
type stateVersions struct {
25+
client *Client
26+
}
27+
28+
// StateVersionOutput describes a particular output of a state version.
29+
type StateVersionOutput struct {
30+
Name string `json:"name"`
31+
Value interface{} `json:"value"`
32+
Sensitive bool `json:"sensitive"`
33+
}
34+
35+
// StateVersionResource describes a resource in a state version.
36+
type StateVersionResource struct {
37+
Type string `json:"type"`
38+
Module string `json:"module,omitempty"`
39+
Address string `json:"address"`
40+
}
41+
42+
// StateVersion is a particular instance of Terraform state.
43+
type StateVersion struct {
44+
ID string `jsonapi:"primary,state-versions"`
45+
Outputs []*StateVersionOutput `jsonapi:"attr,outputs"`
46+
Resources []*StateVersionResource `jsonapi:"attr,resources"`
47+
Force bool `jsonapi:"attr,force"`
48+
Lineage string `jsonapi:"attr,lineage"`
49+
MD5 string `jsonapi:"attr,md5"`
50+
Serial int `jsonapi:"attr,serial"`
51+
Size int `jsonapi:"attr,size"`
52+
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
53+
54+
// Relations
55+
Run *Run `jsonapi:"relation,run,omitempty"`
56+
NextStateVersion *StateVersion `jsonapi:"relation,next-state-version,omitempty"`
57+
PreviousStateVersion *StateVersion `jsonapi:"relation,previous-state-version,omitempty"`
58+
Workspace *Workspace `jsonapi:"relation,workspace"`
59+
}
60+
61+
func (s *stateVersions) Read(ctx context.Context, stateVersionID string) (*StateVersion, error) {
62+
if !validStringID(&stateVersionID) {
63+
return nil, errors.New("invalid value for state version ID")
64+
}
65+
66+
u := fmt.Sprintf("state-versions/%s", url.PathEscape(stateVersionID))
67+
req, err := s.client.newRequest("GET", u, nil)
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
sv := &StateVersion{}
73+
if err := s.client.do(ctx, req, sv); err != nil {
74+
return nil, err
75+
}
76+
77+
return sv, nil
78+
}
79+
80+
func (s *stateVersions) ReadCurrentForWorkspace(ctx context.Context, workspaceID string) (*StateVersion, error) {
81+
if !validStringID(&workspaceID) {
82+
return nil, errors.New("invalid value for workspace ID")
83+
}
84+
85+
u := fmt.Sprintf("workspaces/%s/current-state-version", url.PathEscape(workspaceID))
86+
req, err := s.client.newRequest("GET", u, nil)
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
sv := &StateVersion{}
92+
if err := s.client.do(ctx, req, sv); err != nil {
93+
return nil, err
94+
}
95+
96+
return sv, nil
97+
}

0 commit comments

Comments
 (0)
Please sign in to comment.