github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/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: resourceGithubIssueLabelCreateOrUpdate,
    14  		Read:   resourceGithubIssueLabelRead,
    15  		Update: resourceGithubIssueLabelCreateOrUpdate,
    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  // resourceGithubIssueLabelCreateOrUpdate idempotently creates or updates an
    44  // issue label. Issue labels are keyed off of their "name", so pre-existing
    45  // issue labels result in a 422 HTTP error if they exist outside of Terraform.
    46  // Normally this would not be an issue, except new repositories are created with
    47  // a "default" set of labels, and those labels easily conflict with custom ones.
    48  //
    49  // This function will first check if the label exists, and then issue an update,
    50  // otherwise it will create. This is also advantageous in that we get to use the
    51  // same function for two schema funcs.
    52  
    53  func resourceGithubIssueLabelCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
    54  	client := meta.(*Organization).client
    55  	o := meta.(*Organization).name
    56  	r := d.Get("repository").(string)
    57  	n := d.Get("name").(string)
    58  	c := d.Get("color").(string)
    59  
    60  	label := &github.Label{
    61  		Name:  &n,
    62  		Color: &c,
    63  	}
    64  
    65  	log.Printf("[DEBUG] Querying label existence %s/%s (%s)", o, r, n)
    66  	existing, _, _ := client.Issues.GetLabel(context.TODO(), o, r, n)
    67  
    68  	if existing != nil {
    69  		log.Printf("[DEBUG] Updating label: %s/%s (%s: %s)", o, r, n, c)
    70  
    71  		// Pull out the original name. If we already have a resource, this is the
    72  		// parsed ID. If not, it's the value given to the resource.
    73  		var oname string
    74  		if d.Id() == "" {
    75  			oname = n
    76  		} else {
    77  			_, oname = parseTwoPartID(d.Id())
    78  		}
    79  
    80  		_, _, err := client.Issues.EditLabel(context.TODO(), o, r, oname, label)
    81  		if err != nil {
    82  			return err
    83  		}
    84  	} else {
    85  		log.Printf("[DEBUG] Creating label: %s/%s (%s: %s)", o, r, n, c)
    86  		_, resp, err := client.Issues.CreateLabel(context.TODO(), o, r, label)
    87  		log.Printf("[DEBUG] Response from creating label: %s", *resp)
    88  		if err != nil {
    89  			return err
    90  		}
    91  	}
    92  
    93  	d.SetId(buildTwoPartID(&r, &n))
    94  
    95  	return resourceGithubIssueLabelRead(d, meta)
    96  }
    97  
    98  func resourceGithubIssueLabelRead(d *schema.ResourceData, meta interface{}) error {
    99  	client := meta.(*Organization).client
   100  	r, n := parseTwoPartID(d.Id())
   101  
   102  	log.Printf("[DEBUG] Reading label: %s/%s", r, n)
   103  	githubLabel, _, err := client.Issues.GetLabel(context.TODO(), meta.(*Organization).name, r, n)
   104  	if err != nil {
   105  		d.SetId("")
   106  		return nil
   107  	}
   108  
   109  	d.Set("repository", r)
   110  	d.Set("name", n)
   111  	d.Set("color", githubLabel.Color)
   112  	d.Set("url", githubLabel.URL)
   113  
   114  	return nil
   115  }
   116  
   117  func resourceGithubIssueLabelDelete(d *schema.ResourceData, meta interface{}) error {
   118  	client := meta.(*Organization).client
   119  	r := d.Get("repository").(string)
   120  	n := d.Get("name").(string)
   121  
   122  	log.Printf("[DEBUG] Deleting label: %s/%s", r, n)
   123  	_, err := client.Issues.DeleteLabel(context.TODO(), meta.(*Organization).name, r, n)
   124  	return err
   125  }