-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for complex input variables [WIP]
As requested in #7 This is incomplete, but it already works with primitive types and string lists. Needs to be extended to support complex list types. Maps and objects may be added as well, but first lists need to be implemented fully. Also, the old state needs to be migrated to the new one (Variables type changed) – or maybe let's introduce a new name and keep the old one for compatibility.
- Loading branch information
Showing
6 changed files
with
145 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package hclconv | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/alecthomas/hcl" | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type ListProxy struct { | ||
List []string | ||
} | ||
|
||
func ConvertDynamicAttributeToString(key string, elementValue attr.Value) (string, error) { | ||
switch elementValue := elementValue.(type) { | ||
case types.String: | ||
return elementValue.ValueString(), nil | ||
case types.Bool: | ||
return strconv.FormatBool(elementValue.ValueBool()), nil | ||
case types.Number: | ||
return elementValue.ValueBigFloat().Text('e', 4), nil | ||
case types.List: | ||
result, err := MarshalTFListToHcl(elementValue.Elements()) | ||
if err != nil { | ||
return "", errors.Wrap(err, "could not convert list to hcl") | ||
} | ||
return result, nil | ||
case types.Set: | ||
result, err := MarshalTFListToHcl(elementValue.Elements()) | ||
if err != nil { | ||
return "", errors.Wrap(err, "could not convert set to hcl") | ||
} | ||
return result, nil | ||
case types.Map: | ||
return "", errors.New( | ||
fmt.Sprintf("Maps are currently unsupported as variables (key %s)", key)) | ||
case types.Object: | ||
return "", errors.New( | ||
fmt.Sprintf("Objects are currently unsupported as variables (key %s)", key)) | ||
default: | ||
return "", errors.New( | ||
fmt.Sprintf("Unsupported type for variable %s in object: %s", | ||
key, | ||
reflect.TypeOf(elementValue).String())) | ||
} | ||
} | ||
|
||
func MarshalTFListToHcl(elements []attr.Value) (string, error) { | ||
convertedElements := make([]string, len(elements)) | ||
for i, element := range elements { | ||
var err error | ||
convertedElements[i], err = ConvertDynamicAttributeToString(strconv.FormatInt(int64(i), 10), element) | ||
if err != nil { | ||
return "", errors.Wrap(err, fmt.Sprintf("could not convert element %d to HCL", i)) | ||
} | ||
} | ||
byt, err := hcl.Marshal(&ListProxy{convertedElements}) | ||
if err != nil { | ||
return "Failed to marshal elements to HCL", err | ||
} | ||
hclString := string(byt) | ||
// Remove the "List = " prefix | ||
return strings.TrimPrefix(hclString, "List = "), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters