github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/pagerduty/resource_pagerduty_user.go (about)

     1  package pagerduty
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/PagerDuty/go-pagerduty"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourcePagerDutyUser() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourcePagerDutyUserCreate,
    13  		Read:   resourcePagerDutyUserRead,
    14  		Update: resourcePagerDutyUserUpdate,
    15  		Delete: resourcePagerDutyUserDelete,
    16  		Importer: &schema.ResourceImporter{
    17  			State: resourcePagerDutyUserImport,
    18  		},
    19  		Schema: map[string]*schema.Schema{
    20  			"name": {
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  			},
    24  			"email": {
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  			},
    28  			"color": {
    29  				Type:     schema.TypeString,
    30  				Optional: true,
    31  				Computed: true,
    32  			},
    33  			"role": {
    34  				Type:     schema.TypeString,
    35  				Optional: true,
    36  				Default:  "user",
    37  				ValidateFunc: validateValueFunc([]string{
    38  					"admin",
    39  					"limited_user",
    40  					"owner",
    41  					"read_only_user",
    42  					"user",
    43  				}),
    44  			},
    45  			"job_title": {
    46  				Type:     schema.TypeString,
    47  				Optional: true,
    48  			},
    49  			"avatar_url": {
    50  				Type:     schema.TypeString,
    51  				Computed: true,
    52  			},
    53  			"teams": {
    54  				Type:     schema.TypeSet,
    55  				Optional: true,
    56  				Elem: &schema.Schema{
    57  					Type: schema.TypeString,
    58  				},
    59  				Set: schema.HashString,
    60  			},
    61  			"time_zone": {
    62  				Type:     schema.TypeString,
    63  				Computed: true,
    64  			},
    65  			"html_url": {
    66  				Type:     schema.TypeString,
    67  				Computed: true,
    68  			},
    69  			"invitation_sent": {
    70  				Type:     schema.TypeBool,
    71  				Computed: true,
    72  			},
    73  			"description": {
    74  				Type:     schema.TypeString,
    75  				Optional: true,
    76  				Default:  "Managed by Terraform",
    77  			},
    78  		},
    79  	}
    80  }
    81  
    82  func buildUserStruct(d *schema.ResourceData) *pagerduty.User {
    83  	user := pagerduty.User{
    84  		Name:  d.Get("name").(string),
    85  		Email: d.Get("email").(string),
    86  		APIObject: pagerduty.APIObject{
    87  			ID: d.Id(),
    88  		},
    89  	}
    90  
    91  	if attr, ok := d.GetOk("color"); ok {
    92  		user.Color = attr.(string)
    93  	}
    94  
    95  	if attr, ok := d.GetOk("role"); ok {
    96  		role := attr.(string)
    97  		// Skip setting the role if the user is the owner of the account.
    98  		// Can't change this through the API.
    99  		if role != "owner" {
   100  			user.Role = role
   101  		}
   102  	}
   103  
   104  	if attr, ok := d.GetOk("job_title"); ok {
   105  		user.JobTitle = attr.(string)
   106  	}
   107  
   108  	if attr, ok := d.GetOk("description"); ok {
   109  		user.Description = attr.(string)
   110  	}
   111  
   112  	return &user
   113  }
   114  
   115  func resourcePagerDutyUserCreate(d *schema.ResourceData, meta interface{}) error {
   116  	client := meta.(*pagerduty.Client)
   117  
   118  	user := buildUserStruct(d)
   119  
   120  	log.Printf("[INFO] Creating PagerDuty user %s", user.Name)
   121  
   122  	user, err := client.CreateUser(*user)
   123  
   124  	if err != nil {
   125  		return err
   126  	}
   127  
   128  	d.SetId(user.ID)
   129  
   130  	return resourcePagerDutyUserUpdate(d, meta)
   131  }
   132  
   133  func resourcePagerDutyUserRead(d *schema.ResourceData, meta interface{}) error {
   134  	client := meta.(*pagerduty.Client)
   135  
   136  	log.Printf("[INFO] Reading PagerDuty user %s", d.Id())
   137  
   138  	o := &pagerduty.GetUserOptions{}
   139  
   140  	user, err := client.GetUser(d.Id(), *o)
   141  
   142  	if err != nil {
   143  		return err
   144  	}
   145  
   146  	d.Set("name", user.Name)
   147  	d.Set("email", user.Email)
   148  	d.Set("time_zone", user.Timezone)
   149  	d.Set("color", user.Color)
   150  	d.Set("role", user.Role)
   151  	d.Set("avatar_url", user.AvatarURL)
   152  	d.Set("description", user.Description)
   153  	d.Set("job_title", user.JobTitle)
   154  	d.Set("teams", user.Teams)
   155  
   156  	return nil
   157  }
   158  
   159  func resourcePagerDutyUserUpdate(d *schema.ResourceData, meta interface{}) error {
   160  	client := meta.(*pagerduty.Client)
   161  
   162  	user := buildUserStruct(d)
   163  
   164  	log.Printf("[INFO] Updating PagerDuty user %s", d.Id())
   165  
   166  	if _, err := client.UpdateUser(*user); err != nil {
   167  		return err
   168  	}
   169  
   170  	if d.HasChange("teams") {
   171  		o, n := d.GetChange("teams")
   172  
   173  		if o == nil {
   174  			o = new(schema.Set)
   175  		}
   176  
   177  		if n == nil {
   178  			n = new(schema.Set)
   179  		}
   180  
   181  		os := o.(*schema.Set)
   182  		ns := n.(*schema.Set)
   183  
   184  		remove := expandStringList(os.Difference(ns).List())
   185  		add := expandStringList(ns.Difference(os).List())
   186  
   187  		for _, t := range remove {
   188  			_, tErr := client.GetTeam(t)
   189  
   190  			if tErr != nil {
   191  				log.Printf("[INFO] PagerDuty team: %s not found, removing dangling team reference for user %s", t, d.Id())
   192  				continue
   193  			}
   194  
   195  			log.Printf("[INFO] Removing PagerDuty user %s from team: %s", d.Id(), t)
   196  
   197  			rErr := client.RemoveUserFromTeam(t, d.Id())
   198  			if rErr != nil {
   199  				return rErr
   200  			}
   201  		}
   202  
   203  		for _, t := range add {
   204  			log.Printf("[INFO] Adding PagerDuty user %s to team: %s", d.Id(), t)
   205  
   206  			aErr := client.AddUserToTeam(t, d.Id())
   207  			if aErr != nil {
   208  				return aErr
   209  			}
   210  		}
   211  	}
   212  
   213  	return nil
   214  }
   215  
   216  func resourcePagerDutyUserDelete(d *schema.ResourceData, meta interface{}) error {
   217  	client := meta.(*pagerduty.Client)
   218  
   219  	log.Printf("[INFO] Deleting PagerDuty user %s", d.Id())
   220  
   221  	if err := client.DeleteUser(d.Id()); err != nil {
   222  		return err
   223  	}
   224  
   225  	d.SetId("")
   226  
   227  	return nil
   228  }
   229  
   230  func resourcePagerDutyUserImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
   231  	if err := resourcePagerDutyUserRead(d, meta); err != nil {
   232  		return nil, err
   233  	}
   234  	return []*schema.ResourceData{d}, nil
   235  }