github.com/mkuzmin/terraform@v0.3.7-0.20161118171027-ec4c00ff92a9/builtin/providers/github/resource_github_issue_label.go (about)

     1  package github
     2  
     3  import (
     4  	"github.com/google/go-github/github"
     5  	"github.com/hashicorp/terraform/helper/schema"
     6  )
     7  
     8  func resourceGithubIssueLabel() *schema.Resource {
     9  	return &schema.Resource{
    10  		Create: resourceGithubIssueLabelCreate,
    11  		Read:   resourceGithubIssueLabelRead,
    12  		Update: resourceGithubIssueLabelUpdate,
    13  		Delete: resourceGithubIssueLabelDelete,
    14  
    15  		Schema: map[string]*schema.Schema{
    16  			"repository": &schema.Schema{
    17  				Type:     schema.TypeString,
    18  				Required: true,
    19  				ForceNew: true,
    20  			},
    21  			"name": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  			},
    25  			"color": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  			},
    29  			"url": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Computed: true,
    32  			},
    33  		},
    34  	}
    35  }
    36  
    37  func resourceGithubIssueLabelCreate(d *schema.ResourceData, meta interface{}) error {
    38  	client := meta.(*Organization).client
    39  	r := d.Get("repository").(string)
    40  	n := d.Get("name").(string)
    41  	c := d.Get("color").(string)
    42  
    43  	_, _, err := client.Issues.CreateLabel(meta.(*Organization).name, r, &github.Label{
    44  		Name:  &n,
    45  		Color: &c,
    46  	})
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	d.SetId(buildTwoPartID(&r, &n))
    52  
    53  	return resourceGithubIssueLabelRead(d, meta)
    54  }
    55  
    56  func resourceGithubIssueLabelRead(d *schema.ResourceData, meta interface{}) error {
    57  	client := meta.(*Organization).client
    58  	r := d.Get("repository").(string)
    59  	n := d.Get("name").(string)
    60  
    61  	githubLabel, _, err := client.Issues.GetLabel(meta.(*Organization).name, r, n)
    62  	if err != nil {
    63  		d.SetId("")
    64  		return nil
    65  	}
    66  
    67  	d.Set("color", githubLabel.Color)
    68  	d.Set("url", githubLabel.URL)
    69  
    70  	return nil
    71  }
    72  
    73  func resourceGithubIssueLabelUpdate(d *schema.ResourceData, meta interface{}) error {
    74  	client := meta.(*Organization).client
    75  	r := d.Get("repository").(string)
    76  	n := d.Get("name").(string)
    77  	c := d.Get("color").(string)
    78  
    79  	_, originalName := parseTwoPartID(d.Id())
    80  	_, _, err := client.Issues.EditLabel(meta.(*Organization).name, r, originalName, &github.Label{
    81  		Name:  &n,
    82  		Color: &c,
    83  	})
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	d.SetId(buildTwoPartID(&r, &n))
    89  
    90  	return resourceGithubIssueLabelRead(d, meta)
    91  }
    92  
    93  func resourceGithubIssueLabelDelete(d *schema.ResourceData, meta interface{}) error {
    94  	client := meta.(*Organization).client
    95  	r := d.Get("repository").(string)
    96  	n := d.Get("name").(string)
    97  
    98  	_, err := client.Issues.DeleteLabel(meta.(*Organization).name, r, n)
    99  	return err
   100  }