Skip to content

Commit

Permalink
Merge pull request #38 from mustafasencer/fix-adopt-subscription-reso…
Browse files Browse the repository at this point in the history
…urce

Fix subscription resource adoption
  • Loading branch information
jaypipes authored Jun 2, 2023
2 parents 59329cf + c026c35 commit dfce0fd
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 13 deletions.
4 changes: 2 additions & 2 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: "2023-05-31T12:45:46Z"
build_date: "2023-06-01T10:26:45Z"
build_hash: 8f3ba427974fd6e769926778d54834eaee3b81a3
go_version: go1.19
version: v0.26.1
api_directory_checksum: 017e52b555b690a39a60e82a6f9574ab42b52973
api_version: v1alpha1
aws_sdk_go_version: v1.44.197
generator_config_info:
file_checksum: 9a21f966d921ec65e3229f88851e55c4fb43948a
file_checksum: e8c9e140ef5690daab43e5452645acfd993dc678
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 @@ -166,6 +166,8 @@ resources:
hooks:
sdk_create_post_build_request:
template_path: hooks/subscription/sdk_create_post_build_request.go.tpl
sdk_get_attributes_pre_set_output:
template_path: hooks/subscription/sdk_get_attributes_pre_set_output.go.tpl
fields:
ConfirmationWasAuthenticated:
is_attribute: true
Expand Down
2 changes: 2 additions & 0 deletions generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ resources:
hooks:
sdk_create_post_build_request:
template_path: hooks/subscription/sdk_create_post_build_request.go.tpl
sdk_get_attributes_pre_set_output:
template_path: hooks/subscription/sdk_get_attributes_pre_set_output.go.tpl
fields:
ConfirmationWasAuthenticated:
is_attribute: true
Expand Down
14 changes: 14 additions & 0 deletions pkg/resource/subscription/sdk.go

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

// If one of the below 3 fields (i.e. Protocol, Endpoint, TopicARN/TopicRef) is empty
// populate them with their respective attribute values
// present in the response of the GetSubscriptionAttributes API call
// Use case: adopting an existing subscription by subcription ARN
if ko.Spec.Protocol == nil {
ko.Spec.Protocol = resp.Attributes["Protocol"]
}
if ko.Spec.Endpoint == nil {
ko.Spec.Endpoint = resp.Attributes["Endpoint"]
}
if ko.Spec.TopicARN == nil && ko.Spec.TopicRef == nil {
ko.Spec.TopicARN = resp.Attributes["TopicArn"]
}
6 changes: 4 additions & 2 deletions test/e2e/bootstrap_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

@dataclass
class BootstrapResources(Resources):
Topic: Topic
Queue: Queue
Topic1: Topic
Topic2: Topic
Queue1: Queue
Queue2: Queue

_bootstrap_resources = None

Expand Down
1 change: 1 addition & 0 deletions test/e2e/common/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
TOPIC_RESOURCE_KIND = 'Topic'
SUBSCRIPTION_RESOURCE_KIND = 'Subscription'
TOPIC_RESOURCE_PLURAL = 'topics'
SUBSCRIPTION_RESOURCE_PLURAL = 'subscriptions'
PLATFORM_ENDPOINT_RESOURCE_PLURAL = 'platformendpoints'
Expand Down
23 changes: 17 additions & 6 deletions test/e2e/service_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from e2e import bootstrap_directory
from e2e.bootstrap_resources import BootstrapResources

topic = Topic(name_prefix="subscribe-topic")
topic1 = Topic(name_prefix="subscribe-topic")
topic2 = Topic(name_prefix="adoption-subscribe-topic")

queue_policy = """{
"Statement": [
Expand All @@ -43,19 +44,29 @@
}
"""

queue_policy_vars = {
"$TOPIC_NAME": topic.name,
queue1_policy_vars = {
"$TOPIC_NAME": topic1.name,
}

queue2_policy_vars = {
"$TOPIC_NAME": topic2.name,
}

def service_bootstrap() -> Resources:
logging.getLogger().setLevel(logging.INFO)

resources = BootstrapResources(
Topic=topic,
Queue=Queue(
Topic1=topic1,
Topic2=topic2,
Queue1=Queue(
name_prefix="subscribe-queue",
policy=queue_policy,
policy_vars=queue_policy_vars,
policy_vars=queue1_policy_vars,
),
Queue2=Queue(
name_prefix="adoption-subscribe-queue",
policy=queue_policy,
policy_vars=queue2_policy_vars,
),
)

Expand Down
34 changes: 31 additions & 3 deletions test/e2e/tests/test_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
import time

import pytest
import boto3

from acktest.k8s import condition
from acktest.k8s import resource as k8s
from acktest.resources import random_suffix_name
from acktest import adoption
from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_resource
from e2e.bootstrap_resources import get_bootstrap_resources
from e2e.common.types import SUBSCRIPTION_RESOURCE_PLURAL
from e2e.common.types import SUBSCRIPTION_RESOURCE_KIND, SUBSCRIPTION_RESOURCE_PLURAL
from e2e.replacement_values import REPLACEMENT_VALUES
from e2e import subscription

Expand All @@ -38,8 +40,8 @@ def subscription_sqs():
display_name = "a subscription to a queue"

boot_resources = get_bootstrap_resources()
q = boot_resources.Queue
topic = boot_resources.Topic
q = boot_resources.Queue1
topic = boot_resources.Topic1

replacements = REPLACEMENT_VALUES.copy()
replacements['SUBSCRIPTION_NAME'] = subscription_name
Expand Down Expand Up @@ -134,3 +136,29 @@ def test_crud(self, subscription_sqs):
assert 'healthyRetryPolicy' in got_delivery_policy
exp_healthy_retry_policy = delivery_policy['healthyRetryPolicy']
assert exp_healthy_retry_policy == got_delivery_policy['healthyRetryPolicy']


class TestAdoptSubscription(adoption.AbstractAdoptionTest):
RESOURCE_PLURAL: str = SUBSCRIPTION_RESOURCE_PLURAL
RESOURCE_VERSION: str = CRD_VERSION

_subscription_arn: str

def bootstrap_resource(self):
boot_resources = get_bootstrap_resources()
queue = boot_resources.Queue2
topic = boot_resources.Topic2

client = boto3.client('sns')
resp = client.subscribe(TopicArn=topic.arn, Protocol='sqs', Endpoint=queue.arn, ReturnSubscriptionArn=True)
self._subscription_arn = resp['SubscriptionArn']

def cleanup_resource(self):
client = boto3.client('sns')
client.unsubscribe(SubscriptionArn=self._subscription_arn)

def get_resource_spec(self) -> adoption.AdoptedResourceSpec:
return adoption.AdoptedResourceSpec(
aws=adoption.AdoptedResourceARNIdentifier(additionalKeys={}, arn=self._subscription_arn),
kubernetes=adoption.AdoptedResourceKubernetesIdentifiers(CRD_GROUP, SUBSCRIPTION_RESOURCE_KIND),
)

0 comments on commit dfce0fd

Please sign in to comment.