github.com/dougneal/terraform@v0.6.15-0.20170330092735-b6a3840768a4/builtin/providers/github/resource_github_issue_label.go (about)

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