Skip to content

Commit

Permalink
Fixes merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed May 13, 2022
2 parents 79ac964 + 3b0d5c0 commit d23901f
Show file tree
Hide file tree
Showing 54 changed files with 2,746 additions and 416 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Build
on: pull_request
jobs:
build:
strategy:
matrix:
os:
- ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "^1.17"
- name: Set up environment
run: echo "GOVERSION=$(go version)" >> $GITHUB_ENV
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: build --snapshot --rm-dist
61 changes: 61 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Release
on:
push:
branches:
- main
jobs:
tag:
runs-on: ubuntu-latest
outputs:
new-tag: ${{ steps.bump-tag.outputs.new }}
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Configure Git
run: |
git config --global user.name "$GITHUB_ACTOR"
git config --global user.email "[email protected]"
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "^1.17"
- name: Install ccv
run: >
curl -sSL https://github.com/smlx/ccv/releases/download/v0.3.2/ccv_0.3.2_linux_amd64.tar.gz
| sudo tar -xz -C /usr/local/bin ccv
- name: Bump tag if necessary
id: bump-tag
run: |
if [ -z $(git tag -l $(ccv)) ]; then
git tag $(ccv)
git push --tags
echo "::set-output name=new::true"
fi
release:
needs: tag
if: needs.tag.outputs.new-tag == 'true'
strategy:
matrix:
os:
- ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "^1.17"
- name: Set up environment
run: echo "GOVERSION=$(go version)" >> $GITHUB_ENV
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28 changes: 28 additions & 0 deletions .github/workflows/tag-to-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Tag to Release
on:
push:
tags:
- v*
jobs:
release:
strategy:
matrix:
os:
- ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "^1.17"
- name: Set up environment
run: echo "GOVERSION=$(go version)" >> $GITHUB_ENV
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
14 changes: 14 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Test
on: pull_request
jobs:
go-test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: "^1.17"
- name: Run Tests
run: go test -v ./...
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.idea
cmd/test-resources/template-routes/output
8 changes: 8 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
builds:
- dir: .
ldflags:
- >
-s -w -X main.date={{.Date}} -X "main.goVersion={{.Env.GOVERSION}}"
-X main.shortCommit={{.ShortCommit}} -X main.version={{.Version}}
env:
- CGO_ENABLED=0
52 changes: 34 additions & 18 deletions cmd/config_fastly.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,17 @@ import (
"github.com/uselagoon/build-deploy-tool/internal/lagoon"
)

var domainName string
var (
domainName string
)

var fastlyConfigGeneration = &cobra.Command{
Use: "fastly",
Aliases: []string{"f"},
Short: "Generate fastly configuration for a specific ingress domain",
RunE: func(cmd *cobra.Command, args []string) error {
// environment variables will override what is provided by flags
lagoonFastlyCacheNoCahce = helpers.GetEnv("LAGOON_FASTLY_NOCACHE_SERVICE_ID", lagoonFastlyCacheNoCahce, true)
lagoonFastlyServiceID = helpers.GetEnv("ROUTE_FASTLY_SERVICE_ID", lagoonFastlyServiceID, true)

// get the project and environment variables
projectVariables = helpers.GetEnv("LAGOON_PROJECT_VARIABLES", projectVariables, true)
environmentVariables = helpers.GetEnv("LAGOON_ENVIRONMENT_VARIABLES", environmentVariables, true)

// unmarshal and then merge the two so there is only 1 set of variables to iterate over
projectVars := []lagoon.EnvironmentVariable{}
envVars := []lagoon.EnvironmentVariable{}
json.Unmarshal([]byte(projectVariables), &projectVars)
json.Unmarshal([]byte(environmentVariables), &envVars)
lagoonEnvVars := lagoon.MergeVariables(projectVars, envVars)

// generate the fastly configuration from the provided flags/variables
f, err := lagoon.GenerateFastlyConfiguration(lagoonFastlyCacheNoCahce, lagoonFastlyServiceID, domainName, lagoonEnvVars)
f, err := FastlyConfigGeneration(false, domainName)
if err != nil {
return err
}
Expand All @@ -42,6 +29,33 @@ var fastlyConfigGeneration = &cobra.Command{
},
}

// FastlyConfigGeneration .
func FastlyConfigGeneration(debug bool, domain string) (lagoon.Fastly, error) {
// environment variables will override what is provided by flags
fastlyCacheNoCahce = helpers.GetEnv("LAGOON_FASTLY_NOCACHE_SERVICE_ID", fastlyCacheNoCahce, debug)
fastlyServiceID = helpers.GetEnv("ROUTE_FASTLY_SERVICE_ID", fastlyServiceID, debug)
fastlyAPISecretPrefix = helpers.GetEnv("FASTLY_API_SECRET_PREFIX", fastlyAPISecretPrefix, debug)

// get the project and environment variables
projectVariables = helpers.GetEnv("LAGOON_PROJECT_VARIABLES", projectVariables, debug)
environmentVariables = helpers.GetEnv("LAGOON_ENVIRONMENT_VARIABLES", environmentVariables, debug)

// unmarshal and then merge the two so there is only 1 set of variables to iterate over
projectVars := []lagoon.EnvironmentVariable{}
envVars := []lagoon.EnvironmentVariable{}
json.Unmarshal([]byte(projectVariables), &projectVars)
json.Unmarshal([]byte(environmentVariables), &envVars)
lagoonEnvVars := lagoon.MergeVariables(projectVars, envVars)

// generate the fastly configuration from the provided flags/variables
f := &lagoon.Fastly{}
err := lagoon.GenerateFastlyConfiguration(f, fastlyCacheNoCahce, fastlyServiceID, domain, fastlyAPISecretPrefix, lagoonEnvVars)
if err != nil {
return lagoon.Fastly{}, err
}
return *f, nil
}

func init() {
configCmd.AddCommand(fastlyConfigGeneration)
fastlyConfigGeneration.Flags().StringVarP(&domainName, "domain", "D", "",
Expand All @@ -50,8 +64,10 @@ func init() {
"The projects environment variables JSON payload")
fastlyConfigGeneration.Flags().StringVarP(&environmentVariables, "environment-variables", "V", "",
"The environments environment variables JSON payload")
fastlyConfigGeneration.Flags().StringVarP(&lagoonFastlyCacheNoCahce, "fastly-cache-no-cache-id", "F", "",
fastlyConfigGeneration.Flags().StringVarP(&fastlyCacheNoCahce, "fastly-cache-no-cache-id", "F", "",
"The fastly cache no cache service ID to use")
fastlyConfigGeneration.Flags().StringVarP(&lagoonYml, "lagoon-yml", "l", ".lagoon.yml",
"The .lagoon.yml file to read")
fastlyConfigGeneration.Flags().StringVarP(&fastlyServiceID, "fastly-service-id", "f", "",
"The fastly service ID to use")
}
154 changes: 154 additions & 0 deletions cmd/config_fastly_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package cmd

import (
"os"
"reflect"
"testing"

"github.com/uselagoon/build-deploy-tool/internal/lagoon"
)

func TestGenerateFastlyConfig(t *testing.T) {
type args struct {
projectVars string
envVars string
cacheNoCache string
serviceID string
domain string
secretPrefix string
}
tests := []struct {
name string
args args
want lagoon.Fastly
}{
{
name: "test1 check LAGOON_FASTLY_SERVICE_ID no secret",
args: args{
projectVars: `[{"name":"LAGOON_FASTLY_SERVICE_ID","value":"service-id:true","scope":"global"}]`,
envVars: `[]`,
cacheNoCache: "",
serviceID: "",
domain: "example.com",
secretPrefix: "fastly-api-",
},
want: lagoon.Fastly{
ServiceID: "service-id",
APISecretName: "",
Watch: true,
},
},
{
name: "test2 check LAGOON_FASTLY_SERVICE_IDS no secret",
args: args{
projectVars: `[{"name":"LAGOON_FASTLY_SERVICE_IDS","value":"example.com:service-id:true","scope":"global"}]`,
envVars: `[]`,
cacheNoCache: "",
serviceID: "",
domain: "example.com",
secretPrefix: "fastly-api-",
},
want: lagoon.Fastly{
ServiceID: "service-id",
APISecretName: "",
Watch: true,
},
},
{
name: "test3 check LAGOON_FASTLY_SERVICE_ID with secret",
args: args{
projectVars: `[{"name":"LAGOON_FASTLY_SERVICE_ID","value":"service-id:true:secret","scope":"global"}]`,
envVars: `[]`,
cacheNoCache: "",
serviceID: "",
domain: "example.com",
secretPrefix: "fastly-api-",
},
want: lagoon.Fastly{
ServiceID: "service-id",
APISecretName: "fastly-api-secret",
Watch: true,
},
},
{
name: "test4 check LAGOON_FASTLY_SERVICE_IDS with secret",
args: args{
projectVars: `[{"name":"LAGOON_FASTLY_SERVICE_IDS","value":"example.com:service-id:true:secret","scope":"global"}]`,
envVars: `[]`,
cacheNoCache: "",
serviceID: "",
domain: "example.com",
secretPrefix: "fastly-api-",
},
want: lagoon.Fastly{
ServiceID: "service-id",
APISecretName: "fastly-api-secret",
Watch: true,
},
},
{
name: "test5 check no LAGOON_FASTLY_SERVICE_ID with service id found from ROUTE_FASTLY_SERVICE_ID",
args: args{
projectVars: `[]`,
envVars: `[]`,
cacheNoCache: "",
serviceID: "dedicated-service-id",
domain: "example.com",
secretPrefix: "fastly-api-",
},
want: lagoon.Fastly{
ServiceID: "dedicated-service-id",
Watch: false,
},
},
{
name: "test6 check LAGOON_FASTLY_SERVICE_ID with service id found from ROUTE_FASTLY_SERVICE_ID (should use one from LAGOON_FASTLY_SERVICE_ID)",
args: args{
projectVars: `[{"name":"LAGOON_FASTLY_SERVICE_ID","value":"service-id:true","scope":"global"}]`,
envVars: `[]`,
cacheNoCache: "",
serviceID: "dedicated-service-id",
domain: "example.com",
secretPrefix: "fastly-api-",
},
want: lagoon.Fastly{
ServiceID: "service-id",
Watch: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// set the environment variables from args
err := os.Setenv("LAGOON_FASTLY_NOCACHE_SERVICE_ID", tt.args.cacheNoCache)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("ROUTE_FASTLY_SERVICE_ID", tt.args.serviceID)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("FASTLY_API_SECRET_PREFIX", tt.args.secretPrefix)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("LAGOON_PROJECT_VARIABLES", tt.args.projectVars)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("LAGOON_ENVIRONMENT_VARIABLES", tt.args.envVars)
if err != nil {
t.Errorf("%v", err)
}

// generate the fastly configuration from the provided flags/variables
got, err := FastlyConfigGeneration(false, tt.args.domain)
if err != nil {
t.Errorf("%v", err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("fastlyConfigGeneration() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit d23901f

Please sign in to comment.