Skip to content

Commit febf463

Browse files
committed
aws_sagemaker_image_version - add support for alias fixes #35931
1 parent 5f6ccfc commit febf463

File tree

5 files changed

+148
-1
lines changed

5 files changed

+148
-1
lines changed

internal/service/sagemaker/image_version.go

+85
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ func resourceImageVersion() *schema.Resource {
4343
Type: schema.TypeString,
4444
Computed: true,
4545
},
46+
"aliases": {
47+
Type: schema.TypeSet,
48+
Optional: true,
49+
Elem: &schema.Schema{
50+
Type: schema.TypeString,
51+
},
52+
},
4653
"base_image": {
4754
Type: schema.TypeString,
4855
Required: true,
@@ -142,6 +149,14 @@ func resourceImageVersionCreate(ctx context.Context, d *schema.ResourceData, met
142149
input.ProgrammingLang = aws.String(v.(string))
143150
}
144151

152+
if v, ok := d.GetOk("aliases"); ok {
153+
aliases := v.(*schema.Set).List()
154+
input.Aliases = make([]string, len(aliases))
155+
for i, alias := range aliases {
156+
input.Aliases[i] = alias.(string)
157+
}
158+
}
159+
145160
_, err := conn.CreateImageVersion(ctx, input)
146161
if err != nil {
147162
return sdkdiag.AppendErrorf(diags, "creating SageMaker AI Image Version %s: %s", name, err)
@@ -220,6 +235,30 @@ func resourceImageVersionRead(ctx context.Context, d *schema.ResourceData, meta
220235
d.Set("ml_framework", image.MLFramework)
221236
d.Set("programming_lang", image.ProgrammingLang)
222237

238+
// The AWS SDK doesn't have an Aliases field in DescribeImageVersionOutput
239+
// We need to fetch aliases separately using ListAliases API
240+
idParts := strings.Split(id, ":")
241+
imageName := idParts[0]
242+
versionStr := idParts[1]
243+
versionNum, err := strconv.Atoi(versionStr)
244+
if err != nil {
245+
return sdkdiag.AppendErrorf(diags, "invalid version number in resource ID: %s", d.Id())
246+
}
247+
248+
aliasesInput := &sagemaker.ListAliasesInput{
249+
ImageName: aws.String(imageName),
250+
Version: aws.Int32(int32(versionNum)),
251+
}
252+
253+
aliasesOutput, err := conn.ListAliases(ctx, aliasesInput)
254+
if err != nil {
255+
return sdkdiag.AppendErrorf(diags, "listing aliases for SageMaker AI Image Version (%s): %s", d.Id(), err)
256+
}
257+
258+
if err := d.Set("aliases", aliasesOutput.SageMakerImageVersionAliases); err != nil {
259+
return sdkdiag.AppendErrorf(diags, "setting aliases: %s", err)
260+
}
261+
223262
return diags
224263
}
225264

@@ -274,6 +313,52 @@ func resourceImageVersionUpdate(ctx context.Context, d *schema.ResourceData, met
274313
input.ProgrammingLang = aws.String(d.Get("programming_lang").(string))
275314
}
276315

316+
if d.HasChange("aliases") {
317+
// For UpdateImageVersion, we need to use AliasesToAdd and AliasesToDelete
318+
// instead of Aliases directly
319+
oldAliasesSet, newAliasesSet := d.GetChange("aliases")
320+
oldAliases := oldAliasesSet.(*schema.Set).List()
321+
newAliases := newAliasesSet.(*schema.Set).List()
322+
323+
// Find aliases to add (in new but not in old)
324+
var aliasesToAdd []string
325+
for _, newAlias := range newAliases {
326+
found := false
327+
for _, oldAlias := range oldAliases {
328+
if newAlias.(string) == oldAlias.(string) {
329+
found = true
330+
break
331+
}
332+
}
333+
if !found {
334+
aliasesToAdd = append(aliasesToAdd, newAlias.(string))
335+
}
336+
}
337+
338+
// Find aliases to delete (in old but not in new)
339+
var aliasesToDelete []string
340+
for _, oldAlias := range oldAliases {
341+
found := false
342+
for _, newAlias := range newAliases {
343+
if oldAlias.(string) == newAlias.(string) {
344+
found = true
345+
break
346+
}
347+
}
348+
if !found {
349+
aliasesToDelete = append(aliasesToDelete, oldAlias.(string))
350+
}
351+
}
352+
353+
if len(aliasesToAdd) > 0 {
354+
input.AliasesToAdd = aliasesToAdd
355+
}
356+
357+
if len(aliasesToDelete) > 0 {
358+
input.AliasesToDelete = aliasesToDelete
359+
}
360+
}
361+
277362
if _, err := conn.UpdateImageVersion(ctx, input); err != nil {
278363
return sdkdiag.AppendErrorf(diags, "updating SageMaker AI Image Version (%s): %s", d.Id(), err)
279364
}

internal/service/sagemaker/image_version_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ func TestAccSageMakerImageVersion_full(t *testing.T) {
9494
resource.TestCheckResourceAttr(resourceName, "job_type", "TRAINING"),
9595
resource.TestCheckResourceAttr(resourceName, "ml_framework", "TensorFlow 1.1"),
9696
resource.TestCheckResourceAttr(resourceName, "programming_lang", "Python 3.8"),
97+
resource.TestCheckResourceAttr(resourceName, "aliases.#", "2"),
98+
resource.TestCheckTypeSetElemAttr(resourceName, "aliases.*", "latest"),
99+
resource.TestCheckTypeSetElemAttr(resourceName, "aliases.*", "stable"),
97100
),
98101
},
99102
{
@@ -118,6 +121,9 @@ func TestAccSageMakerImageVersion_full(t *testing.T) {
118121
resource.TestCheckResourceAttr(resourceName, "job_type", "TRAINING"),
119122
resource.TestCheckResourceAttr(resourceName, "ml_framework", "TensorFlow 1.1"),
120123
resource.TestCheckResourceAttr(resourceName, "programming_lang", "Python 3.8"),
124+
resource.TestCheckResourceAttr(resourceName, "aliases.#", "2"),
125+
resource.TestCheckTypeSetElemAttr(resourceName, "aliases.*", "latest"),
126+
resource.TestCheckTypeSetElemAttr(resourceName, "aliases.*", "stable"),
121127
),
122128
},
123129
},
@@ -285,6 +291,7 @@ resource "aws_sagemaker_image_version" "test" {
285291
vendor_guidance = "STABLE"
286292
ml_framework = "TensorFlow 1.1"
287293
programming_lang = "Python 3.8"
294+
aliases = ["latest", "stable"]
288295
}
289296
`, baseImage, notes)
290297
}

website/docs/cdktf/python/r/sagemaker_image_version.html.markdown

+22
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,34 @@ class MyConvertedCode(TerraformStack):
3434
)
3535
```
3636

37+
### With aliases
38+
39+
```python
40+
# DO NOT EDIT. Code generated by 'cdktf convert' - Please report bugs at https://cdk.tf/bug
41+
from constructs import Construct
42+
from cdktf import Token, TerraformStack
43+
#
44+
# Provider bindings are generated by running `cdktf get`.
45+
# See https://cdk.tf/provider-generation for more details.
46+
#
47+
from imports.aws.sagemaker_image_version import SagemakerImageVersion
48+
class MyConvertedCode(TerraformStack):
49+
def __init__(self, scope, name):
50+
super().__init__(scope, name)
51+
SagemakerImageVersion(self, "test",
52+
aliases=["latest", "stable"],
53+
base_image="012345678912.dkr.ecr.us-west-2.amazonaws.com/image:latest",
54+
image_name=Token.as_string(aws_sagemaker_image_test.id)
55+
)
56+
```
57+
3758
## Argument Reference
3859

3960
This resource supports the following arguments:
4061

4162
* `image_name` - (Required) The name of the image. Must be unique to your account.
4263
* `base_image` - (Required) The registry path of the container image on which this image version is based.
64+
* `aliases` - (Optional) A list of aliases for the image version.
4365
* `horovod` - (Optional) Indicates Horovod compatibility.
4466
* `job_type` - (Optional) Indicates SageMaker AI job type compatibility. Valid values are: `TRAINING`, `INFERENCE`, and `NOTEBOOK_KERNEL`.
4567
* `ml_framework` - (Optional) The machine learning framework vended in the image version.

website/docs/cdktf/typescript/r/sagemaker_image_version.html.markdown

+23-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,29 @@ class MyConvertedCode extends TerraformStack {
3434
});
3535
}
3636
}
37+
```
38+
39+
### With aliases
3740

41+
```typescript
42+
// DO NOT EDIT. Code generated by 'cdktf convert' - Please report bugs at https://cdk.tf/bug
43+
import { Construct } from "constructs";
44+
import { Token, TerraformStack } from "cdktf";
45+
/*
46+
* Provider bindings are generated by running `cdktf get`.
47+
* See https://cdk.tf/provider-generation for more details.
48+
*/
49+
import { SagemakerImageVersion } from "./.gen/providers/aws/sagemaker-image-version";
50+
class MyConvertedCode extends TerraformStack {
51+
constructor(scope: Construct, name: string) {
52+
super(scope, name);
53+
new SagemakerImageVersion(this, "test", {
54+
aliases: ["latest", "stable"],
55+
baseImage: "012345678912.dkr.ecr.us-west-2.amazonaws.com/image:latest",
56+
imageName: Token.asString(awsSagemakerImageTest.id),
57+
});
58+
}
59+
}
3860
```
3961

4062
## Argument Reference
@@ -43,6 +65,7 @@ This resource supports the following arguments:
4365

4466
* `imageName` - (Required) The name of the image. Must be unique to your account.
4567
* `baseImage` - (Required) The registry path of the container image on which this image version is based.
68+
* `aliases` - (Optional) A list of aliases for the image version.
4669
* `horovod` - (Optional) Indicates Horovod compatibility.
4770
* `jobType` - (Optional) Indicates SageMaker AI job type compatibility. Valid values are: `TRAINING`, `INFERENCE`, and `NOTEBOOK_KERNEL`.
4871
* `ml_framework` - (Optional) The machine learning framework vended in the image version.
@@ -83,7 +106,6 @@ class MyConvertedCode extends TerraformStack {
83106
);
84107
}
85108
}
86-
87109
```
88110

89111
Using `terraform import`, import SageMaker AI Image Versions using the `name:version` format. For example:

website/docs/r/sagemaker_image_version.html.markdown

+11
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,23 @@ resource "aws_sagemaker_image_version" "test" {
2121
}
2222
```
2323

24+
### With aliases
25+
26+
```terraform
27+
resource "aws_sagemaker_image_version" "test" {
28+
image_name = aws_sagemaker_image.test.id
29+
base_image = "012345678912.dkr.ecr.us-west-2.amazonaws.com/image:latest"
30+
aliases = ["latest", "stable"]
31+
}
32+
```
33+
2434
## Argument Reference
2535

2636
This resource supports the following arguments:
2737

2838
* `image_name` - (Required) The name of the image. Must be unique to your account.
2939
* `base_image` - (Required) The registry path of the container image on which this image version is based.
40+
* `aliases` - (Optional) A list of aliases for the image version.
3041
* `horovod` - (Optional) Indicates Horovod compatibility.
3142
* `job_type` - (Optional) Indicates SageMaker AI job type compatibility. Valid values are: `TRAINING`, `INFERENCE`, and `NOTEBOOK_KERNEL`.
3243
* `ml_framework` - (Optional) The machine learning framework vended in the image version.

0 commit comments

Comments
 (0)