github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/resource_aws_opsworks_user_profile.go (about)

     1  package aws
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/opsworks"
    11  )
    12  
    13  func resourceAwsOpsworksUserProfile() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceAwsOpsworksUserProfileCreate,
    16  		Read:   resourceAwsOpsworksUserProfileRead,
    17  		Update: resourceAwsOpsworksUserProfileUpdate,
    18  		Delete: resourceAwsOpsworksUserProfileDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"id": {
    22  				Type:     schema.TypeString,
    23  				Computed: true,
    24  			},
    25  
    26  			"user_arn": {
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  				ForceNew: true,
    30  			},
    31  
    32  			"allow_self_management": {
    33  				Type:     schema.TypeBool,
    34  				Optional: true,
    35  				Default:  false,
    36  			},
    37  
    38  			"ssh_username": {
    39  				Type:     schema.TypeString,
    40  				Required: true,
    41  			},
    42  
    43  			"ssh_public_key": {
    44  				Type:     schema.TypeString,
    45  				Optional: true,
    46  			},
    47  		},
    48  	}
    49  }
    50  
    51  func resourceAwsOpsworksUserProfileRead(d *schema.ResourceData, meta interface{}) error {
    52  	client := meta.(*AWSClient).opsworksconn
    53  
    54  	req := &opsworks.DescribeUserProfilesInput{
    55  		IamUserArns: []*string{
    56  			aws.String(d.Id()),
    57  		},
    58  	}
    59  
    60  	log.Printf("[DEBUG] Reading OpsWorks user profile: %s", d.Id())
    61  
    62  	resp, err := client.DescribeUserProfiles(req)
    63  	if err != nil {
    64  		if awserr, ok := err.(awserr.Error); ok {
    65  			if awserr.Code() == "ResourceNotFoundException" {
    66  				log.Printf("[DEBUG] OpsWorks user profile (%s) not found", d.Id())
    67  				d.SetId("")
    68  				return nil
    69  			}
    70  		}
    71  		return err
    72  	}
    73  
    74  	for _, profile := range resp.UserProfiles {
    75  		d.Set("allow_self_management", profile.AllowSelfManagement)
    76  		d.Set("user_arn", profile.IamUserArn)
    77  		d.Set("ssh_public_key", profile.SshPublicKey)
    78  		d.Set("ssh_username", profile.SshUsername)
    79  		break
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  func resourceAwsOpsworksUserProfileCreate(d *schema.ResourceData, meta interface{}) error {
    86  	client := meta.(*AWSClient).opsworksconn
    87  
    88  	req := &opsworks.CreateUserProfileInput{
    89  		AllowSelfManagement: aws.Bool(d.Get("allow_self_management").(bool)),
    90  		IamUserArn:          aws.String(d.Get("user_arn").(string)),
    91  		SshPublicKey:        aws.String(d.Get("ssh_public_key").(string)),
    92  		SshUsername:         aws.String(d.Get("ssh_username").(string)),
    93  	}
    94  
    95  	resp, err := client.CreateUserProfile(req)
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	d.SetId(*resp.IamUserArn)
   101  
   102  	return resourceAwsOpsworksUserProfileUpdate(d, meta)
   103  }
   104  
   105  func resourceAwsOpsworksUserProfileUpdate(d *schema.ResourceData, meta interface{}) error {
   106  	client := meta.(*AWSClient).opsworksconn
   107  
   108  	req := &opsworks.UpdateUserProfileInput{
   109  		AllowSelfManagement: aws.Bool(d.Get("allow_self_management").(bool)),
   110  		IamUserArn:          aws.String(d.Get("user_arn").(string)),
   111  		SshPublicKey:        aws.String(d.Get("ssh_public_key").(string)),
   112  		SshUsername:         aws.String(d.Get("ssh_username").(string)),
   113  	}
   114  
   115  	log.Printf("[DEBUG] Updating OpsWorks user profile: %s", req)
   116  
   117  	_, err := client.UpdateUserProfile(req)
   118  	if err != nil {
   119  		return err
   120  	}
   121  
   122  	return resourceAwsOpsworksUserProfileRead(d, meta)
   123  }
   124  
   125  func resourceAwsOpsworksUserProfileDelete(d *schema.ResourceData, meta interface{}) error {
   126  	client := meta.(*AWSClient).opsworksconn
   127  
   128  	req := &opsworks.DeleteUserProfileInput{
   129  		IamUserArn: aws.String(d.Id()),
   130  	}
   131  
   132  	log.Printf("[DEBUG] Deleting OpsWorks user profile: %s", d.Id())
   133  
   134  	_, err := client.DeleteUserProfile(req)
   135  
   136  	return err
   137  }