aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/terraform/helper/resource
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hashicorp/terraform/helper/resource')
-rw-r--r--vendor/github.com/hashicorp/terraform/helper/resource/id.go33
-rw-r--r--vendor/github.com/hashicorp/terraform/helper/resource/testing.go44
2 files changed, 49 insertions, 28 deletions
diff --git a/vendor/github.com/hashicorp/terraform/helper/resource/id.go b/vendor/github.com/hashicorp/terraform/helper/resource/id.go
index 629582b..1cde67c 100644
--- a/vendor/github.com/hashicorp/terraform/helper/resource/id.go
+++ b/vendor/github.com/hashicorp/terraform/helper/resource/id.go
@@ -1,21 +1,17 @@
1package resource 1package resource
2 2
3import ( 3import (
4 "crypto/rand"
5 "fmt" 4 "fmt"
6 "math/big" 5 "strings"
7 "sync" 6 "sync"
7 "time"
8) 8)
9 9
10const UniqueIdPrefix = `terraform-` 10const UniqueIdPrefix = `terraform-`
11 11
12// idCounter is a randomly seeded monotonic counter for generating ordered 12// idCounter is a monotonic counter for generating ordered unique ids.
13// unique ids. It uses a big.Int so we can easily increment a long numeric
14// string. The max possible hex value here with 12 random bytes is
15// "01000000000000000000000000", so there's no chance of rollover during
16// operation.
17var idMutex sync.Mutex 13var idMutex sync.Mutex
18var idCounter = big.NewInt(0).SetBytes(randomBytes(12)) 14var idCounter uint32
19 15
20// Helper for a resource to generate a unique identifier w/ default prefix 16// Helper for a resource to generate a unique identifier w/ default prefix
21func UniqueId() string { 17func UniqueId() string {
@@ -25,15 +21,20 @@ func UniqueId() string {
25// Helper for a resource to generate a unique identifier w/ given prefix 21// Helper for a resource to generate a unique identifier w/ given prefix
26// 22//
27// After the prefix, the ID consists of an incrementing 26 digit value (to match 23// After the prefix, the ID consists of an incrementing 26 digit value (to match
28// previous timestamp output). 24// previous timestamp output). After the prefix, the ID consists of a timestamp
25// and an incrementing 8 hex digit value The timestamp means that multiple IDs
26// created with the same prefix will sort in the order of their creation, even
27// across multiple terraform executions, as long as the clock is not turned back
28// between calls, and as long as any given terraform execution generates fewer
29// than 4 billion IDs.
29func PrefixedUniqueId(prefix string) string { 30func PrefixedUniqueId(prefix string) string {
31 // Be precise to 4 digits of fractional seconds, but remove the dot before the
32 // fractional seconds.
33 timestamp := strings.Replace(
34 time.Now().UTC().Format("20060102150405.0000"), ".", "", 1)
35
30 idMutex.Lock() 36 idMutex.Lock()
31 defer idMutex.Unlock() 37 defer idMutex.Unlock()
32 return fmt.Sprintf("%s%026x", prefix, idCounter.Add(idCounter, big.NewInt(1))) 38 idCounter++
33} 39 return fmt.Sprintf("%s%s%08x", prefix, timestamp, idCounter)
34
35func randomBytes(n int) []byte {
36 b := make([]byte, n)
37 rand.Read(b)
38 return b
39} 40}
diff --git a/vendor/github.com/hashicorp/terraform/helper/resource/testing.go b/vendor/github.com/hashicorp/terraform/helper/resource/testing.go
index ebdbde2..d7de1a0 100644
--- a/vendor/github.com/hashicorp/terraform/helper/resource/testing.go
+++ b/vendor/github.com/hashicorp/terraform/helper/resource/testing.go
@@ -383,11 +383,11 @@ func Test(t TestT, c TestCase) {
383 c.PreCheck() 383 c.PreCheck()
384 } 384 }
385 385
386 ctxProviders, err := testProviderFactories(c) 386 providerResolver, err := testProviderResolver(c)
387 if err != nil { 387 if err != nil {
388 t.Fatal(err) 388 t.Fatal(err)
389 } 389 }
390 opts := terraform.ContextOpts{Providers: ctxProviders} 390 opts := terraform.ContextOpts{ProviderResolver: providerResolver}
391 391
392 // A single state variable to track the lifecycle, starting with no state 392 // A single state variable to track the lifecycle, starting with no state
393 var state *terraform.State 393 var state *terraform.State
@@ -400,15 +400,22 @@ func Test(t TestT, c TestCase) {
400 var err error 400 var err error
401 log.Printf("[WARN] Test: Executing step %d", i) 401 log.Printf("[WARN] Test: Executing step %d", i)
402 402
403 // Determine the test mode to execute 403 if step.Config == "" && !step.ImportState {
404 if step.Config != "" {
405 state, err = testStepConfig(opts, state, step)
406 } else if step.ImportState {
407 state, err = testStepImportState(opts, state, step)
408 } else {
409 err = fmt.Errorf( 404 err = fmt.Errorf(
410 "unknown test mode for step. Please see TestStep docs\n\n%#v", 405 "unknown test mode for step. Please see TestStep docs\n\n%#v",
411 step) 406 step)
407 } else {
408 if step.ImportState {
409 if step.Config == "" {
410 step.Config = testProviderConfig(c)
411 }
412
413 // Can optionally set step.Config in addition to
414 // step.ImportState, to provide config for the import.
415 state, err = testStepImportState(opts, state, step)
416 } else {
417 state, err = testStepConfig(opts, state, step)
418 }
412 } 419 }
413 420
414 // If there was an error, exit 421 // If there was an error, exit
@@ -496,16 +503,29 @@ func Test(t TestT, c TestCase) {
496 } 503 }
497} 504}
498 505
499// testProviderFactories is a helper to build the ResourceProviderFactory map 506// testProviderConfig takes the list of Providers in a TestCase and returns a
507// config with only empty provider blocks. This is useful for Import, where no
508// config is provided, but the providers must be defined.
509func testProviderConfig(c TestCase) string {
510 var lines []string
511 for p := range c.Providers {
512 lines = append(lines, fmt.Sprintf("provider %q {}\n", p))
513 }
514
515 return strings.Join(lines, "")
516}
517
518// testProviderResolver is a helper to build a ResourceProviderResolver
500// with pre instantiated ResourceProviders, so that we can reset them for the 519// with pre instantiated ResourceProviders, so that we can reset them for the
501// test, while only calling the factory function once. 520// test, while only calling the factory function once.
502// Any errors are stored so that they can be returned by the factory in 521// Any errors are stored so that they can be returned by the factory in
503// terraform to match non-test behavior. 522// terraform to match non-test behavior.
504func testProviderFactories(c TestCase) (map[string]terraform.ResourceProviderFactory, error) { 523func testProviderResolver(c TestCase) (terraform.ResourceProviderResolver, error) {
505 ctxProviders := c.ProviderFactories // make(map[string]terraform.ResourceProviderFactory) 524 ctxProviders := c.ProviderFactories
506 if ctxProviders == nil { 525 if ctxProviders == nil {
507 ctxProviders = make(map[string]terraform.ResourceProviderFactory) 526 ctxProviders = make(map[string]terraform.ResourceProviderFactory)
508 } 527 }
528
509 // add any fixed providers 529 // add any fixed providers
510 for k, p := range c.Providers { 530 for k, p := range c.Providers {
511 ctxProviders[k] = terraform.ResourceProviderFactoryFixed(p) 531 ctxProviders[k] = terraform.ResourceProviderFactoryFixed(p)
@@ -527,7 +547,7 @@ func testProviderFactories(c TestCase) (map[string]terraform.ResourceProviderFac
527 } 547 }
528 } 548 }
529 549
530 return ctxProviders, nil 550 return terraform.ResourceProviderResolverFixed(ctxProviders), nil
531} 551}
532 552
533// UnitTest is a helper to force the acceptance testing harness to run in the 553// UnitTest is a helper to force the acceptance testing harness to run in the