Skip to content
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

r/sagemaker_human_task_ui - new resource #20570

Merged
merged 4 commits into from
Aug 17, 2021
Merged
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
3 changes: 3 additions & 0 deletions .changelog/20570.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_sagemaker_human_task_ui
```
28 changes: 28 additions & 0 deletions aws/internal/service/sagemaker/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,31 @@ func WorkteamByName(conn *sagemaker.SageMaker, name string) (*sagemaker.Workteam

return output.Workteam, nil
}

func HumanTaskUiByName(conn *sagemaker.SageMaker, name string) (*sagemaker.DescribeHumanTaskUiOutput, error) {
input := &sagemaker.DescribeHumanTaskUiInput{
HumanTaskUiName: aws.String(name),
}

output, err := conn.DescribeHumanTaskUi(input)

if tfawserr.ErrCodeEquals(err, sagemaker.ErrCodeResourceNotFound) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil {
return nil, &resource.NotFoundError{
Message: "Empty result",
LastRequest: input,
}
}

return output, nil
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,7 @@ func Provider() *schema.Provider {
"aws_sagemaker_feature_group": resourceAwsSagemakerFeatureGroup(),
"aws_sagemaker_image": resourceAwsSagemakerImage(),
"aws_sagemaker_image_version": resourceAwsSagemakerImageVersion(),
"aws_sagemaker_human_task_ui": resourceAwsSagemakerHumanTaskUi(),
"aws_sagemaker_model": resourceAwsSagemakerModel(),
"aws_sagemaker_model_package_group": resourceAwsSagemakerModelPackageGroup(),
"aws_sagemaker_notebook_instance_lifecycle_configuration": resourceAwsSagemakerNotebookInstanceLifeCycleConfiguration(),
Expand Down
204 changes: 204 additions & 0 deletions aws/resource_aws_sagemaker_human_task_ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package aws

import (
"fmt"
"log"
"regexp"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sagemaker"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"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"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func resourceAwsSagemakerHumanTaskUi() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSagemakerHumanTaskUiCreate,
Read: resourceAwsSagemakerHumanTaskUiRead,
Update: resourceAwsSagemakerHumanTaskUiUpdate,
Delete: resourceAwsSagemakerHumanTaskUiDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"ui_template": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"content": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 128000),
},
"content_sha256": {
Type: schema.TypeString,
Computed: true,
},
"url": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"human_task_ui_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 63),
validation.StringMatch(regexp.MustCompile(`^[a-z0-9](-*[a-z0-9])*$`), "Valid characters are a-z, A-Z, 0-9, and - (hyphen)."),
),
},
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
},
CustomizeDiff: SetTagsDiff,
}
}

func resourceAwsSagemakerHumanTaskUiCreate(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("human_task_ui_name").(string)
input := &sagemaker.CreateHumanTaskUiInput{
HumanTaskUiName: aws.String(name),
UiTemplate: expandSagemakerHumanTaskUiUiTemplate(d.Get("ui_template").([]interface{})),
}

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

log.Printf("[DEBUG] Creating SageMaker HumanTaskUi: %s", input)
_, err := conn.CreateHumanTaskUi(input)

if err != nil {
return fmt.Errorf("error creating SageMaker HumanTaskUi (%s): %w", name, err)
}

d.SetId(name)

return resourceAwsSagemakerHumanTaskUiRead(d, meta)
}

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

humanTaskUi, err := finder.HumanTaskUiByName(conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] SageMaker HumanTaskUi (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading SageMaker HumanTaskUi (%s): %w", d.Id(), err)
}

arn := aws.StringValue(humanTaskUi.HumanTaskUiArn)
d.Set("arn", arn)
d.Set("human_task_ui_name", humanTaskUi.HumanTaskUiName)

if err := d.Set("ui_template", flattenSagemakerHumanTaskUiUiTemplate(humanTaskUi.UiTemplate, d.Get("ui_template.0.content").(string))); err != nil {
return fmt.Errorf("error setting ui_template: %w", err)
}

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

if err != nil {
return fmt.Errorf("error listing tags for SageMaker HumanTaskUi (%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 resourceAwsSagemakerHumanTaskUiUpdate(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 HumanTaskUi (%s) tags: %w", d.Id(), err)
}
}

return resourceAwsSagemakerHumanTaskUiRead(d, meta)
}

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

log.Printf("[DEBUG] Deleting SageMaker HumanTaskUi: %s", d.Id())
_, err := conn.DeleteHumanTaskUi(&sagemaker.DeleteHumanTaskUiInput{
HumanTaskUiName: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, sagemaker.ErrCodeResourceNotFound) {
return nil
}

if err != nil {
return fmt.Errorf("error deleting SageMaker HumanTaskUi (%s): %w", d.Id(), err)
}

return nil
}

func expandSagemakerHumanTaskUiUiTemplate(l []interface{}) *sagemaker.UiTemplate {
if len(l) == 0 || l[0] == nil {
return nil
}

m := l[0].(map[string]interface{})

config := &sagemaker.UiTemplate{
Content: aws.String(m["content"].(string)),
}

return config
}

func flattenSagemakerHumanTaskUiUiTemplate(config *sagemaker.UiTemplateInfo, content string) []map[string]interface{} {
if config == nil {
return []map[string]interface{}{}
}

m := map[string]interface{}{
"content_sha256": aws.StringValue(config.ContentSha256),
"url": aws.StringValue(config.Url),
"content": content,
}

return []map[string]interface{}{m}
}
Loading