Skip to content

Commit

Permalink
Adding support for log persistence to global_config (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aayyush committed Mar 22, 2022
1 parent 5288389 commit 4a6bf94
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/core/config/raw/global_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type GlobalCfg struct {
Workflows map[string]Workflow `yaml:"workflows" json:"workflows"`
PolicySets PolicySets `yaml:"policies" json:"policies"`
Metrics Metrics `yaml:"metrics" json:"metrics"`
Jobs Jobs `yaml:"jobs" json:"jobs"`
}

// Repo is the raw schema for repos in the server-side repo config.
Expand All @@ -37,6 +38,7 @@ func (g GlobalCfg) Validate() error {
validation.Field(&g.Repos),
validation.Field(&g.Workflows),
validation.Field(&g.Metrics),
validation.Field(&g.Jobs),
)
if err != nil {
return err
Expand Down Expand Up @@ -133,6 +135,7 @@ func (g GlobalCfg) ToValid(defaultCfg valid.GlobalCfg) valid.GlobalCfg {
Workflows: workflows,
PolicySets: g.PolicySets.ToValid(),
Metrics: g.Metrics.ToValid(),
Jobs: g.Jobs.ToValid(),
}
}

Expand Down
50 changes: 50 additions & 0 deletions server/core/config/raw/jobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package raw

import (
validation "github.com/go-ozzo/ozzo-validation"
"github.com/runatlantis/atlantis/server/core/config/valid"
)

type Jobs struct {
StorageBackend *StorageBackend `yaml:"storage-backend" json:"storage-backend"`
}

type StorageBackend struct {
S3 *S3 `yaml:"s3" json:"s3"`
}

type S3 struct {
BucketName string `yaml:"bucket-name" json:"bucket-name"`
}

func (j Jobs) Validate() error {
return validation.ValidateStruct(&j,
validation.Field(&j.StorageBackend),
)
}

func (s StorageBackend) Validate() error {
return validation.ValidateStruct(&s,
validation.Field(&s.S3),
)
}

func (s S3) Validate() error {
return validation.ValidateStruct(&s,
validation.Field(&s.BucketName, validation.Required),
)
}

func (j *Jobs) ToValid() valid.Jobs {
if j.StorageBackend != nil {
return valid.Jobs{
StorageBackend: &valid.StorageBackend{
S3: &valid.S3{
BucketName: j.StorageBackend.S3.BucketName,
},
},
}
}

return valid.Jobs{}
}
89 changes: 89 additions & 0 deletions server/core/config/raw/jobs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package raw_test

import (
"encoding/json"
"testing"

"github.com/runatlantis/atlantis/server/core/config/raw"
"github.com/stretchr/testify/assert"
yaml "gopkg.in/yaml.v2"
)

func TestJobs_Unmarshal(t *testing.T) {
t.Run("yaml", func(t *testing.T) {

rawYaml := `
storage-backend:
s3:
bucket-name: atlantis-test
`

var result raw.Jobs

err := yaml.UnmarshalStrict([]byte(rawYaml), &result)
assert.NoError(t, err)
})

t.Run("json", func(t *testing.T) {
rawJSON := `
{
"storage-backend": {
"s3": {
"bucket-name": "atlantis-test"
}
}
}
`

var result raw.Jobs

err := json.Unmarshal([]byte(rawJSON), &result)
assert.NoError(t, err)
})
}

func TestJobs_Validate_Success(t *testing.T) {
cases := []struct {
description string
subject raw.Jobs
}{
{
description: "success",
subject: raw.Jobs{
StorageBackend: &raw.StorageBackend{
S3: &raw.S3{
BucketName: "test-bucket",
},
},
},
},
}

for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
assert.NoError(t, c.subject.Validate())
})
}
}

func TestJobs_ValidateError(t *testing.T) {
cases := []struct {
description string
subject raw.Jobs
}{
{
description: "bucket name not specified",
subject: raw.Jobs{
StorageBackend: &raw.StorageBackend{
S3: &raw.S3{},
},
},
},
}

for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
assert.Error(t, c.subject.Validate())
})
}
}
13 changes: 13 additions & 0 deletions server/core/config/valid/global_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ type GlobalCfg struct {
Workflows map[string]Workflow
PolicySets PolicySets
Metrics Metrics
Jobs Jobs
}

type Jobs struct {
StorageBackend *StorageBackend
}

type StorageBackend struct {
S3 *S3
}

type S3 struct {
BucketName string
}

type Metrics struct {
Expand Down

0 comments on commit 4a6bf94

Please sign in to comment.