github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/builtin/providers/aws/resource_aws_iam_user.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 6 "github.com/aws/aws-sdk-go/aws" 7 "github.com/aws/aws-sdk-go/aws/awserr" 8 "github.com/aws/aws-sdk-go/service/iam" 9 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 func resourceAwsIamUser() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAwsIamUserCreate, 16 Read: resourceAwsIamUserRead, 17 // There is an UpdateUser API call, but goamz doesn't support it yet. 18 // XXX but we aren't using goamz anymore. 19 //Update: resourceAwsIamUserUpdate, 20 Delete: resourceAwsIamUserDelete, 21 22 Schema: map[string]*schema.Schema{ 23 "arn": &schema.Schema{ 24 Type: schema.TypeString, 25 Computed: true, 26 }, 27 /* 28 The UniqueID could be used as the Id(), but none of the API 29 calls allow specifying a user by the UniqueID: they require the 30 name. The only way to locate a user by UniqueID is to list them 31 all and that would make this provider unnecessarilly complex 32 and inefficient. Still, there are other reasons one might want 33 the UniqueID, so we can make it availible. 34 */ 35 "unique_id": &schema.Schema{ 36 Type: schema.TypeString, 37 Computed: true, 38 }, 39 "name": &schema.Schema{ 40 Type: schema.TypeString, 41 Required: true, 42 ForceNew: true, 43 }, 44 "path": &schema.Schema{ 45 Type: schema.TypeString, 46 Optional: true, 47 Default: "/", 48 ForceNew: true, 49 }, 50 }, 51 } 52 } 53 54 func resourceAwsIamUserCreate(d *schema.ResourceData, meta interface{}) error { 55 iamconn := meta.(*AWSClient).iamconn 56 name := d.Get("name").(string) 57 58 request := &iam.CreateUserInput{ 59 Path: aws.String(d.Get("path").(string)), 60 UserName: aws.String(name), 61 } 62 63 createResp, err := iamconn.CreateUser(request) 64 if err != nil { 65 return fmt.Errorf("Error creating IAM User %s: %s", name, err) 66 } 67 return resourceAwsIamUserReadResult(d, createResp.User) 68 } 69 70 func resourceAwsIamUserRead(d *schema.ResourceData, meta interface{}) error { 71 iamconn := meta.(*AWSClient).iamconn 72 73 request := &iam.GetUserInput{ 74 UserName: aws.String(d.Id()), 75 } 76 77 getResp, err := iamconn.GetUser(request) 78 if err != nil { 79 if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" { // XXX test me 80 d.SetId("") 81 return nil 82 } 83 return fmt.Errorf("Error reading IAM User %s: %s", d.Id(), err) 84 } 85 return resourceAwsIamUserReadResult(d, getResp.User) 86 } 87 88 func resourceAwsIamUserReadResult(d *schema.ResourceData, user *iam.User) error { 89 d.SetId(*user.UserName) 90 if err := d.Set("name", user.UserName); err != nil { 91 return err 92 } 93 if err := d.Set("arn", user.ARN); err != nil { 94 return err 95 } 96 if err := d.Set("path", user.Path); err != nil { 97 return err 98 } 99 if err := d.Set("unique_id", user.UserID); err != nil { 100 return err 101 } 102 return nil 103 } 104 105 func resourceAwsIamUserDelete(d *schema.ResourceData, meta interface{}) error { 106 iamconn := meta.(*AWSClient).iamconn 107 108 request := &iam.DeleteUserInput{ 109 UserName: aws.String(d.Id()), 110 } 111 112 if _, err := iamconn.DeleteUser(request); err != nil { 113 return fmt.Errorf("Error deleting IAM User %s: %s", d.Id(), err) 114 } 115 return nil 116 }