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