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 fogg apply/plan --no-plugins flag #151

Merged
merged 1 commit into from
Oct 12, 2018
Merged
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
4 changes: 2 additions & 2 deletions apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
const rootPath = "terraform"

// Apply will run a plan and apply all the changes to the current repo.
func Apply(fs afero.Fs, conf *config.Config, tmp *templates.T, upgrade bool) error {
func Apply(fs afero.Fs, conf *config.Config, tmp *templates.T, upgrade bool, noPlugins bool) error {
if !upgrade {
toolVersion, err := util.VersionString()
if err != nil {
Expand All @@ -39,7 +39,7 @@ func Apply(fs afero.Fs, conf *config.Config, tmp *templates.T, upgrade bool) err
return errs.NewUserf("fogg version (%s) is different than version currently used to manage repo (%s). To upgrade add --upgrade.", toolVersion, repoVersion)
}
}
p, err := plan.Eval(conf, false)
p, err := plan.Eval(conf, false, noPlugins)
if err != nil {
return errs.WrapUser(err, "unable to evaluate plan")
}
Expand Down
2 changes: 1 addition & 1 deletion apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestApplySmokeTest(t *testing.T) {
c, e := config.ReadConfig(ioutil.NopCloser(strings.NewReader(json)))
assert.Nil(t, e)

e = Apply(fs, c, templates.Templates, false)
e = Apply(fs, c, templates.Templates, false, false)
assert.Nil(t, e)
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func init() {
applyCmd.Flags().StringP("config", "c", "fogg.json", "Use this to override the fogg config file.")
applyCmd.Flags().BoolP("verbose", "v", false, "use this to turn on verbose output")
applyCmd.Flags().BoolP("upgrade", "u", false, "use this when running a new version of fogg")
applyCmd.Flags().Bool("no-plugins", false, "do not apply fogg plugins; this may result in unexpected behavior.")
rootCmd.AddCommand(applyCmd)
}

Expand Down Expand Up @@ -54,6 +55,11 @@ var applyCmd = &cobra.Command{
return e
}

noPlugins, e := cmd.Flags().GetBool("no-plugins")
if e != nil {
return e
}

// check that we are at root of initialized git repo
openGitOrExit(pwd)

Expand All @@ -65,7 +71,7 @@ var applyCmd = &cobra.Command{
}

// apply
e = apply.Apply(fs, config, templates.Templates, upgrade)
e = apply.Apply(fs, config, templates.Templates, upgrade, noPlugins)

return e
},
Expand Down
8 changes: 7 additions & 1 deletion cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func init() {
planCmd.Flags().StringP("config", "c", "fogg.json", "Use this to override the fogg config file.")
planCmd.Flags().BoolP("verbose", "v", false, "use this to turn on verbose output")
planCmd.Flags().Bool("no-plugins", false, "do not apply fogg plugins; this may result in unexpected behavior.")
rootCmd.AddCommand(planCmd)
}

Expand Down Expand Up @@ -49,6 +50,11 @@ var planCmd = &cobra.Command{
return errs.WrapInternal(e, "couldn't parse config flag")
}

noPlugins, e := cmd.Flags().GetBool("no-plugins")
if e != nil {
return e
}

// check that we are at root of initialized git repo
openGitOrExit(pwd)

Expand All @@ -59,7 +65,7 @@ var planCmd = &cobra.Command{
return e
}

p, e := plan.Eval(config, verbose)
p, e := plan.Eval(config, verbose, noPlugins)
if e != nil {
return e
}
Expand Down
8 changes: 5 additions & 3 deletions plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,17 @@ type Plan struct {
}

// Eval evaluats a config
func Eval(config *config.Config, verbose bool) (*Plan, error) {
func Eval(config *config.Config, verbose bool, noPlugins bool) (*Plan, error) {
p := &Plan{}
v, e := util.VersionString()
if e != nil {
return nil, errs.WrapInternal(e, "unable to parse fogg version")
}
p.Version = v
p.Plugins.SetCustomPluginsPlan(config.Plugins.CustomPlugins)
p.Plugins.SetTerraformProvidersPlan(config.Plugins.TerraformProviders)
if !noPlugins {
p.Plugins.SetCustomPluginsPlan(config.Plugins.CustomPlugins)
p.Plugins.SetTerraformProvidersPlan(config.Plugins.TerraformProviders)
}

var err error
p.Accounts = p.buildAccounts(config)
Expand Down
32 changes: 30 additions & 2 deletions plan/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/chanzuckerberg/fogg/config"
"github.com/chanzuckerberg/fogg/plugins"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -67,7 +68,7 @@ func TestPlanBasic(t *testing.T) {
c, err := config.ReadConfig(r)
assert.Nil(t, err)

plan, e := Eval(c, true)
plan, e := Eval(c, true, false)
assert.Nil(t, e)
assert.NotNil(t, plan)
assert.NotNil(t, plan.Accounts)
Expand All @@ -94,6 +95,33 @@ func TestPlanBasic(t *testing.T) {

assert.NotNil(t, plan.Envs["staging"].Components["comp1"])
assert.Equal(t, "0.100.0", plan.Envs["staging"].Components["comp1"].TerraformVersion)

assert.NotNil(t, plan.Plugins.CustomPlugins)
assert.Len(t, plan.Plugins.CustomPlugins, 1)
assert.NotNil(t, plan.Plugins.CustomPlugins["custom"])
assert.Equal(t, plugins.TypePluginFormatZip, plan.Plugins.CustomPlugins["custom"].Format)
assert.Equal(t, "https://example.com/custom.zip", plan.Plugins.CustomPlugins["custom"].URL)

assert.NotNil(t, plan.Plugins.TerraformProviders)
assert.Len(t, plan.Plugins.TerraformProviders, 1)
assert.NotNil(t, plan.Plugins.TerraformProviders["provider"])
assert.Equal(t, plugins.TypePluginFormatTar, plan.Plugins.TerraformProviders["provider"].Format)
assert.Equal(t, "https://example.com/provider.tar.gz", plan.Plugins.TerraformProviders["provider"].URL)
}

func TestPlanNoPlugins(t *testing.T) {
f, _ := os.Open("testdata/full.json")
defer f.Close()
r := bufio.NewReader(f)
c, err := config.ReadConfig(r)
assert.Nil(t, err)

plan, e := Eval(c, true, true)
assert.Nil(t, e)
assert.NotNil(t, plan)

assert.Nil(t, plan.Plugins.CustomPlugins)
assert.Nil(t, plan.Plugins.TerraformProviders)
}

func TestExtraVarsComposition(t *testing.T) {
Expand All @@ -103,7 +131,7 @@ func TestExtraVarsComposition(t *testing.T) {
c, err := config.ReadConfig(r)
assert.Nil(t, err)

plan, e := Eval(c, true)
plan, e := Eval(c, true, false)
assert.Nil(t, e)
assert.NotNil(t, plan)

Expand Down
16 changes: 15 additions & 1 deletion plan/testdata/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"components": {
"vpc": {
"module_source": "github.com/terraform-aws-modules/terraform-aws-vpc?ref=v1.30.0",
"module_source": "github.com/terraform-aws-modules/terraform-aws-vpc?ref=v1.30.0",
"extra_vars": {
"foo": "bar3"
}
Expand All @@ -38,5 +38,19 @@
}
},
"prod": {}
},
"plugins": {
"terraform_providers": {
"provider": {
"url": "https://example.com/provider.tar.gz",
"format": "tar"
}
},
"custom_plugins": {
"custom": {
"url": "https://example.com/custom.zip",
"format": "zip"
}
}
}
}