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