Skip to content

Commit

Permalink
Merge pull request #22140 from DrFaust92/r/emr_studio_session_mapping
Browse files Browse the repository at this point in the history
r/emr_studio_session_mapping - new resource
  • Loading branch information
ewbankkit authored Dec 9, 2021
2 parents 2d1fffd + b1d716d commit 9b6edeb
Show file tree
Hide file tree
Showing 7 changed files with 547 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/22140.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_emr_studio_session_mapping
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,7 @@ func Provider() *schema.Provider {
"aws_emr_managed_scaling_policy": emr.ResourceManagedScalingPolicy(),
"aws_emr_security_configuration": emr.ResourceSecurityConfiguration(),
"aws_emr_studio": emr.ResourceStudio(),
"aws_emr_studio_session_mapping": emr.ResourceStudioSessionMapping(),

"aws_kinesis_firehose_delivery_stream": firehose.ResourceDeliveryStream(),

Expand Down
33 changes: 33 additions & 0 deletions internal/service/emr/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,36 @@ func FindStudioByID(conn *emr.EMR, id string) (*emr.Studio, error) {

return output.Studio, nil
}

func FindStudioSessionMappingByID(conn *emr.EMR, id string) (*emr.SessionMappingDetail, error) {
studioId, identityType, identityId, err := readStudioSessionMapping(id)
if err != nil {
return nil, err
}

input := &emr.GetStudioSessionMappingInput{
StudioId: aws.String(studioId),
IdentityType: aws.String(identityType),
IdentityId: aws.String(identityId),
}

output, err := conn.GetStudioSessionMapping(input)

if tfawserr.ErrMessageContains(err, emr.ErrCodeInvalidRequestException, "Studio session mapping does not exist") ||
tfawserr.ErrMessageContains(err, emr.ErrCodeInvalidRequestException, "Studio does not exist") {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil || output.SessionMapping == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output.SessionMapping, nil
}
14 changes: 14 additions & 0 deletions internal/service/emr/id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package emr

import (
"fmt"
"strings"
)

func readStudioSessionMapping(id string) (studioId, identityType, identityId string, err error) {
idParts := strings.Split(id, ":")
if len(idParts) != 3 {
return "", "", "", fmt.Errorf("expected ID in format studio-id:identity-type:identity-id, received: %s", id)
}
return idParts[0], idParts[1], idParts[2], nil
}
164 changes: 164 additions & 0 deletions internal/service/emr/studio_session_mapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package emr

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/emr"
"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/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func ResourceStudioSessionMapping() *schema.Resource {
return &schema.Resource{
Create: resourceStudioSessionMappingCreate,
Read: resourceStudioSessionMappingRead,
Update: resourceStudioSessionMappingUpdate,
Delete: resourceStudioSessionMappingDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"identity_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ExactlyOneOf: []string{"identity_id", "identity_name"},
},
"identity_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ExactlyOneOf: []string{"identity_id", "identity_name"},
},
"identity_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(emr.IdentityType_Values(), false),
},
"session_policy_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: verify.ValidARN,
},
"studio_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceStudioSessionMappingCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).EMRConn

var id string
studioId := d.Get("studio_id").(string)
identityType := d.Get("identity_type").(string)
input := &emr.CreateStudioSessionMappingInput{
IdentityType: aws.String(identityType),
SessionPolicyArn: aws.String(d.Get("session_policy_arn").(string)),
StudioId: aws.String(studioId),
}

if v, ok := d.GetOk("identity_id"); ok {
input.IdentityId = aws.String(v.(string))
id = v.(string)
}

if v, ok := d.GetOk("identity_name"); ok {
input.IdentityName = aws.String(v.(string))
id = v.(string)
}

_, err := conn.CreateStudioSessionMapping(input)
if err != nil {
return fmt.Errorf("error creating EMR Studio Session Mapping: %w", err)
}

d.SetId(fmt.Sprintf("%s:%s:%s", studioId, identityType, id))

return resourceStudioSessionMappingRead(d, meta)
}

func resourceStudioSessionMappingUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).EMRConn

studioId, identityType, identityId, err := readStudioSessionMapping(d.Id())
if err != nil {
return err
}

input := &emr.UpdateStudioSessionMappingInput{
SessionPolicyArn: aws.String(d.Get("session_policy_arn").(string)),
IdentityType: aws.String(identityType),
StudioId: aws.String(studioId),
IdentityId: aws.String(identityId),
}

_, err = conn.UpdateStudioSessionMapping(input)
if err != nil {
return fmt.Errorf("error updating EMR Studio Session Mapping: %w", err)
}

return resourceStudioSessionMappingRead(d, meta)
}

func resourceStudioSessionMappingRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).EMRConn

mapping, err := FindStudioSessionMappingByID(conn, d.Id())
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] EMR Studio Session Mapping (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading EMR Studio Session Mapping (%s): %w", d.Id(), err)
}

d.Set("identity_type", mapping.IdentityType)
d.Set("identity_id", mapping.IdentityId)
d.Set("identity_name", mapping.IdentityName)
d.Set("studio_id", mapping.StudioId)
d.Set("session_policy_arn", mapping.SessionPolicyArn)

return nil
}

func resourceStudioSessionMappingDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).EMRConn
studioId, identityType, identityId, err := readStudioSessionMapping(d.Id())
if err != nil {
return err
}

input := &emr.DeleteStudioSessionMappingInput{
IdentityType: aws.String(identityType),
StudioId: aws.String(studioId),
IdentityId: aws.String(identityId),
}

log.Printf("[INFO] Deleting EMR Studio Session Mapping: %s", d.Id())
_, err = conn.DeleteStudioSessionMapping(input)

if err != nil {
if tfawserr.ErrMessageContains(err, emr.ErrCodeInvalidRequestException, "Studio session mapping does not exist.") {
return nil
}
return fmt.Errorf("error deleting EMR Studio Session Mapping (%s): %w", d.Id(), err)
}

return nil
}
Loading

0 comments on commit 9b6edeb

Please sign in to comment.