2018-08-08 23:02:24 +02:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2018-11-16 15:55:34 +01:00
|
|
|
"regexp"
|
|
|
|
"sort"
|
2018-08-08 23:02:24 +02:00
|
|
|
"strconv"
|
2018-11-16 15:55:34 +01:00
|
|
|
"strings"
|
2018-08-08 23:02:24 +02:00
|
|
|
|
2018-08-28 01:47:27 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2018-10-18 17:40:47 +02:00
|
|
|
ctyconvert "github.com/zclconf/go-cty/cty/convert"
|
2018-08-28 01:47:27 +02:00
|
|
|
"github.com/zclconf/go-cty/cty/msgpack"
|
2018-08-08 23:02:24 +02:00
|
|
|
context "golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config/hcl2shim"
|
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2018-08-15 18:03:39 +02:00
|
|
|
"github.com/hashicorp/terraform/plugin/convert"
|
2018-08-08 23:02:24 +02:00
|
|
|
"github.com/hashicorp/terraform/plugin/proto"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2018-08-14 22:39:30 +02:00
|
|
|
// NewGRPCProviderServerShim wraps a terraform.ResourceProvider in a
|
|
|
|
// proto.ProviderServer implementation. If the provided provider is not a
|
|
|
|
// *schema.Provider, this will return nil,
|
|
|
|
func NewGRPCProviderServerShim(p terraform.ResourceProvider) *GRPCProviderServer {
|
|
|
|
sp, ok := p.(*schema.Provider)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &GRPCProviderServer{
|
|
|
|
provider: sp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 23:02:24 +02:00
|
|
|
// GRPCProviderServer handles the server, or plugin side of the rpc connection.
|
|
|
|
type GRPCProviderServer struct {
|
|
|
|
provider *schema.Provider
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) GetSchema(_ context.Context, req *proto.GetProviderSchema_Request) (*proto.GetProviderSchema_Response, error) {
|
|
|
|
resp := &proto.GetProviderSchema_Response{
|
|
|
|
ResourceSchemas: make(map[string]*proto.Schema),
|
|
|
|
DataSourceSchemas: make(map[string]*proto.Schema),
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Provider = &proto.Schema{
|
2018-08-15 18:03:39 +02:00
|
|
|
Block: convert.ConfigSchemaToProto(s.getProviderSchemaBlock()),
|
2018-08-08 23:02:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for typ, res := range s.provider.ResourcesMap {
|
|
|
|
resp.ResourceSchemas[typ] = &proto.Schema{
|
|
|
|
Version: int64(res.SchemaVersion),
|
2018-08-15 18:03:39 +02:00
|
|
|
Block: convert.ConfigSchemaToProto(res.CoreConfigSchema()),
|
2018-08-08 23:02:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for typ, dat := range s.provider.DataSourcesMap {
|
|
|
|
resp.DataSourceSchemas[typ] = &proto.Schema{
|
|
|
|
Version: int64(dat.SchemaVersion),
|
2018-08-15 18:03:39 +02:00
|
|
|
Block: convert.ConfigSchemaToProto(dat.CoreConfigSchema()),
|
2018-08-08 23:02:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) getProviderSchemaBlock() *configschema.Block {
|
|
|
|
return schema.InternalMap(s.provider.Schema).CoreConfigSchema()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) getResourceSchemaBlock(name string) *configschema.Block {
|
|
|
|
res := s.provider.ResourcesMap[name]
|
|
|
|
return res.CoreConfigSchema()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) getDatasourceSchemaBlock(name string) *configschema.Block {
|
|
|
|
dat := s.provider.DataSourcesMap[name]
|
|
|
|
return dat.CoreConfigSchema()
|
|
|
|
}
|
|
|
|
|
2018-10-18 03:30:04 +02:00
|
|
|
func (s *GRPCProviderServer) PrepareProviderConfig(_ context.Context, req *proto.PrepareProviderConfig_Request) (*proto.PrepareProviderConfig_Response, error) {
|
|
|
|
resp := &proto.PrepareProviderConfig_Response{}
|
2018-08-08 23:02:24 +02:00
|
|
|
|
|
|
|
block := s.getProviderSchemaBlock()
|
|
|
|
|
|
|
|
configVal, err := msgpack.Unmarshal(req.Config.Msgpack, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-10-18 17:40:47 +02:00
|
|
|
// lookup any required, top-level attributes that are Null, and see if we
|
|
|
|
// have a Default value available.
|
|
|
|
configVal, _ = cty.Transform(configVal, func(path cty.Path, val cty.Value) (cty.Value, error) {
|
|
|
|
// we're only looking for top-level attributes
|
|
|
|
if len(path) != 1 {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// nothing to do if we already have a value
|
|
|
|
if !val.IsNull() {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the Schema definition for this attribute
|
|
|
|
getAttr, ok := path[0].(cty.GetAttrStep)
|
|
|
|
// these should all exist, but just ignore anything strange
|
|
|
|
if !ok {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
attrSchema := s.provider.Schema[getAttr.Name]
|
|
|
|
// continue to ignore anything that doesn't match
|
|
|
|
if attrSchema == nil {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2018-10-18 18:45:55 +02:00
|
|
|
// this is deprecated, so don't set it
|
|
|
|
if attrSchema.Deprecated != "" || attrSchema.Removed != "" {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2018-10-18 17:40:47 +02:00
|
|
|
// find a default value if it exists
|
|
|
|
def, err := attrSchema.DefaultValue()
|
|
|
|
if err != nil {
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// no default
|
|
|
|
if def == nil {
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a cty.Value and make sure it's the correct type
|
|
|
|
tmpVal := hcl2shim.HCL2ValueFromConfigValue(def)
|
|
|
|
val, err = ctyconvert.Convert(tmpVal, val.Type())
|
2018-10-18 18:45:55 +02:00
|
|
|
|
2018-10-18 17:40:47 +02:00
|
|
|
return val, err
|
|
|
|
})
|
|
|
|
|
|
|
|
configVal, err = block.CoerceValue(configVal)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-08-08 23:02:24 +02:00
|
|
|
config := terraform.NewResourceConfigShimmed(configVal, block)
|
|
|
|
|
|
|
|
warns, errs := s.provider.Validate(config)
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, convert.WarnsAndErrsToProto(warns, errs))
|
2018-08-08 23:02:24 +02:00
|
|
|
|
2018-10-18 17:40:47 +02:00
|
|
|
preparedConfigMP, err := msgpack.Marshal(configVal, block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.PreparedConfig = &proto.DynamicValue{Msgpack: preparedConfigMP}
|
2018-10-18 03:30:04 +02:00
|
|
|
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) ValidateResourceTypeConfig(_ context.Context, req *proto.ValidateResourceTypeConfig_Request) (*proto.ValidateResourceTypeConfig_Response, error) {
|
|
|
|
resp := &proto.ValidateResourceTypeConfig_Response{}
|
|
|
|
|
|
|
|
block := s.getResourceSchemaBlock(req.TypeName)
|
|
|
|
|
|
|
|
configVal, err := msgpack.Unmarshal(req.Config.Msgpack, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
config := terraform.NewResourceConfigShimmed(configVal, block)
|
|
|
|
|
|
|
|
warns, errs := s.provider.ValidateResource(req.TypeName, config)
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, convert.WarnsAndErrsToProto(warns, errs))
|
2018-08-08 23:02:24 +02:00
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) ValidateDataSourceConfig(_ context.Context, req *proto.ValidateDataSourceConfig_Request) (*proto.ValidateDataSourceConfig_Response, error) {
|
|
|
|
resp := &proto.ValidateDataSourceConfig_Response{}
|
|
|
|
|
|
|
|
block := s.getDatasourceSchemaBlock(req.TypeName)
|
|
|
|
|
|
|
|
configVal, err := msgpack.Unmarshal(req.Config.Msgpack, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
config := terraform.NewResourceConfigShimmed(configVal, block)
|
|
|
|
|
2018-09-21 13:33:28 +02:00
|
|
|
warns, errs := s.provider.ValidateDataSource(req.TypeName, config)
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, convert.WarnsAndErrsToProto(warns, errs))
|
2018-08-08 23:02:24 +02:00
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) UpgradeResourceState(_ context.Context, req *proto.UpgradeResourceState_Request) (*proto.UpgradeResourceState_Response, error) {
|
|
|
|
resp := &proto.UpgradeResourceState_Response{}
|
|
|
|
|
|
|
|
res := s.provider.ResourcesMap[req.TypeName]
|
|
|
|
block := res.CoreConfigSchema()
|
|
|
|
|
|
|
|
version := int(req.Version)
|
|
|
|
|
|
|
|
var jsonMap map[string]interface{}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// if there's a JSON state, we need to decode it.
|
2018-10-06 18:38:00 +02:00
|
|
|
if len(req.RawState.Json) > 0 {
|
2018-08-08 23:02:24 +02:00
|
|
|
err = json.Unmarshal(req.RawState.Json, &jsonMap)
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We first need to upgrade a flatmap state if it exists.
|
|
|
|
// There should never be both a JSON and Flatmap state in the request.
|
|
|
|
if req.RawState.Flatmap != nil {
|
|
|
|
jsonMap, version, err = s.upgradeFlatmapState(version, req.RawState.Flatmap, res)
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// complete the upgrade of the JSON states
|
|
|
|
jsonMap, err = s.upgradeJSONState(version, jsonMap, res)
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we need to turn the state into the default json representation, so
|
|
|
|
// that it can be re-decoded using the actual schema.
|
|
|
|
val, err := schema.JSONMapToStateValue(jsonMap, block)
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// encode the final state to the expected msgpack format
|
|
|
|
newStateMP, err := msgpack.Marshal(val, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.UpgradedState = &proto.DynamicValue{Msgpack: newStateMP}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// upgradeFlatmapState takes a legacy flatmap state, upgrades it using Migrate
|
|
|
|
// state if necessary, and converts it to the new JSON state format decoded as a
|
|
|
|
// map[string]interface{}.
|
|
|
|
// upgradeFlatmapState returns the json map along with the corresponding schema
|
|
|
|
// version.
|
|
|
|
func (s *GRPCProviderServer) upgradeFlatmapState(version int, m map[string]string, res *schema.Resource) (map[string]interface{}, int, error) {
|
|
|
|
// this will be the version we've upgraded so, defaulting to the given
|
|
|
|
// version in case no migration was called.
|
|
|
|
upgradedVersion := version
|
|
|
|
|
|
|
|
// first determine if we need to call the legacy MigrateState func
|
|
|
|
requiresMigrate := version < res.SchemaVersion
|
|
|
|
|
|
|
|
schemaType := res.CoreConfigSchema().ImpliedType()
|
|
|
|
|
|
|
|
// if there are any StateUpgraders, then we need to only compare
|
|
|
|
// against the first version there
|
|
|
|
if len(res.StateUpgraders) > 0 {
|
|
|
|
requiresMigrate = version < res.StateUpgraders[0].Version
|
|
|
|
}
|
|
|
|
|
|
|
|
if requiresMigrate {
|
|
|
|
if res.MigrateState == nil {
|
|
|
|
return nil, 0, errors.New("cannot upgrade state, missing MigrateState function")
|
|
|
|
}
|
|
|
|
|
|
|
|
is := &terraform.InstanceState{
|
|
|
|
ID: m["id"],
|
|
|
|
Attributes: m,
|
|
|
|
Meta: map[string]interface{}{
|
|
|
|
"schema_version": strconv.Itoa(version),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
is, err := res.MigrateState(version, is, s.provider.Meta())
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-assign the map in case there was a copy made, making sure to keep
|
|
|
|
// the ID
|
|
|
|
m := is.Attributes
|
|
|
|
m["id"] = is.ID
|
|
|
|
|
|
|
|
// if there are further upgraders, then we've only updated that far
|
|
|
|
if len(res.StateUpgraders) > 0 {
|
|
|
|
schemaType = res.StateUpgraders[0].Type
|
|
|
|
upgradedVersion = res.StateUpgraders[0].Version
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// the schema version may be newer than the MigrateState functions
|
|
|
|
// handled and older than the current, but still stored in the flatmap
|
|
|
|
// form. If that's the case, we need to find the correct schema type to
|
|
|
|
// convert the state.
|
|
|
|
for _, upgrader := range res.StateUpgraders {
|
|
|
|
if upgrader.Version == version {
|
|
|
|
schemaType = upgrader.Type
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we know the state is up to the latest version that handled the
|
|
|
|
// flatmap format state. Now we can upgrade the format and continue from
|
|
|
|
// there.
|
|
|
|
newConfigVal, err := hcl2shim.HCL2ValueFromFlatmap(m, schemaType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonMap, err := schema.StateValueToJSONMap(newConfigVal, schemaType)
|
|
|
|
return jsonMap, upgradedVersion, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) upgradeJSONState(version int, m map[string]interface{}, res *schema.Resource) (map[string]interface{}, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for _, upgrader := range res.StateUpgraders {
|
|
|
|
if version != upgrader.Version {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err = upgrader.Upgrade(m, s.provider.Meta())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
version++
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) Stop(_ context.Context, _ *proto.Stop_Request) (*proto.Stop_Response, error) {
|
|
|
|
resp := &proto.Stop_Response{}
|
|
|
|
|
|
|
|
err := s.provider.Stop()
|
|
|
|
if err != nil {
|
|
|
|
resp.Error = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) Configure(_ context.Context, req *proto.Configure_Request) (*proto.Configure_Response, error) {
|
|
|
|
resp := &proto.Configure_Response{}
|
|
|
|
|
|
|
|
block := s.getProviderSchemaBlock()
|
|
|
|
|
|
|
|
configVal, err := msgpack.Unmarshal(req.Config.Msgpack, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
config := terraform.NewResourceConfigShimmed(configVal, block)
|
|
|
|
err = s.provider.Configure(config)
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) ReadResource(_ context.Context, req *proto.ReadResource_Request) (*proto.ReadResource_Response, error) {
|
|
|
|
resp := &proto.ReadResource_Response{}
|
|
|
|
|
|
|
|
res := s.provider.ResourcesMap[req.TypeName]
|
|
|
|
block := res.CoreConfigSchema()
|
|
|
|
|
|
|
|
stateVal, err := msgpack.Unmarshal(req.CurrentState.Msgpack, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-11-14 05:00:02 +01:00
|
|
|
instanceState, err := res.ShimInstanceStateFromValue(stateVal)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
2018-08-08 23:02:24 +02:00
|
|
|
|
|
|
|
newInstanceState, err := res.RefreshWithoutUpgrade(instanceState, s.provider.Meta())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-08-28 02:43:37 +02:00
|
|
|
if newInstanceState == nil || newInstanceState.ID == "" {
|
|
|
|
// The old provider API used an empty id to signal that the remote
|
|
|
|
// object appears to have been deleted, but our new protocol expects
|
|
|
|
// to see a null value (in the cty sense) in that case.
|
2018-10-31 18:43:50 +01:00
|
|
|
newStateMP, err := msgpack.Marshal(cty.NullVal(block.ImpliedType()), block.ImpliedType())
|
2018-08-28 02:43:37 +02:00
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
|
|
|
}
|
|
|
|
resp.NewState = &proto.DynamicValue{
|
2018-10-31 18:43:50 +01:00
|
|
|
Msgpack: newStateMP,
|
2018-08-28 02:43:37 +02:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-08-08 23:02:24 +02:00
|
|
|
// helper/schema should always copy the ID over, but do it again just to be safe
|
|
|
|
newInstanceState.Attributes["id"] = newInstanceState.ID
|
|
|
|
|
2018-11-16 15:55:34 +01:00
|
|
|
newInstanceState.Attributes = normalizeFlatmapContainers(newInstanceState.Attributes)
|
|
|
|
|
2018-10-30 17:59:45 +01:00
|
|
|
newStateVal, err := hcl2shim.HCL2ValueFromFlatmap(newInstanceState.Attributes, block.ImpliedType())
|
2018-08-08 23:02:24 +02:00
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-10-30 17:59:45 +01:00
|
|
|
newStateVal = copyTimeoutValues(newStateVal, stateVal)
|
|
|
|
|
|
|
|
newStateMP, err := msgpack.Marshal(newStateVal, block.ImpliedType())
|
2018-08-08 23:02:24 +02:00
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.NewState = &proto.DynamicValue{
|
2018-10-30 17:59:45 +01:00
|
|
|
Msgpack: newStateMP,
|
2018-08-08 23:02:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) PlanResourceChange(_ context.Context, req *proto.PlanResourceChange_Request) (*proto.PlanResourceChange_Response, error) {
|
|
|
|
resp := &proto.PlanResourceChange_Response{}
|
|
|
|
|
|
|
|
res := s.provider.ResourcesMap[req.TypeName]
|
|
|
|
block := res.CoreConfigSchema()
|
|
|
|
|
|
|
|
priorStateVal, err := msgpack.Unmarshal(req.PriorState.Msgpack, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
proposedNewStateVal, err := msgpack.Unmarshal(req.ProposedNewState.Msgpack, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
info := &terraform.InstanceInfo{
|
|
|
|
Type: req.TypeName,
|
|
|
|
}
|
|
|
|
|
2018-11-14 05:00:02 +01:00
|
|
|
priorState, err := res.ShimInstanceStateFromValue(priorStateVal)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-10-06 18:38:00 +02:00
|
|
|
priorPrivate := make(map[string]interface{})
|
|
|
|
if len(req.PriorPrivate) > 0 {
|
|
|
|
if err := json.Unmarshal(req.PriorPrivate, &priorPrivate); err != nil {
|
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 17:59:45 +01:00
|
|
|
|
2018-10-06 18:38:00 +02:00
|
|
|
priorState.Meta = priorPrivate
|
2018-08-08 23:02:24 +02:00
|
|
|
|
2018-10-29 21:11:19 +01:00
|
|
|
// turn the proposed state into a legacy configuration
|
2018-08-08 23:02:24 +02:00
|
|
|
config := terraform.NewResourceConfigShimmed(proposedNewStateVal, block)
|
|
|
|
|
2018-10-16 01:33:33 +02:00
|
|
|
diff, err := s.provider.SimpleDiff(info, priorState, config)
|
2018-08-08 23:02:24 +02:00
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-08-28 02:34:14 +02:00
|
|
|
if diff == nil {
|
2018-10-30 20:58:00 +01:00
|
|
|
// schema.Provider.Diff returns nil if it ends up making a diff with no
|
|
|
|
// changes, but our new interface wants us to return an actual change
|
|
|
|
// description that _shows_ there are no changes. This is usually the
|
|
|
|
// PriorSate, however if there was no prior state and no diff, then we
|
|
|
|
// use the ProposedNewState.
|
|
|
|
if !priorStateVal.IsNull() {
|
|
|
|
resp.PlannedState = req.PriorState
|
|
|
|
} else {
|
|
|
|
resp.PlannedState = req.ProposedNewState
|
|
|
|
}
|
2018-08-28 02:34:14 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-11-16 15:55:34 +01:00
|
|
|
// strip out non-diffs
|
|
|
|
for k, v := range diff.Attributes {
|
|
|
|
if v.New == v.Old && !v.NewComputed && !v.NewRemoved {
|
|
|
|
delete(diff.Attributes, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 03:04:56 +01:00
|
|
|
if priorState == nil {
|
|
|
|
priorState = &terraform.InstanceState{}
|
|
|
|
}
|
|
|
|
|
2018-08-08 23:02:24 +02:00
|
|
|
// now we need to apply the diff to the prior state, so get the planned state
|
2018-11-14 03:04:56 +01:00
|
|
|
plannedAttrs, err := diff.Apply(priorState.Attributes, block)
|
2018-11-16 15:55:34 +01:00
|
|
|
|
|
|
|
plannedAttrs = normalizeFlatmapContainers(plannedAttrs)
|
|
|
|
|
2018-11-14 03:04:56 +01:00
|
|
|
plannedStateVal, err := hcl2shim.HCL2ValueFromFlatmap(plannedAttrs, block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
plannedStateVal, err = block.CoerceValue(plannedStateVal)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
2018-08-08 23:02:24 +02:00
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-10-30 17:59:45 +01:00
|
|
|
plannedStateVal = copyTimeoutValues(plannedStateVal, proposedNewStateVal)
|
|
|
|
|
2018-08-08 23:02:24 +02:00
|
|
|
plannedMP, err := msgpack.Marshal(plannedStateVal, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
2018-08-27 22:43:35 +02:00
|
|
|
resp.PlannedState = &proto.DynamicValue{
|
|
|
|
Msgpack: plannedMP,
|
|
|
|
}
|
2018-08-08 23:02:24 +02:00
|
|
|
|
|
|
|
// the Meta field gets encoded into PlannedPrivate
|
2018-10-30 17:59:45 +01:00
|
|
|
if diff.Meta != nil {
|
|
|
|
plannedPrivate, err := json.Marshal(diff.Meta)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
resp.PlannedPrivate = plannedPrivate
|
2018-08-08 23:02:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// collect the attributes that require instance replacement, and convert
|
|
|
|
// them to cty.Paths.
|
|
|
|
var requiresNew []string
|
|
|
|
for attr, d := range diff.Attributes {
|
|
|
|
if d.RequiresNew {
|
|
|
|
requiresNew = append(requiresNew, attr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 01:33:33 +02:00
|
|
|
// If anything requires a new resource already, or the "id" field indicates
|
|
|
|
// that we will be creating a new resource, then we need to add that to
|
|
|
|
// RequiresReplace so that core can tell if the instance is being replaced
|
|
|
|
// even if changes are being suppressed via "ignore_changes".
|
|
|
|
id := plannedStateVal.GetAttr("id")
|
|
|
|
if len(requiresNew) > 0 || id.IsNull() || !id.IsKnown() {
|
|
|
|
requiresNew = append(requiresNew, "id")
|
|
|
|
}
|
|
|
|
|
2018-08-08 23:02:24 +02:00
|
|
|
requiresReplace, err := hcl2shim.RequiresReplace(requiresNew, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert these to the protocol structures
|
|
|
|
for _, p := range requiresReplace {
|
|
|
|
resp.RequiresReplace = append(resp.RequiresReplace, pathToAttributePath(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) ApplyResourceChange(_ context.Context, req *proto.ApplyResourceChange_Request) (*proto.ApplyResourceChange_Response, error) {
|
|
|
|
resp := &proto.ApplyResourceChange_Response{}
|
|
|
|
|
|
|
|
res := s.provider.ResourcesMap[req.TypeName]
|
|
|
|
block := res.CoreConfigSchema()
|
|
|
|
|
|
|
|
priorStateVal, err := msgpack.Unmarshal(req.PriorState.Msgpack, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
plannedStateVal, err := msgpack.Unmarshal(req.PlannedState.Msgpack, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
info := &terraform.InstanceInfo{
|
|
|
|
Type: req.TypeName,
|
|
|
|
}
|
|
|
|
|
2018-11-14 05:00:02 +01:00
|
|
|
priorState, err := res.ShimInstanceStateFromValue(priorStateVal)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
2018-08-08 23:02:24 +02:00
|
|
|
|
2018-10-06 18:38:00 +02:00
|
|
|
private := make(map[string]interface{})
|
|
|
|
if len(req.PlannedPrivate) > 0 {
|
|
|
|
if err := json.Unmarshal(req.PlannedPrivate, &private); err != nil {
|
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
2018-08-08 23:02:24 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 03:13:05 +02:00
|
|
|
var diff *terraform.InstanceDiff
|
|
|
|
destroy := false
|
|
|
|
|
|
|
|
// a null state means we are destroying the instance
|
|
|
|
if plannedStateVal.IsNull() {
|
|
|
|
destroy = true
|
|
|
|
diff = &terraform.InstanceDiff{
|
|
|
|
Attributes: make(map[string]*terraform.ResourceAttrDiff),
|
|
|
|
Meta: make(map[string]interface{}),
|
|
|
|
Destroy: true,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
diff, err = schema.DiffFromValues(priorStateVal, plannedStateVal, res)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
2018-08-08 23:02:24 +02:00
|
|
|
}
|
|
|
|
|
2018-10-06 19:06:41 +02:00
|
|
|
if diff == nil {
|
|
|
|
diff = &terraform.InstanceDiff{
|
|
|
|
Attributes: make(map[string]*terraform.ResourceAttrDiff),
|
|
|
|
Meta: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 17:59:45 +01:00
|
|
|
|
2018-11-16 15:55:34 +01:00
|
|
|
// strip out non-diffs
|
|
|
|
for k, v := range diff.Attributes {
|
|
|
|
if v.New == v.Old && !v.NewComputed && !v.NewRemoved {
|
|
|
|
delete(diff.Attributes, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 17:59:45 +01:00
|
|
|
if private != nil {
|
|
|
|
diff.Meta = private
|
|
|
|
}
|
2018-10-06 19:06:41 +02:00
|
|
|
|
2018-08-08 23:02:24 +02:00
|
|
|
newInstanceState, err := s.provider.Apply(info, priorState, diff)
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-11-16 15:55:34 +01:00
|
|
|
if newInstanceState != nil {
|
|
|
|
newInstanceState.Attributes = normalizeFlatmapContainers(newInstanceState.Attributes)
|
|
|
|
}
|
|
|
|
|
2018-10-16 03:13:05 +02:00
|
|
|
newStateVal := cty.NullVal(block.ImpliedType())
|
|
|
|
|
|
|
|
// We keep the null val if we destroyed the resource, otherwise build the
|
|
|
|
// entire object, even if the new state was nil.
|
|
|
|
if !destroy {
|
|
|
|
newStateVal, err = schema.StateValueFromInstanceState(newInstanceState, block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
2018-08-08 23:02:24 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 17:59:45 +01:00
|
|
|
newStateVal = copyTimeoutValues(newStateVal, plannedStateVal)
|
|
|
|
|
2018-08-08 23:02:24 +02:00
|
|
|
newStateMP, err := msgpack.Marshal(newStateVal, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
2018-08-28 01:47:27 +02:00
|
|
|
resp.NewState = &proto.DynamicValue{
|
|
|
|
Msgpack: newStateMP,
|
|
|
|
}
|
2018-08-08 23:02:24 +02:00
|
|
|
|
2018-10-16 03:13:05 +02:00
|
|
|
if newInstanceState != nil {
|
|
|
|
meta, err := json.Marshal(newInstanceState.Meta)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
resp.Private = meta
|
2018-08-08 23:02:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) ImportResourceState(_ context.Context, req *proto.ImportResourceState_Request) (*proto.ImportResourceState_Response, error) {
|
|
|
|
resp := &proto.ImportResourceState_Response{}
|
|
|
|
|
|
|
|
block := s.getResourceSchemaBlock(req.TypeName)
|
|
|
|
|
|
|
|
info := &terraform.InstanceInfo{
|
|
|
|
Type: req.TypeName,
|
|
|
|
}
|
|
|
|
|
|
|
|
newInstanceStates, err := s.provider.ImportState(info, req.Id)
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, is := range newInstanceStates {
|
|
|
|
// copy the ID again just to be sure it wasn't missed
|
|
|
|
is.Attributes["id"] = is.ID
|
|
|
|
|
|
|
|
newStateVal, err := hcl2shim.HCL2ValueFromFlatmap(is.Attributes, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newStateMP, err := msgpack.Marshal(newStateVal, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
meta, err := json.Marshal(is.Meta)
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// the legacy implementation could only import one type at a time
|
|
|
|
importedResource := &proto.ImportResourceState_ImportedResource{
|
|
|
|
TypeName: req.TypeName,
|
|
|
|
State: &proto.DynamicValue{
|
|
|
|
Msgpack: newStateMP,
|
|
|
|
},
|
|
|
|
Private: meta,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.ImportedResources = append(resp.ImportedResources, importedResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProviderServer) ReadDataSource(_ context.Context, req *proto.ReadDataSource_Request) (*proto.ReadDataSource_Response, error) {
|
|
|
|
resp := &proto.ReadDataSource_Response{}
|
|
|
|
|
|
|
|
res := s.provider.DataSourcesMap[req.TypeName]
|
|
|
|
block := res.CoreConfigSchema()
|
|
|
|
|
|
|
|
configVal, err := msgpack.Unmarshal(req.Config.Msgpack, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
info := &terraform.InstanceInfo{
|
|
|
|
Type: req.TypeName,
|
|
|
|
}
|
|
|
|
|
|
|
|
config := terraform.NewResourceConfigShimmed(configVal, block)
|
|
|
|
|
|
|
|
// we need to still build the diff separately with the Read method to match
|
|
|
|
// the old behavior
|
|
|
|
diff, err := s.provider.ReadDataDiff(info, config)
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we can get the new complete data source
|
|
|
|
newInstanceState, err := s.provider.ReadDataApply(info, diff)
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newStateVal, err := schema.StateValueFromInstanceState(newInstanceState, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-10-30 17:59:45 +01:00
|
|
|
newStateVal = copyTimeoutValues(newStateVal, configVal)
|
|
|
|
|
2018-08-08 23:02:24 +02:00
|
|
|
newStateMP, err := msgpack.Marshal(newStateVal, block.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
2018-10-02 22:22:03 +02:00
|
|
|
resp.State = &proto.DynamicValue{
|
|
|
|
Msgpack: newStateMP,
|
|
|
|
}
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pathToAttributePath(path cty.Path) *proto.AttributePath {
|
|
|
|
var steps []*proto.AttributePath_Step
|
|
|
|
|
|
|
|
for _, step := range path {
|
|
|
|
switch s := step.(type) {
|
|
|
|
case cty.GetAttrStep:
|
|
|
|
steps = append(steps, &proto.AttributePath_Step{
|
|
|
|
Selector: &proto.AttributePath_Step_AttributeName{
|
|
|
|
AttributeName: s.Name,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
case cty.IndexStep:
|
|
|
|
ty := s.Key.Type()
|
|
|
|
switch ty {
|
|
|
|
case cty.Number:
|
|
|
|
i, _ := s.Key.AsBigFloat().Int64()
|
|
|
|
steps = append(steps, &proto.AttributePath_Step{
|
|
|
|
Selector: &proto.AttributePath_Step_ElementKeyInt{
|
|
|
|
ElementKeyInt: i,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
case cty.String:
|
|
|
|
steps = append(steps, &proto.AttributePath_Step{
|
|
|
|
Selector: &proto.AttributePath_Step_ElementKeyString{
|
|
|
|
ElementKeyString: s.Key.AsString(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.AttributePath{Steps: steps}
|
|
|
|
}
|
2018-10-30 17:59:45 +01:00
|
|
|
|
2018-11-16 15:55:34 +01:00
|
|
|
// normalizeFlatmapContainers removes empty containers, and fixes counts in a
|
|
|
|
// set of flatmapped attributes.
|
|
|
|
func normalizeFlatmapContainers(attrs map[string]string) map[string]string {
|
|
|
|
keyRx := regexp.MustCompile(`.*\.[%#]$`)
|
|
|
|
|
|
|
|
// find container keys
|
|
|
|
var keys []string
|
|
|
|
for k := range attrs {
|
|
|
|
if keyRx.MatchString(k) {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort the keys in reverse, so that we check the longest subkeys first
|
|
|
|
sort.Slice(keys, func(i, j int) bool {
|
|
|
|
a, b := keys[i], keys[j]
|
|
|
|
|
|
|
|
if strings.HasPrefix(a, b) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(b, a) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return a > b
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
prefix := k[:len(k)-1]
|
|
|
|
indexes := map[string]int{}
|
|
|
|
for cand := range attrs {
|
|
|
|
if cand == k {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(cand, prefix) {
|
|
|
|
idx := cand[len(prefix):]
|
|
|
|
dot := strings.Index(idx, ".")
|
|
|
|
if dot > 0 {
|
|
|
|
idx = idx[:dot]
|
|
|
|
}
|
|
|
|
indexes[idx]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(indexes) > 0 {
|
|
|
|
attrs[k] = strconv.Itoa(len(indexes))
|
|
|
|
} else {
|
|
|
|
delete(attrs, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
|
2018-10-30 17:59:45 +01:00
|
|
|
// helper/schema throws away timeout values from the config and stores them in
|
|
|
|
// the Private/Meta fields. we need to copy those values into the planned state
|
|
|
|
// so that core doesn't see a perpetual diff with the timeout block.
|
|
|
|
func copyTimeoutValues(to cty.Value, from cty.Value) cty.Value {
|
|
|
|
// if `from` is null, then there are no attributes, and if `to` is null we
|
|
|
|
// are planning to remove it altogether.
|
|
|
|
if from.IsNull() || to.IsNull() {
|
|
|
|
return to
|
|
|
|
}
|
|
|
|
|
|
|
|
fromAttrs := from.AsValueMap()
|
|
|
|
timeouts, ok := fromAttrs[schema.TimeoutsConfigKey]
|
|
|
|
|
|
|
|
// no timeouts to copy
|
|
|
|
// timeouts shouldn't be unknown, but don't copy possibly invalid values
|
|
|
|
if !ok || timeouts.IsNull() || !timeouts.IsWhollyKnown() {
|
|
|
|
return to
|
|
|
|
}
|
|
|
|
|
|
|
|
toAttrs := to.AsValueMap()
|
|
|
|
toAttrs[schema.TimeoutsConfigKey] = timeouts
|
|
|
|
|
|
|
|
return cty.ObjectVal(toAttrs)
|
|
|
|
}
|