Skip to content

Commit

Permalink
Make Topic Name an immutable field
Browse files Browse the repository at this point in the history
* Make topic name immutable
* Add tests
  • Loading branch information
michaelhtm committed Oct 25, 2024
1 parent 6c63feb commit 44e8066
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 21 deletions.
10 changes: 5 additions & 5 deletions apis/v1alpha1/ack-generate-metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
ack_generate_info:
build_date: "2024-10-24T02:09:19Z"
build_hash: 36c2d234498c2bc4f60773ab8df632af4067f43b
build_date: "2024-10-25T16:25:52Z"
build_hash: 846d41fb7572ef07f0df63d4f85151cab64ac000
go_version: go1.23.2
version: v0.39.1
api_directory_checksum: 07a2ec03f194442b69cf147a88a4cc33badaa895
version: v0.39.1-1-g846d41f-dirty
api_directory_checksum: bb2924a5a5951ce46444a2d1fdc56059e156a9b7
api_version: v1alpha1
aws_sdk_go_version: v1.49.0
generator_config_info:
file_checksum: e95b5fd9a0571826102c9546eae4cc73228a8395
file_checksum: da7d9b038cf0421583a14109fe0e04628911ab69
original_file_name: generator.yaml
last_modification:
reason: API generation
2 changes: 2 additions & 0 deletions apis/v1alpha1/generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ resources:
sdk_get_attributes_post_set_output:
template_path: hooks/topic/sdk_get_attributes_post_set_output.go.tpl
fields:
Name:
is_immutable: true
TopicArn:
is_attribute: true
is_arn: true
Expand Down
2 changes: 2 additions & 0 deletions generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ resources:
sdk_get_attributes_post_set_output:
template_path: hooks/topic/sdk_get_attributes_post_set_output.go.tpl
fields:
Name:
is_immutable: true
TopicArn:
is_attribute: true
is_arn: true
Expand Down
4 changes: 3 additions & 1 deletion pkg/resource/platform_application/sdk.go

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

4 changes: 3 additions & 1 deletion pkg/resource/platform_endpoint/sdk.go

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

5 changes: 5 additions & 0 deletions pkg/resource/topic/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package topic
import (
"context"
"fmt"
"strings"

ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
Expand Down Expand Up @@ -60,6 +61,10 @@ func (rm *resourceManager) customUpdate(
defer func() {
exit(err)
}()
if immutableFieldChanges := rm.getImmutableFieldChanges(delta); len(immutableFieldChanges) > 0 {
msg := fmt.Sprintf("Immutable Spec fields have been modified: %s", strings.Join(immutableFieldChanges, ","))
return nil, ackerr.NewTerminalError(fmt.Errorf(msg))
}
if delta.DifferentAt("Spec.Tags") {
if err := rm.syncTags(ctx, desired, latest); err != nil {
return nil, err
Expand Down
24 changes: 17 additions & 7 deletions pkg/resource/topic/sdk.go

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

12 changes: 5 additions & 7 deletions templates/hooks/topic/sdk_get_attributes_pre_set_output.go.tpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@

// If the Name field is empty, populate it with the last part of the topic ARN
// Populate the name field with the last part of the topic ARN
// This is a workaround for the fact that the Name field is required by the
// CreateTopic API call, but not by the GetTopicAttributes API call
// Use case: adopting an existing topic by topic ARN
if ko.Spec.Name == nil {
topicName, err := rm.getTopicNameFromARN(tmpARN)
if err != nil {
return nil, err
}
ko.Spec.Name = &topicName
topicName, err := rm.getTopicNameFromARN(tmpARN)
if err != nil {
return nil, err
}
ko.Spec.Name = &topicName

15 changes: 15 additions & 0 deletions test/e2e/tests/test_topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,21 @@ def test_crud(self, simple_topic):
expect_after_update_tags, latest_tags,
)

updates = {
"spec": {"name": "my-simple-topic-edited"}
}
k8s.patch_custom_resource(ref, updates)
time.sleep(MODIFY_WAIT_AFTER_SECONDS)
k8s.wait_resource_consumed_by_controller(ref)
condition.assert_type_status(ref, condition.CONDITION_TYPE_TERMINAL)

expected_msg = "Immutable Spec fields have been modified: Name"
terminal_condition = k8s.get_resource_condition(ref, condition.CONDITION_TYPE_TERMINAL)
# Example condition message:
# An error occurred (BadRequestException) when calling the CreateApiMapping operation:
# Invalid API identifier specified: xxxxxxxx
assert expected_msg in terminal_condition['message']

def test_crud_fifo(self, fifo_topic):
ref, res = fifo_topic

Expand Down

0 comments on commit 44e8066

Please sign in to comment.