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