Skip to content

Commit 5d24bb4

Browse files
authored
Merge pull request #30786 from dod38fr/b-sagemaker-segv
fix segv in sagemaker app resource
2 parents de0c196 + 7455ebe commit 5d24bb4

File tree

3 files changed

+23
-40
lines changed

3 files changed

+23
-40
lines changed

.changelog/30786.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/aws_sagemaker_app: Fix crash when app is not found
3+
```

internal/service/sagemaker/app_test.go

+10-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"regexp"
77
"testing"
88

9-
"github.com/aws/aws-sdk-go/aws"
109
"github.com/aws/aws-sdk-go/service/sagemaker"
1110
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1211
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -248,39 +247,36 @@ func testAccCheckAppDestroy(ctx context.Context) resource.TestCheckFunc {
248247
continue
249248
}
250249

251-
userProfileOrSpaceName := ""
252250
domainID := rs.Primary.Attributes["domain_id"]
253251
appType := rs.Primary.Attributes["app_type"]
254252
appName := rs.Primary.Attributes["app_name"]
255253

254+
var userProfileOrSpaceName string
256255
if v, ok := rs.Primary.Attributes["user_profile_name"]; ok {
257256
userProfileOrSpaceName = v
258257
}
259-
260258
if v, ok := rs.Primary.Attributes["space_name"]; ok {
261259
userProfileOrSpaceName = v
262260
}
263261

264-
app, err := tfsagemaker.FindAppByName(ctx, conn, domainID, userProfileOrSpaceName, appType, appName)
262+
_, err := tfsagemaker.FindAppByName(ctx, conn, domainID, userProfileOrSpaceName, appType, appName)
265263

266264
if tfresource.NotFound(err) {
267265
continue
268266
}
269267

270268
if err != nil {
271-
return fmt.Errorf("reading SageMaker App (%s): %w", rs.Primary.ID, err)
269+
return err
272270
}
273271

274-
if aws.StringValue(app.AppArn) == rs.Primary.ID {
275-
return fmt.Errorf("sagemaker App %q still exists", rs.Primary.ID)
276-
}
272+
return fmt.Errorf("SageMaker App (%s) still exists", rs.Primary.ID)
277273
}
278274

279275
return nil
280276
}
281277
}
282278

283-
func testAccCheckAppExists(ctx context.Context, n string, app *sagemaker.DescribeAppOutput) resource.TestCheckFunc {
279+
func testAccCheckAppExists(ctx context.Context, n string, v *sagemaker.DescribeAppOutput) resource.TestCheckFunc {
284280
return func(s *terraform.State) error {
285281
rs, ok := s.RootModule().Resources[n]
286282
if !ok {
@@ -292,25 +288,26 @@ func testAccCheckAppExists(ctx context.Context, n string, app *sagemaker.Describ
292288
}
293289

294290
conn := acctest.Provider.Meta().(*conns.AWSClient).SageMakerConn()
295-
userProfileOrSpaceName := ""
291+
296292
domainID := rs.Primary.Attributes["domain_id"]
297293
appType := rs.Primary.Attributes["app_type"]
298294
appName := rs.Primary.Attributes["app_name"]
299295

296+
var userProfileOrSpaceName string
300297
if v, ok := rs.Primary.Attributes["user_profile_name"]; ok {
301298
userProfileOrSpaceName = v
302299
}
303-
304300
if v, ok := rs.Primary.Attributes["space_name"]; ok {
305301
userProfileOrSpaceName = v
306302
}
307303

308-
resp, err := tfsagemaker.FindAppByName(ctx, conn, domainID, userProfileOrSpaceName, appType, appName)
304+
output, err := tfsagemaker.FindAppByName(ctx, conn, domainID, userProfileOrSpaceName, appType, appName)
305+
309306
if err != nil {
310307
return err
311308
}
312309

313-
*app = *resp
310+
*v = *output
314311

315312
return nil
316313
}

internal/service/sagemaker/find.go

+10-27
Original file line numberDiff line numberDiff line change
@@ -271,23 +271,22 @@ func FindAppImageConfigByName(ctx context.Context, conn *sagemaker.SageMaker, ap
271271
}
272272

273273
func listAppsByName(ctx context.Context, conn *sagemaker.SageMaker, domainID, userProfileOrSpaceName, appType, appName string) (*sagemaker.AppDetails, error) {
274-
var apps []*sagemaker.AppDetails
275-
276274
input := &sagemaker.ListAppsInput{
277275
DomainIdEquals: aws.String(domainID),
278276
}
277+
var output []*sagemaker.AppDetails
279278

280279
err := conn.ListAppsPagesWithContext(ctx, input, func(page *sagemaker.ListAppsOutput, lastPage bool) bool {
281280
if page == nil {
282281
return !lastPage
283282
}
284283

285-
for _, app := range page.Apps {
286-
if app == nil {
284+
for _, v := range page.Apps {
285+
if v == nil {
287286
continue
288287
}
289288

290-
apps = append(apps, app)
289+
output = append(output, v)
291290
}
292291

293292
return !lastPage
@@ -297,29 +296,15 @@ func listAppsByName(ctx context.Context, conn *sagemaker.SageMaker, domainID, us
297296
return nil, err
298297
}
299298

300-
if len(apps) == 0 {
301-
return nil, nil
302-
}
303-
304-
var foundApp *sagemaker.AppDetails
305-
for _, app := range apps {
306-
if aws.StringValue(app.AppName) == appName &&
307-
aws.StringValue(app.AppType) == appType &&
308-
(aws.StringValue(app.SpaceName) == userProfileOrSpaceName ||
309-
aws.StringValue(app.UserProfileName) == userProfileOrSpaceName) {
310-
foundApp = app
299+
for _, v := range output {
300+
if aws.StringValue(v.AppName) == appName && aws.StringValue(v.AppType) == appType && (aws.StringValue(v.SpaceName) == userProfileOrSpaceName || aws.StringValue(v.UserProfileName) == userProfileOrSpaceName) {
301+
return v, nil
311302
}
312303
}
313304

314-
if foundApp == nil {
315-
return nil, tfresource.NewEmptyResultError(input)
316-
}
317-
318-
return foundApp, nil
305+
return nil, &retry.NotFoundError{}
319306
}
320307

321-
// FindAppByName returns the domain corresponding to the specified domain id.
322-
// Returns nil if no domain is found.
323308
func FindAppByName(ctx context.Context, conn *sagemaker.SageMaker, domainID, userProfileOrSpaceName, appType, appName string) (*sagemaker.DescribeAppOutput, error) {
324309
foundApp, err := listAppsByName(ctx, conn, domainID, userProfileOrSpaceName, appType, appName)
325310

@@ -328,15 +313,13 @@ func FindAppByName(ctx context.Context, conn *sagemaker.SageMaker, domainID, use
328313
}
329314

330315
input := &sagemaker.DescribeAppInput{
331-
DomainId: aws.String(domainID),
332-
AppType: aws.String(appType),
333316
AppName: aws.String(appName),
317+
AppType: aws.String(appType),
318+
DomainId: aws.String(domainID),
334319
}
335-
336320
if foundApp.SpaceName != nil {
337321
input.SpaceName = foundApp.SpaceName
338322
}
339-
340323
if foundApp.UserProfileName != nil {
341324
input.UserProfileName = foundApp.UserProfileName
342325
}

0 commit comments

Comments
 (0)