Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init commit to create PR #4

Open
wants to merge 86 commits into
base: typeset_base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
5475cb6
init commit to create PR
instaclustr-wenbodu Nov 30, 2023
dcf3b6c
add set into terraform schema, be able to convert yaml array+ignore o…
instaclustr-wenbodu Nov 30, 2023
0d0b612
update openapi spec to include typeSet
instaclustr-wenbodu Nov 30, 2023
936da83
update with typeset
instaclustr-wenbodu Dec 4, 2023
958bc47
update with typeset
instaclustr-wenbodu Dec 4, 2023
f4c0816
update with typeset add some logs
instaclustr-wenbodu Dec 5, 2023
d274cb9
update with typeset add some logs
instaclustr-wenbodu Dec 5, 2023
6693154
update with typeset add some logs
instaclustr-wenbodu Dec 5, 2023
5e2e091
update with typeset add some logs
instaclustr-wenbodu Dec 5, 2023
0fa67b2
update with typeset add some logs
instaclustr-wenbodu Dec 5, 2023
db5456a
update with typeset add some logs
instaclustr-wenbodu Dec 5, 2023
90f7105
update to have a hash function
instaclustr-wenbodu Dec 6, 2023
15d1268
update to have a hash function
instaclustr-wenbodu Dec 6, 2023
07104e4
update to have a hash function
instaclustr-wenbodu Dec 6, 2023
c0bde65
update to have a hash function
instaclustr-wenbodu Dec 6, 2023
161dfdf
update to have a hash function
instaclustr-wenbodu Dec 6, 2023
8dd8429
update to have a hash function
instaclustr-wenbodu Dec 6, 2023
37ada4d
update to have a hash function
instaclustr-wenbodu Dec 6, 2023
c563fb3
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
f29f955
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
2e44bd5
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
06a264b
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
c859a20
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
71a03cc
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
28bbc16
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
2a9f4b7
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
ec26911
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
5090a3c
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
4b4e99e
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
8155cf4
introduce TypeSet
instaclustr-wenbodu Dec 13, 2023
22f5601
introduce TypeSet
instaclustr-wenbodu Dec 13, 2023
4b77bbd
update
instaclustr-wenbodu Dec 13, 2023
59391b1
update
instaclustr-wenbodu Dec 14, 2023
4c741f3
update
instaclustr-wenbodu Dec 14, 2023
1041f17
update
instaclustr-wenbodu Dec 14, 2023
b641ee0
update
instaclustr-wenbodu Dec 14, 2023
c50c876
update
instaclustr-wenbodu Dec 14, 2023
d120c80
update
instaclustr-wenbodu Dec 14, 2023
e5bcdb3
update
instaclustr-wenbodu Dec 14, 2023
ce1422f
update
instaclustr-wenbodu Dec 14, 2023
b190124
update
instaclustr-wenbodu Dec 14, 2023
fa4c8c0
update
instaclustr-wenbodu Dec 14, 2023
5ef2c2a
update
instaclustr-wenbodu Dec 14, 2023
305ed98
update
instaclustr-wenbodu Dec 14, 2023
9d75dcb
update
instaclustr-wenbodu Dec 14, 2023
361f32e
update
instaclustr-wenbodu Dec 14, 2023
83ad582
update
instaclustr-wenbodu Dec 15, 2023
4b91d8f
update
instaclustr-wenbodu Dec 15, 2023
bb28070
update
instaclustr-wenbodu Dec 15, 2023
bf3a4de
update
instaclustr-wenbodu Dec 17, 2023
9da8981
update
instaclustr-wenbodu Dec 18, 2023
4a1b30a
update
instaclustr-wenbodu Dec 18, 2023
2c70c6e
update
instaclustr-wenbodu Dec 18, 2023
73e944e
update
instaclustr-wenbodu Dec 18, 2023
b1947a2
update a bit
instaclustr-wenbodu Dec 18, 2023
6c0185d
update a bit
instaclustr-wenbodu Dec 18, 2023
7139f93
update a bit
instaclustr-wenbodu Dec 18, 2023
c696efc
update a bit
instaclustr-wenbodu Dec 18, 2023
48768ef
update a bit
instaclustr-wenbodu Dec 18, 2023
0ca9082
update a bit
instaclustr-wenbodu Dec 18, 2023
8538266
update a bit
instaclustr-wenbodu Dec 18, 2023
9c7aa90
update a bit
instaclustr-wenbodu Dec 19, 2023
3127bd3
update a bit
instaclustr-wenbodu Dec 19, 2023
ebea793
update a bit
instaclustr-wenbodu Dec 19, 2023
295c205
update a bit
instaclustr-wenbodu Dec 19, 2023
c6359dc
update a bit
instaclustr-wenbodu Dec 19, 2023
e24548b
update a bit
instaclustr-wenbodu Dec 19, 2023
8ac7ab6
update a bit
instaclustr-wenbodu Dec 19, 2023
2fcd28d
update a bit
instaclustr-wenbodu Dec 19, 2023
e89a50e
update a bit
instaclustr-wenbodu Dec 19, 2023
d06338c
update a bit
instaclustr-wenbodu Dec 19, 2023
8e523f7
update a bit
instaclustr-wenbodu Dec 19, 2023
03d2e12
update a bit
instaclustr-wenbodu Dec 19, 2023
cc5c929
update a bit
instaclustr-wenbodu Dec 19, 2023
a8beb19
update a bit
instaclustr-wenbodu Dec 19, 2023
84814ba
update a bit
instaclustr-wenbodu Dec 19, 2023
4012274
update a bit
instaclustr-wenbodu Dec 19, 2023
c6c4d92
update a bit
instaclustr-wenbodu Dec 19, 2023
acdfe3e
update a bit
instaclustr-wenbodu Dec 19, 2023
b52d8b8
update a bit
instaclustr-wenbodu Dec 19, 2023
fc0938d
update a bit
instaclustr-wenbodu Dec 19, 2023
f706f0c
update a bit
instaclustr-wenbodu Dec 19, 2023
d1a0f28
update a bit
instaclustr-wenbodu Dec 22, 2023
78c259b
introduce typeSet
instaclustr-wenbodu Jan 7, 2024
681091d
introduce typeSet
instaclustr-wenbodu Jan 7, 2024
a0cc970
introduce typeSet
instaclustr-wenbodu Jan 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/go-openapi/loads v0.0.0-20171207192234-2a2b323bab96
github.com/go-openapi/spec v0.19.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk v1.1.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/iancoleman/strcase v0.0.0-20180726023541-3605ed457bf7
github.com/mitchellh/go-homedir v1.1.0
Expand Down
17 changes: 4 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.33/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zach-klippenstein/goregen v0.0.0-20160303162051-795b5e3961ea/go.mod h1:eNr558nEUjP8acGw8FFjTeWvSgU1stO7FAO6eknhHe4=
github.com/zclconf/go-cty v1.0.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
Expand Down Expand Up @@ -482,9 +482,8 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20180530234432-1e491301e022/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -521,10 +520,7 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220407224826-aac1ed45d8e3 h1:EN5+DfgmRMvRUrMGERW2gQl3Vc+Z7ZMnI/xdEpPSf0c=
golang.org/x/net v0.0.0-20220407224826-aac1ed45d8e3/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand All @@ -541,7 +537,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -581,10 +577,7 @@ golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f h1:8w7RhxzTVgUzw/AH/9mUV5q0vMgy40SQRursCcfmkCw=
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down Expand Up @@ -640,8 +633,6 @@ golang.org/x/tools v0.0.0-20200515010526-7d3b6ebf133d/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200713011307-fd294ab11aed/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200717024301-6ddee64345a6/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20=
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
257 changes: 252 additions & 5 deletions openapi/common.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package openapi

import (
"bytes"
"context"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"io/ioutil"
"log"
"net/http"
"reflect"
"sort"
"strconv"

"github.com/dikhan/terraform-provider-openapi/v3/openapi/openapierr"
Expand Down Expand Up @@ -136,7 +139,8 @@ func updateStateWithPayloadDataAndOptions(openAPIResource SpecResource, remoteDa
propValue = processIgnoreOrderIfEnabled(*property, propertyLocalStateValue, propertyRemoteValue)
}

value, err := convertPayloadToLocalStateDataValue(property, propValue, propertyLocalStateValue)
value, err := convertPayloadToLocalStateDataValue(property, propValue, propertyLocalStateValue, true)

if err != nil {
return err
}
Expand Down Expand Up @@ -193,12 +197,148 @@ func processIgnoreOrderIfEnabled(property SpecSchemaDefinitionProperty, inputPro
}
return remoteValue
}
func hashByName(v interface{}) int {
m, ok := v.(map[string]interface{})
if !ok {
// Handle error: v is not a map[string]interface{}
}

name, ok := m["name"].(string)
if !ok {
// Handle error: name field is not a string or does not exist
}

return hashcode.String(name)
}

func hashComplexObject(v interface{}) int {
var buffer bytes.Buffer

switch v := v.(type) {
case map[string]interface{}:
// Sort the keys so that the order is consistent
var keys []string
for k := range v {
keys = append(keys, k)
}
sort.Strings(keys)

// Hash each key-value pair
for _, k := range keys {
buffer.WriteString(k)
buffer.WriteString(fmt.Sprintf("%v", hashComplexObject(v[k])))
}
case []interface{}:
// Hash each element in the slice
for _, elem := range v {
buffer.WriteString(fmt.Sprintf("%v", hashComplexObject(elem)))
}
default:
// For primitive types, just write the value to the buffer
buffer.WriteString(fmt.Sprintf("%v", v))
}

// Compute and return the hash of the concatenated string
return hashcode.String(buffer.String())
}

//func deepConvertArrayToSet(property *SpecSchemaDefinitionProperty, v interface{}) (interface{}, error) {
// switch v := v.(type) {
// case []interface{}:
// // For slices, create a new set and add each element to the set
// if property.IgnoreItemsOrder {
// set := schema.NewSet(hashComplexObject, []interface{}{})
// for k, elem := range v {
// convertedElem, err := deepConvertArrayToSet(property.SpecSchemaDefinition.Properties, elem)
// if err != nil {
// return nil, err
// }
// set.Add(convertedElem)
// }
// return set, nil
// }
// case map[string]interface{}:
// // For maps, create a new map and convert each value in the map
// newMap := make(map[string]interface{})
// for key, value := range v {
// convertedValue, err := deepConvertArrayToSet(property.SpecSchemaDefinition.Properties[key], value)
// if err != nil {
// return nil, err
// }
// newMap[key] = convertedValue
// }
// return newMap, nil
// default:
// // For other types, return the value as is
// return v, nil
// }
//}

func deepConvertArrayToSet(property *SpecSchemaDefinitionProperty, v interface{}) (interface{}, error) {
//log.Printf("[INFO] input of deep copy %s %s", property.String(), v)
switch v := v.(type) {
case []interface{}:
// For slices, create a new set and add each element to the set
if property.isSetProperty() {
set := schema.NewSet(hashComplexObject, []interface{}{})
for _, elem := range v {
if property.isSetOfObjectsProperty() {
convertedElem, err := deepConvertArrayToSetMapNew(property.SpecSchemaDefinition.Properties, elem)
if err != nil {
return nil, err
}
set.Add(convertedElem)
} else {
set.Add(elem)
}
}
//log.Printf("[INFO] output of deep copy %s %s %s", property.String(), v, set)
return set, nil
}
return v, nil
default:
// For other types, return the value as is
return v, nil
}
}

func deepConvertArrayToSetMapNew(properties []*SpecSchemaDefinitionProperty, object interface{}) (interface{}, error) {
//log.Printf("[INFO] input of deep copy map %s %s", properties, object)
inputMap, ok := object.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("object is not a map")
}

// Create a new map and convert each value in the map
newMap := make(map[string]interface{})
for key, value := range inputMap {
//log.Printf("[INFO] key,value %s %s", key, value)
for _, property := range properties {
if key == property.Name {
//log.Printf("[INFO] key,value %s %s", key, value)
if property.isSetOfObjectsProperty() {
//log.Printf("[INFO] key,value %s %s", key, value)
convertedValue, err := deepConvertArrayToSet(property, value)
if err != nil {
return nil, err
}
newMap[key] = convertedValue.(*schema.Set)
} else {
newMap[key] = value
}
}
}
}

return newMap, nil
}

func convertPayloadToLocalStateDataValue(property *SpecSchemaDefinitionProperty, propertyValue interface{}, propertyLocalStateValue interface{}) (interface{}, error) {
func convertPayloadToLocalStateDataValue(property *SpecSchemaDefinitionProperty, propertyValue interface{}, propertyLocalStateValue interface{}, isFromAPI bool) (interface{}, error) {
if property.WriteOnly {
return propertyLocalStateValue, nil
}

//log.Printf("[INFO] propertyValue: %s %s %s", reflect.TypeOf(propertyValue), reflect.TypeOf(propertyValue).Kind(), propertyValue)
//log.Printf("[INFO] propertyLocalStateValue: %s %s %s", reflect.TypeOf(propertyLocalStateValue), reflect.TypeOf(propertyLocalStateValue).Kind(), propertyLocalStateValue)
switch property.Type {
case TypeObject:
return convertObjectToLocalStateData(property, propertyValue, propertyLocalStateValue)
Expand Down Expand Up @@ -237,6 +377,109 @@ func convertPayloadToLocalStateDataValue(property *SpecSchemaDefinitionProperty,
return arrayInput, nil
}
return nil, fmt.Errorf("property '%s' is supposed to be an array objects", property.Name)
case TypeSet:
log.Printf("[INFO] ofTypeSet")
if isSetOfPrimitives, _ := property.isTerraformSetOfSimpleValues(); isSetOfPrimitives {
return propertyValue, nil
}
if property.isSetOfObjectsProperty() {
arrayInput := []interface{}{}

arrayValue := make([]interface{}, 0)
if propertyValue != nil {
arrayValue = propertyValue.([]interface{})
}

var setLocalValue *schema.Set

if propertyLocalStateValue == nil {
setLocalValue = schema.NewSet(schema.HashString, []interface{}{})
} else {
setLocalValue = propertyLocalStateValue.(*schema.Set)
}
for _, remoteVal := range arrayValue {
hashCodeRemote := hashComplexObject(remoteVal)
matched := false
for _, localVal := range setLocalValue.List() {
hashCodeLocal := hashComplexObject(localVal)
if hashCodeLocal == hashCodeRemote {
objectValue, err := convertObjectToLocalStateData(property, remoteVal, localVal)
if err != nil {
return err, nil
}
matched = true
arrayInput = append(arrayInput, objectValue)
}
}
if matched == false {
objectValue, err := convertObjectToLocalStateData(property, remoteVal, nil)
if err != nil {
return err, nil
}
arrayInput = append(arrayInput, objectValue)
}
}
return arrayInput, nil
//if property.isSetOfObjectsProperty() {
// setInput := schema.NewSet(hashComplexObject, []interface{}{})
// var setValue interface{}
// var err error
// if isFromAPI {
// arrayValue := make([]interface{}, 0)
// if propertyValue != nil {
// arrayValue = propertyValue.([]interface{})
// }
// setValue, err = deepConvertArrayToSet(property, arrayValue)
// } else {
// setValue = propertyValue
// }
// //log.Printf("[INFO] arrayValue: %s", arrayValue)
// var setLocalValue *schema.Set
//
// if propertyLocalStateValue == nil {
// setLocalValue = schema.NewSet(schema.HashString, []interface{}{})
// } else {
// setLocalValue = propertyLocalStateValue.(*schema.Set)
// }
// if err != nil {
// return err, nil
// }
// log.Printf("[INFO] setValue: %s", setValue)
// for _, v1 := range setValue.(*schema.Set).List() {
// // Do something with v
// hashCodeRemote := hashComplexObject(v1)
// matched := false
// for _, v2 := range setLocalValue.List() {
// hashCodeLocal := hashComplexObject(v2)
// //log.Printf("[INFO] properties: %s", property.String())
// //log.Printf("[INFO] remote: %s %d", v1, hashCodeRemote)
// //log.Printf("[INFO] local: %s %d", v2, hashCodeLocal)
// if hashCodeLocal == hashCodeRemote {
// objectValue, err := convertObjectToLocalStateData(property, v1, v2)
// matched = true
// if err != nil {
// return err, nil
// }
// setInput.Add(objectValue)
// }
// }
// if matched == false {
// //log.Printf("[INFO] properties: %s", property.String())
// //log.Printf("[INFO] remote: %s %d", v1, hashCodeRemote)
// objectValue, err := convertObjectToLocalStateData(property, v1, nil)
// //log.Printf("[INFO] object Value: %s", objectValue)
// matched = true
// if err != nil {
// return err, nil
// }
// setInput.Add(objectValue)
// }
// }
//log.Printf("[INFO] setInput: %s", setInput)
//
//return setInput, nil
}
return nil, fmt.Errorf("property '%s' is supposed to be an set objects", property.Name)
case TypeString:
if propertyValue == nil {
return nil, nil
Expand Down Expand Up @@ -273,6 +516,7 @@ func convertObjectToLocalStateData(property *SpecSchemaDefinitionProperty, prope
if propertyValue != nil {
mapValue = propertyValue.(map[string]interface{})
}
//log.Printf("[INFO] mapValue: %s", mapValue)

localStateMapValue := make(map[string]interface{})
if propertyLocalStateValue != nil {
Expand All @@ -286,10 +530,11 @@ func convertObjectToLocalStateData(property *SpecSchemaDefinitionProperty, prope
for _, schemaDefinitionProperty := range property.SpecSchemaDefinition.Properties {
propertyName := schemaDefinitionProperty.Name
propertyValue := mapValue[propertyName]

//log.Printf("[INFO] property name and remoteValue: %s %s %s", propertyName, propertyValue, localStateMapValue[propertyName])
// Here we are processing the items of the list which are objects. In this case we need to keep the original
// types as Terraform honors property types for resource schemas attached to TypeList properties
propValue, err := convertPayloadToLocalStateDataValue(schemaDefinitionProperty, propertyValue, localStateMapValue[propertyName])
propValue, err := convertPayloadToLocalStateDataValue(schemaDefinitionProperty, propertyValue, localStateMapValue[propertyName], false)

if err != nil {
return nil, err
}
Expand All @@ -309,6 +554,8 @@ func convertObjectToLocalStateData(property *SpecSchemaDefinitionProperty, prope

// setResourceDataProperty sets the expectedValue for the given schemaDefinitionPropertyName using the terraform compliant property name
func setResourceDataProperty(schemaDefinitionProperty SpecSchemaDefinitionProperty, value interface{}, resourceLocalData *schema.ResourceData) error {
//log.Printf("[INFO] lastValue: %s", value)
//log.Printf("[INFO] lastLocalValue: %s", value)
return resourceLocalData.Set(schemaDefinitionProperty.GetTerraformCompliantPropertyName(), value)
}

Expand Down
Loading