8
8
import nixops .resources
9
9
import nixops_aws .ec2_utils
10
10
11
+ from typing import List
12
+
11
13
from .types .sns_topic import SnsTopicOptions
12
14
13
15
@@ -110,9 +112,9 @@ def topic_exists(self, arn):
110
112
return True
111
113
return False
112
114
113
- def create (self , defn , check , allow_reboot , allow_recreate ):
115
+ def create (self , defn : SNSTopicDefinition , check , allow_reboot , allow_recreate ):
114
116
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 ()
116
118
)
117
119
if not self .access_key_id :
118
120
raise Exception (
@@ -121,40 +123,37 @@ def create(self, defn, check, allow_reboot, allow_recreate):
121
123
122
124
arn = self .arn
123
125
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
126
127
):
127
128
self .log ("topic definition changed, recreating..." )
128
129
self ._destroy ()
129
130
self ._conn = None
130
131
131
- self .region = defn .config [ " region" ]
132
+ self .region = defn .config . region
132
133
133
134
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 )
136
137
arn = topic .get ("CreateTopicResponse" ).get ("CreateTopicResult" )["TopicArn" ]
137
138
138
- if defn .config [ " displayName" ] is not None :
139
+ if defn .config . displayName is not None :
139
140
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 ,
143
142
)
144
143
145
- if defn .config [ " policy" ] != "" :
144
+ if defn .config . policy != "" :
146
145
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
148
147
)
149
148
150
149
current_subscribers , current_subscriptions_arns = self .get_current_subscribers (
151
150
arn = arn
152
151
)
153
152
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
158
157
if endpoint not in current_subscribers :
159
158
self .log (
160
159
"adding SNS subscriber with endpoint '{0}'..." .format (endpoint )
@@ -180,11 +179,11 @@ def create(self, defn, check, allow_reboot, allow_recreate):
180
179
181
180
with self .depl ._db :
182
181
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
186
185
self .arn = arn
187
- self .subscriptions = defn .config [ " subscriptions" ]
186
+ self .subscriptions = defn .config . subscriptions
188
187
189
188
def get_current_subscribers (self , arn ):
190
189
response = self ._connect ().get_all_subscriptions_by_topic (topic = arn )
@@ -201,11 +200,11 @@ def get_current_subscribers(self, arn):
201
200
]
202
201
return current_endpoints , current_subscriptions_arns
203
202
204
- def get_defn_endpoints (self , defn ) :
203
+ def get_defn_endpoints (self , defn : SNSTopicDefinition ) -> List [ str ] :
205
204
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 )
209
208
return defn_endpoints
210
209
211
210
def destroy (self , wipe = False ):
0 commit comments