Skip to content

Commit a7bb612

Browse files
authored
Merge pull request #42141 from DrFaust92/sagemaker-lifecycle-tags
r/sagemaker_notebook_lifecycle_config - support tags
2 parents 0d63db1 + e2dc273 commit a7bb612

File tree

5 files changed

+102
-17
lines changed

5 files changed

+102
-17
lines changed

.changelog/42141.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/aws_sagemaker_notebook_lifecycle_configuration: Add `tags` argument and `tags_all` attribute
3+
```

internal/service/sagemaker/notebook_instance_lifecycle_configuration.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ import (
1818
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1919
"github.com/hashicorp/terraform-provider-aws/internal/conns"
2020
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
21+
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
2122
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
2223
"github.com/hashicorp/terraform-provider-aws/names"
2324
)
2425

2526
// @SDKResource("aws_sagemaker_notebook_instance_lifecycle_configuration", name="Notebook Instance Lifecycle Configuration")
27+
// @Tags(identifierAttribute="arn")
2628
func resourceNotebookInstanceLifeCycleConfiguration() *schema.Resource {
2729
return &schema.Resource{
2830
CreateWithoutTimeout: resourceNotebookInstanceLifeCycleConfigurationCreate,
@@ -38,25 +40,24 @@ func resourceNotebookInstanceLifeCycleConfiguration() *schema.Resource {
3840
Type: schema.TypeString,
3941
Computed: true,
4042
},
41-
4243
names.AttrName: {
4344
Type: schema.TypeString,
4445
Optional: true,
4546
ForceNew: true,
4647
ValidateFunc: validName,
4748
},
48-
4949
"on_create": {
5050
Type: schema.TypeString,
5151
Optional: true,
5252
ValidateFunc: validation.StringLenBetween(0, 16384),
5353
},
54-
5554
"on_start": {
5655
Type: schema.TypeString,
5756
Optional: true,
5857
ValidateFunc: validation.StringLenBetween(0, 16384),
5958
},
59+
names.AttrTags: tftags.TagsSchema(),
60+
names.AttrTagsAll: tftags.TagsSchemaComputed(),
6061
},
6162
}
6263
}
@@ -74,6 +75,7 @@ func resourceNotebookInstanceLifeCycleConfigurationCreate(ctx context.Context, d
7475

7576
createOpts := &sagemaker.CreateNotebookInstanceLifecycleConfigInput{
7677
NotebookInstanceLifecycleConfigName: aws.String(name),
78+
Tags: getTagsIn(ctx),
7779
}
7880

7981
// on_create is technically a list of NotebookInstanceLifecycleHook elements, but the list has to be length 1
@@ -141,24 +143,27 @@ func resourceNotebookInstanceLifeCycleConfigurationUpdate(ctx context.Context, d
141143
var diags diag.Diagnostics
142144
conn := meta.(*conns.AWSClient).SageMakerClient(ctx)
143145

144-
updateOpts := &sagemaker.UpdateNotebookInstanceLifecycleConfigInput{
145-
NotebookInstanceLifecycleConfigName: aws.String(d.Get(names.AttrName).(string)),
146-
}
146+
if d.HasChangesExcept(names.AttrTags, names.AttrTagsAll) {
147+
updateOpts := &sagemaker.UpdateNotebookInstanceLifecycleConfigInput{
148+
NotebookInstanceLifecycleConfigName: aws.String(d.Get(names.AttrName).(string)),
149+
}
147150

148-
if v, ok := d.GetOk("on_create"); ok {
149-
onCreateHook := awstypes.NotebookInstanceLifecycleHook{Content: aws.String(v.(string))}
150-
updateOpts.OnCreate = []awstypes.NotebookInstanceLifecycleHook{onCreateHook}
151-
}
151+
if v, ok := d.GetOk("on_create"); ok {
152+
onCreateHook := awstypes.NotebookInstanceLifecycleHook{Content: aws.String(v.(string))}
153+
updateOpts.OnCreate = []awstypes.NotebookInstanceLifecycleHook{onCreateHook}
154+
}
152155

153-
if v, ok := d.GetOk("on_start"); ok {
154-
onStartHook := awstypes.NotebookInstanceLifecycleHook{Content: aws.String(v.(string))}
155-
updateOpts.OnStart = []awstypes.NotebookInstanceLifecycleHook{onStartHook}
156-
}
156+
if v, ok := d.GetOk("on_start"); ok {
157+
onStartHook := awstypes.NotebookInstanceLifecycleHook{Content: aws.String(v.(string))}
158+
updateOpts.OnStart = []awstypes.NotebookInstanceLifecycleHook{onStartHook}
159+
}
157160

158-
_, err := conn.UpdateNotebookInstanceLifecycleConfig(ctx, updateOpts)
159-
if err != nil {
160-
return sdkdiag.AppendErrorf(diags, "updating SageMaker AI Notebook Instance Lifecycle Configuration: %s", err)
161+
_, err := conn.UpdateNotebookInstanceLifecycleConfig(ctx, updateOpts)
162+
if err != nil {
163+
return sdkdiag.AppendErrorf(diags, "updating SageMaker AI Notebook Instance Lifecycle Configuration: %s", err)
164+
}
161165
}
166+
162167
return append(diags, resourceNotebookInstanceLifeCycleConfigurationRead(ctx, d, meta)...)
163168
}
164169

internal/service/sagemaker/notebook_instance_lifecycle_configuration_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestAccSageMakerNotebookInstanceLifecycleConfiguration_basic(t *testing.T)
4141
resource.TestCheckNoResourceAttr(resourceName, "on_create"),
4242
resource.TestCheckNoResourceAttr(resourceName, "on_start"),
4343
acctest.CheckResourceAttrRegionalARN(ctx, resourceName, names.AttrARN, "sagemaker", fmt.Sprintf("notebook-instance-lifecycle-config/%s", rName)),
44+
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, "0"),
4445
),
4546
},
4647
{
@@ -90,6 +91,52 @@ func TestAccSageMakerNotebookInstanceLifecycleConfiguration_update(t *testing.T)
9091
})
9192
}
9293

94+
func TestAccSageMakerNotebookInstanceLifecycleConfiguration_tags(t *testing.T) {
95+
ctx := acctest.Context(t)
96+
var lifecycleConfig sagemaker.DescribeNotebookInstanceLifecycleConfigOutput
97+
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
98+
resourceName := "aws_sagemaker_notebook_instance_lifecycle_configuration.test"
99+
100+
resource.ParallelTest(t, resource.TestCase{
101+
PreCheck: func() { acctest.PreCheck(ctx, t) },
102+
ErrorCheck: acctest.ErrorCheck(t, names.SageMakerServiceID),
103+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
104+
CheckDestroy: testAccCheckNotebookInstanceLifecycleConfigurationDestroy(ctx),
105+
Steps: []resource.TestStep{
106+
{
107+
Config: testAccNotebookInstanceLifecycleConfigurationConfig_tags1(rName, acctest.CtKey1, acctest.CtValue1),
108+
Check: resource.ComposeTestCheckFunc(
109+
testAccCheckNotebookInstanceLifecycleConfigurationExists(ctx, resourceName, &lifecycleConfig),
110+
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, "1"),
111+
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsKey1, acctest.CtValue1),
112+
),
113+
},
114+
{
115+
ResourceName: resourceName,
116+
ImportState: true,
117+
ImportStateVerify: true,
118+
},
119+
{
120+
Config: testAccNotebookInstanceLifecycleConfigurationConfig_tags2(rName, acctest.CtKey1, acctest.CtValue1Updated, acctest.CtKey2, acctest.CtValue2),
121+
Check: resource.ComposeTestCheckFunc(
122+
testAccCheckNotebookInstanceLifecycleConfigurationExists(ctx, resourceName, &lifecycleConfig),
123+
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, "2"),
124+
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsKey1, acctest.CtValue1Updated),
125+
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsKey2, acctest.CtValue2),
126+
),
127+
},
128+
{
129+
Config: testAccNotebookInstanceLifecycleConfigurationConfig_tags1(rName, acctest.CtKey2, acctest.CtValue2),
130+
Check: resource.ComposeTestCheckFunc(
131+
testAccCheckNotebookInstanceLifecycleConfigurationExists(ctx, resourceName, &lifecycleConfig),
132+
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, "1"),
133+
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsKey2, acctest.CtValue2),
134+
),
135+
},
136+
},
137+
})
138+
}
139+
93140
func TestAccSageMakerNotebookInstanceLifecycleConfiguration_disappears(t *testing.T) {
94141
ctx := acctest.Context(t)
95142
var lifecycleConfig sagemaker.DescribeNotebookInstanceLifecycleConfigOutput
@@ -181,3 +228,28 @@ resource "aws_sagemaker_notebook_instance_lifecycle_configuration" "test" {
181228
}
182229
`, rName)
183230
}
231+
232+
func testAccNotebookInstanceLifecycleConfigurationConfig_tags1(rName, tagKey1, tagValue1 string) string {
233+
return fmt.Sprintf(`
234+
resource "aws_sagemaker_notebook_instance_lifecycle_configuration" "test" {
235+
name = %[1]q
236+
237+
tags = {
238+
%[2]q = %[3]q
239+
}
240+
}
241+
`, rName, tagKey1, tagValue1)
242+
}
243+
244+
func testAccNotebookInstanceLifecycleConfigurationConfig_tags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
245+
return fmt.Sprintf(`
246+
resource "aws_sagemaker_notebook_instance_lifecycle_configuration" "test" {
247+
name = %[1]q
248+
249+
tags = {
250+
%[2]q = %[3]q
251+
%[4]q = %[5]q
252+
}
253+
}
254+
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
255+
}

internal/service/sagemaker/service_package_gen.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/docs/r/sagemaker_notebook_instance_lifecycle_configuration.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ This resource supports the following arguments:
2929
* `name` - (Optional) The name of the lifecycle configuration (must be unique). If omitted, Terraform will assign a random, unique name.
3030
* `on_create` - (Optional) A shell script (base64-encoded) that runs only once when the SageMaker AI Notebook Instance is created.
3131
* `on_start` - (Optional) A shell script (base64-encoded) that runs every time the SageMaker AI Notebook Instance is started including the time it's created.
32+
* `tags` - (Optional) A mapping of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.
3233

3334
## Attribute Reference
3435

3536
This resource exports the following attributes in addition to the arguments above:
3637

3738
* `arn` - The Amazon Resource Name (ARN) assigned by AWS to this lifecycle configuration.
39+
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block).
3840

3941
## Import
4042

0 commit comments

Comments
 (0)