helper/schema: add GetOk
This commit is contained in:
parent
37cf52fa27
commit
7be2f1b091
|
@ -78,7 +78,6 @@ func resourceAwsInstance() *schema.Resource {
|
||||||
"source_dest_check": &schema.Schema{
|
"source_dest_check": &schema.Schema{
|
||||||
Type: schema.TypeBool,
|
Type: schema.TypeBool,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
// TODO: Hidden
|
|
||||||
},
|
},
|
||||||
|
|
||||||
"user_data": &schema.Schema{
|
"user_data": &schema.Schema{
|
||||||
|
@ -195,11 +194,10 @@ func resourceAwsInstanceCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
instance = instanceRaw.(*ec2.Instance)
|
instance = instanceRaw.(*ec2.Instance)
|
||||||
|
|
||||||
// Initialize the connection info
|
// Initialize the connection info
|
||||||
/*
|
d.SetConnInfo(map[string]string{
|
||||||
TODO: conninfo
|
"type": "ssh",
|
||||||
rs.ConnInfo["type"] = "ssh"
|
"host": instance.PublicIpAddress,
|
||||||
rs.ConnInfo["host"] = instance.PublicIpAddress
|
})
|
||||||
*/
|
|
||||||
|
|
||||||
// Set our attributes
|
// Set our attributes
|
||||||
if err := resourceAwsInstanceRead(d, meta); err != nil {
|
if err := resourceAwsInstanceRead(d, meta); err != nil {
|
||||||
|
|
|
@ -23,6 +23,15 @@ const (
|
||||||
getSourceSet
|
getSourceSet
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// getResult is the internal structure that is generated when a Get
|
||||||
|
// is called that contains some extra data that might be used.
|
||||||
|
type getResult struct {
|
||||||
|
Value interface{}
|
||||||
|
Exists bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var getResultEmpty getResult
|
||||||
|
|
||||||
// ResourceData is used to query and set the attributes of a resource.
|
// ResourceData is used to query and set the attributes of a resource.
|
||||||
type ResourceData struct {
|
type ResourceData struct {
|
||||||
schema map[string]*Schema
|
schema map[string]*Schema
|
||||||
|
@ -36,25 +45,38 @@ type ResourceData struct {
|
||||||
once sync.Once
|
once sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the data for the given key, or nil if the key doesn't exist.
|
// Get returns the data for the given key, or nil if the key doesn't exist
|
||||||
|
// in the schema.
|
||||||
//
|
//
|
||||||
// The type of the data returned will be according to the schema specified.
|
// If the key does exist in the schema but doesn't exist in the configuration,
|
||||||
// Primitives will be their respective types in Go, lists will always be
|
// then the default value for that type will be returned. For strings, this is
|
||||||
// []interface{}, and sub-resources will be map[string]interface{}.
|
// "", for numbers it is 0, etc.
|
||||||
|
//
|
||||||
|
// If you also want to test if something is set at all, use GetOk.
|
||||||
func (d *ResourceData) Get(key string) interface{} {
|
func (d *ResourceData) Get(key string) interface{} {
|
||||||
var parts []string
|
v, _ := d.GetOk(key)
|
||||||
if key != "" {
|
return v
|
||||||
parts = strings.Split(key, ".")
|
|
||||||
}
|
|
||||||
|
|
||||||
return d.getObject("", parts, d.schema, getSourceSet)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetChange returns the old and new value for a given key.
|
// GetChange returns the old and new value for a given key.
|
||||||
//
|
//
|
||||||
// If there is no change, then old and new will simply be the same.
|
// If there is no change, then old and new will simply be the same.
|
||||||
func (d *ResourceData) GetChange(key string) (interface{}, interface{}) {
|
func (d *ResourceData) GetChange(key string) (interface{}, interface{}) {
|
||||||
return d.getChange(key, getSourceConfig, getSourceDiff)
|
o, n := d.getChange(key, getSourceConfig, getSourceDiff)
|
||||||
|
return o.Value, n.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOk returns the data for the given key and whether or not the key
|
||||||
|
// existed or not in the configuration. The second boolean result will also
|
||||||
|
// be false if a key is given that isn't in the schema at all.
|
||||||
|
func (d *ResourceData) GetOk(key string) (interface{}, bool) {
|
||||||
|
var parts []string
|
||||||
|
if key != "" {
|
||||||
|
parts = strings.Split(key, ".")
|
||||||
|
}
|
||||||
|
|
||||||
|
r := d.getObject("", parts, d.schema, getSourceSet)
|
||||||
|
return r.Value, r.Exists
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasChange returns whether or not the given key has been changed.
|
// HasChange returns whether or not the given key has been changed.
|
||||||
|
@ -171,15 +193,21 @@ func (d *ResourceData) init() {
|
||||||
func (d *ResourceData) diffChange(k string) (interface{}, interface{}, bool) {
|
func (d *ResourceData) diffChange(k string) (interface{}, interface{}, bool) {
|
||||||
// Get the change between the state and the config.
|
// Get the change between the state and the config.
|
||||||
o, n := d.getChange(k, getSourceState, getSourceConfig)
|
o, n := d.getChange(k, getSourceState, getSourceConfig)
|
||||||
|
if !o.Exists {
|
||||||
|
o.Value = nil
|
||||||
|
}
|
||||||
|
if !n.Exists {
|
||||||
|
n.Value = nil
|
||||||
|
}
|
||||||
|
|
||||||
// Return the old, new, and whether there is a change
|
// Return the old, new, and whether there is a change
|
||||||
return o, n, !reflect.DeepEqual(o, n)
|
return o.Value, n.Value, !reflect.DeepEqual(o.Value, n.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ResourceData) getChange(
|
func (d *ResourceData) getChange(
|
||||||
key string,
|
key string,
|
||||||
oldLevel getSource,
|
oldLevel getSource,
|
||||||
newLevel getSource) (interface{}, interface{}) {
|
newLevel getSource) (getResult, getResult) {
|
||||||
var parts, parts2 []string
|
var parts, parts2 []string
|
||||||
if key != "" {
|
if key != "" {
|
||||||
parts = strings.Split(key, ".")
|
parts = strings.Split(key, ".")
|
||||||
|
@ -195,7 +223,7 @@ func (d *ResourceData) get(
|
||||||
k string,
|
k string,
|
||||||
parts []string,
|
parts []string,
|
||||||
schema *Schema,
|
schema *Schema,
|
||||||
source getSource) interface{} {
|
source getSource) getResult {
|
||||||
switch schema.Type {
|
switch schema.Type {
|
||||||
case TypeList:
|
case TypeList:
|
||||||
return d.getList(k, parts, schema, source)
|
return d.getList(k, parts, schema, source)
|
||||||
|
@ -218,24 +246,25 @@ func (d *ResourceData) getSet(
|
||||||
k string,
|
k string,
|
||||||
parts []string,
|
parts []string,
|
||||||
schema *Schema,
|
schema *Schema,
|
||||||
source getSource) interface{} {
|
source getSource) getResult {
|
||||||
s := &Set{F: schema.Set}
|
s := &Set{F: schema.Set}
|
||||||
|
result := getResult{Value: s}
|
||||||
raw := d.getList(k, nil, schema, source)
|
raw := d.getList(k, nil, schema, source)
|
||||||
if raw == nil {
|
if !raw.Exists {
|
||||||
if len(parts) > 0 {
|
if len(parts) > 0 {
|
||||||
return d.getList(k, parts, schema, source)
|
return d.getList(k, parts, schema, source)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
list := raw.([]interface{})
|
list := raw.Value.([]interface{})
|
||||||
if len(list) == 0 {
|
if len(list) == 0 {
|
||||||
if len(parts) > 0 {
|
if len(parts) > 0 {
|
||||||
return d.getList(k, parts, schema, source)
|
return d.getList(k, parts, schema, source)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is a reverse map of hash code => index in config used to
|
// This is a reverse map of hash code => index in config used to
|
||||||
|
@ -269,12 +298,12 @@ func (d *ResourceData) getSet(
|
||||||
index := parts[0]
|
index := parts[0]
|
||||||
indexInt, err := strconv.ParseInt(index, 0, 0)
|
indexInt, err := strconv.ParseInt(index, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return getResultEmpty
|
||||||
}
|
}
|
||||||
|
|
||||||
codes := s.listCode()
|
codes := s.listCode()
|
||||||
if int(indexInt) >= len(codes) {
|
if int(indexInt) >= len(codes) {
|
||||||
return nil
|
return getResultEmpty
|
||||||
}
|
}
|
||||||
code := codes[indexInt]
|
code := codes[indexInt]
|
||||||
realIndex := indexMap[code]
|
realIndex := indexMap[code]
|
||||||
|
@ -283,17 +312,19 @@ func (d *ResourceData) getSet(
|
||||||
return d.getList(k, parts, schema, source)
|
return d.getList(k, parts, schema, source)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s
|
result.Exists = true
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ResourceData) getMap(
|
func (d *ResourceData) getMap(
|
||||||
k string,
|
k string,
|
||||||
parts []string,
|
parts []string,
|
||||||
schema *Schema,
|
schema *Schema,
|
||||||
source getSource) interface{} {
|
source getSource) getResult {
|
||||||
elemSchema := &Schema{Type: TypeString}
|
elemSchema := &Schema{Type: TypeString}
|
||||||
|
|
||||||
result := make(map[string]interface{})
|
result := make(map[string]interface{})
|
||||||
|
resultSet := false
|
||||||
prefix := k + "."
|
prefix := k + "."
|
||||||
|
|
||||||
if d.state != nil && source >= getSourceState {
|
if d.state != nil && source >= getSourceState {
|
||||||
|
@ -303,7 +334,8 @@ func (d *ResourceData) getMap(
|
||||||
}
|
}
|
||||||
|
|
||||||
single := k[len(prefix):]
|
single := k[len(prefix):]
|
||||||
result[single] = d.getPrimitive(k, nil, elemSchema, source)
|
result[single] = d.getPrimitive(k, nil, elemSchema, source).Value
|
||||||
|
resultSet = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -311,6 +343,7 @@ func (d *ResourceData) getMap(
|
||||||
// For config, we always set the result to exactly what was requested
|
// For config, we always set the result to exactly what was requested
|
||||||
if m, ok := d.config.Get(k); ok {
|
if m, ok := d.config.Get(k); ok {
|
||||||
result = m.(map[string]interface{})
|
result = m.(map[string]interface{})
|
||||||
|
resultSet = true
|
||||||
} else {
|
} else {
|
||||||
result = nil
|
result = nil
|
||||||
}
|
}
|
||||||
|
@ -321,13 +354,14 @@ func (d *ResourceData) getMap(
|
||||||
if !strings.HasPrefix(k, prefix) {
|
if !strings.HasPrefix(k, prefix) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
resultSet = true
|
||||||
|
|
||||||
single := k[len(prefix):]
|
single := k[len(prefix):]
|
||||||
|
|
||||||
if v.NewRemoved {
|
if v.NewRemoved {
|
||||||
delete(result, single)
|
delete(result, single)
|
||||||
} else {
|
} else {
|
||||||
result[single] = d.getPrimitive(k, nil, elemSchema, source)
|
result[single] = d.getPrimitive(k, nil, elemSchema, source).Value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -338,6 +372,8 @@ func (d *ResourceData) getMap(
|
||||||
if !strings.HasPrefix(k, prefix) {
|
if !strings.HasPrefix(k, prefix) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
resultSet = true
|
||||||
|
|
||||||
if !cleared {
|
if !cleared {
|
||||||
// We clear the results if they are in the set map
|
// We clear the results if they are in the set map
|
||||||
result = make(map[string]interface{})
|
result = make(map[string]interface{})
|
||||||
|
@ -345,30 +381,34 @@ func (d *ResourceData) getMap(
|
||||||
}
|
}
|
||||||
|
|
||||||
single := k[len(prefix):]
|
single := k[len(prefix):]
|
||||||
result[single] = d.getPrimitive(k, nil, elemSchema, source)
|
result[single] = d.getPrimitive(k, nil, elemSchema, source).Value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we're requesting a specific element, return that
|
// If we're requesting a specific element, return that
|
||||||
|
var resultValue interface{} = result
|
||||||
if len(parts) > 0 {
|
if len(parts) > 0 {
|
||||||
return result[parts[0]]
|
resultValue = result[parts[0]]
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return getResult{
|
||||||
|
Value: resultValue,
|
||||||
|
Exists: resultSet,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ResourceData) getObject(
|
func (d *ResourceData) getObject(
|
||||||
k string,
|
k string,
|
||||||
parts []string,
|
parts []string,
|
||||||
schema map[string]*Schema,
|
schema map[string]*Schema,
|
||||||
source getSource) interface{} {
|
source getSource) getResult {
|
||||||
if len(parts) > 0 {
|
if len(parts) > 0 {
|
||||||
// We're requesting a specific key in an object
|
// We're requesting a specific key in an object
|
||||||
key := parts[0]
|
key := parts[0]
|
||||||
parts = parts[1:]
|
parts = parts[1:]
|
||||||
s, ok := schema[key]
|
s, ok := schema[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return getResultEmpty
|
||||||
}
|
}
|
||||||
|
|
||||||
if k != "" {
|
if k != "" {
|
||||||
|
@ -383,17 +423,20 @@ func (d *ResourceData) getObject(
|
||||||
// Get the entire object
|
// Get the entire object
|
||||||
result := make(map[string]interface{})
|
result := make(map[string]interface{})
|
||||||
for field, _ := range schema {
|
for field, _ := range schema {
|
||||||
result[field] = d.getObject(k, []string{field}, schema, source)
|
result[field] = d.getObject(k, []string{field}, schema, source).Value
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return getResult{
|
||||||
|
Value: result,
|
||||||
|
Exists: true,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ResourceData) getList(
|
func (d *ResourceData) getList(
|
||||||
k string,
|
k string,
|
||||||
parts []string,
|
parts []string,
|
||||||
schema *Schema,
|
schema *Schema,
|
||||||
source getSource) interface{} {
|
source getSource) getResult {
|
||||||
if len(parts) > 0 {
|
if len(parts) > 0 {
|
||||||
// We still have parts left over meaning we're accessing an
|
// We still have parts left over meaning we're accessing an
|
||||||
// element of this list.
|
// element of this list.
|
||||||
|
@ -403,12 +446,7 @@ func (d *ResourceData) getList(
|
||||||
// Special case if we're accessing the count of the list
|
// Special case if we're accessing the count of the list
|
||||||
if idx == "#" {
|
if idx == "#" {
|
||||||
schema := &Schema{Type: TypeInt}
|
schema := &Schema{Type: TypeInt}
|
||||||
result := d.get(k+".#", parts, schema, source)
|
return d.get(k+".#", parts, schema, source)
|
||||||
if result == nil {
|
|
||||||
result = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
key := fmt.Sprintf("%s.%s", k, idx)
|
key := fmt.Sprintf("%s.%s", k, idx)
|
||||||
|
@ -421,22 +459,24 @@ func (d *ResourceData) getList(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the entire list.
|
// Get the entire list.
|
||||||
result := make(
|
count := d.getList(k, []string{"#"}, schema, source)
|
||||||
[]interface{},
|
result := make([]interface{}, count.Value.(int))
|
||||||
d.getList(k, []string{"#"}, schema, source).(int))
|
|
||||||
for i, _ := range result {
|
for i, _ := range result {
|
||||||
is := strconv.FormatInt(int64(i), 10)
|
is := strconv.FormatInt(int64(i), 10)
|
||||||
result[i] = d.getList(k, []string{is}, schema, source)
|
result[i] = d.getList(k, []string{is}, schema, source).Value
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return getResult{
|
||||||
|
Value: result,
|
||||||
|
Exists: count.Exists,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ResourceData) getPrimitive(
|
func (d *ResourceData) getPrimitive(
|
||||||
k string,
|
k string,
|
||||||
parts []string,
|
parts []string,
|
||||||
schema *Schema,
|
schema *Schema,
|
||||||
source getSource) interface{} {
|
source getSource) getResult {
|
||||||
var result string
|
var result string
|
||||||
var resultSet bool
|
var resultSet bool
|
||||||
if d.state != nil && source >= getSourceState {
|
if d.state != nil && source >= getSourceState {
|
||||||
|
@ -474,13 +514,15 @@ func (d *ResourceData) getPrimitive(
|
||||||
}
|
}
|
||||||
|
|
||||||
if !resultSet {
|
if !resultSet {
|
||||||
return nil
|
result = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var resultValue interface{}
|
||||||
switch schema.Type {
|
switch schema.Type {
|
||||||
case TypeBool:
|
case TypeBool:
|
||||||
if result == "" {
|
if result == "" {
|
||||||
return false
|
resultValue = false
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
v, err := strconv.ParseBool(result)
|
v, err := strconv.ParseBool(result)
|
||||||
|
@ -488,13 +530,14 @@ func (d *ResourceData) getPrimitive(
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return v
|
resultValue = v
|
||||||
case TypeString:
|
case TypeString:
|
||||||
// Use the value as-is. We just put this case here to be explicit.
|
// Use the value as-is. We just put this case here to be explicit.
|
||||||
return result
|
resultValue = result
|
||||||
case TypeInt:
|
case TypeInt:
|
||||||
if result == "" {
|
if result == "" {
|
||||||
return 0
|
resultValue = 0
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
v, err := strconv.ParseInt(result, 0, 0)
|
v, err := strconv.ParseInt(result, 0, 0)
|
||||||
|
@ -502,10 +545,15 @@ func (d *ResourceData) getPrimitive(
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return int(v)
|
resultValue = int(v)
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("Unknown type: %s", schema.Type))
|
panic(fmt.Sprintf("Unknown type: %s", schema.Type))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return getResult{
|
||||||
|
Value: resultValue,
|
||||||
|
Exists: resultSet,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *ResourceData) set(
|
func (d *ResourceData) set(
|
||||||
|
@ -727,10 +775,10 @@ func (d *ResourceData) stateList(
|
||||||
prefix string,
|
prefix string,
|
||||||
schema *Schema) map[string]string {
|
schema *Schema) map[string]string {
|
||||||
countRaw := d.get(prefix, []string{"#"}, schema, getSourceSet)
|
countRaw := d.get(prefix, []string{"#"}, schema, getSourceSet)
|
||||||
if countRaw == nil {
|
if !countRaw.Exists {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
count := countRaw.(int)
|
count := countRaw.Value.(int)
|
||||||
|
|
||||||
result := make(map[string]string)
|
result := make(map[string]string)
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
|
@ -759,13 +807,13 @@ func (d *ResourceData) stateMap(
|
||||||
prefix string,
|
prefix string,
|
||||||
schema *Schema) map[string]string {
|
schema *Schema) map[string]string {
|
||||||
v := d.getMap(prefix, nil, schema, getSourceSet)
|
v := d.getMap(prefix, nil, schema, getSourceSet)
|
||||||
if v == nil {
|
if !v.Exists {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
elemSchema := &Schema{Type: TypeString}
|
elemSchema := &Schema{Type: TypeString}
|
||||||
result := make(map[string]string)
|
result := make(map[string]string)
|
||||||
for mk, _ := range v.(map[string]interface{}) {
|
for mk, _ := range v.Value.(map[string]interface{}) {
|
||||||
mp := fmt.Sprintf("%s.%s", prefix, mk)
|
mp := fmt.Sprintf("%s.%s", prefix, mk)
|
||||||
for k, v := range d.stateSingle(mp, elemSchema) {
|
for k, v := range d.stateSingle(mp, elemSchema) {
|
||||||
result[k] = v
|
result[k] = v
|
||||||
|
@ -822,11 +870,11 @@ func (d *ResourceData) stateSet(
|
||||||
prefix string,
|
prefix string,
|
||||||
schema *Schema) map[string]string {
|
schema *Schema) map[string]string {
|
||||||
raw := d.get(prefix, nil, schema, getSourceSet)
|
raw := d.get(prefix, nil, schema, getSourceSet)
|
||||||
if raw == nil {
|
if !raw.Exists {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
set := raw.(*Set)
|
set := raw.Value.(*Set)
|
||||||
list := set.List()
|
list := set.List()
|
||||||
result := make(map[string]string)
|
result := make(map[string]string)
|
||||||
result[prefix+".#"] = strconv.FormatInt(int64(len(list)), 10)
|
result[prefix+".#"] = strconv.FormatInt(int64(len(list)), 10)
|
||||||
|
|
|
@ -37,9 +37,8 @@ func TestResourceDataGet(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
Key: "availability_zone",
|
Key: "availability_zone",
|
||||||
|
Value: "",
|
||||||
Value: nil,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -524,7 +523,7 @@ func TestResourceDataGetChange(t *testing.T) {
|
||||||
|
|
||||||
Key: "availability_zone",
|
Key: "availability_zone",
|
||||||
|
|
||||||
OldValue: nil,
|
OldValue: "",
|
||||||
NewValue: "foo",
|
NewValue: "foo",
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -577,6 +576,169 @@ func TestResourceDataGetChange(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResourceDataGetOk(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Schema map[string]*Schema
|
||||||
|
State *terraform.ResourceState
|
||||||
|
Diff *terraform.ResourceDiff
|
||||||
|
Key string
|
||||||
|
Value interface{}
|
||||||
|
Ok bool
|
||||||
|
}{
|
||||||
|
/*
|
||||||
|
* Primitives
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
Schema: map[string]*Schema{
|
||||||
|
"availability_zone": &Schema{
|
||||||
|
Type: TypeString,
|
||||||
|
Optional: true,
|
||||||
|
Computed: true,
|
||||||
|
ForceNew: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
State: nil,
|
||||||
|
|
||||||
|
Diff: &terraform.ResourceDiff{
|
||||||
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
||||||
|
"availability_zone": &terraform.ResourceAttrDiff{
|
||||||
|
Old: "",
|
||||||
|
New: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
Key: "availability_zone",
|
||||||
|
Value: "",
|
||||||
|
Ok: true,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
Schema: map[string]*Schema{
|
||||||
|
"availability_zone": &Schema{
|
||||||
|
Type: TypeString,
|
||||||
|
Optional: true,
|
||||||
|
Computed: true,
|
||||||
|
ForceNew: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
State: nil,
|
||||||
|
|
||||||
|
Diff: nil,
|
||||||
|
|
||||||
|
Key: "availability_zone",
|
||||||
|
Value: "",
|
||||||
|
Ok: false,
|
||||||
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Lists
|
||||||
|
*/
|
||||||
|
|
||||||
|
{
|
||||||
|
Schema: map[string]*Schema{
|
||||||
|
"ports": &Schema{
|
||||||
|
Type: TypeList,
|
||||||
|
Optional: true,
|
||||||
|
Elem: &Schema{Type: TypeInt},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
State: nil,
|
||||||
|
|
||||||
|
Diff: nil,
|
||||||
|
|
||||||
|
Key: "ports",
|
||||||
|
Value: []interface{}{},
|
||||||
|
Ok: false,
|
||||||
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Map
|
||||||
|
*/
|
||||||
|
|
||||||
|
{
|
||||||
|
Schema: map[string]*Schema{
|
||||||
|
"ports": &Schema{
|
||||||
|
Type: TypeMap,
|
||||||
|
Optional: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
State: nil,
|
||||||
|
|
||||||
|
Diff: nil,
|
||||||
|
|
||||||
|
Key: "ports",
|
||||||
|
Value: map[string]interface{}{},
|
||||||
|
Ok: false,
|
||||||
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Set
|
||||||
|
*/
|
||||||
|
|
||||||
|
{
|
||||||
|
Schema: map[string]*Schema{
|
||||||
|
"ports": &Schema{
|
||||||
|
Type: TypeSet,
|
||||||
|
Optional: true,
|
||||||
|
Elem: &Schema{Type: TypeInt},
|
||||||
|
Set: func(a interface{}) int { return a.(int) },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
State: nil,
|
||||||
|
|
||||||
|
Diff: nil,
|
||||||
|
|
||||||
|
Key: "ports",
|
||||||
|
Value: []interface{}{},
|
||||||
|
Ok: false,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
Schema: map[string]*Schema{
|
||||||
|
"ports": &Schema{
|
||||||
|
Type: TypeSet,
|
||||||
|
Optional: true,
|
||||||
|
Elem: &Schema{Type: TypeInt},
|
||||||
|
Set: func(a interface{}) int { return a.(int) },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
State: nil,
|
||||||
|
|
||||||
|
Diff: nil,
|
||||||
|
|
||||||
|
Key: "ports.0",
|
||||||
|
Value: 0,
|
||||||
|
Ok: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tc := range cases {
|
||||||
|
d, err := schemaMap(tc.Schema).Data(tc.State, tc.Diff)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
v, ok := d.GetOk(tc.Key)
|
||||||
|
if s, ok := v.(*Set); ok {
|
||||||
|
v = s.List()
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(v, tc.Value) {
|
||||||
|
t.Fatalf("Bad: %d\n\n%#v", i, v)
|
||||||
|
}
|
||||||
|
if ok != tc.Ok {
|
||||||
|
t.Fatalf("Bad: %d\n\n%#v", i, ok)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestResourceDataHasChange(t *testing.T) {
|
func TestResourceDataHasChange(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
Schema map[string]*Schema
|
Schema map[string]*Schema
|
||||||
|
@ -771,7 +933,7 @@ func TestResourceDataSet(t *testing.T) {
|
||||||
Err: true,
|
Err: true,
|
||||||
|
|
||||||
GetKey: "availability_zone",
|
GetKey: "availability_zone",
|
||||||
GetValue: nil,
|
GetValue: "",
|
||||||
},
|
},
|
||||||
|
|
||||||
// List of primitives, set element
|
// List of primitives, set element
|
||||||
|
|
Loading…
Reference in New Issue