github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  		Importer: &schema.ResourceImporter{
    15  			State: schema.ImportStatePassthrough,
    16  		},
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"repository": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  			"name": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  			},
    28  			"color": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  			},
    32  			"url": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Computed: true,
    35  			},
    36  		},
    37  	}
    38  }
    39  
    40  func resourceGithubIssueLabelCreate(d *schema.ResourceData, meta interface{}) error {
    41  	client := meta.(*Organization).client
    42  	r := d.Get("repository").(string)
    43  	n := d.Get("name").(string)
    44  	c := d.Get("color").(string)
    45  
    46  	_, _, err := client.Issues.CreateLabel(meta.(*Organization).name, r, &github.Label{
    47  		Name:  &n,
    48  		Color: &c,
    49  	})
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	d.SetId(buildTwoPartID(&r, &n))
    55  
    56  	return resourceGithubIssueLabelRead(d, meta)
    57  }
    58  
    59  func resourceGithubIssueLabelRead(d *schema.ResourceData, meta interface{}) error {
    60  	client := meta.(*Organization).client
    61  	r, n := parseTwoPartID(d.Id())
    62  
    63  	githubLabel, _, err := client.Issues.GetLabel(meta.(*Organization).name, r, n)
    64  	if err != nil {
    65  		d.SetId("")
    66  		return nil
    67  	}
    68  
    69  	d.Set("repository", r)
    70  	d.Set("name", n)
    71  	d.Set("color", githubLabel.Color)
    72  	d.Set("url", githubLabel.URL)
    73  
    74  	return nil
    75  }
    76  
    77  func resourceGithubIssueLabelUpdate(d *schema.ResourceData, meta interface{}) error {
    78  	client := meta.(*Organization).client
    79  	r := d.Get("repository").(string)
    80  	n := d.Get("name").(string)
    81  	c := d.Get("color").(string)
    82  
    83  	_, originalName := parseTwoPartID(d.Id())
    84  	_, _, err := client.Issues.EditLabel(meta.(*Organization).name, r, originalName, &github.Label{
    85  		Name:  &n,
    86  		Color: &c,
    87  	})
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	d.SetId(buildTwoPartID(&r, &n))
    93  
    94  	return resourceGithubIssueLabelRead(d, meta)
    95  }
    96  
    97  func resourceGithubIssueLabelDelete(d *schema.ResourceData, meta interface{}) error {
    98  	client := meta.(*Organization).client
    99  	r := d.Get("repository").(string)
   100  	n := d.Get("name").(string)
   101  
   102  	_, err := client.Issues.DeleteLabel(meta.(*Organization).name, r, n)
   103  	return err
   104  }