github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/builtin/providers/github/resource_github_issue_label.go (about)

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