github.com/adamar/terraform@v0.2.2-0.20141016210445-2e703afdad0e/config/config.go (about) 1 // The config package is responsible for loading and validating the 2 // configuration. 3 package config 4 5 import ( 6 "fmt" 7 "regexp" 8 "strconv" 9 "strings" 10 11 "github.com/hashicorp/terraform/flatmap" 12 "github.com/hashicorp/terraform/helper/multierror" 13 "github.com/mitchellh/mapstructure" 14 "github.com/mitchellh/reflectwalk" 15 ) 16 17 // NameRegexp is the regular expression that all names (modules, providers, 18 // resources, etc.) must follow. 19 var NameRegexp = regexp.MustCompile(`\A[A-Za-z0-9\-\_]+\z`) 20 21 // Config is the configuration that comes from loading a collection 22 // of Terraform templates. 23 type Config struct { 24 // Dir is the path to the directory where this configuration was 25 // loaded from. If it is blank, this configuration wasn't loaded from 26 // any meaningful directory. 27 Dir string 28 29 Modules []*Module 30 ProviderConfigs []*ProviderConfig 31 Resources []*Resource 32 Variables []*Variable 33 Outputs []*Output 34 35 // The fields below can be filled in by loaders for validation 36 // purposes. 37 unknownKeys []string 38 } 39 40 // Module is a module used within a configuration. 41 // 42 // This does not represent a module itself, this represents a module 43 // call-site within an existing configuration. 44 type Module struct { 45 Name string 46 Source string 47 RawConfig *RawConfig 48 } 49 50 // ProviderConfig is the configuration for a resource provider. 51 // 52 // For example, Terraform needs to set the AWS access keys for the AWS 53 // resource provider. 54 type ProviderConfig struct { 55 Name string 56 RawConfig *RawConfig 57 } 58 59 // A resource represents a single Terraform resource in the configuration. 60 // A Terraform resource is something that represents some component that 61 // can be created and managed, and has some properties associated with it. 62 type Resource struct { 63 Name string 64 Type string 65 RawCount *RawConfig 66 RawConfig *RawConfig 67 Provisioners []*Provisioner 68 DependsOn []string 69 Lifecycle ResourceLifecycle 70 } 71 72 // ResourceLifecycle is used to store the lifecycle tuning parameters 73 // to allow customized behavior 74 type ResourceLifecycle struct { 75 CreateBeforeDestroy bool `hcl:"create_before_destroy"` 76 } 77 78 // Provisioner is a configured provisioner step on a resource. 79 type Provisioner struct { 80 Type string 81 RawConfig *RawConfig 82 ConnInfo *RawConfig 83 } 84 85 // Variable is a variable defined within the configuration. 86 type Variable struct { 87 Name string 88 Default interface{} 89 Description string 90 } 91 92 // Output is an output defined within the configuration. An output is 93 // resulting data that is highlighted by Terraform when finished. 94 type Output struct { 95 Name string 96 RawConfig *RawConfig 97 } 98 99 // VariableType is the type of value a variable is holding, and returned 100 // by the Type() function on variables. 101 type VariableType byte 102 103 const ( 104 VariableTypeUnknown VariableType = iota 105 VariableTypeString 106 VariableTypeMap 107 ) 108 109 // ProviderConfigName returns the name of the provider configuration in 110 // the given mapping that maps to the proper provider configuration 111 // for this resource. 112 func ProviderConfigName(t string, pcs []*ProviderConfig) string { 113 lk := "" 114 for _, v := range pcs { 115 k := v.Name 116 if strings.HasPrefix(t, k) && len(k) > len(lk) { 117 lk = k 118 } 119 } 120 121 return lk 122 } 123 124 // A unique identifier for this module. 125 func (r *Module) Id() string { 126 return fmt.Sprintf("%s", r.Name) 127 } 128 129 // Count returns the count of this resource. 130 func (r *Resource) Count() (int, error) { 131 v, err := strconv.ParseInt(r.RawCount.Value().(string), 0, 0) 132 if err != nil { 133 return 0, err 134 } 135 136 return int(v), nil 137 } 138 139 // A unique identifier for this resource. 140 func (r *Resource) Id() string { 141 return fmt.Sprintf("%s.%s", r.Type, r.Name) 142 } 143 144 // Validate does some basic semantic checking of the configuration. 145 func (c *Config) Validate() error { 146 if c == nil { 147 return nil 148 } 149 150 var errs []error 151 152 for _, k := range c.unknownKeys { 153 errs = append(errs, fmt.Errorf( 154 "Unknown root level key: %s", k)) 155 } 156 157 vars := c.InterpolatedVariables() 158 varMap := make(map[string]*Variable) 159 for _, v := range c.Variables { 160 varMap[v.Name] = v 161 } 162 163 for _, v := range c.Variables { 164 if v.Type() == VariableTypeUnknown { 165 errs = append(errs, fmt.Errorf( 166 "Variable '%s': must be string or mapping", 167 v.Name)) 168 continue 169 } 170 171 interp := false 172 fn := func(i Interpolation) (string, error) { 173 interp = true 174 return "", nil 175 } 176 177 w := &interpolationWalker{F: fn} 178 if v.Default != nil { 179 if err := reflectwalk.Walk(v.Default, w); err == nil { 180 if interp { 181 errs = append(errs, fmt.Errorf( 182 "Variable '%s': cannot contain interpolations", 183 v.Name)) 184 } 185 } 186 } 187 } 188 189 // Check for references to user variables that do not actually 190 // exist and record those errors. 191 for source, vs := range vars { 192 for _, v := range vs { 193 uv, ok := v.(*UserVariable) 194 if !ok { 195 continue 196 } 197 198 if _, ok := varMap[uv.Name]; !ok { 199 errs = append(errs, fmt.Errorf( 200 "%s: unknown variable referenced: %s", 201 source, 202 uv.Name)) 203 } 204 } 205 } 206 207 // Check that all count variables are valid. 208 for source, vs := range vars { 209 for _, rawV := range vs { 210 switch v := rawV.(type) { 211 case *CountVariable: 212 if v.Type == CountValueInvalid { 213 errs = append(errs, fmt.Errorf( 214 "%s: invalid count variable: %s", 215 source, 216 v.FullKey())) 217 } 218 case *PathVariable: 219 if v.Type == PathValueInvalid { 220 errs = append(errs, fmt.Errorf( 221 "%s: invalid path variable: %s", 222 source, 223 v.FullKey())) 224 } 225 } 226 } 227 } 228 229 // Check that all references to modules are valid 230 modules := make(map[string]*Module) 231 dupped := make(map[string]struct{}) 232 for _, m := range c.Modules { 233 // Check for duplicates 234 if _, ok := modules[m.Id()]; ok { 235 if _, ok := dupped[m.Id()]; !ok { 236 dupped[m.Id()] = struct{}{} 237 238 errs = append(errs, fmt.Errorf( 239 "%s: module repeated multiple times", 240 m.Id())) 241 } 242 } 243 244 if _, ok := modules[m.Id()]; !ok { 245 // If we haven't seen this module before, check that the 246 // source has no interpolations. 247 rc, err := NewRawConfig(map[string]interface{}{ 248 "root": m.Source, 249 }) 250 if err != nil { 251 errs = append(errs, fmt.Errorf( 252 "%s: module source error: %s", 253 m.Id(), err)) 254 } else if len(rc.Interpolations) > 0 { 255 errs = append(errs, fmt.Errorf( 256 "%s: module source cannot contain interpolations", 257 m.Id())) 258 } 259 260 // Check that the name matches our regexp 261 if !NameRegexp.Match([]byte(m.Name)) { 262 errs = append(errs, fmt.Errorf( 263 "%s: module name can only contain letters, numbers, "+ 264 "dashes, and underscores", 265 m.Id())) 266 } 267 } 268 269 modules[m.Id()] = m 270 } 271 dupped = nil 272 273 // Check that all variables for modules reference modules that 274 // exist. 275 for source, vs := range vars { 276 for _, v := range vs { 277 mv, ok := v.(*ModuleVariable) 278 if !ok { 279 continue 280 } 281 282 if _, ok := modules[mv.Name]; !ok { 283 errs = append(errs, fmt.Errorf( 284 "%s: unknown module referenced: %s", 285 source, 286 mv.Name)) 287 } 288 } 289 } 290 291 // Check that all references to resources are valid 292 resources := make(map[string]*Resource) 293 dupped = make(map[string]struct{}) 294 for _, r := range c.Resources { 295 if _, ok := resources[r.Id()]; ok { 296 if _, ok := dupped[r.Id()]; !ok { 297 dupped[r.Id()] = struct{}{} 298 299 errs = append(errs, fmt.Errorf( 300 "%s: resource repeated multiple times", 301 r.Id())) 302 } 303 } 304 305 resources[r.Id()] = r 306 } 307 dupped = nil 308 309 // Validate resources 310 for n, r := range resources { 311 // Verify count variables 312 for _, v := range r.RawCount.Variables { 313 switch v.(type) { 314 case *CountVariable: 315 errs = append(errs, fmt.Errorf( 316 "%s: resource count can't reference count variable: %s", 317 n, 318 v.FullKey())) 319 case *ModuleVariable: 320 errs = append(errs, fmt.Errorf( 321 "%s: resource count can't reference module variable: %s", 322 n, 323 v.FullKey())) 324 case *ResourceVariable: 325 errs = append(errs, fmt.Errorf( 326 "%s: resource count can't reference resource variable: %s", 327 n, 328 v.FullKey())) 329 case *UserVariable: 330 // Good 331 default: 332 panic("Unknown type in count var: " + n) 333 } 334 } 335 336 // Interpolate with a fixed number to verify that its a number 337 r.RawCount.interpolate(func(Interpolation) (string, error) { 338 return "5", nil 339 }) 340 _, err := strconv.ParseInt(r.RawCount.Value().(string), 0, 0) 341 if err != nil { 342 errs = append(errs, fmt.Errorf( 343 "%s: resource count must be an integer", 344 n)) 345 } 346 r.RawCount.init() 347 348 for _, d := range r.DependsOn { 349 if _, ok := resources[d]; !ok { 350 errs = append(errs, fmt.Errorf( 351 "%s: resource depends on non-existent resource '%s'", 352 n, d)) 353 } 354 } 355 } 356 357 for source, vs := range vars { 358 for _, v := range vs { 359 rv, ok := v.(*ResourceVariable) 360 if !ok { 361 continue 362 } 363 364 id := fmt.Sprintf("%s.%s", rv.Type, rv.Name) 365 if _, ok := resources[id]; !ok { 366 errs = append(errs, fmt.Errorf( 367 "%s: unknown resource '%s' referenced in variable %s", 368 source, 369 id, 370 rv.FullKey())) 371 continue 372 } 373 } 374 } 375 376 // Check that all outputs are valid 377 for _, o := range c.Outputs { 378 invalid := false 379 for k, _ := range o.RawConfig.Raw { 380 if k != "value" { 381 invalid = true 382 break 383 } 384 } 385 if invalid { 386 errs = append(errs, fmt.Errorf( 387 "%s: output should only have 'value' field", o.Name)) 388 } 389 } 390 391 // Check that all variables are in the proper context 392 for source, rc := range c.rawConfigs() { 393 walker := &interpolationWalker{ 394 ContextF: c.validateVarContextFn(source, &errs), 395 } 396 if err := reflectwalk.Walk(rc.Raw, walker); err != nil { 397 errs = append(errs, fmt.Errorf( 398 "%s: error reading config: %s", source, err)) 399 } 400 } 401 402 if len(errs) > 0 { 403 return &multierror.Error{Errors: errs} 404 } 405 406 return nil 407 } 408 409 // InterpolatedVariables is a helper that returns a mapping of all the interpolated 410 // variables within the configuration. This is used to verify references 411 // are valid in the Validate step. 412 func (c *Config) InterpolatedVariables() map[string][]InterpolatedVariable { 413 result := make(map[string][]InterpolatedVariable) 414 for source, rc := range c.rawConfigs() { 415 for _, v := range rc.Variables { 416 result[source] = append(result[source], v) 417 } 418 } 419 return result 420 } 421 422 // rawConfigs returns all of the RawConfigs that are available keyed by 423 // a human-friendly source. 424 func (c *Config) rawConfigs() map[string]*RawConfig { 425 result := make(map[string]*RawConfig) 426 for _, pc := range c.ProviderConfigs { 427 source := fmt.Sprintf("provider config '%s'", pc.Name) 428 result[source] = pc.RawConfig 429 } 430 431 for _, rc := range c.Resources { 432 source := fmt.Sprintf("resource '%s'", rc.Id()) 433 result[source+" count"] = rc.RawCount 434 result[source+" config"] = rc.RawConfig 435 } 436 437 for _, o := range c.Outputs { 438 source := fmt.Sprintf("output '%s'", o.Name) 439 result[source] = o.RawConfig 440 } 441 442 return result 443 } 444 445 func (c *Config) validateVarContextFn( 446 source string, errs *[]error) interpolationWalkerContextFunc { 447 return func(loc reflectwalk.Location, i Interpolation) { 448 vi, ok := i.(*VariableInterpolation) 449 if !ok { 450 return 451 } 452 453 rv, ok := vi.Variable.(*ResourceVariable) 454 if !ok { 455 return 456 } 457 458 if rv.Multi && rv.Index == -1 && loc != reflectwalk.SliceElem { 459 *errs = append(*errs, fmt.Errorf( 460 "%s: multi-variable must be in a slice", source)) 461 } 462 } 463 } 464 465 func (m *Module) mergerName() string { 466 return m.Id() 467 } 468 469 func (m *Module) mergerMerge(other merger) merger { 470 m2 := other.(*Module) 471 472 result := *m 473 result.Name = m2.Name 474 result.RawConfig = result.RawConfig.merge(m2.RawConfig) 475 476 if m2.Source != "" { 477 result.Source = m2.Source 478 } 479 480 return &result 481 } 482 483 func (o *Output) mergerName() string { 484 return o.Name 485 } 486 487 func (o *Output) mergerMerge(m merger) merger { 488 o2 := m.(*Output) 489 490 result := *o 491 result.Name = o2.Name 492 result.RawConfig = result.RawConfig.merge(o2.RawConfig) 493 494 return &result 495 } 496 497 func (c *ProviderConfig) mergerName() string { 498 return c.Name 499 } 500 501 func (c *ProviderConfig) mergerMerge(m merger) merger { 502 c2 := m.(*ProviderConfig) 503 504 result := *c 505 result.Name = c2.Name 506 result.RawConfig = result.RawConfig.merge(c2.RawConfig) 507 508 return &result 509 } 510 511 func (r *Resource) mergerName() string { 512 return fmt.Sprintf("%s.%s", r.Type, r.Name) 513 } 514 515 func (r *Resource) mergerMerge(m merger) merger { 516 r2 := m.(*Resource) 517 518 result := *r 519 result.Name = r2.Name 520 result.Type = r2.Type 521 result.RawConfig = result.RawConfig.merge(r2.RawConfig) 522 523 if r2.RawCount.Value() != "1" { 524 result.RawCount = r2.RawCount 525 } 526 527 if len(r2.Provisioners) > 0 { 528 result.Provisioners = r2.Provisioners 529 } 530 531 return &result 532 } 533 534 // DefaultsMap returns a map of default values for this variable. 535 func (v *Variable) DefaultsMap() map[string]string { 536 if v.Default == nil { 537 return nil 538 } 539 540 n := fmt.Sprintf("var.%s", v.Name) 541 switch v.Type() { 542 case VariableTypeString: 543 return map[string]string{n: v.Default.(string)} 544 case VariableTypeMap: 545 result := flatmap.Flatten(map[string]interface{}{ 546 n: v.Default.(map[string]string), 547 }) 548 result[n] = v.Name 549 550 return result 551 default: 552 return nil 553 } 554 } 555 556 // Merge merges two variables to create a new third variable. 557 func (v *Variable) Merge(v2 *Variable) *Variable { 558 // Shallow copy the variable 559 result := *v 560 561 // The names should be the same, but the second name always wins. 562 result.Name = v2.Name 563 564 if v2.Default != nil { 565 result.Default = v2.Default 566 } 567 if v2.Description != "" { 568 result.Description = v2.Description 569 } 570 571 return &result 572 } 573 574 // Type returns the type of varialbe this is. 575 func (v *Variable) Type() VariableType { 576 if v.Default == nil { 577 return VariableTypeString 578 } 579 580 var strVal string 581 if err := mapstructure.WeakDecode(v.Default, &strVal); err == nil { 582 v.Default = strVal 583 return VariableTypeString 584 } 585 586 var m map[string]string 587 if err := mapstructure.WeakDecode(v.Default, &m); err == nil { 588 v.Default = m 589 return VariableTypeMap 590 } 591 592 return VariableTypeUnknown 593 } 594 595 func (v *Variable) mergerName() string { 596 return v.Name 597 } 598 599 func (v *Variable) mergerMerge(m merger) merger { 600 return v.Merge(m.(*Variable)) 601 } 602 603 // Required tests whether a variable is required or not. 604 func (v *Variable) Required() bool { 605 return v.Default == nil 606 }