Skip to content

Commit

Permalink
Remove the invitation ARN as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
YakDriver committed Aug 14, 2019
1 parent 1226a0a commit 8525f6d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 53 deletions.
77 changes: 30 additions & 47 deletions aws/resource_aws_ram_resource_share_accepter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@ func resourceAwsRamResourceShareAccepter() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"invitation_arn": {
"share_arn": {
Type: schema.TypeString,
Optional: true,
Required: true,
ForceNew: true,
Computed: true,
ValidateFunc: validateArn,
},

"share_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validateArn,
"invitation_arn": {
Type: schema.TypeString,
Computed: true,
},

"share_id": {
Expand Down Expand Up @@ -84,35 +80,32 @@ func resourceAwsRamResourceShareAccepter() *schema.Resource {
func resourceAwsRamResourceShareAccepterCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ramconn

input := &ram.AcceptResourceShareInvitationInput{
ClientToken: aws.String(resource.UniqueId()),
shareARN := d.Get("share_arn").(string)

// need invitation arn
invitation, err := resourceAwsRamResourceShareGetInvitation(conn, shareARN, ram.ResourceShareInvitationStatusPending)

if err != nil {
return err
}

if v, ok := d.GetOk("invitation_arn"); ok && v.(string) != "" {
input.ResourceShareInvitationArn = aws.String(v.(string))
} else if v, ok := d.GetOk("share_arn"); ok && v.(string) != "" {
// need invitation arn
invitation, err := resourceAwsRamResourceShareGetInvitation(d, meta, v.(string), ram.ResourceShareInvitationStatusPending)
if err != nil {
return err
}
if invitation == nil || aws.StringValue(invitation.ResourceShareInvitationArn) == "" {
return fmt.Errorf("No RAM resource share invitation by ARN (%s) found", v.(string))
}
if invitation == nil || aws.StringValue(invitation.ResourceShareInvitationArn) == "" {
return fmt.Errorf("No RAM resource share invitation by ARN (%s) found", shareARN)
}

input.ResourceShareInvitationArn = invitation.ResourceShareInvitationArn
} else {
return fmt.Errorf("Either an invitation ARN or share ARN are required")
input := &ram.AcceptResourceShareInvitationInput{
ClientToken: aws.String(resource.UniqueId()),
ResourceShareInvitationArn: invitation.ResourceShareInvitationArn,
}

log.Printf("[DEBUG] Accept RAM resource share invitation request: %s", input)
output, err := conn.AcceptResourceShareInvitation(input)

if err != nil {
return fmt.Errorf("Error accepting RAM resource share invitation: %s", err)
}

d.SetId(aws.StringValue(output.ResourceShareInvitation.ResourceShareArn))
d.Set("invitation_arn", output.ResourceShareInvitation.ResourceShareInvitationArn)
d.SetId(shareARN)

stateConf := &resource.StateChangeConf{
Pending: []string{ram.ResourceShareInvitationStatusPending},
Expand All @@ -124,6 +117,7 @@ func resourceAwsRamResourceShareAccepterCreate(d *schema.ResourceData, meta inte
}

_, err = stateConf.WaitForState()

if err != nil {
return fmt.Errorf("Error waiting for RAM resource share (%s) state: %s", d.Id(), err)
}
Expand All @@ -134,7 +128,7 @@ func resourceAwsRamResourceShareAccepterCreate(d *schema.ResourceData, meta inte
func resourceAwsRamResourceShareAccepterRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ramconn

invitation, err := resourceAwsRamResourceShareGetInvitation(d, meta, d.Id(), ram.ResourceShareInvitationStatusAccepted)
invitation, err := resourceAwsRamResourceShareGetInvitation(conn, d.Id(), ram.ResourceShareInvitationStatusAccepted)

if err == nil && invitation == nil {
log.Printf("[WARN] No RAM resource share invitation by ARN (%s) found, removing from state", d.Id())
Expand Down Expand Up @@ -184,19 +178,21 @@ func resourceAwsRamResourceShareAccepterRead(d *schema.ResourceData, meta interf
func resourceAwsRamResourceShareAccepterDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ramconn

v, ok := d.GetOk("receiver_account_id")
if !ok {
receiverAccountID := d.Get("receiver_account_id").(string)

if receiverAccountID == "" {
return fmt.Errorf("The receiver account ID is required to leave a resource share")
}
receiverID := v.(string)

input := &ram.DisassociateResourceShareInput{
ClientToken: aws.String(resource.UniqueId()),
ResourceShareArn: aws.String(d.Id()),
Principals: []*string{aws.String(receiverID)},
Principals: []*string{aws.String(receiverAccountID)},
}
log.Printf("[DEBUG] Leaving RAM resource share request: %s", input)
log.Printf("[DEBUG] Leave RAM resource share request: %s", input)

_, err := conn.DisassociateResourceShare(input)

if err != nil {
return fmt.Errorf("Error leaving RAM resource share: %s", err)
}
Expand All @@ -219,18 +215,11 @@ func resourceAwsRamResourceShareAccepterDelete(d *schema.ResourceData, meta inte
return nil
}

func resourceAwsRamResourceShareGetInvitation(d *schema.ResourceData, meta interface{}, resourceShareARN, status string) (*ram.ResourceShareInvitation, error) {
conn := meta.(*AWSClient).ramconn

func resourceAwsRamResourceShareGetInvitation(conn *ram.RAM, resourceShareARN, status string) (*ram.ResourceShareInvitation, error) {
input := &ram.GetResourceShareInvitationsInput{
ResourceShareArns: []*string{aws.String(resourceShareARN)},
}

invitationARN := d.Get("invitation_arn").(string)
if invitationARN != "" {
input.ResourceShareInvitationArns = []*string{aws.String(invitationARN)}
}

var invitation *ram.ResourceShareInvitation
err := conn.GetResourceShareInvitationsPages(input, func(page *ram.GetResourceShareInvitationsOutput, lastPage bool) bool {
for _, rsi := range page.ResourceShareInvitations {
Expand All @@ -243,12 +232,6 @@ func resourceAwsRamResourceShareGetInvitation(d *schema.ResourceData, meta inter
return !lastPage
})

// if invitation not found with invitation ARN, try to find without
if invitation == nil && invitationARN != "" {
d.Set("invitation_arn", "")
return resourceAwsRamResourceShareGetInvitation(d, meta, resourceShareARN, status)
}

if invitation == nil {
return nil, nil
}
Expand Down
10 changes: 4 additions & 6 deletions website/docs/r/ram_resource_share_accepter.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Manage accepting a Resource Access Manager (RAM) Resource Share invitation. From

## Example Usage

This configuration provides an example of using multiple Terraform AWS providers to configure two different AWS accounts. In the _sender_ account, the configuration creates a `aws_ram_resource_share` and uses a data source in the _receiver_ account to create a `aws_ram_principal_assocation` resource with the _receiver's_ account ID. In the _receiver_ account, the configuration accepts the invitation to share resources with the `aws_ram_resource_share_accepter`.
This configuration provides an example of using multiple Terraform AWS providers to configure two different AWS accounts. In the _sender_ account, the configuration creates a `aws_ram_resource_share` and uses a data source in the _receiver_ account to create a `aws_ram_principal_association` resource with the _receiver's_ account ID. In the _receiver_ account, the configuration accepts the invitation to share resources with the `aws_ram_resource_share_accepter`.

```hcl
provider "aws" {
Expand Down Expand Up @@ -55,15 +55,13 @@ resource "aws_ram_resource_share_accepter" "receiver_accept" {

The following arguments are supported:

~> **Note:** One of either `share_arn` or `invitation_arn` is required. Using `share_arn` where multiple resources share invitations exist between the same _sender_ and _receiver_, may result in this resource selecting an unexpected invitation. In that case, use `invitation_arn`.

* `share_arn` - (Optional) The ARN of the resource share.
* `invitation_arn` - (Optional) The ARN of the resource share invitation.
* `share_arn` - (Required) The ARN of the resource share.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `invitation_arn` - The ARN of the resource share invitation.
* `share_id` - The ID of the resource share as displayed in the console.
* `status` - The status of the invitation (e.g., ACCEPTED, REJECTED).
* `receiver_account_id` - The account ID of the receiver account which accepts the invitation.
Expand All @@ -73,7 +71,7 @@ In addition to all arguments above, the following attributes are exported:

## Import

Resource share accepters can be imported using the invitation ARN, e.g.
Resource share accepters can be imported using the resource share ARN, e.g.

```
$ terraform import aws_ram_resource_share_accepter.example arn:aws:ram:us-east-1:123456789012:resource-share/c4b56393-e8d9-89d9-6dc9-883752de4767
Expand Down

0 comments on commit 8525f6d

Please sign in to comment.