github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/datadog/resource_datadog_user.go (about) 1 package datadog 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 "gopkg.in/zorkian/go-datadog-api.v2" 10 ) 11 12 func resourceDatadogUser() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceDatadogUserCreate, 15 Read: resourceDatadogUserRead, 16 Update: resourceDatadogUserUpdate, 17 Delete: resourceDatadogUserDelete, 18 Exists: resourceDatadogUserExists, 19 Importer: &schema.ResourceImporter{ 20 State: resourceDatadogUserImport, 21 }, 22 23 Schema: map[string]*schema.Schema{ 24 "disabled": { 25 Type: schema.TypeBool, 26 Optional: true, 27 Default: false, 28 }, 29 "email": { 30 Type: schema.TypeString, 31 Required: true, 32 }, 33 "handle": { 34 Type: schema.TypeString, 35 Required: true, 36 }, 37 "is_admin": { 38 Type: schema.TypeBool, 39 Optional: true, 40 Default: false, 41 }, 42 "name": { 43 Type: schema.TypeString, 44 Required: true, 45 }, 46 "role": { 47 Type: schema.TypeString, 48 Optional: true, 49 }, 50 "verified": { 51 Type: schema.TypeBool, 52 Optional: true, 53 }, 54 }, 55 } 56 } 57 58 func resourceDatadogUserExists(d *schema.ResourceData, meta interface{}) (b bool, e error) { 59 // Exists - This is called to verify a resource still exists. It is called prior to Read, 60 // and lowers the burden of Read to be able to assume the resource exists. 61 client := meta.(*datadog.Client) 62 63 if _, err := client.GetUser(d.Id()); err != nil { 64 if strings.Contains(err.Error(), "404 Not Found") { 65 return false, nil 66 } 67 return false, err 68 } 69 70 return true, nil 71 } 72 73 func resourceDatadogUserCreate(d *schema.ResourceData, meta interface{}) error { 74 client := meta.(*datadog.Client) 75 76 var u datadog.User 77 u.SetDisabled(d.Get("disabled").(bool)) 78 u.SetEmail(d.Get("email").(string)) 79 u.SetHandle(d.Get("handle").(string)) 80 u.SetIsAdmin(d.Get("is_admin").(bool)) 81 u.SetName(d.Get("name").(string)) 82 u.SetRole(d.Get("role").(string)) 83 84 // Datadog does not actually delete users, so CreateUser might return a 409. 85 // We ignore that case and proceed, likely re-enabling the user. 86 if _, err := client.CreateUser(u.Handle, u.Name); err != nil { 87 if !strings.Contains(err.Error(), "API error 409 Conflict") { 88 return fmt.Errorf("error creating user: %s", err.Error()) 89 } 90 log.Printf("[INFO] Updating existing Datadog user %q", u.Handle) 91 } 92 93 if err := client.UpdateUser(u); err != nil { 94 return fmt.Errorf("error creating user: %s", err.Error()) 95 } 96 97 d.SetId(u.GetHandle()) 98 99 return nil 100 } 101 102 func resourceDatadogUserRead(d *schema.ResourceData, meta interface{}) error { 103 client := meta.(*datadog.Client) 104 105 u, err := client.GetUser(d.Id()) 106 if err != nil { 107 return err 108 } 109 110 log.Printf("[DEBUG] user: %v", u) 111 d.Set("disabled", u.GetDisabled()) 112 d.Set("email", u.GetEmail()) 113 d.Set("handle", u.GetHandle()) 114 d.Set("is_admin", u.GetIsAdmin()) 115 d.Set("name", u.GetName()) 116 d.Set("role", u.GetRole()) 117 d.Set("verified", u.GetVerified()) 118 119 return nil 120 } 121 122 func resourceDatadogUserUpdate(d *schema.ResourceData, meta interface{}) error { 123 client := meta.(*datadog.Client) 124 var u datadog.User 125 u.SetDisabled(d.Get("disabled").(bool)) 126 u.SetEmail(d.Get("email").(string)) 127 u.SetHandle(d.Id()) 128 u.SetIsAdmin(d.Get("is_admin").(bool)) 129 u.SetName(d.Get("name").(string)) 130 u.SetRole(d.Get("role").(string)) 131 132 if err := client.UpdateUser(u); err != nil { 133 return fmt.Errorf("error updating user: %s", err.Error()) 134 } 135 136 return resourceDatadogUserRead(d, meta) 137 } 138 139 func resourceDatadogUserDelete(d *schema.ResourceData, meta interface{}) error { 140 client := meta.(*datadog.Client) 141 142 // Datadog does not actually delete users, but instead marks them as disabled. 143 // Bypass DeleteUser if GetUser returns User.Disabled == true, otherwise it will 400. 144 if u, err := client.GetUser(d.Id()); err == nil && u.GetDisabled() { 145 return nil 146 } 147 148 if err := client.DeleteUser(d.Id()); err != nil { 149 return err 150 } 151 152 return nil 153 } 154 155 func resourceDatadogUserImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { 156 if err := resourceDatadogUserRead(d, meta); err != nil { 157 return nil, err 158 } 159 return []*schema.ResourceData{d}, nil 160 }