-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
195 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,5 +87,3 @@ EOT | |
|
||
- `id` (String) Hexadecimal encoding of the checksum of the output. | ||
- `output` (String) The merged output. | ||
|
||
|
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,26 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "yaml_merge function - terraform-provider-utils" | ||
subcategory: "" | ||
description: |- | ||
Merge a list of YAML strings | ||
--- | ||
|
||
# function: yaml_merge | ||
|
||
Merge a list of YAML strings into a single YAML string, where maps are deep merged and list entries are compared against existing list entries and if all primitive values match, the entries are deep merged. YAML `!env` tags can be used to resolve values from environment variables. | ||
|
||
|
||
|
||
## Signature | ||
|
||
<!-- signature generated by tfplugindocs --> | ||
```text | ||
yaml_merge(input list of string) string | ||
``` | ||
|
||
## Arguments | ||
|
||
<!-- arguments generated by tfplugindocs --> | ||
1. `input` (List of String) A list of YAML strings that is merged. | ||
|
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,76 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
var _ function.Function = YamlMergeFunction{} | ||
|
||
func NewYamlMergeFunction() function.Function { | ||
return &YamlMergeFunction{} | ||
} | ||
|
||
type YamlMergeFunction struct{} | ||
|
||
func (r YamlMergeFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { | ||
resp.Name = "yaml_merge" | ||
} | ||
|
||
func (r YamlMergeFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { | ||
resp.Definition = function.Definition{ | ||
Summary: "Merge a list of YAML strings", | ||
MarkdownDescription: "Merge a list of YAML strings into a single YAML string, where maps are deep merged and list entries are compared against existing list entries and if all primitive values match, the entries are deep merged. YAML `!env` tags can be used to resolve values from environment variables.", | ||
Parameters: []function.Parameter{ | ||
function.ListParameter{ | ||
Name: "input", | ||
ElementType: types.StringType, | ||
MarkdownDescription: "A list of YAML strings that is merged.", | ||
}, | ||
}, | ||
Return: function.StringReturn{}, | ||
} | ||
} | ||
|
||
func (r YamlMergeFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { | ||
var input []string | ||
|
||
resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &input)) | ||
|
||
if resp.Error != nil { | ||
return | ||
} | ||
|
||
merged := map[interface{}]interface{}{} | ||
vMerged := reflect.ValueOf(merged) | ||
for _, input := range input { | ||
var data map[interface{}]interface{} | ||
b := []byte(input) | ||
|
||
err := YamlUnmarshal(b, &data) | ||
if err != nil { | ||
function.ConcatFuncErrors(resp.Error, function.NewFuncError("Error reading YAML string: "+err.Error())) | ||
return | ||
} | ||
|
||
vData := reflect.ValueOf(data) | ||
|
||
err = MergeMaps(vMerged, vData, true) | ||
if err != nil { | ||
function.ConcatFuncErrors(resp.Error, function.NewFuncError("Error merging YAML: "+err.Error())) | ||
return | ||
} | ||
} | ||
|
||
output, err := yaml.Marshal(merged) | ||
if err != nil { | ||
function.ConcatFuncErrors(resp.Error, function.NewFuncError("Error converting results to YAML: "+err.Error())) | ||
return | ||
} | ||
|
||
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, string(output))) | ||
} |
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,43 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
) | ||
|
||
func TestExampleFunction_Known(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(tfversion.Version1_8_0), | ||
}, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccFucntionUtilsYamlMerge_config(basic_inputYaml1, basic_inputYaml2, map[string]string{"ELEM1": "value1"}), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", basic_ouputYaml), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccFucntionUtilsYamlMerge_config(yaml1, yaml2 string, envs map[string]string) string { | ||
for k, v := range envs { | ||
os.Setenv(k, v) | ||
} | ||
return fmt.Sprintf(` | ||
locals { | ||
yaml1 = <<-EOT%sEOT | ||
yaml2 = <<-EOT%sEOT | ||
} | ||
output "test" { | ||
value = provider::utils::yaml_merge([local.yaml1, local.yaml2]) | ||
} | ||
`, yaml1, yaml2) | ||
} |
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