Skip to content

Commit f59bb70

Browse files
committed
feat: support noLocks and noVersioning with all state providers
1 parent b264059 commit f59bb70

File tree

3 files changed

+70
-18
lines changed

3 files changed

+70
-18
lines changed

state/gcp.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ import (
1919

2020
// GCP is a state provider type, leveraging GCS
2121
type GCP struct {
22-
svc *storage.Client
23-
buckets []string
22+
svc *storage.Client
23+
buckets []string
24+
noLocks bool
25+
noVersioning bool
2426
}
2527

2628
// NewGCP creates an GCP object
27-
func NewGCP(gcp config.GCPConfig) (*GCP, error) {
29+
func NewGCP(gcp config.GCPConfig, noLocks, noVersioning bool) (*GCP, error) {
2830
ctx := context.Background()
2931

3032
var client *storage.Client
@@ -52,8 +54,10 @@ func NewGCP(gcp config.GCPConfig) (*GCP, error) {
5254
}
5355

5456
gcpInstance = &GCP{
55-
svc: client,
56-
buckets: gcp.GCSBuckets,
57+
svc: client,
58+
buckets: gcp.GCSBuckets,
59+
noLocks: noLocks,
60+
noVersioning: noVersioning,
5761
}
5862

5963
log.WithFields(log.Fields{
@@ -67,7 +71,7 @@ func NewGCP(gcp config.GCPConfig) (*GCP, error) {
6771
func NewGCPCollection(c *config.Config) ([]*GCP, error) {
6872
var gcpInstances []*GCP
6973
for _, gcp := range c.GCP {
70-
gcpInstance, err := NewGCP(gcp)
74+
gcpInstance, err := NewGCP(gcp, c.Provider.NoLocks, c.Provider.NoVersioning)
7175
if err != nil || gcpInstance == nil {
7276
return nil, err
7377
}
@@ -79,6 +83,11 @@ func NewGCPCollection(c *config.Config) ([]*GCP, error) {
7983

8084
// GetLocks returns a map of locks by State path
8185
func (a *GCP) GetLocks() (locks map[string]LockInfo, err error) {
86+
if a.noLocks {
87+
locks = make(map[string]LockInfo)
88+
return
89+
}
90+
8291
ctx := context.Background()
8392
ctx, cancel := context.WithTimeout(ctx, time.Second*60)
8493
defer cancel()
@@ -167,7 +176,7 @@ func (a *GCP) GetState(st, versionID string) (sf *statefile.File, err error) {
167176
fileName := st[bucketSplit+1:]
168177

169178
obj := a.svc.Bucket(bucketName).Object(fileName)
170-
if versionID != "" {
179+
if versionID != "" && !a.noVersioning {
171180
version, err := strconv.ParseInt(versionID, 10, 64)
172181
if err != nil {
173182
return nil, err
@@ -205,6 +214,14 @@ func (a *GCP) GetState(st, versionID string) (sf *statefile.File, err error) {
205214

206215
// GetVersions returns a slice of Version objects
207216
func (a *GCP) GetVersions(state string) (versions []Version, err error) {
217+
if a.noVersioning {
218+
versions = append(versions, Version{
219+
ID: state,
220+
LastModified: time.Now(),
221+
})
222+
return
223+
}
224+
208225
versions = []Version{}
209226
ctx := context.Background()
210227
ctx, cancel := context.WithTimeout(ctx, time.Second*60)

state/gitlab.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/url"
77
"regexp"
88
"strconv"
9+
"time"
910

1011
"github.com/camptocamp/terraboard/config"
1112
"github.com/camptocamp/terraboard/internal/terraform/states/statefile"
@@ -14,18 +15,22 @@ import (
1415

1516
// Gitlab is a state provider type, leveraging GitLab
1617
type Gitlab struct {
17-
Client gitlab.Client
18+
Client gitlab.Client
19+
noLocks bool
20+
noVersioning bool
1821
}
1922

2023
// NewGitlab creates a new Gitlab object
21-
func NewGitlab(gl config.GitlabConfig) *Gitlab {
24+
func NewGitlab(gl config.GitlabConfig, noLocks, noVersioning bool) *Gitlab {
2225
var instance *Gitlab
2326
if gl.Token == "" {
2427
return nil
2528
}
2629

2730
instance = &Gitlab{
28-
Client: gitlab.NewClient(gl.Address, gl.Token),
31+
Client: gitlab.NewClient(gl.Address, gl.Token),
32+
noLocks: noLocks,
33+
noVersioning: noVersioning,
2934
}
3035
return instance
3136
}
@@ -34,7 +39,7 @@ func NewGitlab(gl config.GitlabConfig) *Gitlab {
3439
func NewGitlabCollection(c *config.Config) []*Gitlab {
3540
var gitlabInstances []*Gitlab
3641
for _, gitlab := range c.Gitlab {
37-
if glInstance := NewGitlab(gitlab); glInstance != nil {
42+
if glInstance := NewGitlab(gitlab, c.Provider.NoLocks, c.Provider.NoVersioning); glInstance != nil {
3843
gitlabInstances = append(gitlabInstances, glInstance)
3944
}
4045
}
@@ -44,6 +49,11 @@ func NewGitlabCollection(c *config.Config) []*Gitlab {
4449

4550
// GetLocks returns a map of locks by State path
4651
func (g *Gitlab) GetLocks() (locks map[string]LockInfo, err error) {
52+
if g.noLocks {
53+
locks = make(map[string]LockInfo)
54+
return
55+
}
56+
4757
locks = make(map[string]LockInfo)
4858
var projects gitlab.Projects
4959
projects, err = g.Client.GetProjectsWithTerraformStates()
@@ -89,6 +99,14 @@ func (g *Gitlab) GetStates() (states []string, err error) {
8999

90100
// GetVersions returns a slice of Version objects
91101
func (g *Gitlab) GetVersions(state string) (versions []Version, err error) {
102+
if g.noVersioning {
103+
versions = append(versions, Version{
104+
ID: state,
105+
LastModified: time.Now(),
106+
})
107+
return
108+
}
109+
92110
var projects gitlab.Projects
93111
projects, err = g.Client.GetProjectsWithTerraformStates()
94112
if err != nil {

state/tfe.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import (
1414
// TFE is a state provider type, leveraging Terraform Enterprise
1515
type TFE struct {
1616
*tfe.Client
17-
org string
18-
ctx *context.Context
17+
org string
18+
ctx *context.Context
19+
noLocks bool
20+
noVersioning bool
1921
}
2022

2123
// NewTFE creates a new TFE object
22-
func NewTFE(tfeObj config.TFEConfig) (*TFE, error) {
24+
func NewTFE(tfeObj config.TFEConfig, noLocks, noVersioning bool) (*TFE, error) {
2325
var tfeInstance *TFE
2426
if tfeObj.Token == "" {
2527
return nil, nil
@@ -37,9 +39,11 @@ func NewTFE(tfeObj config.TFEConfig) (*TFE, error) {
3739

3840
ctx := context.Background()
3941
tfeInstance = &TFE{
40-
Client: client,
41-
org: tfeObj.Organization,
42-
ctx: &ctx,
42+
Client: client,
43+
org: tfeObj.Organization,
44+
ctx: &ctx,
45+
noLocks: noLocks,
46+
noVersioning: noVersioning,
4347
}
4448

4549
return tfeInstance, nil
@@ -49,7 +53,7 @@ func NewTFE(tfeObj config.TFEConfig) (*TFE, error) {
4953
func NewTFECollection(c *config.Config) ([]*TFE, error) {
5054
var tfeInstances []*TFE
5155
for _, tfe := range c.TFE {
52-
tfeInstance, err := NewTFE(tfe)
56+
tfeInstance, err := NewTFE(tfe, c.Provider.NoLocks, c.Provider.NoVersioning)
5357
if err != nil || tfeInstance == nil {
5458
return nil, err
5559
}
@@ -61,6 +65,11 @@ func NewTFECollection(c *config.Config) ([]*TFE, error) {
6165

6266
// GetLocks returns a map of locks by State path
6367
func (t *TFE) GetLocks() (locks map[string]LockInfo, err error) {
68+
if t.noLocks {
69+
locks = make(map[string]LockInfo)
70+
return
71+
}
72+
6473
locks = make(map[string]LockInfo)
6574

6675
options := tfe.WorkspaceListOptions{
@@ -132,6 +141,14 @@ func (t *TFE) GetStates() (states []string, err error) {
132141

133142
// GetVersions returns a slice of Version objects
134143
func (t *TFE) GetVersions(state string) (versions []Version, err error) {
144+
if t.noVersioning {
145+
versions = append(versions, Version{
146+
ID: state,
147+
LastModified: time.Now(),
148+
})
149+
return
150+
}
151+
135152
options := tfe.StateVersionListOptions{
136153
ListOptions: tfe.ListOptions{
137154
PageNumber: 1,

0 commit comments

Comments
 (0)