github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/opsgenie/resource_opsgenie_team.go (about) 1 package opsgenie 2 3 import ( 4 "log" 5 6 "fmt" 7 "strings" 8 9 "regexp" 10 11 "github.com/hashicorp/terraform/helper/schema" 12 "github.com/opsgenie/opsgenie-go-sdk/team" 13 ) 14 15 func resourceOpsGenieTeam() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceOpsGenieTeamCreate, 18 Read: resourceOpsGenieTeamRead, 19 Update: resourceOpsGenieTeamUpdate, 20 Delete: resourceOpsGenieTeamDelete, 21 Importer: &schema.ResourceImporter{ 22 State: schema.ImportStatePassthrough, 23 }, 24 Schema: map[string]*schema.Schema{ 25 "name": { 26 Type: schema.TypeString, 27 Required: true, 28 ValidateFunc: validateOpsGenieTeamName, 29 }, 30 "description": { 31 Type: schema.TypeString, 32 Optional: true, 33 }, 34 "member": { 35 Type: schema.TypeList, 36 Optional: true, 37 Elem: &schema.Resource{ 38 Schema: map[string]*schema.Schema{ 39 "username": { 40 Type: schema.TypeString, 41 Required: true, 42 }, 43 44 "role": { 45 Type: schema.TypeString, 46 Optional: true, 47 Default: "user", 48 ValidateFunc: validateOpsGenieTeamRole, 49 }, 50 }, 51 }, 52 }, 53 }, 54 } 55 } 56 57 func resourceOpsGenieTeamCreate(d *schema.ResourceData, meta interface{}) error { 58 client := meta.(*OpsGenieClient).teams 59 60 name := d.Get("name").(string) 61 description := d.Get("description").(string) 62 63 createRequest := team.CreateTeamRequest{ 64 Name: name, 65 Description: description, 66 Members: expandOpsGenieTeamMembers(d), 67 } 68 69 log.Printf("[INFO] Creating OpsGenie team '%s'", name) 70 71 createResponse, err := client.Create(createRequest) 72 if err != nil { 73 return err 74 } 75 76 err = checkOpsGenieResponse(createResponse.Code, createResponse.Status) 77 if err != nil { 78 return err 79 } 80 81 getRequest := team.GetTeamRequest{ 82 Name: name, 83 } 84 85 getResponse, err := client.Get(getRequest) 86 if err != nil { 87 return err 88 } 89 90 d.SetId(getResponse.Id) 91 92 return resourceOpsGenieTeamRead(d, meta) 93 } 94 95 func resourceOpsGenieTeamRead(d *schema.ResourceData, meta interface{}) error { 96 client := meta.(*OpsGenieClient).teams 97 98 listRequest := team.ListTeamsRequest{} 99 listResponse, err := client.List(listRequest) 100 if err != nil { 101 return err 102 } 103 104 var found *team.GetTeamResponse 105 for _, team := range listResponse.Teams { 106 if team.Id == d.Id() { 107 found = &team 108 break 109 } 110 } 111 112 if found == nil { 113 d.SetId("") 114 log.Printf("[INFO] Team %q not found. Removing from state", d.Get("name").(string)) 115 return nil 116 } 117 118 getRequest := team.GetTeamRequest{ 119 Id: d.Id(), 120 } 121 122 getResponse, err := client.Get(getRequest) 123 if err != nil { 124 return err 125 } 126 127 d.Set("name", getResponse.Name) 128 d.Set("description", getResponse.Description) 129 d.Set("member", flattenOpsGenieTeamMembers(getResponse.Members)) 130 131 return nil 132 } 133 134 func resourceOpsGenieTeamUpdate(d *schema.ResourceData, meta interface{}) error { 135 client := meta.(*OpsGenieClient).teams 136 name := d.Get("name").(string) 137 description := d.Get("description").(string) 138 139 updateRequest := team.UpdateTeamRequest{ 140 Id: d.Id(), 141 Name: name, 142 Description: description, 143 Members: expandOpsGenieTeamMembers(d), 144 } 145 146 log.Printf("[INFO] Updating OpsGenie team '%s'", name) 147 148 updateResponse, err := client.Update(updateRequest) 149 if err != nil { 150 return err 151 } 152 153 err = checkOpsGenieResponse(updateResponse.Code, updateResponse.Status) 154 if err != nil { 155 return err 156 } 157 158 return nil 159 } 160 161 func resourceOpsGenieTeamDelete(d *schema.ResourceData, meta interface{}) error { 162 log.Printf("[INFO] Deleting OpsGenie team '%s'", d.Get("name").(string)) 163 client := meta.(*OpsGenieClient).teams 164 165 deleteRequest := team.DeleteTeamRequest{ 166 Id: d.Id(), 167 } 168 169 _, err := client.Delete(deleteRequest) 170 if err != nil { 171 return err 172 } 173 174 return nil 175 } 176 177 func flattenOpsGenieTeamMembers(input []team.Member) []interface{} { 178 members := make([]interface{}, 0, len(input)) 179 for _, inputMember := range input { 180 outputMember := make(map[string]interface{}) 181 outputMember["username"] = inputMember.User 182 outputMember["role"] = inputMember.Role 183 184 members = append(members, outputMember) 185 } 186 187 return members 188 } 189 190 func expandOpsGenieTeamMembers(d *schema.ResourceData) []team.Member { 191 input := d.Get("member").([]interface{}) 192 193 members := make([]team.Member, 0, len(input)) 194 if input == nil { 195 return members 196 } 197 198 for _, v := range input { 199 config := v.(map[string]interface{}) 200 201 username := config["username"].(string) 202 role := config["role"].(string) 203 204 member := team.Member{ 205 User: username, 206 Role: role, 207 } 208 209 members = append(members, member) 210 } 211 212 return members 213 } 214 215 func validateOpsGenieTeamName(v interface{}, k string) (ws []string, errors []error) { 216 value := v.(string) 217 if !regexp.MustCompile(`^[a-zA-Z0-9_]+$`).MatchString(value) { 218 errors = append(errors, fmt.Errorf( 219 "only alpha numeric characters and underscores are allowed in %q: %q", k, value)) 220 } 221 222 if len(value) >= 100 { 223 errors = append(errors, fmt.Errorf("%q cannot be longer than 100 characters: %q %d", k, value, len(value))) 224 } 225 226 return 227 } 228 229 func validateOpsGenieTeamRole(v interface{}, k string) (ws []string, errors []error) { 230 value := strings.ToLower(v.(string)) 231 families := map[string]bool{ 232 "admin": true, 233 "user": true, 234 } 235 236 if !families[value] { 237 errors = append(errors, fmt.Errorf("OpsGenie Team Role can only be 'Admin' or 'User'")) 238 } 239 240 return 241 }