github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/opsgenie/resource_opsgenie_user.go (about)

     1  package opsgenie
     2  
     3  import (
     4  	"log"
     5  
     6  	"fmt"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/opsgenie/opsgenie-go-sdk/user"
    10  )
    11  
    12  func resourceOpsGenieUser() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceOpsGenieUserCreate,
    15  		Read:   resourceOpsGenieUserRead,
    16  		Update: resourceOpsGenieUserUpdate,
    17  		Delete: resourceOpsGenieUserDelete,
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  		Schema: map[string]*schema.Schema{
    22  			"username": {
    23  				Type:         schema.TypeString,
    24  				ForceNew:     true,
    25  				Required:     true,
    26  				ValidateFunc: validateOpsGenieUserUsername,
    27  			},
    28  			"full_name": {
    29  				Type:         schema.TypeString,
    30  				Required:     true,
    31  				ValidateFunc: validateOpsGenieUserFullName,
    32  			},
    33  			"role": {
    34  				Type:         schema.TypeString,
    35  				Required:     true,
    36  				ValidateFunc: validateOpsGenieUserRole,
    37  			},
    38  			"locale": {
    39  				Type:     schema.TypeString,
    40  				Optional: true,
    41  				Default:  "en_US",
    42  			},
    43  			"timezone": {
    44  				Type:     schema.TypeString,
    45  				Optional: true,
    46  				Default:  "America/New_York",
    47  			},
    48  		},
    49  	}
    50  }
    51  
    52  func resourceOpsGenieUserCreate(d *schema.ResourceData, meta interface{}) error {
    53  	client := meta.(*OpsGenieClient).users
    54  
    55  	username := d.Get("username").(string)
    56  	fullName := d.Get("full_name").(string)
    57  	role := d.Get("role").(string)
    58  	locale := d.Get("locale").(string)
    59  	timeZone := d.Get("timezone").(string)
    60  
    61  	createRequest := user.CreateUserRequest{
    62  		Username: username,
    63  		Fullname: fullName,
    64  		Role:     role,
    65  		Locale:   locale,
    66  		Timezone: timeZone,
    67  	}
    68  
    69  	log.Printf("[INFO] Creating OpsGenie user '%s'", username)
    70  	createResponse, err := client.Create(createRequest)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	err = checkOpsGenieResponse(createResponse.Code, createResponse.Status)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	getRequest := user.GetUserRequest{
    81  		Username: username,
    82  	}
    83  
    84  	getResponse, err := client.Get(getRequest)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	d.SetId(getResponse.Id)
    90  
    91  	return resourceOpsGenieUserRead(d, meta)
    92  }
    93  
    94  func resourceOpsGenieUserRead(d *schema.ResourceData, meta interface{}) error {
    95  	client := meta.(*OpsGenieClient).users
    96  
    97  	listRequest := user.ListUsersRequest{}
    98  	listResponse, err := client.List(listRequest)
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	var found *user.GetUserResponse
   104  	for _, user := range listResponse.Users {
   105  		if user.Id == d.Id() {
   106  			found = &user
   107  			break
   108  		}
   109  	}
   110  
   111  	if found == nil {
   112  		d.SetId("")
   113  		log.Printf("[INFO] User %q not found. Removing from state", d.Get("username").(string))
   114  		return nil
   115  	}
   116  
   117  	getRequest := user.GetUserRequest{
   118  		Id: d.Id(),
   119  	}
   120  
   121  	getResponse, err := client.Get(getRequest)
   122  	if err != nil {
   123  		return err
   124  	}
   125  
   126  	d.Set("username", getResponse.Username)
   127  	d.Set("full_name", getResponse.Fullname)
   128  	d.Set("role", getResponse.Role)
   129  	d.Set("locale", getResponse.Locale)
   130  	d.Set("timezone", getResponse.Timezone)
   131  
   132  	return nil
   133  }
   134  
   135  func resourceOpsGenieUserUpdate(d *schema.ResourceData, meta interface{}) error {
   136  	client := meta.(*OpsGenieClient).users
   137  
   138  	username := d.Get("username").(string)
   139  	fullName := d.Get("full_name").(string)
   140  	role := d.Get("role").(string)
   141  	locale := d.Get("locale").(string)
   142  	timeZone := d.Get("timezone").(string)
   143  
   144  	log.Printf("[INFO] Updating OpsGenie user '%s'", username)
   145  
   146  	updateRequest := user.UpdateUserRequest{
   147  		Id:       d.Id(),
   148  		Fullname: fullName,
   149  		Role:     role,
   150  		Locale:   locale,
   151  		Timezone: timeZone,
   152  	}
   153  
   154  	updateResponse, err := client.Update(updateRequest)
   155  	if err != nil {
   156  		return err
   157  	}
   158  
   159  	err = checkOpsGenieResponse(updateResponse.Code, updateResponse.Status)
   160  	if err != nil {
   161  		return err
   162  	}
   163  
   164  	return nil
   165  }
   166  
   167  func resourceOpsGenieUserDelete(d *schema.ResourceData, meta interface{}) error {
   168  	log.Printf("[INFO] Deleting OpsGenie user '%s'", d.Get("username").(string))
   169  	client := meta.(*OpsGenieClient).users
   170  
   171  	deleteRequest := user.DeleteUserRequest{
   172  		Id: d.Id(),
   173  	}
   174  
   175  	_, err := client.Delete(deleteRequest)
   176  	if err != nil {
   177  		return err
   178  	}
   179  
   180  	return nil
   181  }
   182  
   183  func validateOpsGenieUserUsername(v interface{}, k string) (ws []string, errors []error) {
   184  	value := v.(string)
   185  
   186  	if len(value) >= 100 {
   187  		errors = append(errors, fmt.Errorf("%q cannot be longer than 100 characters: %q %d", k, value, len(value)))
   188  	}
   189  
   190  	return
   191  }
   192  
   193  func validateOpsGenieUserFullName(v interface{}, k string) (ws []string, errors []error) {
   194  	value := v.(string)
   195  
   196  	if len(value) >= 512 {
   197  		errors = append(errors, fmt.Errorf("%q cannot be longer than 512 characters: %q %d", k, value, len(value)))
   198  	}
   199  
   200  	return
   201  }
   202  
   203  func validateOpsGenieUserRole(v interface{}, k string) (ws []string, errors []error) {
   204  	value := v.(string)
   205  
   206  	if len(value) >= 512 {
   207  		errors = append(errors, fmt.Errorf("%q cannot be longer than 512 characters: %q %d", k, value, len(value)))
   208  	}
   209  
   210  	return
   211  }