Skip to content

$40976 Fix validation for SageMaker App Image Config: API rejects valid UID/GID pairs defined in provider schema #41112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 31 additions & 77 deletions internal/service/sagemaker/app_image_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_sagemaker_app_image_config", name="App Image Config")
// @Tags(identifierAttribute="arn")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this removed?

func resourceAppImageConfig() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAppImageConfigCreate,
Expand Down Expand Up @@ -85,31 +83,7 @@ func resourceAppImageConfig() *schema.Resource {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"default_gid": {
Type: schema.TypeInt,
Optional: true,
Default: 100,
ValidateFunc: validation.IntInSlice([]int{0, 100}),
},
"default_uid": {
Type: schema.TypeInt,
Optional: true,
Default: 1000,
ValidateFunc: validation.IntInSlice([]int{0, 1000}),
},
"mount_path": {
Type: schema.TypeString,
Optional: true,
Default: "/home/sagemaker-user",
ValidateFunc: validation.All(
validation.StringLenBetween(1, 1024),
validation.StringMatch(regexache.MustCompile(`^\/.*`), "Must start with `/`."),
),
},
},
},
Elem: fileSystemConfigSchema(),
},
},
},
Expand Down Expand Up @@ -148,31 +122,7 @@ func resourceAppImageConfig() *schema.Resource {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"default_gid": {
Type: schema.TypeInt,
Optional: true,
Default: 100,
ValidateFunc: validation.IntInSlice([]int{0, 100}),
},
"default_uid": {
Type: schema.TypeInt,
Optional: true,
Default: 1000,
ValidateFunc: validation.IntInSlice([]int{0, 1000}),
},
"mount_path": {
Type: schema.TypeString,
Optional: true,
Default: "/home/sagemaker-user",
ValidateFunc: validation.All(
validation.StringLenBetween(1, 1024),
validation.StringMatch(regexache.MustCompile(`^\/.*`), "Must start with `/`."),
),
},
},
},
Elem: fileSystemConfigSchema(),
},
},
},
Expand All @@ -187,31 +137,7 @@ func resourceAppImageConfig() *schema.Resource {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"default_gid": {
Type: schema.TypeInt,
Optional: true,
Default: 100,
ValidateFunc: validation.IntInSlice([]int{0, 100}),
},
"default_uid": {
Type: schema.TypeInt,
Optional: true,
Default: 1000,
ValidateFunc: validation.IntInSlice([]int{0, 1000}),
},
"mount_path": {
Type: schema.TypeString,
Optional: true,
Default: "/home/sagemaker-user",
ValidateFunc: validation.All(
validation.StringLenBetween(1, 1024),
validation.StringMatch(regexache.MustCompile(`^\/.*`), "Must start with `/`."),
),
},
},
},
Elem: fileSystemConfigSchema(),
},
"kernel_spec": {
Type: schema.TypeList,
Expand Down Expand Up @@ -309,6 +235,34 @@ func resourceAppImageConfigRead(ctx context.Context, d *schema.ResourceData, met
return diags
}

func fileSystemConfigSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"default_gid": {
Type: schema.TypeInt,
Optional: true,
Default: 100,
ValidateFunc: validation.IntInSlice([]int{100}), // Remove 0 as it's not supported
},
"default_uid": {
Type: schema.TypeInt,
Optional: true,
Default: 1000,
ValidateFunc: validation.IntInSlice([]int{1000}), // Remove 0 as it's not supported
},
"mount_path": {
Type: schema.TypeString,
Optional: true,
Default: "/home/sagemaker-user",
ValidateFunc: validation.All(
validation.StringLenBetween(1, 1024),
validation.StringMatch(regexache.MustCompile(`^\/.*`), "Must start with `/`."),
),
},
},
}
}

func resourceAppImageConfigUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).SageMakerClient(ctx)
Expand Down
147 changes: 100 additions & 47 deletions internal/service/sagemaker/app_image_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package sagemaker_test
import (
"context"
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go-v2/service/sagemaker"
Expand Down Expand Up @@ -95,53 +96,6 @@ func TestAccSageMakerAppImageConfig_KernelGatewayImage_kernelSpecs(t *testing.T)
})
}

func TestAccSageMakerAppImageConfig_KernelGatewayImage_fileSystem(t *testing.T) {
ctx := acctest.Context(t)
var config sagemaker.DescribeAppImageConfigOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_sagemaker_app_image_config.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.SageMakerServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAppImageConfigDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccAppImageConfigConfig_kernelGatewayFileSystem1(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppImageConfigExists(ctx, resourceName, &config),
resource.TestCheckResourceAttr(resourceName, "app_image_config_name", rName),
resource.TestCheckResourceAttr(resourceName, "kernel_gateway_image_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "kernel_gateway_image_config.0.kernel_spec.#", "1"),
resource.TestCheckResourceAttr(resourceName, "kernel_gateway_image_config.0.file_system_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "kernel_gateway_image_config.0.file_system_config.0.default_gid", "100"),
resource.TestCheckResourceAttr(resourceName, "kernel_gateway_image_config.0.file_system_config.0.default_uid", "1000"),
resource.TestCheckResourceAttr(resourceName, "kernel_gateway_image_config.0.file_system_config.0.mount_path", "/home/sagemaker-user"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAppImageConfigConfig_kernelGatewayFileSystem2(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppImageConfigExists(ctx, resourceName, &config),
resource.TestCheckResourceAttr(resourceName, "app_image_config_name", rName),
resource.TestCheckResourceAttr(resourceName, "kernel_gateway_image_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "kernel_gateway_image_config.0.kernel_spec.#", "1"),
resource.TestCheckResourceAttr(resourceName, "kernel_gateway_image_config.0.file_system_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "kernel_gateway_image_config.0.file_system_config.0.default_gid", "0"),
resource.TestCheckResourceAttr(resourceName, "kernel_gateway_image_config.0.file_system_config.0.default_uid", "0"),
resource.TestCheckResourceAttr(resourceName, "kernel_gateway_image_config.0.file_system_config.0.mount_path", "/test"),
),
},
},
})
}

func TestAccSageMakerAppImageConfig_CodeEditor(t *testing.T) {
ctx := acctest.Context(t)
var config sagemaker.DescribeAppImageConfigOutput
Expand Down Expand Up @@ -242,6 +196,105 @@ func TestAccSageMakerAppImageConfig_JupyterLab(t *testing.T) {
})
}

func TestAccSageMakerAppImageConfig_KernelGatewayImage_fileSystemValidation(t *testing.T) {
ctx := acctest.Context(t)
var config sagemaker.DescribeAppImageConfigOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_sagemaker_app_image_config.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.SageMakerServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAppImageConfigDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccAppImageConfigConfig_kernelGatewayFileSystemValid(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppImageConfigExists(ctx, resourceName, &config),
resource.TestCheckResourceAttr(resourceName, "kernel_gateway_image_config.0.file_system_config.0.default_gid", "100"),
resource.TestCheckResourceAttr(resourceName, "kernel_gateway_image_config.0.file_system_config.0.default_uid", "1000"),
),
},
{
Config: testAccAppImageConfigConfig_kernelGatewayFileSystemInvalidGID(rName),
ExpectError: regexp.MustCompile(`expected kernel_gateway_image_config\.0\.file_system_config\.0\.default_gid to be one of \[100\], got 0`),
PlanOnly: true, // Only run plan phase, don't try to apply
},
{
Config: testAccAppImageConfigConfig_kernelGatewayFileSystemInvalidUID(rName),
ExpectError: regexp.MustCompile(`expected kernel_gateway_image_config\.0\.file_system_config\.0\.default_uid to be one of \[1000\], got 0`),
PlanOnly: true, // Only run plan phase, don't try to apply
},
// Add a final step with valid configuration to ensure clean destroy
{
Config: testAccAppImageConfigConfig_kernelGatewayFileSystemValid(rName),
},
},
})
}

// Test configurations remain the same
func testAccAppImageConfigConfig_kernelGatewayFileSystemValid(rName string) string {
return fmt.Sprintf(`
resource "aws_sagemaker_app_image_config" "test" {
app_image_config_name = %[1]q

kernel_gateway_image_config {
kernel_spec {
name = %[1]q
}

file_system_config {
default_gid = 100
default_uid = 1000
mount_path = "/home/sagemaker-user"
}
}
}
`, rName)
}

func testAccAppImageConfigConfig_kernelGatewayFileSystemInvalidGID(rName string) string {
return fmt.Sprintf(`
resource "aws_sagemaker_app_image_config" "test" {
app_image_config_name = %[1]q

kernel_gateway_image_config {
kernel_spec {
name = %[1]q
}

file_system_config {
default_gid = 0
default_uid = 1000
mount_path = "/home/sagemaker-user"
}
}
}
`, rName)
}

func testAccAppImageConfigConfig_kernelGatewayFileSystemInvalidUID(rName string) string {
return fmt.Sprintf(`
resource "aws_sagemaker_app_image_config" "test" {
app_image_config_name = %[1]q

kernel_gateway_image_config {
kernel_spec {
name = %[1]q
}

file_system_config {
default_gid = 100
default_uid = 0
mount_path = "/home/sagemaker-user"
}
}
}
`, rName)
}

func TestAccSageMakerAppImageConfig_tags(t *testing.T) {
ctx := acctest.Context(t)
var app sagemaker.DescribeAppImageConfigOutput
Expand Down
Loading