terraform: ResourceConfig.DeepCopy

This implements DeepCopy, still need to implement Equals to make this
more useful. Coming in the next commit but this still has its own full
functionality + tests.
This commit is contained in:
Mitchell Hashimoto 2016-09-27 16:09:32 -07:00
parent 99e706b502
commit 56901e5cfd
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 34 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"strings"
"github.com/hashicorp/terraform/config"
"github.com/mitchellh/copystructure"
)
// ResourceProvisionerConfig is used to pair a provisioner
@ -93,6 +94,25 @@ func NewResourceConfig(c *config.RawConfig) *ResourceConfig {
return result
}
// DeepCopy performs a deep copy of the configuration. This makes it safe
// to modify any of the structures that are part of the resource config without
// affecting the original configuration.
func (c *ResourceConfig) DeepCopy() *ResourceConfig {
// Copy, this will copy all the exported attributes
copy, err := copystructure.Config{Lock: true}.Copy(c)
if err != nil {
panic(err)
}
// Force the type
result := copy.(*ResourceConfig)
// For the raw configuration, we can just use its own copy method
result.raw = c.raw.Copy()
return result
}
// CheckSet checks that the given list of configuration keys is
// properly set. If not, errors are returned for each unset key.
//

View File

@ -1,6 +1,7 @@
package terraform
import (
"fmt"
"reflect"
"testing"
@ -212,6 +213,19 @@ func TestResourceConfigGet(t *testing.T) {
if !reflect.DeepEqual(v, tc.Value) {
t.Fatalf("%d bad: %#v", i, v)
}
// If we have vars, we don't test copying
if len(tc.Vars) > 0 {
continue
}
// Test copying
t.Run(fmt.Sprintf("copy-%d", i), func(t *testing.T) {
copy := rc.DeepCopy()
if !reflect.DeepEqual(copy, rc) {
t.Fatalf("bad:\n\n%#v\n\n%#v", copy, rc)
}
})
}
}