Support global configuration

Previously, config information was stored per stack. With this change,
we now allow config values which apply to every stack a program may
target.

When passed without the `-s <stack>` argument, `pulumi config`
operates on the "global" configuration. Stack specific information can
be modified by passing an explicit stack.

Stack specific configuration overwrites global configuration.

Conside the following Pulumi.yaml:

```
name: hello-world
runtime: nodejs
description: a hello world program
config:
  hello-world:config:message Hello, from Pulumi
stacks:
  production:
    config:
      hello-world:config:message Hello, from Production
```

This program contains a single configuration value,
"hello-world:config:message" which has the value "Hello, from Pulumi"
when the program is activated into any stack except for "production"
where the value is "Hello, from Production".
This commit is contained in:
Matt Ellis 2017-10-17 14:19:47 -07:00
parent 9cf9428638
commit e361098941
3 changed files with 57 additions and 21 deletions

View file

@ -23,10 +23,7 @@ func newConfigCmd() *cobra.Command {
Use: "config [<key> [value]]",
Short: "Query, set, replace, or unset configuration values",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
stackName, err := explicitOrCurrent(stack)
if err != nil {
return err
}
stackName := tokens.QName(stack)
if len(args) == 0 {
return listConfig(stackName)
@ -50,7 +47,7 @@ func newConfigCmd() *cobra.Command {
cmd.PersistentFlags().StringVarP(
&stack, "stack", "s", "",
"Choose an stack other than the currently selected one")
"Target a specific stack instead of all stacks")
cmd.PersistentFlags().BoolVar(
&unset, "unset", false,
"Unset a configuration value")
@ -139,7 +136,11 @@ func getConfiguration(stackName tokens.QName) (map[tokens.ModuleMember]string, e
return nil, err
}
return pkg.Stacks[stackName].Config, nil
if stackName == "" {
return pkg.Config, nil
}
return mergeConfigs(pkg.Config, pkg.Stacks[stackName].Config), nil
}
func deleteConfiguration(stackName tokens.QName, key tokens.ModuleMember) error {
@ -148,8 +149,14 @@ func deleteConfiguration(stackName tokens.QName, key tokens.ModuleMember) error
return err
}
if pkg.Stacks[stackName].Config != nil {
delete(pkg.Stacks[stackName].Config, key)
if stackName == "" {
if pkg.Config != nil {
delete(pkg.Config, key)
}
} else {
if pkg.Stacks[stackName].Config != nil {
delete(pkg.Stacks[stackName].Config, key)
}
}
return savePackage(pkg)
@ -161,17 +168,46 @@ func setConfiguration(stackName tokens.QName, key tokens.ModuleMember, value str
return err
}
if pkg.Stacks == nil {
pkg.Stacks = make(map[tokens.QName]pack.StackInfo)
}
if stackName == "" {
if pkg.Config == nil {
pkg.Config = make(map[tokens.ModuleMember]string)
}
if pkg.Stacks[stackName].Config == nil {
si := pkg.Stacks[stackName]
si.Config = make(map[tokens.ModuleMember]string)
pkg.Stacks[stackName] = si
}
pkg.Config[key] = value
} else {
if pkg.Stacks == nil {
pkg.Stacks = make(map[tokens.QName]pack.StackInfo)
}
pkg.Stacks[stackName].Config[key] = value
if pkg.Stacks[stackName].Config == nil {
si := pkg.Stacks[stackName]
si.Config = make(map[tokens.ModuleMember]string)
pkg.Stacks[stackName] = si
}
pkg.Stacks[stackName].Config[key] = value
}
return savePackage(pkg)
}
func mergeConfigs(global, stack map[tokens.ModuleMember]string) map[tokens.ModuleMember]string {
if stack == nil {
return global
}
if global == nil {
return stack
}
merged := make(map[tokens.ModuleMember]string)
for key, value := range global {
merged[key] = value
}
for key, value := range stack {
merged[key] = value
}
return merged
}

View file

@ -26,14 +26,12 @@ func (p localStackProvider) GetTarget(name tokens.QName) (*deploy.Target, error)
return nil, err
}
pkg, err := getPackage()
config, err := getConfiguration(name)
if err != nil {
return nil, err
}
if pkg.Stacks != nil {
target.Config = pkg.Stacks[name].Config
}
target.Config = config
return target, err
}

View file

@ -33,6 +33,8 @@ type Package struct {
Analyzers *Analyzers `json:"analyzers,omitempty" yaml:"analyzers,omitempty"` // any analyzers enabled for this project.
Config map[tokens.ModuleMember]string `json:"config,omitempty" yaml:"config,omitempty"` // optional config that applies to all stacks.
Stacks map[tokens.QName]StackInfo `json:"stacks,omitempty" yaml:"stacks,omitempty"` // optional stack specific information.
Doc *diag.Document `json:"-" yaml:"-"` // the document from which this package came.