2014-08-13 23:23:22 +02:00
|
|
|
package schema
|
|
|
|
|
2014-08-16 01:32:43 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-18 19:00:41 +02:00
|
|
|
"reflect"
|
2014-08-16 01:32:43 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-08-19 00:41:12 +02:00
|
|
|
"sync"
|
2014-08-16 01:32:43 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-08-17 00:02:51 +02:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2014-08-16 01:32:43 +02:00
|
|
|
)
|
|
|
|
|
2014-08-27 06:52:09 +02:00
|
|
|
// ResourceData is used to query and set the attributes of a resource.
|
|
|
|
//
|
|
|
|
// ResourceData is the primary argument received for CRUD operations on
|
|
|
|
// a resource as well as configuration of a provider. It is a powerful
|
|
|
|
// structure that can be used to not only query data, but check for changes,
|
|
|
|
// define partial state updates, etc.
|
|
|
|
//
|
|
|
|
// The most relevant methods to take a look at are Get, Set, and Partial.
|
|
|
|
type ResourceData struct {
|
|
|
|
// Settable (internally)
|
|
|
|
schema map[string]*Schema
|
|
|
|
config *terraform.ResourceConfig
|
2014-09-17 02:07:13 +02:00
|
|
|
state *terraform.InstanceState
|
2014-09-18 01:33:24 +02:00
|
|
|
diff *terraform.InstanceDiff
|
2014-08-27 06:52:09 +02:00
|
|
|
diffing bool
|
|
|
|
|
|
|
|
// Don't set
|
2015-01-09 03:02:19 +01:00
|
|
|
multiReader *MultiLevelFieldReader
|
|
|
|
setMap map[string]string
|
|
|
|
newState *terraform.InstanceState
|
|
|
|
partial bool
|
|
|
|
partialMap map[string]struct{}
|
|
|
|
once sync.Once
|
2014-08-27 06:52:09 +02:00
|
|
|
}
|
|
|
|
|
2014-08-18 18:58:44 +02:00
|
|
|
// getSource represents the level we want to get for a value (internally).
|
|
|
|
// Any source less than or equal to the level will be loaded (whichever
|
|
|
|
// has a value first).
|
|
|
|
type getSource byte
|
|
|
|
|
|
|
|
const (
|
2014-10-11 19:40:54 +02:00
|
|
|
getSourceState getSource = 1 << iota
|
2014-08-21 00:45:34 +02:00
|
|
|
getSourceConfig
|
2014-08-18 18:58:44 +02:00
|
|
|
getSourceSet
|
2014-10-21 00:32:30 +02:00
|
|
|
getSourceExact // Only get from the _exact_ level
|
|
|
|
getSourceDiff // Apply the diff on top our level
|
|
|
|
getSourceLevelMask getSource = getSourceState | getSourceConfig | getSourceSet
|
|
|
|
getSourceMax getSource = getSourceSet
|
2014-08-18 18:58:44 +02:00
|
|
|
)
|
|
|
|
|
2014-08-22 08:03:04 +02:00
|
|
|
// 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 {
|
2014-08-22 21:09:06 +02:00
|
|
|
Value interface{}
|
|
|
|
ValueProcessed interface{}
|
2014-10-10 04:09:06 +02:00
|
|
|
Computed bool
|
2014-08-22 21:09:06 +02:00
|
|
|
Exists bool
|
|
|
|
Schema *Schema
|
2014-08-22 08:03:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var getResultEmpty getResult
|
|
|
|
|
|
|
|
// Get returns the data for the given key, or nil if the key doesn't exist
|
|
|
|
// in the schema.
|
2014-08-16 02:46:05 +02:00
|
|
|
//
|
2014-08-22 08:03:04 +02:00
|
|
|
// If the key does exist in the schema but doesn't exist in the configuration,
|
|
|
|
// then the default value for that type will be returned. For strings, this is
|
|
|
|
// "", for numbers it is 0, etc.
|
|
|
|
//
|
2014-08-27 06:52:09 +02:00
|
|
|
// If you want to test if something is set at all in the configuration,
|
|
|
|
// use GetOk.
|
2014-08-15 04:55:47 +02:00
|
|
|
func (d *ResourceData) Get(key string) interface{} {
|
2014-08-22 08:03:04 +02:00
|
|
|
v, _ := d.GetOk(key)
|
|
|
|
return v
|
2014-08-18 18:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetChange returns the old and new value for a given key.
|
|
|
|
//
|
2014-08-27 06:52:09 +02:00
|
|
|
// HasChange should be used to check if a change exists. It is possible
|
|
|
|
// that both the old and new value are the same if the old value was not
|
|
|
|
// set and the new value is. This is common, for example, for boolean
|
|
|
|
// fields which have a zero value of false.
|
2014-08-18 18:58:44 +02:00
|
|
|
func (d *ResourceData) GetChange(key string) (interface{}, interface{}) {
|
2014-10-21 00:32:30 +02:00
|
|
|
o, n := d.getChange(key, getSourceConfig, getSourceConfig|getSourceDiff)
|
2014-08-22 08:03:04 +02:00
|
|
|
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.
|
2014-08-27 06:52:09 +02:00
|
|
|
//
|
|
|
|
// The first result will not necessarilly be nil if the value doesn't exist.
|
|
|
|
// The second result should be checked to determine this information.
|
2014-08-22 08:03:04 +02:00
|
|
|
func (d *ResourceData) GetOk(key string) (interface{}, bool) {
|
2014-10-21 00:32:30 +02:00
|
|
|
r := d.getRaw(key, getSourceSet|getSourceDiff)
|
2014-08-22 21:09:06 +02:00
|
|
|
return r.Value, r.Exists
|
|
|
|
}
|
|
|
|
|
2014-08-27 05:19:44 +02:00
|
|
|
func (d *ResourceData) getRaw(key string, level getSource) getResult {
|
2014-08-22 08:03:04 +02:00
|
|
|
var parts []string
|
|
|
|
if key != "" {
|
|
|
|
parts = strings.Split(key, ".")
|
|
|
|
}
|
|
|
|
|
2014-08-27 05:19:44 +02:00
|
|
|
return d.getObject("", parts, d.schema, level)
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
|
|
|
|
2014-08-18 19:00:41 +02:00
|
|
|
// HasChange returns whether or not the given key has been changed.
|
|
|
|
func (d *ResourceData) HasChange(key string) bool {
|
|
|
|
o, n := d.GetChange(key)
|
|
|
|
return !reflect.DeepEqual(o, n)
|
|
|
|
}
|
|
|
|
|
2014-12-15 23:02:16 +01:00
|
|
|
// hasComputedSubKeys walks through a schema and returns whether or not the
|
2014-12-12 23:21:20 +01:00
|
|
|
// given key contains any subkeys that are computed.
|
|
|
|
func (d *ResourceData) hasComputedSubKeys(key string, schema *Schema) bool {
|
|
|
|
prefix := key + "."
|
|
|
|
|
|
|
|
switch t := schema.Elem.(type) {
|
|
|
|
case *Resource:
|
|
|
|
for k, schema := range t.Schema {
|
|
|
|
if d.config.IsComputed(prefix + k) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if d.hasComputedSubKeys(prefix+k, schema) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-08-27 05:19:44 +02:00
|
|
|
// Partial turns partial state mode on/off.
|
|
|
|
//
|
|
|
|
// When partial state mode is enabled, then only key prefixes specified
|
|
|
|
// by SetPartial will be in the final state. This allows providers to return
|
|
|
|
// partial states for partially applied resources (when errors occur).
|
|
|
|
func (d *ResourceData) Partial(on bool) {
|
|
|
|
d.partial = on
|
|
|
|
if on {
|
2014-10-09 01:35:14 +02:00
|
|
|
if d.partialMap == nil {
|
|
|
|
d.partialMap = make(map[string]struct{})
|
|
|
|
}
|
2014-08-27 05:19:44 +02:00
|
|
|
} else {
|
|
|
|
d.partialMap = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-17 00:02:51 +02:00
|
|
|
// Set sets the value for the given key.
|
|
|
|
//
|
|
|
|
// If the key is invalid or the value is not a correct type, an error
|
|
|
|
// will be returned.
|
|
|
|
func (d *ResourceData) Set(key string, value interface{}) error {
|
2015-01-09 03:02:19 +01:00
|
|
|
d.once.Do(d.init)
|
2014-08-17 00:02:51 +02:00
|
|
|
parts := strings.Split(key, ".")
|
|
|
|
return d.setObject("", parts, d.schema, value)
|
|
|
|
}
|
|
|
|
|
2014-08-27 05:19:44 +02:00
|
|
|
// SetPartial adds the key prefix to the final state output while
|
|
|
|
// in partial state mode.
|
|
|
|
//
|
|
|
|
// If partial state mode is disabled, then this has no effect. Additionally,
|
|
|
|
// whenever partial state mode is toggled, the partial data is cleared.
|
|
|
|
func (d *ResourceData) SetPartial(k string) {
|
|
|
|
if d.partial {
|
|
|
|
d.partialMap[k] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 18:32:40 +02:00
|
|
|
// Id returns the ID of the resource.
|
|
|
|
func (d *ResourceData) Id() string {
|
|
|
|
var result string
|
|
|
|
|
|
|
|
if d.state != nil {
|
|
|
|
result = d.state.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.newState != nil {
|
|
|
|
result = d.newState.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-08-22 07:15:47 +02:00
|
|
|
// ConnInfo returns the connection info for this resource.
|
|
|
|
func (d *ResourceData) ConnInfo() map[string]string {
|
|
|
|
if d.newState != nil {
|
2014-09-17 02:07:13 +02:00
|
|
|
return d.newState.Ephemeral.ConnInfo
|
2014-08-22 07:15:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.state != nil {
|
2014-09-17 02:07:13 +02:00
|
|
|
return d.state.Ephemeral.ConnInfo
|
2014-08-19 00:41:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-18 05:20:11 +02:00
|
|
|
// SetId sets the ID of the resource. If the value is blank, then the
|
|
|
|
// resource is destroyed.
|
|
|
|
func (d *ResourceData) SetId(v string) {
|
2014-08-19 00:41:12 +02:00
|
|
|
d.once.Do(d.init)
|
2014-08-18 05:20:11 +02:00
|
|
|
d.newState.ID = v
|
|
|
|
}
|
|
|
|
|
2014-08-22 07:15:47 +02:00
|
|
|
// SetConnInfo sets the connection info for a resource.
|
|
|
|
func (d *ResourceData) SetConnInfo(v map[string]string) {
|
|
|
|
d.once.Do(d.init)
|
2014-09-17 02:07:13 +02:00
|
|
|
d.newState.Ephemeral.ConnInfo = v
|
2014-08-19 00:41:12 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 02:07:13 +02:00
|
|
|
// State returns the new InstanceState after the diff and any Set
|
2014-08-17 23:12:54 +02:00
|
|
|
// calls.
|
2014-09-17 02:07:13 +02:00
|
|
|
func (d *ResourceData) State() *terraform.InstanceState {
|
|
|
|
var result terraform.InstanceState
|
2014-08-18 18:32:40 +02:00
|
|
|
result.ID = d.Id()
|
2014-08-22 07:19:33 +02:00
|
|
|
|
|
|
|
// If we have no ID, then this resource doesn't exist and we just
|
|
|
|
// return nil.
|
|
|
|
if result.ID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-17 23:12:54 +02:00
|
|
|
result.Attributes = d.stateObject("", d.schema)
|
2014-09-17 02:07:13 +02:00
|
|
|
result.Ephemeral.ConnInfo = d.ConnInfo()
|
2014-08-18 04:45:26 +02:00
|
|
|
|
2014-08-20 19:37:59 +02:00
|
|
|
if v := d.Id(); v != "" {
|
|
|
|
result.Attributes["id"] = d.Id()
|
|
|
|
}
|
|
|
|
|
2014-08-17 23:12:54 +02:00
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2014-08-19 00:41:12 +02:00
|
|
|
func (d *ResourceData) init() {
|
2015-01-09 03:02:19 +01:00
|
|
|
// Initialize the field that will store our new state
|
2014-09-17 02:07:13 +02:00
|
|
|
var copyState terraform.InstanceState
|
2014-08-19 00:41:12 +02:00
|
|
|
if d.state != nil {
|
|
|
|
copyState = *d.state
|
|
|
|
}
|
|
|
|
d.newState = ©State
|
2015-01-09 03:02:19 +01:00
|
|
|
|
|
|
|
// Initialize the map for storing set data
|
|
|
|
d.setMap = make(map[string]string)
|
|
|
|
|
|
|
|
// Initialize the reader for getting data from the
|
|
|
|
// underlying sources (config, diff, etc.)
|
|
|
|
readers := make(map[string]FieldReader)
|
|
|
|
var stateAttributes map[string]string
|
|
|
|
if d.state != nil {
|
|
|
|
stateAttributes = d.state.Attributes
|
|
|
|
readers["state"] = &MapFieldReader{
|
|
|
|
Schema: d.schema,
|
|
|
|
Map: BasicMapReader(stateAttributes),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.config != nil {
|
|
|
|
readers["config"] = &ConfigFieldReader{
|
|
|
|
Schema: d.schema,
|
|
|
|
Config: d.config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.diff != nil {
|
|
|
|
readers["diff"] = &DiffFieldReader{
|
|
|
|
Schema: d.schema,
|
|
|
|
Diff: d.diff,
|
|
|
|
Source: &MultiLevelFieldReader{
|
|
|
|
Levels: []string{"state", "config"},
|
|
|
|
Readers: readers,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
readers["set"] = &MapFieldReader{
|
|
|
|
Schema: d.schema,
|
|
|
|
Map: BasicMapReader(d.setMap),
|
|
|
|
}
|
|
|
|
d.multiReader = &MultiLevelFieldReader{
|
|
|
|
Levels: []string{
|
|
|
|
"state",
|
|
|
|
"config",
|
|
|
|
"diff",
|
|
|
|
"set",
|
|
|
|
},
|
|
|
|
|
|
|
|
Readers: readers,
|
|
|
|
}
|
2014-08-19 00:41:12 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 04:09:06 +02:00
|
|
|
func (d *ResourceData) diffChange(
|
|
|
|
k string) (interface{}, interface{}, bool, bool) {
|
2014-08-21 00:45:34 +02:00
|
|
|
// Get the change between the state and the config.
|
2014-10-11 19:40:54 +02:00
|
|
|
o, n := d.getChange(k, getSourceState, getSourceConfig|getSourceExact)
|
2014-08-22 08:03:04 +02:00
|
|
|
if !o.Exists {
|
|
|
|
o.Value = nil
|
|
|
|
}
|
|
|
|
if !n.Exists {
|
|
|
|
n.Value = nil
|
|
|
|
}
|
2014-08-21 00:45:34 +02:00
|
|
|
|
|
|
|
// Return the old, new, and whether there is a change
|
2014-10-10 04:09:06 +02:00
|
|
|
return o.Value, n.Value, !reflect.DeepEqual(o.Value, n.Value), n.Computed
|
2014-08-21 00:45:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ResourceData) getChange(
|
|
|
|
key string,
|
|
|
|
oldLevel getSource,
|
2014-08-22 08:03:04 +02:00
|
|
|
newLevel getSource) (getResult, getResult) {
|
2014-08-21 06:02:42 +02:00
|
|
|
var parts, parts2 []string
|
2014-08-21 00:45:34 +02:00
|
|
|
if key != "" {
|
|
|
|
parts = strings.Split(key, ".")
|
2014-08-21 06:02:42 +02:00
|
|
|
parts2 = strings.Split(key, ".")
|
2014-08-21 00:45:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
o := d.getObject("", parts, d.schema, oldLevel)
|
2014-08-21 06:02:42 +02:00
|
|
|
n := d.getObject("", parts2, d.schema, newLevel)
|
2014-08-21 00:45:34 +02:00
|
|
|
return o, n
|
|
|
|
}
|
|
|
|
|
2014-08-16 01:32:43 +02:00
|
|
|
func (d *ResourceData) get(
|
|
|
|
k string,
|
|
|
|
parts []string,
|
2014-08-18 18:58:44 +02:00
|
|
|
schema *Schema,
|
2014-08-22 08:03:04 +02:00
|
|
|
source getSource) getResult {
|
2015-01-09 03:02:19 +01:00
|
|
|
d.once.Do(d.init)
|
|
|
|
|
|
|
|
level := "set"
|
|
|
|
flags := source & ^getSourceLevelMask
|
|
|
|
diff := flags&getSourceDiff != 0
|
|
|
|
exact := flags&getSourceExact != 0
|
|
|
|
source = source & getSourceLevelMask
|
|
|
|
if source >= getSourceSet {
|
|
|
|
level = "set"
|
|
|
|
} else if diff {
|
|
|
|
level = "diff"
|
|
|
|
} else if source >= getSourceConfig {
|
|
|
|
level = "config"
|
|
|
|
} else {
|
|
|
|
level = "state"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the address of the key we're looking for and ask the FieldReader
|
|
|
|
addr := append(strings.Split(k, "."), parts...)
|
|
|
|
for i, v := range addr {
|
|
|
|
if v[0] == '~' {
|
|
|
|
addr[i] = v[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var result FieldReadResult
|
|
|
|
var err error
|
|
|
|
if exact {
|
|
|
|
result, err = d.multiReader.ReadFieldExact(addr, level)
|
|
|
|
} else {
|
|
|
|
result, err = d.multiReader.ReadFieldMerge(addr, level)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the result doesn't exist, then we set the value to the zero value
|
|
|
|
if result.Value == nil {
|
2015-01-09 03:47:35 +01:00
|
|
|
if schemaL := addrToSchema(addr, d.schema); len(schemaL) > 0 {
|
|
|
|
schema := schemaL[len(schemaL)-1]
|
|
|
|
result.Value = schema.Type.Zero()
|
|
|
|
|
|
|
|
// The zero value of a set is nil, but we want it
|
|
|
|
// to actually be an empty set object...
|
|
|
|
if schema.Type == TypeSet && result.Value == nil {
|
|
|
|
result.Value = &Set{F: schema.Set}
|
|
|
|
}
|
2015-01-09 03:02:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transform the FieldReadResult into a getResult. It might be worth
|
|
|
|
// merging these two structures one day.
|
|
|
|
return getResult{
|
|
|
|
Value: result.Value,
|
|
|
|
ValueProcessed: result.ValueProcessed,
|
|
|
|
Computed: result.Computed,
|
|
|
|
Exists: result.Exists,
|
|
|
|
Schema: schema,
|
2014-08-21 02:51:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ResourceData) getSet(
|
|
|
|
k string,
|
|
|
|
parts []string,
|
|
|
|
schema *Schema,
|
2014-08-22 08:03:04 +02:00
|
|
|
source getSource) getResult {
|
2014-08-21 06:02:42 +02:00
|
|
|
s := &Set{F: schema.Set}
|
2014-08-22 17:45:54 +02:00
|
|
|
result := getResult{Schema: schema, Value: s}
|
2014-12-12 23:21:20 +01:00
|
|
|
prefix := k + "."
|
2014-10-11 19:40:54 +02:00
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
// Get the set. For sets, the entire source must be exact: the
|
2014-10-11 19:40:54 +02:00
|
|
|
// entire set must come from set, diff, state, etc. So we go backwards
|
|
|
|
// and once we get a result, we take it. Or, we never get a result.
|
2014-12-12 23:21:20 +01:00
|
|
|
var indexMap map[int]int
|
|
|
|
codes := make(map[string]int)
|
2014-10-21 00:32:30 +02:00
|
|
|
sourceLevel := source & getSourceLevelMask
|
|
|
|
sourceFlags := source & ^getSourceLevelMask
|
2014-12-12 23:21:20 +01:00
|
|
|
sourceDiff := sourceFlags&getSourceDiff != 0
|
|
|
|
for setSource := sourceLevel; setSource > 0; setSource >>= 1 {
|
2014-10-21 00:32:30 +02:00
|
|
|
// If we're already asking for an exact source and it doesn't
|
|
|
|
// match, then leave since the original source was the match.
|
2014-12-12 23:21:20 +01:00
|
|
|
if sourceFlags&getSourceExact != 0 && setSource != sourceLevel {
|
2014-10-11 19:40:54 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
if d.config != nil && setSource == getSourceConfig {
|
|
|
|
raw := d.getList(k, nil, schema, setSource)
|
|
|
|
// If the entire list is computed, then the entire set is
|
|
|
|
// necessarilly computed.
|
|
|
|
if raw.Computed {
|
|
|
|
result.Computed = true
|
|
|
|
if len(parts) > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
if raw.Exists {
|
|
|
|
result.Exists = true
|
|
|
|
|
|
|
|
list := raw.Value.([]interface{})
|
|
|
|
indexMap = make(map[int]int, len(list))
|
|
|
|
|
|
|
|
// Build the set from all the items using the given hash code
|
|
|
|
for i, v := range list {
|
|
|
|
code := s.add(v)
|
|
|
|
|
|
|
|
// Check if any of the keys in this item are computed
|
|
|
|
computed := false
|
|
|
|
if len(d.config.ComputedKeys) > 0 {
|
|
|
|
prefix := fmt.Sprintf("%s.%d", k, i)
|
|
|
|
computed = d.hasComputedSubKeys(prefix, schema)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we are computed and if so negatate the hash to
|
|
|
|
// this is a approximate hash
|
|
|
|
if computed {
|
|
|
|
s.m[-code] = s.m[code]
|
|
|
|
delete(s.m, code)
|
|
|
|
code = -code
|
|
|
|
}
|
|
|
|
indexMap[code] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
2014-10-11 19:40:54 +02:00
|
|
|
}
|
2014-12-12 23:21:20 +01:00
|
|
|
|
|
|
|
if d.state != nil && setSource == getSourceState {
|
|
|
|
for k, _ := range d.state.Attributes {
|
|
|
|
if !strings.HasPrefix(k, prefix) || strings.HasPrefix(k, prefix+"#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
parts := strings.Split(k[len(prefix):], ".")
|
|
|
|
idx := parts[0]
|
|
|
|
if _, ok := codes[idx]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
code, err := strconv.Atoi(strings.Replace(parts[0], "~", "-", -1))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unable to convert %s to int: %v", idx, err))
|
|
|
|
}
|
|
|
|
codes[idx] = code
|
|
|
|
}
|
2014-08-21 06:02:42 +02:00
|
|
|
}
|
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
if d.setMap != nil && setSource == getSourceSet {
|
|
|
|
for k, _ := range d.setMap {
|
|
|
|
if !strings.HasPrefix(k, prefix) || strings.HasPrefix(k, prefix+"#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
parts := strings.Split(k[len(prefix):], ".")
|
|
|
|
idx := parts[0]
|
|
|
|
if _, ok := codes[idx]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2014-08-21 02:51:27 +02:00
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
code, err := strconv.Atoi(strings.Replace(parts[0], "~", "-", -1))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unable to convert %s to int: %v", idx, err))
|
|
|
|
}
|
|
|
|
codes[idx] = code
|
|
|
|
}
|
|
|
|
}
|
2014-10-10 18:13:04 +02:00
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
if d.diff != nil && sourceDiff {
|
|
|
|
for k, _ := range d.diff.Attributes {
|
|
|
|
if !strings.HasPrefix(k, prefix) || strings.HasPrefix(k, prefix+"#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
parts := strings.Split(k[len(prefix):], ".")
|
|
|
|
idx := parts[0]
|
|
|
|
if _, ok := codes[idx]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
code, err := strconv.Atoi(strings.Replace(parts[0], "~", "-", -1))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unable to convert %s to int: %v", idx, err))
|
|
|
|
}
|
|
|
|
codes[idx] = code
|
|
|
|
}
|
2014-08-21 06:02:42 +02:00
|
|
|
}
|
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
if len(codes) > 0 {
|
|
|
|
break
|
|
|
|
}
|
2014-10-21 08:14:14 +02:00
|
|
|
}
|
2014-08-21 02:51:27 +02:00
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
if indexMap == nil {
|
|
|
|
s.m = make(map[int]interface{})
|
|
|
|
for idx, code := range codes {
|
|
|
|
switch t := schema.Elem.(type) {
|
|
|
|
case *Resource:
|
|
|
|
// Get the entire object
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
for field, _ := range t.Schema {
|
|
|
|
m[field] = d.getObject(prefix+idx, []string{field}, t.Schema, source).Value
|
|
|
|
}
|
|
|
|
s.m[code] = m
|
|
|
|
result.Exists = true
|
2014-12-15 23:02:16 +01:00
|
|
|
case *Schema:
|
|
|
|
// Get a single value
|
|
|
|
s.m[code] = d.get(prefix+idx, nil, t, source).Value
|
|
|
|
result.Exists = true
|
2014-12-12 23:21:20 +01:00
|
|
|
}
|
2014-08-21 02:51:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parts) > 0 {
|
2014-12-12 23:21:20 +01:00
|
|
|
// We still have parts left over meaning we're accessing an
|
|
|
|
// element of this set.
|
|
|
|
idx := parts[0]
|
|
|
|
parts = parts[1:]
|
|
|
|
|
|
|
|
// Special case if we're accessing the count of the set
|
|
|
|
if idx == "#" {
|
|
|
|
schema := &Schema{Type: TypeInt}
|
|
|
|
return d.get(prefix+"#", parts, schema, source)
|
2014-08-21 02:51:27 +02:00
|
|
|
}
|
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
if source&getSourceLevelMask == getSourceConfig {
|
|
|
|
i, err := strconv.Atoi(strings.Replace(idx, "~", "-", -1))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unable to convert %s to int: %v", idx, err))
|
|
|
|
}
|
|
|
|
if i, ok := indexMap[i]; ok {
|
|
|
|
idx = strconv.Itoa(i)
|
|
|
|
}
|
2014-08-21 02:51:27 +02:00
|
|
|
}
|
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
switch t := schema.Elem.(type) {
|
|
|
|
case *Resource:
|
|
|
|
return d.getObject(prefix+idx, parts, t.Schema, source)
|
2014-12-15 23:02:16 +01:00
|
|
|
case *Schema:
|
|
|
|
return d.get(prefix+idx, parts, t, source)
|
2014-12-12 23:21:20 +01:00
|
|
|
}
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
2014-08-21 02:51:27 +02:00
|
|
|
|
2014-08-22 08:03:04 +02:00
|
|
|
return result
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
|
|
|
|
2014-08-18 23:00:03 +02:00
|
|
|
func (d *ResourceData) getMap(
|
|
|
|
k string,
|
|
|
|
parts []string,
|
|
|
|
schema *Schema,
|
2014-08-22 08:03:04 +02:00
|
|
|
source getSource) getResult {
|
2014-08-18 23:00:03 +02:00
|
|
|
elemSchema := &Schema{Type: TypeString}
|
|
|
|
|
2014-08-19 00:07:09 +02:00
|
|
|
result := make(map[string]interface{})
|
2014-08-22 08:03:04 +02:00
|
|
|
resultSet := false
|
2014-08-18 23:00:03 +02:00
|
|
|
prefix := k + "."
|
|
|
|
|
2014-10-21 00:32:30 +02:00
|
|
|
flags := source & ^getSourceLevelMask
|
|
|
|
level := source & getSourceLevelMask
|
|
|
|
exact := flags&getSourceExact != 0
|
|
|
|
diff := flags&getSourceDiff != 0
|
2014-08-18 23:00:03 +02:00
|
|
|
|
2014-10-21 00:32:30 +02:00
|
|
|
if !exact || level == getSourceState {
|
|
|
|
if d.state != nil && level >= getSourceState {
|
2014-10-11 19:40:54 +02:00
|
|
|
for k, _ := range d.state.Attributes {
|
|
|
|
if !strings.HasPrefix(k, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
single := k[len(prefix):]
|
|
|
|
result[single] = d.getPrimitive(k, nil, elemSchema, source).Value
|
|
|
|
resultSet = true
|
|
|
|
}
|
2014-08-18 23:00:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 00:32:30 +02:00
|
|
|
if d.config != nil && level == getSourceConfig {
|
2014-08-21 00:45:34 +02:00
|
|
|
// For config, we always set the result to exactly what was requested
|
2014-10-09 02:35:14 +02:00
|
|
|
if mraw, ok := d.config.Get(k); ok {
|
2014-10-09 03:21:21 +02:00
|
|
|
result = make(map[string]interface{})
|
2014-10-09 02:35:14 +02:00
|
|
|
switch m := mraw.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
for _, innerRaw := range m {
|
|
|
|
for k, v := range innerRaw.(map[string]interface{}) {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 02:54:00 +02:00
|
|
|
resultSet = true
|
|
|
|
case []map[string]interface{}:
|
|
|
|
for _, innerRaw := range m {
|
|
|
|
for k, v := range innerRaw {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 02:35:14 +02:00
|
|
|
resultSet = true
|
|
|
|
case map[string]interface{}:
|
|
|
|
result = m
|
|
|
|
resultSet = true
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown type: %#v", mraw))
|
|
|
|
}
|
2014-08-21 00:45:34 +02:00
|
|
|
} else {
|
|
|
|
result = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 00:32:30 +02:00
|
|
|
if d.diff != nil && diff {
|
|
|
|
for k, v := range d.diff.Attributes {
|
|
|
|
if !strings.HasPrefix(k, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
resultSet = true
|
2014-08-18 23:00:03 +02:00
|
|
|
|
2014-10-21 00:32:30 +02:00
|
|
|
single := k[len(prefix):]
|
2014-08-18 23:00:03 +02:00
|
|
|
|
2014-10-21 00:32:30 +02:00
|
|
|
if v.NewRemoved {
|
|
|
|
delete(result, single)
|
|
|
|
} else {
|
|
|
|
result[single] = d.getPrimitive(k, nil, elemSchema, source).Value
|
2014-08-19 00:07:09 +02:00
|
|
|
}
|
2014-08-18 23:00:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 00:32:30 +02:00
|
|
|
if !exact || level == getSourceSet {
|
|
|
|
if d.setMap != nil && level >= getSourceSet {
|
2014-10-11 19:40:54 +02:00
|
|
|
cleared := false
|
2014-10-21 09:06:37 +02:00
|
|
|
if v, ok := d.setMap[k]; ok && v == "" {
|
|
|
|
// We've cleared the map
|
|
|
|
result = make(map[string]interface{})
|
2014-10-11 19:40:54 +02:00
|
|
|
resultSet = true
|
2014-10-21 09:06:37 +02:00
|
|
|
} else {
|
|
|
|
for k, _ := range d.setMap {
|
|
|
|
if !strings.HasPrefix(k, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
resultSet = true
|
2014-08-22 08:03:04 +02:00
|
|
|
|
2014-10-21 09:06:37 +02:00
|
|
|
if !cleared {
|
|
|
|
// We clear the results if they are in the set map
|
|
|
|
result = make(map[string]interface{})
|
|
|
|
cleared = true
|
|
|
|
}
|
2014-08-18 23:00:03 +02:00
|
|
|
|
2014-10-21 09:06:37 +02:00
|
|
|
single := k[len(prefix):]
|
|
|
|
result[single] = d.getPrimitive(
|
|
|
|
k, nil, elemSchema, source).Value
|
|
|
|
}
|
2014-10-11 19:40:54 +02:00
|
|
|
}
|
2014-08-18 23:00:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 02:51:27 +02:00
|
|
|
// If we're requesting a specific element, return that
|
2014-08-22 08:03:04 +02:00
|
|
|
var resultValue interface{} = result
|
2014-08-21 02:51:27 +02:00
|
|
|
if len(parts) > 0 {
|
2014-08-22 08:03:04 +02:00
|
|
|
resultValue = result[parts[0]]
|
2014-08-21 02:51:27 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 08:03:04 +02:00
|
|
|
return getResult{
|
|
|
|
Value: resultValue,
|
|
|
|
Exists: resultSet,
|
2014-08-22 17:45:54 +02:00
|
|
|
Schema: schema,
|
2014-08-22 08:03:04 +02:00
|
|
|
}
|
2014-08-18 23:00:03 +02:00
|
|
|
}
|
|
|
|
|
2014-08-16 01:32:43 +02:00
|
|
|
func (d *ResourceData) getObject(
|
|
|
|
k string,
|
|
|
|
parts []string,
|
2014-08-18 18:58:44 +02:00
|
|
|
schema map[string]*Schema,
|
2014-08-22 08:03:04 +02:00
|
|
|
source getSource) getResult {
|
2014-08-16 02:39:08 +02:00
|
|
|
if len(parts) > 0 {
|
|
|
|
// We're requesting a specific key in an object
|
|
|
|
key := parts[0]
|
|
|
|
parts = parts[1:]
|
|
|
|
s, ok := schema[key]
|
|
|
|
if !ok {
|
2014-08-22 08:03:04 +02:00
|
|
|
return getResultEmpty
|
2014-08-16 02:39:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if k != "" {
|
|
|
|
// If we're not at the root, then we need to append
|
|
|
|
// the key to get the full key path.
|
|
|
|
key = fmt.Sprintf("%s.%s", k, key)
|
|
|
|
}
|
|
|
|
|
2014-08-18 18:58:44 +02:00
|
|
|
return d.get(key, parts, s, source)
|
2014-08-16 02:39:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the entire object
|
|
|
|
result := make(map[string]interface{})
|
|
|
|
for field, _ := range schema {
|
2014-08-22 08:03:04 +02:00
|
|
|
result[field] = d.getObject(k, []string{field}, schema, source).Value
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 08:03:04 +02:00
|
|
|
return getResult{
|
|
|
|
Value: result,
|
|
|
|
Exists: true,
|
2014-08-22 17:45:54 +02:00
|
|
|
Schema: &Schema{
|
|
|
|
Elem: schema,
|
|
|
|
},
|
2014-08-22 08:03:04 +02:00
|
|
|
}
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ResourceData) getList(
|
|
|
|
k string,
|
|
|
|
parts []string,
|
2014-08-18 18:58:44 +02:00
|
|
|
schema *Schema,
|
2014-08-22 08:03:04 +02:00
|
|
|
source getSource) getResult {
|
2014-08-16 01:32:43 +02:00
|
|
|
if len(parts) > 0 {
|
|
|
|
// We still have parts left over meaning we're accessing an
|
|
|
|
// element of this list.
|
|
|
|
idx := parts[0]
|
|
|
|
parts = parts[1:]
|
|
|
|
|
|
|
|
// Special case if we're accessing the count of the list
|
|
|
|
if idx == "#" {
|
|
|
|
schema := &Schema{Type: TypeInt}
|
2014-08-22 08:03:04 +02:00
|
|
|
return d.get(k+".#", parts, schema, source)
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
key := fmt.Sprintf("%s.%s", k, idx)
|
|
|
|
switch t := schema.Elem.(type) {
|
|
|
|
case *Resource:
|
2014-08-18 18:58:44 +02:00
|
|
|
return d.getObject(key, parts, t.Schema, source)
|
2014-08-16 01:32:43 +02:00
|
|
|
case *Schema:
|
2014-08-18 18:58:44 +02:00
|
|
|
return d.get(key, parts, t, source)
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the entire list.
|
2014-10-10 04:09:06 +02:00
|
|
|
var result []interface{}
|
2014-08-22 08:03:04 +02:00
|
|
|
count := d.getList(k, []string{"#"}, schema, source)
|
2014-10-10 04:09:06 +02:00
|
|
|
if !count.Computed {
|
|
|
|
result = make([]interface{}, count.Value.(int))
|
|
|
|
for i, _ := range result {
|
|
|
|
is := strconv.FormatInt(int64(i), 10)
|
|
|
|
result[i] = d.getList(k, []string{is}, schema, source).Value
|
|
|
|
}
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 08:03:04 +02:00
|
|
|
return getResult{
|
2014-10-10 04:09:06 +02:00
|
|
|
Value: result,
|
|
|
|
Computed: count.Computed,
|
|
|
|
Exists: count.Exists,
|
|
|
|
Schema: schema,
|
2014-08-22 08:03:04 +02:00
|
|
|
}
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ResourceData) getPrimitive(
|
|
|
|
k string,
|
|
|
|
parts []string,
|
2014-08-18 18:58:44 +02:00
|
|
|
schema *Schema,
|
2014-08-22 08:03:04 +02:00
|
|
|
source getSource) getResult {
|
2014-08-16 01:32:43 +02:00
|
|
|
var result string
|
2014-08-22 21:09:06 +02:00
|
|
|
var resultProcessed interface{}
|
2014-10-10 04:09:06 +02:00
|
|
|
var resultComputed, resultSet bool
|
2014-10-21 00:32:30 +02:00
|
|
|
flags := source & ^getSourceLevelMask
|
|
|
|
source = source & getSourceLevelMask
|
|
|
|
exact := flags&getSourceExact != 0
|
|
|
|
diff := flags&getSourceDiff != 0
|
2014-10-11 19:40:54 +02:00
|
|
|
|
|
|
|
if !exact || source == getSourceState {
|
|
|
|
if d.state != nil && source >= getSourceState {
|
|
|
|
result, resultSet = d.state.Attributes[k]
|
|
|
|
}
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
|
|
|
|
2014-10-11 19:40:54 +02:00
|
|
|
// No exact check is needed here because config is always exact
|
2014-08-21 00:45:34 +02:00
|
|
|
if d.config != nil && source == getSourceConfig {
|
|
|
|
// For config, we always return the exact value
|
|
|
|
if v, ok := d.config.Get(k); ok {
|
|
|
|
if err := mapstructure.WeakDecode(v, &result); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-08-21 06:02:42 +02:00
|
|
|
|
|
|
|
resultSet = true
|
2014-08-21 00:45:34 +02:00
|
|
|
} else {
|
|
|
|
result = ""
|
2014-08-21 06:02:42 +02:00
|
|
|
resultSet = false
|
2014-08-21 00:45:34 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 04:09:06 +02:00
|
|
|
// If it is computed, set that.
|
|
|
|
resultComputed = d.config.IsComputed(k)
|
2014-08-21 00:45:34 +02:00
|
|
|
}
|
|
|
|
|
2014-10-21 00:32:30 +02:00
|
|
|
if d.diff != nil && diff {
|
|
|
|
attrD, ok := d.diff.Attributes[k]
|
|
|
|
if ok {
|
|
|
|
if !attrD.NewComputed {
|
|
|
|
result = attrD.New
|
|
|
|
if attrD.NewExtra != nil {
|
|
|
|
// If NewExtra != nil, then we have processed data as the New,
|
|
|
|
// so we store that but decode the unprocessed data into result
|
|
|
|
resultProcessed = result
|
2014-08-22 17:57:44 +02:00
|
|
|
|
2014-10-21 00:32:30 +02:00
|
|
|
err := mapstructure.WeakDecode(attrD.NewExtra, &result)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-10-11 19:40:54 +02:00
|
|
|
}
|
2014-10-21 00:32:30 +02:00
|
|
|
|
|
|
|
resultSet = true
|
|
|
|
} else {
|
|
|
|
result = ""
|
|
|
|
resultSet = false
|
2014-08-28 00:43:47 +02:00
|
|
|
}
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-11 19:40:54 +02:00
|
|
|
if !exact || source == getSourceSet {
|
|
|
|
if d.setMap != nil && source >= getSourceSet {
|
|
|
|
if v, ok := d.setMap[k]; ok {
|
|
|
|
result = v
|
|
|
|
resultSet = true
|
|
|
|
}
|
2014-08-17 00:02:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !resultSet {
|
2014-08-22 08:03:04 +02:00
|
|
|
result = ""
|
2014-08-17 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 08:03:04 +02:00
|
|
|
var resultValue interface{}
|
2014-08-16 01:32:43 +02:00
|
|
|
switch schema.Type {
|
2014-08-20 01:46:36 +02:00
|
|
|
case TypeBool:
|
|
|
|
if result == "" {
|
2014-08-22 08:03:04 +02:00
|
|
|
resultValue = false
|
|
|
|
break
|
2014-08-20 01:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
v, err := strconv.ParseBool(result)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-08-22 08:03:04 +02:00
|
|
|
resultValue = v
|
2014-08-16 01:32:43 +02:00
|
|
|
case TypeString:
|
|
|
|
// Use the value as-is. We just put this case here to be explicit.
|
2014-08-22 08:03:04 +02:00
|
|
|
resultValue = result
|
2014-08-16 01:32:43 +02:00
|
|
|
case TypeInt:
|
|
|
|
if result == "" {
|
2014-08-22 08:03:04 +02:00
|
|
|
resultValue = 0
|
|
|
|
break
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 04:09:06 +02:00
|
|
|
if resultComputed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2014-08-16 01:32:43 +02:00
|
|
|
v, err := strconv.ParseInt(result, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-08-22 08:03:04 +02:00
|
|
|
resultValue = int(v)
|
2014-08-16 01:32:43 +02:00
|
|
|
default:
|
2014-08-25 06:50:35 +02:00
|
|
|
panic(fmt.Sprintf("Unknown type: %#v", schema.Type))
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
2014-08-22 08:03:04 +02:00
|
|
|
|
|
|
|
return getResult{
|
2014-08-22 21:09:06 +02:00
|
|
|
Value: resultValue,
|
|
|
|
ValueProcessed: resultProcessed,
|
2014-10-10 04:09:06 +02:00
|
|
|
Computed: resultComputed,
|
2014-08-22 21:09:06 +02:00
|
|
|
Exists: resultSet,
|
|
|
|
Schema: schema,
|
2014-08-22 08:03:04 +02:00
|
|
|
}
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
2014-08-17 00:02:51 +02:00
|
|
|
|
2014-08-17 20:38:16 +02:00
|
|
|
func (d *ResourceData) set(
|
|
|
|
k string,
|
|
|
|
parts []string,
|
|
|
|
schema *Schema,
|
|
|
|
value interface{}) error {
|
|
|
|
switch schema.Type {
|
|
|
|
case TypeList:
|
|
|
|
return d.setList(k, parts, schema, value)
|
2014-08-18 23:00:03 +02:00
|
|
|
case TypeMap:
|
|
|
|
return d.setMapValue(k, parts, schema, value)
|
2014-08-21 03:11:14 +02:00
|
|
|
case TypeSet:
|
|
|
|
return d.setSet(k, parts, schema, value)
|
|
|
|
case TypeBool:
|
|
|
|
fallthrough
|
|
|
|
case TypeInt:
|
|
|
|
fallthrough
|
|
|
|
case TypeString:
|
2014-08-17 20:38:16 +02:00
|
|
|
return d.setPrimitive(k, schema, value)
|
2014-08-21 03:11:14 +02:00
|
|
|
default:
|
2014-08-25 06:50:35 +02:00
|
|
|
panic(fmt.Sprintf("%s: unknown type %#v", k, schema.Type))
|
2014-08-17 20:38:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ResourceData) setList(
|
|
|
|
k string,
|
|
|
|
parts []string,
|
|
|
|
schema *Schema,
|
|
|
|
value interface{}) error {
|
|
|
|
if len(parts) > 0 {
|
2015-01-09 03:02:19 +01:00
|
|
|
return fmt.Errorf("%s: can only set the full list, not elements", k)
|
|
|
|
}
|
2014-08-17 20:38:16 +02:00
|
|
|
|
2015-01-09 03:02:19 +01:00
|
|
|
setElement := func(k string, idx string, value interface{}) error {
|
2014-08-17 20:38:16 +02:00
|
|
|
if idx == "#" {
|
|
|
|
return fmt.Errorf("%s: can't set count of list", k)
|
|
|
|
}
|
|
|
|
|
|
|
|
key := fmt.Sprintf("%s.%s", k, idx)
|
|
|
|
switch t := schema.Elem.(type) {
|
|
|
|
case *Resource:
|
2015-01-09 03:02:19 +01:00
|
|
|
return d.setObject(key, nil, t.Schema, value)
|
2014-08-17 20:38:16 +02:00
|
|
|
case *Schema:
|
2015-01-09 03:02:19 +01:00
|
|
|
return d.set(key, nil, t, value)
|
2014-08-17 20:38:16 +02:00
|
|
|
}
|
2015-01-09 03:02:19 +01:00
|
|
|
|
|
|
|
return nil
|
2014-08-17 20:38:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var vs []interface{}
|
|
|
|
if err := mapstructure.Decode(value, &vs); err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", k, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the entire list.
|
|
|
|
var err error
|
|
|
|
for i, elem := range vs {
|
|
|
|
is := strconv.FormatInt(int64(i), 10)
|
2015-01-09 03:02:19 +01:00
|
|
|
err = setElement(k, is, elem)
|
2014-08-17 20:38:16 +02:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
for i, _ := range vs {
|
|
|
|
is := strconv.FormatInt(int64(i), 10)
|
2015-01-09 03:02:19 +01:00
|
|
|
setElement(k, is, nil)
|
2014-08-17 20:38:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.setMap[k+".#"] = strconv.FormatInt(int64(len(vs)), 10)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-18 23:00:03 +02:00
|
|
|
func (d *ResourceData) setMapValue(
|
|
|
|
k string,
|
|
|
|
parts []string,
|
|
|
|
schema *Schema,
|
|
|
|
value interface{}) error {
|
|
|
|
elemSchema := &Schema{Type: TypeString}
|
|
|
|
if len(parts) > 0 {
|
|
|
|
return fmt.Errorf("%s: full map must be set, no a single element", k)
|
|
|
|
}
|
|
|
|
|
2014-08-19 00:17:18 +02:00
|
|
|
v := reflect.ValueOf(value)
|
|
|
|
if v.Kind() != reflect.Map {
|
|
|
|
return fmt.Errorf("%s: must be a map", k)
|
|
|
|
}
|
|
|
|
if v.Type().Key().Kind() != reflect.String {
|
|
|
|
return fmt.Errorf("%s: keys must strings", k)
|
|
|
|
}
|
|
|
|
vs := make(map[string]interface{})
|
|
|
|
for _, mk := range v.MapKeys() {
|
|
|
|
mv := v.MapIndex(mk)
|
|
|
|
vs[mk.String()] = mv.Interface()
|
|
|
|
}
|
|
|
|
|
2014-10-21 09:06:37 +02:00
|
|
|
if len(vs) == 0 {
|
|
|
|
// The empty string here means the map is removed.
|
|
|
|
d.setMap[k] = ""
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(d.setMap, k)
|
2014-08-18 23:00:03 +02:00
|
|
|
for subKey, v := range vs {
|
|
|
|
err := d.set(fmt.Sprintf("%s.%s", k, subKey), nil, elemSchema, v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-17 00:02:51 +02:00
|
|
|
func (d *ResourceData) setObject(
|
|
|
|
k string,
|
|
|
|
parts []string,
|
|
|
|
schema map[string]*Schema,
|
|
|
|
value interface{}) error {
|
|
|
|
if len(parts) > 0 {
|
|
|
|
// We're setting a specific key in an object
|
|
|
|
key := parts[0]
|
|
|
|
parts = parts[1:]
|
|
|
|
|
|
|
|
s, ok := schema[key]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%s (internal): unknown key to set: %s", k, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if k != "" {
|
|
|
|
// If we're not at the root, then we need to append
|
|
|
|
// the key to get the full key path.
|
|
|
|
key = fmt.Sprintf("%s.%s", k, key)
|
|
|
|
}
|
|
|
|
|
2014-08-17 20:38:16 +02:00
|
|
|
return d.set(key, parts, s, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the entire object. First decode into a proper structure
|
|
|
|
var v map[string]interface{}
|
|
|
|
if err := mapstructure.Decode(value, &v); err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", k, err)
|
2014-08-17 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
2014-08-17 20:38:16 +02:00
|
|
|
// Set each element in turn
|
|
|
|
var err error
|
|
|
|
for k1, v1 := range v {
|
|
|
|
err = d.setObject(k, []string{k1}, schema, v1)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
for k1, _ := range v {
|
|
|
|
d.setObject(k, []string{k1}, schema, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2014-08-17 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ResourceData) setPrimitive(
|
|
|
|
k string,
|
|
|
|
schema *Schema,
|
|
|
|
v interface{}) error {
|
2014-08-17 20:38:16 +02:00
|
|
|
if v == nil {
|
|
|
|
delete(d.setMap, k)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-17 00:02:51 +02:00
|
|
|
var set string
|
|
|
|
switch schema.Type {
|
2014-08-20 01:46:36 +02:00
|
|
|
case TypeBool:
|
|
|
|
var b bool
|
|
|
|
if err := mapstructure.Decode(v, &b); err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", k, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
set = strconv.FormatBool(b)
|
2014-08-17 00:02:51 +02:00
|
|
|
case TypeString:
|
|
|
|
if err := mapstructure.Decode(v, &set); err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", k, err)
|
|
|
|
}
|
|
|
|
case TypeInt:
|
|
|
|
var n int
|
|
|
|
if err := mapstructure.Decode(v, &n); err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", k, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
set = strconv.FormatInt(int64(n), 10)
|
|
|
|
default:
|
2014-08-25 06:50:35 +02:00
|
|
|
return fmt.Errorf("Unknown type: %#v", schema.Type)
|
2014-08-17 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
2014-08-17 20:38:16 +02:00
|
|
|
d.setMap[k] = set
|
2014-08-17 00:02:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
2014-08-17 23:12:54 +02:00
|
|
|
|
2014-08-21 03:11:14 +02:00
|
|
|
func (d *ResourceData) setSet(
|
|
|
|
k string,
|
|
|
|
parts []string,
|
|
|
|
schema *Schema,
|
|
|
|
value interface{}) error {
|
|
|
|
if len(parts) > 0 {
|
|
|
|
return fmt.Errorf("%s: can only set the full set, not elements", k)
|
|
|
|
}
|
|
|
|
|
2014-10-11 19:40:54 +02:00
|
|
|
// If it is a slice, then we have to turn it into a *Set so that
|
|
|
|
// we get the proper order back based on the hash code.
|
|
|
|
if v := reflect.ValueOf(value); v.Kind() == reflect.Slice {
|
2014-12-12 23:21:20 +01:00
|
|
|
// Build a temp *ResourceData to use for the conversion
|
|
|
|
tempD := &ResourceData{
|
|
|
|
setMap: make(map[string]string),
|
2015-01-09 03:02:19 +01:00
|
|
|
schema: map[string]*Schema{k: schema},
|
2014-12-12 23:21:20 +01:00
|
|
|
}
|
2015-01-09 03:02:19 +01:00
|
|
|
tempD.once.Do(tempD.init)
|
2014-12-12 23:21:20 +01:00
|
|
|
|
2014-10-11 19:40:54 +02:00
|
|
|
// Set the entire list, this lets us get sane values out of it
|
2014-12-12 23:21:20 +01:00
|
|
|
if err := tempD.setList(k, nil, schema, value); err != nil {
|
2014-10-11 19:40:54 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the set by going over the list items in order and
|
|
|
|
// hashing them into the set. The reason we go over the list and
|
|
|
|
// not the `value` directly is because this forces all types
|
|
|
|
// to become []interface{} (generic) instead of []string, which
|
|
|
|
// most hash functions are expecting.
|
|
|
|
s := &Set{F: schema.Set}
|
|
|
|
source := getSourceSet | getSourceExact
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
is := strconv.FormatInt(int64(i), 10)
|
2015-01-09 03:02:19 +01:00
|
|
|
result := tempD.get(k, []string{is}, schema, source)
|
2014-10-11 19:40:54 +02:00
|
|
|
if !result.Exists {
|
|
|
|
panic("just set item doesn't exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Add(result.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
value = s
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
switch t := schema.Elem.(type) {
|
|
|
|
case *Resource:
|
|
|
|
for code, elem := range value.(*Set).m {
|
|
|
|
for field, _ := range t.Schema {
|
|
|
|
subK := fmt.Sprintf("%s.%d", k, code)
|
2015-01-09 03:47:35 +01:00
|
|
|
value := elem.(map[string]interface{})[field]
|
|
|
|
err := d.setObject(subK, []string{field}, t.Schema, value)
|
2014-12-12 23:21:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-15 23:02:16 +01:00
|
|
|
case *Schema:
|
|
|
|
for code, elem := range value.(*Set).m {
|
|
|
|
subK := fmt.Sprintf("%s.%d", k, code)
|
|
|
|
err := d.set(subK, nil, t, elem)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 23:21:20 +01:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("%s: unknown element type (internal)", k)
|
2014-08-21 03:11:14 +02:00
|
|
|
}
|
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
d.setMap[k+".#"] = strconv.Itoa(value.(*Set).Len())
|
|
|
|
return nil
|
2014-08-21 03:11:14 +02:00
|
|
|
}
|
|
|
|
|
2014-08-17 23:12:54 +02:00
|
|
|
func (d *ResourceData) stateList(
|
|
|
|
prefix string,
|
|
|
|
schema *Schema) map[string]string {
|
2014-08-27 05:19:44 +02:00
|
|
|
countRaw := d.get(prefix, []string{"#"}, schema, d.stateSource(prefix))
|
2014-08-22 08:03:04 +02:00
|
|
|
if !countRaw.Exists {
|
2014-10-11 00:58:38 +02:00
|
|
|
if schema.Computed {
|
|
|
|
// If it is computed, then it always _exists_ in the state,
|
|
|
|
// it is just empty.
|
|
|
|
countRaw.Exists = true
|
|
|
|
countRaw.Value = 0
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
2014-08-17 23:12:54 +02:00
|
|
|
}
|
2014-08-22 08:03:04 +02:00
|
|
|
count := countRaw.Value.(int)
|
2014-08-17 23:12:54 +02:00
|
|
|
|
|
|
|
result := make(map[string]string)
|
2014-10-11 00:58:38 +02:00
|
|
|
if count > 0 || schema.Computed {
|
2014-08-19 04:12:49 +02:00
|
|
|
result[prefix+".#"] = strconv.FormatInt(int64(count), 10)
|
|
|
|
}
|
2014-08-17 23:12:54 +02:00
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
key := fmt.Sprintf("%s.%d", prefix, i)
|
|
|
|
|
|
|
|
var m map[string]string
|
|
|
|
switch t := schema.Elem.(type) {
|
|
|
|
case *Resource:
|
|
|
|
m = d.stateObject(key, t.Schema)
|
|
|
|
case *Schema:
|
|
|
|
m = d.stateSingle(key, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range m {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-08-18 23:00:03 +02:00
|
|
|
func (d *ResourceData) stateMap(
|
|
|
|
prefix string,
|
|
|
|
schema *Schema) map[string]string {
|
2014-08-27 05:19:44 +02:00
|
|
|
v := d.getMap(prefix, nil, schema, d.stateSource(prefix))
|
2014-08-22 08:03:04 +02:00
|
|
|
if !v.Exists {
|
2014-08-18 23:00:03 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
elemSchema := &Schema{Type: TypeString}
|
|
|
|
result := make(map[string]string)
|
2014-08-22 08:03:04 +02:00
|
|
|
for mk, _ := range v.Value.(map[string]interface{}) {
|
2014-08-18 23:00:03 +02:00
|
|
|
mp := fmt.Sprintf("%s.%s", prefix, mk)
|
|
|
|
for k, v := range d.stateSingle(mp, elemSchema) {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-08-17 23:12:54 +02:00
|
|
|
func (d *ResourceData) stateObject(
|
|
|
|
prefix string,
|
|
|
|
schema map[string]*Schema) map[string]string {
|
|
|
|
result := make(map[string]string)
|
|
|
|
for k, v := range schema {
|
|
|
|
key := k
|
|
|
|
if prefix != "" {
|
|
|
|
key = prefix + "." + key
|
|
|
|
}
|
|
|
|
|
|
|
|
for k1, v1 := range d.stateSingle(key, v) {
|
|
|
|
result[k1] = v1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ResourceData) statePrimitive(
|
|
|
|
prefix string,
|
|
|
|
schema *Schema) map[string]string {
|
2014-08-27 05:19:44 +02:00
|
|
|
raw := d.getRaw(prefix, d.stateSource(prefix))
|
2014-08-22 21:09:06 +02:00
|
|
|
if !raw.Exists {
|
2014-08-17 23:12:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-22 21:09:06 +02:00
|
|
|
v := raw.Value
|
|
|
|
if raw.ValueProcessed != nil {
|
|
|
|
v = raw.ValueProcessed
|
|
|
|
}
|
|
|
|
|
2014-08-17 23:12:54 +02:00
|
|
|
var vs string
|
|
|
|
switch schema.Type {
|
2014-08-20 01:46:36 +02:00
|
|
|
case TypeBool:
|
|
|
|
vs = strconv.FormatBool(v.(bool))
|
2014-08-17 23:12:54 +02:00
|
|
|
case TypeString:
|
|
|
|
vs = v.(string)
|
|
|
|
case TypeInt:
|
|
|
|
vs = strconv.FormatInt(int64(v.(int)), 10)
|
|
|
|
default:
|
2014-08-25 06:50:35 +02:00
|
|
|
panic(fmt.Sprintf("Unknown type: %#v", schema.Type))
|
2014-08-17 23:12:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return map[string]string{
|
|
|
|
prefix: vs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 02:51:27 +02:00
|
|
|
func (d *ResourceData) stateSet(
|
|
|
|
prefix string,
|
|
|
|
schema *Schema) map[string]string {
|
2014-08-27 05:19:44 +02:00
|
|
|
raw := d.get(prefix, nil, schema, d.stateSource(prefix))
|
2014-08-22 08:03:04 +02:00
|
|
|
if !raw.Exists {
|
2014-10-11 00:58:38 +02:00
|
|
|
if schema.Computed {
|
|
|
|
// If it is computed, then it always _exists_ in the state,
|
|
|
|
// it is just empty.
|
|
|
|
raw.Exists = true
|
|
|
|
raw.Value = new(Set)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
2014-08-21 02:51:27 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 08:03:04 +02:00
|
|
|
set := raw.Value.(*Set)
|
2014-08-21 02:51:27 +02:00
|
|
|
result := make(map[string]string)
|
2014-12-12 23:21:20 +01:00
|
|
|
result[prefix+".#"] = strconv.Itoa(set.Len())
|
|
|
|
|
|
|
|
for _, idx := range set.listCode() {
|
|
|
|
key := fmt.Sprintf("%s.%d", prefix, idx)
|
2014-08-21 02:51:27 +02:00
|
|
|
|
|
|
|
var m map[string]string
|
|
|
|
switch t := schema.Elem.(type) {
|
2014-12-12 23:21:20 +01:00
|
|
|
case *Resource:
|
|
|
|
m = d.stateObject(key, t.Schema)
|
2014-12-15 23:02:16 +01:00
|
|
|
case *Schema:
|
|
|
|
m = d.stateSingle(key, t)
|
2014-08-21 02:51:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range m {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-08-17 23:12:54 +02:00
|
|
|
func (d *ResourceData) stateSingle(
|
|
|
|
prefix string,
|
|
|
|
schema *Schema) map[string]string {
|
|
|
|
switch schema.Type {
|
|
|
|
case TypeList:
|
|
|
|
return d.stateList(prefix, schema)
|
2014-08-18 23:00:03 +02:00
|
|
|
case TypeMap:
|
|
|
|
return d.stateMap(prefix, schema)
|
2014-08-21 02:51:27 +02:00
|
|
|
case TypeSet:
|
|
|
|
return d.stateSet(prefix, schema)
|
|
|
|
case TypeBool:
|
|
|
|
fallthrough
|
|
|
|
case TypeInt:
|
|
|
|
fallthrough
|
|
|
|
case TypeString:
|
2014-08-17 23:12:54 +02:00
|
|
|
return d.statePrimitive(prefix, schema)
|
2014-08-21 02:51:27 +02:00
|
|
|
default:
|
2014-08-25 06:50:35 +02:00
|
|
|
panic(fmt.Sprintf("%s: unknown type %#v", prefix, schema.Type))
|
2014-08-17 23:12:54 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-27 05:19:44 +02:00
|
|
|
|
|
|
|
func (d *ResourceData) stateSource(prefix string) getSource {
|
|
|
|
// If we're not doing a partial apply, then get the set level
|
|
|
|
if !d.partial {
|
2014-10-21 00:32:30 +02:00
|
|
|
return getSourceSet | getSourceDiff
|
2014-08-27 05:19:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, only return getSourceSet if its in the partial map.
|
|
|
|
// Otherwise we use state level only.
|
|
|
|
for k, _ := range d.partialMap {
|
|
|
|
if strings.HasPrefix(prefix, k) {
|
2014-10-21 00:32:30 +02:00
|
|
|
return getSourceSet | getSourceDiff
|
2014-08-27 05:19:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return getSourceState
|
|
|
|
}
|