Skip to content

Commit 943b636

Browse files
committed
Code improvements
1 parent 779ce34 commit 943b636

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

internal/service/transcribe/start_transcription_job_action.go

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package transcribe
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"time"
1011

@@ -21,6 +22,11 @@ import (
2122
"github.com/hashicorp/terraform-provider-aws/names"
2223
)
2324

25+
const (
26+
transcriptionJobPollInterval = 5 * time.Second
27+
transcriptionJobProgressInterval = 30 * time.Second
28+
)
29+
2430
// @Action(aws_transcribe_start_transcription_job, name="Start Transcription Job")
2531
func newStartTranscriptionJobAction(_ context.Context) (action.ActionWithConfigure, error) {
2632
return &startTranscriptionJobAction{}, nil
@@ -138,31 +144,30 @@ func (a *startTranscriptionJobAction) Invoke(ctx context.Context, req action.Inv
138144
},
139145
}
140146

141-
// Validate language configuration
142-
hasLanguageCode := !config.LanguageCode.IsNull() && !config.LanguageCode.IsUnknown()
143-
hasIdentifyLanguage := !config.IdentifyLanguage.IsNull() && config.IdentifyLanguage.ValueBool()
144-
hasIdentifyMultipleLanguages := !config.IdentifyMultipleLanguages.IsNull() && config.IdentifyMultipleLanguages.ValueBool()
145-
146-
languageConfigCount := 0
147-
if hasLanguageCode {
148-
languageConfigCount++
147+
// Validate language configuration - exactly one must be specified
148+
languageOptions := []bool{
149+
!config.LanguageCode.IsNull() && !config.LanguageCode.IsUnknown(),
150+
!config.IdentifyLanguage.IsNull() && config.IdentifyLanguage.ValueBool(),
151+
!config.IdentifyMultipleLanguages.IsNull() && config.IdentifyMultipleLanguages.ValueBool(),
149152
}
150-
if hasIdentifyLanguage {
151-
languageConfigCount++
152-
}
153-
if hasIdentifyMultipleLanguages {
154-
languageConfigCount++
153+
154+
activeCount := 0
155+
for _, active := range languageOptions {
156+
if active {
157+
activeCount++
158+
}
155159
}
156160

157-
if languageConfigCount == 0 {
161+
switch activeCount {
162+
case 0:
158163
resp.Diagnostics.AddError(
159164
"Missing Language Configuration",
160165
"You must specify exactly one of: language_code, identify_language, or identify_multiple_languages",
161166
)
162167
return
163-
}
164-
165-
if languageConfigCount > 1 {
168+
case 1:
169+
// Valid - continue
170+
default:
166171
resp.Diagnostics.AddError(
167172
"Conflicting Language Configuration",
168173
"You can only specify one of: language_code, identify_language, or identify_multiple_languages",
@@ -171,13 +176,13 @@ func (a *startTranscriptionJobAction) Invoke(ctx context.Context, req action.Inv
171176
}
172177

173178
// Set language configuration
174-
if hasLanguageCode {
179+
if languageOptions[0] {
175180
input.LanguageCode = config.LanguageCode.ValueEnum()
176181
}
177-
if hasIdentifyLanguage {
182+
if languageOptions[1] {
178183
input.IdentifyLanguage = aws.Bool(true)
179184
}
180-
if hasIdentifyMultipleLanguages {
185+
if languageOptions[2] {
181186
input.IdentifyMultipleLanguages = aws.Bool(true)
182187
}
183188

@@ -222,8 +227,8 @@ func (a *startTranscriptionJobAction) Invoke(ctx context.Context, req action.Inv
222227
return actionwait.FetchResult[*awstypes.TranscriptionJob]{Status: actionwait.Status(status), Value: getOutput.TranscriptionJob}, nil
223228
}, actionwait.Options[*awstypes.TranscriptionJob]{
224229
Timeout: timeout,
225-
Interval: actionwait.FixedInterval(5 * time.Second),
226-
ProgressInterval: 30 * time.Second,
230+
Interval: actionwait.FixedInterval(transcriptionJobPollInterval),
231+
ProgressInterval: transcriptionJobProgressInterval,
227232
SuccessStates: []actionwait.Status{
228233
actionwait.Status(awstypes.TranscriptionJobStatusInProgress),
229234
actionwait.Status(awstypes.TranscriptionJobStatusCompleted),
@@ -239,23 +244,26 @@ func (a *startTranscriptionJobAction) Invoke(ctx context.Context, req action.Inv
239244
},
240245
})
241246
if err != nil {
242-
switch e := err.(type) {
243-
case *actionwait.TimeoutError:
247+
var timeoutErr *actionwait.TimeoutError
248+
var failureErr *actionwait.FailureStateError
249+
var unexpectedErr *actionwait.UnexpectedStateError
250+
251+
if errors.As(err, &timeoutErr) {
244252
resp.Diagnostics.AddError(
245253
"Timeout Waiting for Transcription Job",
246254
fmt.Sprintf("Transcription job %s did not reach a running state within %v", transcriptionJobName, timeout),
247255
)
248-
case *actionwait.FailureStateError:
256+
} else if errors.As(err, &failureErr) {
249257
resp.Diagnostics.AddError(
250258
"Transcription Job Failed",
251-
fmt.Sprintf("Transcription job %s failed: %s", transcriptionJobName, e.Status),
259+
fmt.Sprintf("Transcription job %s failed: %s", transcriptionJobName, failureErr.Status),
252260
)
253-
case *actionwait.UnexpectedStateError:
261+
} else if errors.As(err, &unexpectedErr) {
254262
resp.Diagnostics.AddError(
255263
"Unexpected Transcription Job Status",
256-
fmt.Sprintf("Transcription job %s entered unexpected status: %s", transcriptionJobName, e.Status),
264+
fmt.Sprintf("Transcription job %s entered unexpected status: %s", transcriptionJobName, unexpectedErr.Status),
257265
)
258-
default:
266+
} else {
259267
resp.Diagnostics.AddError(
260268
"Error Waiting for Transcription Job",
261269
fmt.Sprintf("Error while waiting for transcription job %s: %s", transcriptionJobName, err),

0 commit comments

Comments
 (0)