Skip to content

Commit

Permalink
Merge pull request #21037 from DrFaust92/r/sagemaker_app_image_config…
Browse files Browse the repository at this point in the history
…_tags

r/sagemaker_app_image_config - tagging support
  • Loading branch information
ewbankkit authored Sep 27, 2021
2 parents 47bf15a + efdf9c2 commit 98c17ba
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/21037.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_sagemaker_app_image_config: Add tagging support.
```
37 changes: 37 additions & 0 deletions aws/resource_aws_sagemaker_app_image_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go/service/sagemaker"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/sagemaker/finder"
)

Expand Down Expand Up @@ -94,18 +95,27 @@ func resourceAwsSagemakerAppImageConfig() *schema.Resource {
},
},
},
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
},
CustomizeDiff: SetTagsDiff,
}
}

func resourceAwsSagemakerAppImageConfigCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sagemakerconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{})))

name := d.Get("app_image_config_name").(string)
input := &sagemaker.CreateAppImageConfigInput{
AppImageConfigName: aws.String(name),
}

if len(tags) > 0 {
input.Tags = tags.IgnoreAws().SagemakerTags()
}

if v, ok := d.GetOk("kernel_gateway_image_config"); ok && len(v.([]interface{})) > 0 {
input.KernelGatewayImageConfig = expandSagemakerAppImageConfigKernelGatewayImageConfig(v.([]interface{}))
}
Expand All @@ -122,6 +132,8 @@ func resourceAwsSagemakerAppImageConfigCreate(d *schema.ResourceData, meta inter

func resourceAwsSagemakerAppImageConfigRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sagemakerconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

image, err := finder.AppImageConfigByName(conn, d.Id())
if err != nil {
Expand All @@ -142,12 +154,37 @@ func resourceAwsSagemakerAppImageConfigRead(d *schema.ResourceData, meta interfa
return fmt.Errorf("error setting kernel_gateway_image_config: %w", err)
}

tags, err := keyvaluetags.SagemakerListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for SageMaker App Image Config (%s): %w", d.Id(), err)
}

tags = tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return fmt.Errorf("error setting tags_all: %w", err)
}

return nil
}

func resourceAwsSagemakerAppImageConfigUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sagemakerconn

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := keyvaluetags.SagemakerUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating SageMaker App Image Config (%s) tags: %w", d.Id(), err)
}
}

if d.HasChange("kernel_gateway_image_config") {

input := &sagemaker.UpdateAppImageConfigInput{
Expand Down
70 changes: 70 additions & 0 deletions aws/resource_aws_sagemaker_app_image_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,51 @@ func TestAccAWSSagemakerAppImageConfig_kernelGatewayImageConfig_fileSystemConfig
})
}

func TestAccAWSSagemakerAppImageConfig_tags(t *testing.T) {
var app sagemaker.DescribeAppImageConfigOutput
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_sagemaker_app_image_config.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, sagemaker.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSagemakerAppDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSagemakerAppConfigImageConfigTags1(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSagemakerAppImageConfigExists(resourceName, &app),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSSagemakerAppConfigImageConfigTags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSagemakerAppImageConfigExists(resourceName, &app),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccAWSSagemakerAppConfigImageConfigTags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSagemakerAppImageConfigExists(resourceName, &app),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
})
}

func TestAccAWSSagemakerAppImageConfig_disappears(t *testing.T) {
var config sagemaker.DescribeAppImageConfigOutput
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down Expand Up @@ -334,3 +379,28 @@ resource "aws_sagemaker_app_image_config" "test" {
}
`, rName)
}

func testAccAWSSagemakerAppConfigImageConfigTags1(rName, tagKey1, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_sagemaker_app_image_config" "test" {
app_image_config_name = %[1]q
tags = {
%[2]q = %[3]q
}
}
`, rName, tagKey1, tagValue1)
}

func testAccAWSSagemakerAppConfigImageConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_sagemaker_app_image_config" "test" {
app_image_config_name = %[1]q
tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}
2 changes: 2 additions & 0 deletions website/docs/r/sagemaker_app_image_config.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ The following arguments are supported:

* `app_image_config_name` - (Required) The name of the App Image Config.
* `kernel_gateway_image_config` - (Optional) The configuration for the file system and kernels in a SageMaker image running as a KernelGateway app. See [Kernel Gateway Image Config](#kernel-gateway-image-config) details below.
* `tags` - (Optional) A map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

### Kernel Gateway Image Config

Expand All @@ -73,6 +74,7 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The name of the App Image Config.
* `arn` - The Amazon Resource Name (ARN) assigned by AWS to this App Image Config.
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block).

## Import

Expand Down

0 comments on commit 98c17ba

Please sign in to comment.