Skip to content

Bugfix/compare attribute definitions #59

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

Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion apis/v1alpha1/ack-generate-metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ api_directory_checksum: 553eee36730dd0637424a8d9348b37ee90eb594d
api_version: v1alpha1
aws_sdk_go_version: v1.44.93
generator_config_info:
file_checksum: cf8320749a4188537376f8cec2b93e5aea7b3b52
file_checksum: 8b17e0975b403cc552eba69aec5cd03a30956729
original_file_name: generator.yaml
last_modification:
reason: API generation
3 changes: 3 additions & 0 deletions apis/v1alpha1/generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ resources:
Tags:
compare:
is_ignored: true
AttributeDefinitions:
compare:
is_ignored: true
exceptions:
errors:
404:
Expand Down
3 changes: 3 additions & 0 deletions generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ resources:
Tags:
compare:
is_ignored: true
AttributeDefinitions:
compare:
is_ignored: true
exceptions:
errors:
404:
Expand Down
3 changes: 0 additions & 3 deletions pkg/resource/table/delta.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion pkg/resource/table/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,15 @@ func customPreCompare(
a *resource,
b *resource,
) {
// TODO(hilalymh): customDeltaFunctions for AttributeDefinitions
// customDeltaFunctions for AttributeDefinitions
// see https://github.com/aws-controllers-k8s/community/issues/1599
if len(a.ko.Spec.AttributeDefinitions) != len(b.ko.Spec.AttributeDefinitions) {
delta.Add("Spec.AttributeDefinitions", a.ko.Spec.AttributeDefinitions, b.ko.Spec.AttributeDefinitions)
} else if a.ko.Spec.AttributeDefinitions != nil && b.ko.Spec.AttributeDefinitions != nil {
if !equalAttributeDefinitions(a.ko.Spec.AttributeDefinitions, b.ko.Spec.AttributeDefinitions) {
delta.Add("Spec.AttributeDefinitions", a.ko.Spec.AttributeDefinitions, b.ko.Spec.AttributeDefinitions)
}
}
// TODO(hilalymh): customDeltaFunctions for GlobalSecondaryIndexes

// See https://github.com/aws-controllers-k8s/community/issues/1595
Expand All @@ -444,3 +452,21 @@ func customPreCompare(
}
}
}

func equalAttributeDefinitions(a, b []*v1alpha1.AttributeDefinition) bool {
for _, aElement := range a {
found := false
for _, bElement := range b {
if equalStrings(aElement.AttributeName, bElement.AttributeName) {
found = true
if !equalStrings(aElement.AttributeType, bElement.AttributeType) {
return false
}
}
}
if !found {
return false
}
}
return true
}
238 changes: 238 additions & 0 deletions pkg/resource/table/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,241 @@ func Test_customPreCompare(t *testing.T) {
}
})
}

func Test_newResourceDelta_customDeltaFunction_AttributeDefinitions(t *testing.T) {
type args struct {
a *resource
b *resource
}
tests := []struct {
name string
args args
want bool
}{
{
name: "both desired and latest are nil",
args: args{
a: &resource{
ko: &v1alpha1.Table{
Spec: v1alpha1.TableSpec{
AttributeDefinitions: nil,
},
},
},
b: &resource{
ko: &v1alpha1.Table{
Spec: v1alpha1.TableSpec{
AttributeDefinitions: nil,
},
},
},
},
want: true,
},
{
name: "desired is not nil",
args: args{
a: &resource{
ko: &v1alpha1.Table{
Spec: v1alpha1.TableSpec{
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
{
AttributeName: aws.String("externalId"),
AttributeType: aws.String("S"),
},
},
},
},
},
b: &resource{
ko: &v1alpha1.Table{
Spec: v1alpha1.TableSpec{
AttributeDefinitions: nil,
},
},
},
},
want: false,
},
{
name: "latest is not nil",
args: args{
a: &resource{
ko: &v1alpha1.Table{
Spec: v1alpha1.TableSpec{
AttributeDefinitions: nil,
},
},
},
b: &resource{
ko: &v1alpha1.Table{
Spec: v1alpha1.TableSpec{
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
{
AttributeName: aws.String("externalId"),
AttributeType: aws.String("S"),
},
},
},
},
},
},
want: false,
},
{
name: "desired and latest are equal",
args: args{
a: &resource{
ko: &v1alpha1.Table{
Spec: v1alpha1.TableSpec{
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
{
AttributeName: aws.String("externalId"),
AttributeType: aws.String("S"),
},
{
AttributeName: aws.String("id"),
AttributeType: aws.String("S"),
},
},
},
},
},
b: &resource{
ko: &v1alpha1.Table{
Spec: v1alpha1.TableSpec{
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
{
AttributeName: aws.String("id"),
AttributeType: aws.String("S"),
},
{
AttributeName: aws.String("externalId"),
AttributeType: aws.String("S"),
},
},
},
},
},
},
want: true,
},
{
name: "desired is updated",
args: args{
a: &resource{
ko: &v1alpha1.Table{
Spec: v1alpha1.TableSpec{
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
{
AttributeName: aws.String("externalId"),
AttributeType: aws.String("N"),
},
{
AttributeName: aws.String("id"),
AttributeType: aws.String("S"),
},
},
},
},
},
b: &resource{
ko: &v1alpha1.Table{
Spec: v1alpha1.TableSpec{
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
{
AttributeName: aws.String("id"),
AttributeType: aws.String("S"),
},
{
AttributeName: aws.String("externalId"),
AttributeType: aws.String("S"),
},
},
},
},
},
},
want: false,
},
{
name: "removed in desired",
args: args{
a: &resource{
ko: &v1alpha1.Table{
Spec: v1alpha1.TableSpec{
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
{
AttributeName: aws.String("id"),
AttributeType: aws.String("S"),
},
},
},
},
},
b: &resource{
ko: &v1alpha1.Table{
Spec: v1alpha1.TableSpec{
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
{
AttributeName: aws.String("id"),
AttributeType: aws.String("S"),
},
{
AttributeName: aws.String("externalId"),
AttributeType: aws.String("S"),
},
},
},
},
},
},
want: false,
},
{
name: "added in desired",
args: args{
a: &resource{
ko: &v1alpha1.Table{
Spec: v1alpha1.TableSpec{
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
{
AttributeName: aws.String("id"),
AttributeType: aws.String("S"),
},
{
AttributeName: aws.String("externalId"),
AttributeType: aws.String("S"),
},
},
},
},
},
b: &resource{
ko: &v1alpha1.Table{
Spec: v1alpha1.TableSpec{
AttributeDefinitions: []*v1alpha1.AttributeDefinition{
{
AttributeName: aws.String("id"),
AttributeType: aws.String("S"),
},
},
},
},
},
},
want: false,
},
}

isEqual := func(delta *compare.Delta) bool {
return len(delta.Differences) == 0
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if delta := newResourceDelta(tt.args.a, tt.args.b); isEqual(delta) != tt.want {
t.Errorf("Compare attribution defintions should be %v", tt.want)
}
})
}
}