Skip to content

Commit a23860c

Browse files
committed
sns: use attributes instead of dictionary access
1 parent 2591215 commit a23860c

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

nixops_aws/resources/sns_topic.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import nixops.resources
99
import nixops_aws.ec2_utils
1010

11+
from typing import List
12+
1113
from .types.sns_topic import SnsTopicOptions
1214

1315

@@ -110,9 +112,9 @@ def topic_exists(self, arn):
110112
return True
111113
return False
112114

113-
def create(self, defn, check, allow_reboot, allow_recreate):
115+
def create(self, defn: SNSTopicDefinition, check, allow_reboot, allow_recreate):
114116
self.access_key_id = (
115-
defn.config["accessKeyId"] or nixops_aws.ec2_utils.get_access_key_id()
117+
defn.config.accessKeyId or nixops_aws.ec2_utils.get_access_key_id()
116118
)
117119
if not self.access_key_id:
118120
raise Exception(
@@ -121,40 +123,37 @@ def create(self, defn, check, allow_reboot, allow_recreate):
121123

122124
arn = self.arn
123125
if self.state == self.UP and (
124-
self.topic_name != defn.config["name"]
125-
or self.region != defn.config["region"]
126+
self.topic_name != defn.config.name or self.region != defn.config.region
126127
):
127128
self.log("topic definition changed, recreating...")
128129
self._destroy()
129130
self._conn = None
130131

131-
self.region = defn.config["region"]
132+
self.region = defn.config.region
132133

133134
if self.arn is None or not self.topic_exists(arn=self.arn):
134-
self.log("creating SNS topic ‘{0}’...".format(defn.config["name"]))
135-
topic = self._connect().create_topic(defn.config["name"])
135+
self.log("creating SNS topic ‘{0}’...".format(defn.config.name))
136+
topic = self._connect().create_topic(defn.config.name)
136137
arn = topic.get("CreateTopicResponse").get("CreateTopicResult")["TopicArn"]
137138

138-
if defn.config["displayName"] is not None:
139+
if defn.config.displayName is not None:
139140
self._connect().set_topic_attributes(
140-
topic=arn,
141-
attr_name="DisplayName",
142-
attr_value=defn.config["displayName"],
141+
topic=arn, attr_name="DisplayName", attr_value=defn.config.displayName,
143142
)
144143

145-
if defn.config["policy"] != "":
144+
if defn.config.policy != "":
146145
self._connect().set_topic_attributes(
147-
topic=arn, attr_name="Policy", attr_value=defn.config["policy"]
146+
topic=arn, attr_name="Policy", attr_value=defn.config.policy
148147
)
149148

150149
current_subscribers, current_subscriptions_arns = self.get_current_subscribers(
151150
arn=arn
152151
)
153152

154-
if len(defn.config["subscriptions"]) > 0:
155-
for subscriber in defn.config["subscriptions"]:
156-
protocol = subscriber["protocol"]
157-
endpoint = subscriber["endpoint"]
153+
if len(defn.config.subscriptions) > 0:
154+
for subscriber in defn.config.subscriptions:
155+
protocol = subscriber.protocol
156+
endpoint = subscriber.endpoint
158157
if endpoint not in current_subscribers:
159158
self.log(
160159
"adding SNS subscriber with endpoint '{0}'...".format(endpoint)
@@ -180,11 +179,11 @@ def create(self, defn, check, allow_reboot, allow_recreate):
180179

181180
with self.depl._db:
182181
self.state = self.UP
183-
self.topic_name = defn.config["name"]
184-
self.display_name = defn.config["displayName"]
185-
self.policy = defn.config["policy"]
182+
self.topic_name = defn.config.name
183+
self.display_name = defn.config.displayName
184+
self.policy = defn.config.policy
186185
self.arn = arn
187-
self.subscriptions = defn.config["subscriptions"]
186+
self.subscriptions = defn.config.subscriptions
188187

189188
def get_current_subscribers(self, arn):
190189
response = self._connect().get_all_subscriptions_by_topic(topic=arn)
@@ -201,11 +200,11 @@ def get_current_subscribers(self, arn):
201200
]
202201
return current_endpoints, current_subscriptions_arns
203202

204-
def get_defn_endpoints(self, defn):
203+
def get_defn_endpoints(self, defn: SNSTopicDefinition) -> List[str]:
205204
defn_endpoints = []
206-
if len(defn.config["subscriptions"]) > 0:
207-
for subscriber in defn.config["subscriptions"]:
208-
defn_endpoints.append(subscriber["endpoint"])
205+
if len(defn.config.subscriptions) > 0:
206+
for subscriber in defn.config.subscriptions:
207+
defn_endpoints.append(subscriber.endpoint)
209208
return defn_endpoints
210209

211210
def destroy(self, wipe=False):

nixops_aws/resources/types/sns_topic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class SubscriptionsOptions(ResourceOptions):
99

1010

1111
class SnsTopicOptions(ResourceOptions):
12-
accessKeyId: str
12+
accessKeyId: Optional[str]
1313
arn: str
1414
displayName: Optional[str]
1515
name: str

0 commit comments

Comments
 (0)