@@ -143,9 +143,14 @@ func (r *resourceSourceLocation) Read(ctx context.Context, req resource.ReadRequ
143
143
}
144
144
145
145
func (r * resourceSourceLocation ) Update (ctx context.Context , req resource.UpdateRequest , resp * resource.UpdateResponse ) {
146
- var plan sourceLocationModel
147
- diags := req .Plan .Get (ctx , & plan )
148
- resp .Diagnostics .Append (diags ... )
146
+ var currentState , plan sourceLocationModel
147
+
148
+ resp .Diagnostics .Append (req .State .Get (ctx , & currentState )... )
149
+ if resp .Diagnostics .HasError () {
150
+ return
151
+ }
152
+
153
+ resp .Diagnostics .Append (req .Plan .Get (ctx , & plan )... )
149
154
if resp .Diagnostics .HasError () {
150
155
return
151
156
}
@@ -158,117 +163,54 @@ func (r *resourceSourceLocation) Update(ctx context.Context, req resource.Update
158
163
return
159
164
}
160
165
161
- oldTags := sourceLocation .Tags
162
- newTags := plan .Tags
163
-
164
- // Check if tags are different
165
- if ! reflect .DeepEqual (oldTags , newTags ) {
166
- err = V2UpdatesTags (r .client , oldTags , newTags , * sourceLocation .Arn )
167
- if err != nil {
168
- resp .Diagnostics .AddError (
169
- "Error while updating playback configuration tags" + err .Error (),
170
- err .Error (),
171
- )
172
- }
166
+ err = V2UpdatesTags (r .client , sourceLocation .Tags , plan .Tags , * sourceLocation .Arn )
167
+ if err != nil {
168
+ resp .Diagnostics .AddError (
169
+ "Error while updating playback configuration tags" + err .Error (),
170
+ err .Error (),
171
+ )
173
172
}
174
173
175
- if ! reflect .DeepEqual (sourceLocation .AccessConfiguration , plan .AccessConfiguration ) {
176
- // delete source location
177
- name := plan .Name
178
- err := deleteSourceLocation (r .client , name )
174
+ if ! reflect .DeepEqual (currentState .AccessConfiguration , plan .AccessConfiguration ) {
175
+ updatedSourceLocation , err := recreateSourceLocation (r .client , plan )
179
176
if err != nil {
180
- resp .Diagnostics .AddError (
181
- "Error while deleting source location " + err .Error (),
182
- err .Error (),
183
- )
177
+ resp .Diagnostics .AddError ("Error while recreating source location " + err .Error (), err .Error ())
184
178
return
185
179
}
180
+ plan = * updatedSourceLocation
186
181
187
- // create source location
188
- params := getCreateSourceLocationInput (plan )
189
- sourceLocation , err := r .client .CreateSourceLocation (ctx , & params )
182
+ } else {
183
+ params := getUpdateSourceLocationInput (plan )
184
+ sourceLocationUpdated , err := r .client .UpdateSourceLocation (ctx , & params )
190
185
if err != nil {
191
186
resp .Diagnostics .AddError (
192
- "Error while creating new source location with new access configuration " + err .Error (),
187
+ "Error updating source location. " + err .Error (),
193
188
err .Error (),
194
189
)
195
190
return
196
191
}
197
-
198
- plan = writeSourceLocationToPlan (plan , * sourceLocation , true )
192
+ plan = writeSourceLocationToPlan (plan , mediatailor .CreateSourceLocationOutput (* sourceLocationUpdated ), true )
199
193
}
200
194
201
- params := getUpdateSourceLocationInput (plan )
202
-
203
- sourceLocationUpdated , err := r .client .UpdateSourceLocation (ctx , & params )
204
- if err != nil {
205
- resp .Diagnostics .AddError (
206
- "Error updating source location " + err .Error (),
207
- err .Error (),
208
- )
209
- return
210
- }
211
-
212
- plan = writeSourceLocationToPlan (plan , mediatailor .CreateSourceLocationOutput (* sourceLocationUpdated ), true )
213
-
214
- diags = resp .State .Set (ctx , plan )
215
- resp .Diagnostics .Append (diags ... )
195
+ resp .Diagnostics .Append (resp .State .Set (ctx , plan )... )
216
196
if resp .Diagnostics .HasError () {
217
197
return
218
198
}
219
199
}
220
200
221
201
func (r * resourceSourceLocation ) Delete (ctx context.Context , req resource.DeleteRequest , resp * resource.DeleteResponse ) {
222
202
var state sourceLocationModel
223
- diags := req .State .Get (ctx , & state )
224
- resp .Diagnostics .Append (diags ... )
203
+ resp .Diagnostics .Append (req .State .Get (ctx , & state )... )
225
204
if resp .Diagnostics .HasError () {
226
205
return
227
206
}
228
207
229
208
name := state .Name
230
209
231
- vodSourcesList , err := r .client .ListVodSources (ctx , & mediatailor.ListVodSourcesInput {SourceLocationName : name })
232
- if err != nil {
233
- resp .Diagnostics .AddError (
234
- "Error retrieving vod sources " + err .Error (),
235
- err .Error (),
236
- )
237
- return
238
- }
239
- for _ , vodSource := range vodSourcesList .Items {
240
- _ , err := r .client .DeleteVodSource (ctx , & mediatailor.DeleteVodSourceInput {SourceLocationName : name , VodSourceName : vodSource .VodSourceName })
241
- if err != nil {
242
- resp .Diagnostics .AddError (
243
- "Error deleting vod sources " + err .Error (),
244
- err .Error (),
245
- )
246
- return
247
- }
248
- }
249
-
250
- liveSourcesList , err := r .client .ListLiveSources (ctx , & mediatailor.ListLiveSourcesInput {SourceLocationName : name })
251
- if err != nil {
252
- resp .Diagnostics .AddError (
253
- "Error retrieving live sources " + err .Error (),
254
- err .Error (),
255
- )
256
- return
257
- }
258
- for _ , liveSource := range liveSourcesList .Items {
259
- if _ , err := r .client .DeleteLiveSource (ctx , & mediatailor.DeleteLiveSourceInput {LiveSourceName : liveSource .LiveSourceName , SourceLocationName : name }); err != nil {
260
- resp .Diagnostics .AddError (
261
- "Error deleting live sources " + err .Error (),
262
- err .Error (),
263
- )
264
- return
265
- }
266
- }
267
-
268
- _ , err = r .client .DeleteSourceLocation (ctx , & mediatailor.DeleteSourceLocationInput {SourceLocationName : name })
210
+ err := deleteSourceLocation (r .client , name )
269
211
if err != nil {
270
212
resp .Diagnostics .AddError (
271
- "Error deleting resource " + err . Error () ,
213
+ "Error while deleting source location" ,
272
214
err .Error (),
273
215
)
274
216
return
0 commit comments