Skip to content

Commit 4679778

Browse files
committed
fix: source location import
OTT-6909
1 parent 1d180aa commit 4679778

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

awsmt/data_source_source_location.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (d *dataSourceSourceLocation) Read(ctx context.Context, req datasource.Read
104104
return
105105
}
106106

107-
data = writeSourceLocationToPlan(data, mediatailor.CreateSourceLocationOutput(*sourceLocation), false)
107+
data = writeSourceLocationToPlan(data, mediatailor.CreateSourceLocationOutput(*sourceLocation))
108108

109109
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
110110
}

awsmt/helpers_source_location.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ func getUpdateSourceLocationInput(model models.SourceLocationModel) mediatailor.
137137
return params
138138
}
139139

140-
func writeSourceLocationToPlan(model models.SourceLocationModel, sourceLocation mediatailor.CreateSourceLocationOutput, isResource bool) models.SourceLocationModel {
140+
func writeSourceLocationToPlan(model models.SourceLocationModel, sourceLocation mediatailor.CreateSourceLocationOutput) models.SourceLocationModel {
141141
// Set state
142142

143143
model = readSourceLocationComputedValues(model, sourceLocation)
144144

145-
model = readAccessConfiguration(model, sourceLocation, isResource)
145+
model = readAccessConfiguration(model, sourceLocation)
146146

147147
model = readDefaultSegmentDeliveryConfiguration(model, sourceLocation)
148148

@@ -179,17 +179,14 @@ func readSourceLocationComputedValues(model models.SourceLocationModel, sourceLo
179179
return model
180180
}
181181

182-
func readAccessConfiguration(model models.SourceLocationModel, sourceLocation mediatailor.CreateSourceLocationOutput, isResource bool) models.SourceLocationModel {
182+
func readAccessConfiguration(model models.SourceLocationModel, sourceLocation mediatailor.CreateSourceLocationOutput) models.SourceLocationModel {
183183
if sourceLocation.AccessConfiguration == nil {
184184
return model
185185
}
186-
if model.AccessConfiguration == nil && isResource {
187-
return model
188-
}
189186

190187
model.AccessConfiguration = &models.AccessConfigurationModel{}
191188

192-
if !(isResource && model.AccessConfiguration == nil) {
189+
if string(sourceLocation.AccessConfiguration.AccessType) != "" {
193190
accessType := string(sourceLocation.AccessConfiguration.AccessType)
194191
model.AccessConfiguration.AccessType = &accessType
195192
}
@@ -304,6 +301,6 @@ func recreateSourceLocation(client *mediatailor.Client, plan models.SourceLocati
304301
if err != nil {
305302
return nil, fmt.Errorf("error while creating new source location with new access configuration %v", err.Error())
306303
}
307-
model := writeSourceLocationToPlan(plan, *sourceLocation, true)
304+
model := writeSourceLocationToPlan(plan, *sourceLocation)
308305
return &model, nil
309306
}

awsmt/resource_source_location.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/hashicorp/terraform-plugin-framework/resource"
99
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1010
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
11+
"strings"
1112
"terraform-provider-mediatailor/awsmt/models"
1213
)
1314

@@ -108,7 +109,7 @@ func (r *resourceSourceLocation) Create(ctx context.Context, req resource.Create
108109
return
109110
}
110111

111-
plan = writeSourceLocationToPlan(plan, *sourceLocation, true)
112+
plan = writeSourceLocationToPlan(plan, *sourceLocation)
112113

113114
diags = resp.State.Set(ctx, plan)
114115
resp.Diagnostics.Append(diags...)
@@ -133,7 +134,7 @@ func (r *resourceSourceLocation) Read(ctx context.Context, req resource.ReadRequ
133134
return
134135
}
135136

136-
state = writeSourceLocationToPlan(state, mediatailor.CreateSourceLocationOutput(*sourceLocation), true)
137+
state = writeSourceLocationToPlan(state, mediatailor.CreateSourceLocationOutput(*sourceLocation))
137138

138139
diags = resp.State.Set(ctx, &state)
139140
resp.Diagnostics.Append(diags...)
@@ -189,7 +190,7 @@ func (r *resourceSourceLocation) Update(ctx context.Context, req resource.Update
189190
)
190191
return
191192
}
192-
plan = writeSourceLocationToPlan(plan, mediatailor.CreateSourceLocationOutput(*sourceLocationUpdated), true)
193+
plan = writeSourceLocationToPlan(plan, mediatailor.CreateSourceLocationOutput(*sourceLocationUpdated))
193194
}
194195

195196
resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
@@ -218,5 +219,29 @@ func (r *resourceSourceLocation) Delete(ctx context.Context, req resource.Delete
218219
}
219220

220221
func (r *resourceSourceLocation) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
221-
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
222+
// Split the import ID to support various import formats
223+
idParts := strings.Split(req.ID, "/")
224+
225+
if len(idParts) == 1 {
226+
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
227+
return
228+
}
229+
230+
// Support ARN import format
231+
if strings.HasPrefix(req.ID, "arn:aws:mediatailor:") {
232+
arnParts := strings.Split(req.ID, ":")
233+
if len(arnParts) >= 6 {
234+
resourcePath := arnParts[5]
235+
resourceParts := strings.Split(resourcePath, "/")
236+
if len(resourceParts) >= 2 && resourceParts[0] == "sourceLocation" {
237+
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), resourceParts[1])...)
238+
return
239+
}
240+
}
241+
}
242+
243+
resp.Diagnostics.AddError(
244+
"Invalid import ID",
245+
"Expected import ID to be either the source location name or the full ARN",
246+
)
222247
}

0 commit comments

Comments
 (0)