From 219a0e849bc6d2eb795a28e1a67a6594eb700bf0 Mon Sep 17 00:00:00 2001 From: Paul Tyng Date: Fri, 1 May 2020 15:46:29 -0400 Subject: [PATCH 1/4] Remove httpclient package We should surface a User-Agent builder on the provider with the core version --- httpclient/useragent.go | 26 ----------------------- httpclient/useragent_test.go | 41 ------------------------------------ 2 files changed, 67 deletions(-) delete mode 100644 httpclient/useragent.go delete mode 100644 httpclient/useragent_test.go diff --git a/httpclient/useragent.go b/httpclient/useragent.go deleted file mode 100644 index 9d8a54b028..0000000000 --- a/httpclient/useragent.go +++ /dev/null @@ -1,26 +0,0 @@ -package httpclient - -import ( - "fmt" - "log" - "os" - "strings" - - "github.com/hashicorp/terraform-plugin-sdk/v2/meta" -) - -const uaEnvVar = "TF_APPEND_USER_AGENT" - -func TerraformUserAgent(version string) string { - ua := fmt.Sprintf("HashiCorp Terraform/%s (+https://www.terraform.io) Terraform Plugin SDK/%s", version, meta.SDKVersionString()) - - if add := os.Getenv(uaEnvVar); add != "" { - add = strings.TrimSpace(add) - if len(add) > 0 { - ua += " " + add - log.Printf("[DEBUG] Using modified User-Agent: %s", ua) - } - } - - return ua -} diff --git a/httpclient/useragent_test.go b/httpclient/useragent_test.go deleted file mode 100644 index 11ed7cbdbc..0000000000 --- a/httpclient/useragent_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package httpclient - -import ( - "fmt" - "os" - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/v2/meta" -) - -func TestUserAgentAppendViaEnvVar(t *testing.T) { - if oldenv, isSet := os.LookupEnv(uaEnvVar); isSet { - defer os.Setenv(uaEnvVar, oldenv) - } else { - defer os.Unsetenv(uaEnvVar) - } - - expectedBase := "HashiCorp Terraform/0.0.0 (+https://www.terraform.io) Terraform Plugin SDK/" + meta.SDKVersionString() - - testCases := []struct { - envVarValue string - expected string - }{ - {"", expectedBase}, - {" ", expectedBase}, - {" \n", expectedBase}, - {"test/1", expectedBase + " test/1"}, - {"test/1 (comment)", expectedBase + " test/1 (comment)"}, - } - - for i, tc := range testCases { - t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { - os.Unsetenv(uaEnvVar) - os.Setenv(uaEnvVar, tc.envVarValue) - givenUA := TerraformUserAgent("0.0.0") - if givenUA != tc.expected { - t.Fatalf("Expected User-Agent '%s' does not match '%s'", tc.expected, givenUA) - } - }) - } -} From 2b2a44a6ff1f0e5e861af3d98d16946f4ba5ff3b Mon Sep 17 00:00:00 2001 From: Paul Tyng Date: Fri, 1 May 2020 16:03:35 -0400 Subject: [PATCH 2/4] Remove hashcode Please use crc32.ChecksumIEEE directly if you need to calculate a hash for a string --- helper/schema/field_reader_config_test.go | 2 +- helper/schema/schema_test.go | 2 +- helper/schema/set.go | 2 +- helper/schema/shims_test.go | 2 +- {helper => internal/helper}/hashcode/hashcode.go | 0 {helper => internal/helper}/hashcode/hashcode_test.go | 0 6 files changed, 4 insertions(+), 4 deletions(-) rename {helper => internal/helper}/hashcode/hashcode.go (100%) rename {helper => internal/helper}/hashcode/hashcode_test.go (100%) diff --git a/helper/schema/field_reader_config_test.go b/helper/schema/field_reader_config_test.go index 69ef5cde9b..e0496f2842 100644 --- a/helper/schema/field_reader_config_test.go +++ b/helper/schema/field_reader_config_test.go @@ -6,8 +6,8 @@ import ( "reflect" "testing" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/hashcode" "github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/hcl2shim" + "github.com/hashicorp/terraform-plugin-sdk/v2/internal/helper/hashcode" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 6fc5a703cd..a47d9b412f 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -14,9 +14,9 @@ import ( "github.com/hashicorp/go-cty/cty" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/hashcode" "github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/hcl2shim" "github.com/hashicorp/terraform-plugin-sdk/v2/internal/diagutils" + "github.com/hashicorp/terraform-plugin-sdk/v2/internal/helper/hashcode" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) diff --git a/helper/schema/set.go b/helper/schema/set.go index 1518391e1b..a510e60ff0 100644 --- a/helper/schema/set.go +++ b/helper/schema/set.go @@ -8,7 +8,7 @@ import ( "strconv" "sync" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/hashcode" + "github.com/hashicorp/terraform-plugin-sdk/v2/internal/helper/hashcode" ) // HashString hashes strings. If you want a Set of strings, this is the diff --git a/helper/schema/shims_test.go b/helper/schema/shims_test.go index d33663acbe..678bda3f62 100644 --- a/helper/schema/shims_test.go +++ b/helper/schema/shims_test.go @@ -14,10 +14,10 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/hashicorp/go-cty/cty" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/hashcode" "github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/configschema" "github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/hcl2shim" "github.com/hashicorp/terraform-plugin-sdk/v2/internal/diagutils" + "github.com/hashicorp/terraform-plugin-sdk/v2/internal/helper/hashcode" "github.com/hashicorp/terraform-plugin-sdk/v2/internal/providers" "github.com/hashicorp/terraform-plugin-sdk/v2/internal/tfdiags" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" diff --git a/helper/hashcode/hashcode.go b/internal/helper/hashcode/hashcode.go similarity index 100% rename from helper/hashcode/hashcode.go rename to internal/helper/hashcode/hashcode.go diff --git a/helper/hashcode/hashcode_test.go b/internal/helper/hashcode/hashcode_test.go similarity index 100% rename from helper/hashcode/hashcode_test.go rename to internal/helper/hashcode/hashcode_test.go From d0a751516ba03ad8fdaaac825d8b4d59056e8cad Mon Sep 17 00:00:00 2001 From: Paul Tyng Date: Fri, 1 May 2020 15:52:42 -0400 Subject: [PATCH 3/4] Remove mutexkv --- helper/mutexkv/mutexkv.go | 51 -------------------------- helper/mutexkv/mutexkv_test.go | 67 ---------------------------------- 2 files changed, 118 deletions(-) delete mode 100644 helper/mutexkv/mutexkv.go delete mode 100644 helper/mutexkv/mutexkv_test.go diff --git a/helper/mutexkv/mutexkv.go b/helper/mutexkv/mutexkv.go deleted file mode 100644 index 6917f2142b..0000000000 --- a/helper/mutexkv/mutexkv.go +++ /dev/null @@ -1,51 +0,0 @@ -package mutexkv - -import ( - "log" - "sync" -) - -// MutexKV is a simple key/value store for arbitrary mutexes. It can be used to -// serialize changes across arbitrary collaborators that share knowledge of the -// keys they must serialize on. -// -// The initial use case is to let aws_security_group_rule resources serialize -// their access to individual security groups based on SG ID. -type MutexKV struct { - lock sync.Mutex - store map[string]*sync.Mutex -} - -// Locks the mutex for the given key. Caller is responsible for calling Unlock -// for the same key -func (m *MutexKV) Lock(key string) { - log.Printf("[DEBUG] Locking %q", key) - m.get(key).Lock() - log.Printf("[DEBUG] Locked %q", key) -} - -// Unlock the mutex for the given key. Caller must have called Lock for the same key first -func (m *MutexKV) Unlock(key string) { - log.Printf("[DEBUG] Unlocking %q", key) - m.get(key).Unlock() - log.Printf("[DEBUG] Unlocked %q", key) -} - -// Returns a mutex for the given key, no guarantee of its lock status -func (m *MutexKV) get(key string) *sync.Mutex { - m.lock.Lock() - defer m.lock.Unlock() - mutex, ok := m.store[key] - if !ok { - mutex = &sync.Mutex{} - m.store[key] = mutex - } - return mutex -} - -// Returns a properly initalized MutexKV -func NewMutexKV() *MutexKV { - return &MutexKV{ - store: make(map[string]*sync.Mutex), - } -} diff --git a/helper/mutexkv/mutexkv_test.go b/helper/mutexkv/mutexkv_test.go deleted file mode 100644 index 9835600747..0000000000 --- a/helper/mutexkv/mutexkv_test.go +++ /dev/null @@ -1,67 +0,0 @@ -package mutexkv - -import ( - "testing" - "time" -) - -func TestMutexKVLock(t *testing.T) { - mkv := NewMutexKV() - - mkv.Lock("foo") - - doneCh := make(chan struct{}) - - go func() { - mkv.Lock("foo") - close(doneCh) - }() - - select { - case <-doneCh: - t.Fatal("Second lock was able to be taken. This shouldn't happen.") - case <-time.After(50 * time.Millisecond): - // pass - } -} - -func TestMutexKVUnlock(t *testing.T) { - mkv := NewMutexKV() - - mkv.Lock("foo") - mkv.Unlock("foo") - - doneCh := make(chan struct{}) - - go func() { - mkv.Lock("foo") - close(doneCh) - }() - - select { - case <-doneCh: - // pass - case <-time.After(50 * time.Millisecond): - t.Fatal("Second lock blocked after unlock. This shouldn't happen.") - } -} - -func TestMutexKVDifferentKeys(t *testing.T) { - mkv := NewMutexKV() - - mkv.Lock("foo") - - doneCh := make(chan struct{}) - - go func() { - mkv.Lock("bar") - close(doneCh) - }() - - select { - case <-doneCh: - // pass - case <-time.After(50 * time.Millisecond): - t.Fatal("Second lock on a different key blocked. This shouldn't happen.") - } -} From 96a025f29c674b38965679bb294b72efd6cf74fa Mon Sep 17 00:00:00 2001 From: Paul Tyng Date: Fri, 1 May 2020 15:54:27 -0400 Subject: [PATCH 4/4] Remove pathorcontents Please copy the implementation to your own provider if you still need this code --- go.mod | 2 +- helper/pathorcontents/read.go | 40 -------- helper/pathorcontents/read_test.go | 149 ----------------------------- 3 files changed, 1 insertion(+), 190 deletions(-) delete mode 100644 helper/pathorcontents/read.go delete mode 100644 helper/pathorcontents/read_test.go diff --git a/go.mod b/go.mod index 1598f95b3c..9ce08acbec 100644 --- a/go.mod +++ b/go.mod @@ -29,7 +29,7 @@ require ( github.com/keybase/go-crypto v0.0.0-20161004153544-93f5b35093ba github.com/kylelemons/godebug v1.1.0 // indirect github.com/mitchellh/copystructure v1.0.0 - github.com/mitchellh/go-homedir v1.1.0 + github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/go-testing-interface v1.0.4 github.com/mitchellh/go-wordwrap v1.0.0 // indirect github.com/mitchellh/mapstructure v1.1.2 diff --git a/helper/pathorcontents/read.go b/helper/pathorcontents/read.go deleted file mode 100644 index d0d4847dae..0000000000 --- a/helper/pathorcontents/read.go +++ /dev/null @@ -1,40 +0,0 @@ -// Helpers for dealing with file paths and their contents -package pathorcontents - -import ( - "io/ioutil" - "os" - - "github.com/mitchellh/go-homedir" -) - -// If the argument is a path, Read loads it and returns the contents, -// otherwise the argument is assumed to be the desired contents and is simply -// returned. -// -// The boolean second return value can be called `wasPath` - it indicates if a -// path was detected and a file loaded. -func Read(poc string) (string, bool, error) { - if len(poc) == 0 { - return poc, false, nil - } - - path := poc - if path[0] == '~' { - var err error - path, err = homedir.Expand(path) - if err != nil { - return path, true, err - } - } - - if _, err := os.Stat(path); err == nil { - contents, err := ioutil.ReadFile(path) - if err != nil { - return string(contents), true, err - } - return string(contents), true, nil - } - - return poc, false, nil -} diff --git a/helper/pathorcontents/read_test.go b/helper/pathorcontents/read_test.go deleted file mode 100644 index c3016e5c5b..0000000000 --- a/helper/pathorcontents/read_test.go +++ /dev/null @@ -1,149 +0,0 @@ -package pathorcontents - -import ( - "io" - "io/ioutil" - "os" - "os/user" - "strings" - "testing" - - "github.com/mitchellh/go-homedir" -) - -func TestRead_Path(t *testing.T) { - isPath := true - f, cleanup := testTempFile(t) - defer cleanup() - - if _, err := io.WriteString(f, "foobar"); err != nil { - t.Fatalf("err: %s", err) - } - f.Close() - - contents, wasPath, err := Read(f.Name()) - - if err != nil { - t.Fatalf("err: %s", err) - } - if wasPath != isPath { - t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath) - } - if contents != "foobar" { - t.Fatalf("expected contents %s, got %s", "foobar", contents) - } -} - -func TestRead_TildePath(t *testing.T) { - isPath := true - home, err := homedir.Dir() - if err != nil { - t.Fatalf("err: %s", err) - } - f, cleanup := testTempFile(t, home) - defer cleanup() - - if _, err := io.WriteString(f, "foobar"); err != nil { - t.Fatalf("err: %s", err) - } - f.Close() - - r := strings.NewReplacer(home, "~") - homePath := r.Replace(f.Name()) - contents, wasPath, err := Read(homePath) - - if err != nil { - t.Fatalf("err: %s", err) - } - if wasPath != isPath { - t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath) - } - if contents != "foobar" { - t.Fatalf("expected contents %s, got %s", "foobar", contents) - } -} - -func TestRead_PathNoPermission(t *testing.T) { - // This skip condition is intended to get this test out of the way of users - // who are building and testing Terraform from within a Linux-based Docker - // container, where it is common for processes to be running as effectively - // root within the container. - if u, err := user.Current(); err == nil && u.Uid == "0" { - t.Skip("This test is invalid when running as root, since root can read every file") - } - - isPath := true - f, cleanup := testTempFile(t) - defer cleanup() - - if _, err := io.WriteString(f, "foobar"); err != nil { - t.Fatalf("err: %s", err) - } - f.Close() - - if err := os.Chmod(f.Name(), 0); err != nil { - t.Fatalf("err: %s", err) - } - - contents, wasPath, err := Read(f.Name()) - - if err == nil { - t.Fatal("Expected error, got none!") - } - if wasPath != isPath { - t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath) - } - if contents != "" { - t.Fatalf("expected contents %s, got %s", "", contents) - } -} - -func TestRead_Contents(t *testing.T) { - isPath := false - input := "hello" - - contents, wasPath, err := Read(input) - - if err != nil { - t.Fatalf("err: %s", err) - } - if wasPath != isPath { - t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath) - } - if contents != input { - t.Fatalf("expected contents %s, got %s", input, contents) - } -} - -func TestRead_TildeContents(t *testing.T) { - isPath := false - input := "~/hello/notafile" - - contents, wasPath, err := Read(input) - - if err != nil { - t.Fatalf("err: %s", err) - } - if wasPath != isPath { - t.Fatalf("expected wasPath: %t, got %t", isPath, wasPath) - } - if contents != input { - t.Fatalf("expected contents %s, got %s", input, contents) - } -} - -// Returns an open tempfile based at baseDir and a function to clean it up. -func testTempFile(t *testing.T, baseDir ...string) (*os.File, func()) { - base := "" - if len(baseDir) == 1 { - base = baseDir[0] - } - f, err := ioutil.TempFile(base, "tf") - if err != nil { - t.Fatalf("err: %s", err) - } - - return f, func() { - os.Remove(f.Name()) - } -}