Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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 pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ func setExternalTagsWithPaved(externalTags map[string]string, paved *fieldpath.P
type InjectedKey struct {
Key string
DefaultValue string
Description string
}
Comment thread
fernandezcuesta marked this conversation as resolved.

// ListMapKeys is the list map keys when the server-side apply merge strategy
Expand Down
6 changes: 5 additions & 1 deletion pkg/types/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,14 @@ func injectServerSideApplyListMergeKeys(cfg *config.Resource) error { //nolint:g
return errors.Errorf("element schema for the object list %q already contains the argument key %q", f, k)
}
}
desc := descriptionInjectedKey
if s.ListMergeStrategy.ListMapKeys.InjectedKey.Description != "" {
desc = s.ListMergeStrategy.ListMapKeys.InjectedKey.Description
}
el.Schema[s.ListMergeStrategy.ListMapKeys.InjectedKey.Key] = &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: descriptionInjectedKey,
Description: desc,
}
if s.ListMergeStrategy.ListMapKeys.InjectedKey.DefaultValue != "" {
el.Schema[s.ListMergeStrategy.ListMapKeys.InjectedKey.Key].Default = s.ListMergeStrategy.ListMapKeys.InjectedKey.DefaultValue
Expand Down
91 changes: 91 additions & 0 deletions pkg/types/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,97 @@ func TestBuilder_generateTypeName(t *testing.T) {
}
}

func TestInjectServerSideApplyListMergeKeys(t *testing.T) {
type want struct {
description string
err error
}
cases := map[string]struct {
cfg *config.Resource
want want
}{
"DefaultDescription": {
cfg: &config.Resource{
TerraformResource: &schema.Resource{
Schema: map[string]*schema.Schema{
"rule": {
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {Type: schema.TypeString},
},
},
},
},
},
ServerSideApplyMergeStrategies: map[string]config.MergeStrategy{
"rule": {
ListMergeStrategy: config.ListMergeStrategy{
MergeStrategy: config.ListTypeMap,
ListMapKeys: config.ListMapKeys{
InjectedKey: config.InjectedKey{
Key: "index",
},
},
},
},
},
},
want: want{
description: descriptionInjectedKey,
},
},
"CustomDescription": {
cfg: &config.Resource{
TerraformResource: &schema.Resource{
Schema: map[string]*schema.Schema{
"rule": {
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {Type: schema.TypeString},
},
},
},
},
},
ServerSideApplyMergeStrategies: map[string]config.MergeStrategy{
"rule": {
ListMergeStrategy: config.ListMergeStrategy{
MergeStrategy: config.ListTypeMap,
ListMapKeys: config.ListMapKeys{
InjectedKey: config.InjectedKey{
Key: "index",
Description: "Custom description for the injected key.",
},
},
},
},
},
},
want: want{
description: "Custom description for the injected key.",
},
},
}
for n, tc := range cases {
t.Run(n, func(t *testing.T) {
err := injectServerSideApplyListMergeKeys(tc.cfg)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Fatalf("injectServerSideApplyListMergeKeys(...): -want error, +got error: %s", diff)
}
if err != nil {
return
}
el := tc.cfg.TerraformResource.Schema["rule"].Elem.(*schema.Resource)
got := el.Schema["index"].Description
if diff := cmp.Diff(tc.want.description, got); diff != "" {
t.Errorf("injectServerSideApplyListMergeKeys(...) description: -want, +got: %s", diff)
}
})
}
}

func TestBuild(t *testing.T) {
type args struct {
crdScope CRDScope
Expand Down
Loading