github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/heroku/resource_heroku_pipeline_coupling.go (about)

     1  package heroku
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  
     8  	"github.com/cyberdelia/heroku-go/v3"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceHerokuPipelineCoupling() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceHerokuPipelineCouplingCreate,
    15  		Read:   resourceHerokuPipelineCouplingRead,
    16  		Delete: resourceHerokuPipelineCouplingDelete,
    17  
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"app_id": {
    24  				Type:     schema.TypeString,
    25  				Computed: true,
    26  			},
    27  			"app": {
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  				ForceNew: true,
    31  			},
    32  			"pipeline": {
    33  				Type:         schema.TypeString,
    34  				Required:     true,
    35  				ForceNew:     true,
    36  				ValidateFunc: validateUUID,
    37  			},
    38  			"stage": {
    39  				Type:         schema.TypeString,
    40  				Required:     true,
    41  				ForceNew:     true,
    42  				ValidateFunc: validatePipelineStageName,
    43  			},
    44  		},
    45  	}
    46  }
    47  
    48  func resourceHerokuPipelineCouplingCreate(d *schema.ResourceData, meta interface{}) error {
    49  	client := meta.(*heroku.Service)
    50  
    51  	opts := heroku.PipelineCouplingCreateOpts{
    52  		App:      d.Get("app").(string),
    53  		Pipeline: d.Get("pipeline").(string),
    54  		Stage:    d.Get("stage").(string),
    55  	}
    56  
    57  	log.Printf("[DEBUG] PipelineCoupling create configuration: %#v", opts)
    58  
    59  	p, err := client.PipelineCouplingCreate(context.TODO(), opts)
    60  	if err != nil {
    61  		return fmt.Errorf("Error creating pipeline: %s", err)
    62  	}
    63  
    64  	d.SetId(p.ID)
    65  
    66  	log.Printf("[INFO] PipelineCoupling ID: %s", d.Id())
    67  
    68  	return resourceHerokuPipelineCouplingRead(d, meta)
    69  }
    70  
    71  func resourceHerokuPipelineCouplingDelete(d *schema.ResourceData, meta interface{}) error {
    72  	client := meta.(*heroku.Service)
    73  
    74  	log.Printf("[INFO] Deleting pipeline: %s", d.Id())
    75  
    76  	_, err := client.PipelineCouplingDelete(context.TODO(), d.Id())
    77  	if err != nil {
    78  		return fmt.Errorf("Error deleting pipeline: %s", err)
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  func resourceHerokuPipelineCouplingRead(d *schema.ResourceData, meta interface{}) error {
    85  	client := meta.(*heroku.Service)
    86  
    87  	p, err := client.PipelineCouplingInfo(context.TODO(), d.Id())
    88  	if err != nil {
    89  		return fmt.Errorf("Error retrieving pipeline: %s", err)
    90  	}
    91  
    92  	// grab App info
    93  	app, err := client.AppInfo(context.TODO(), p.App.ID)
    94  	if err != nil {
    95  		log.Printf("[WARN] Error looking up addional App info for pipeline coupling (%s): %s", d.Id(), err)
    96  	} else {
    97  		d.Set("app", app.Name)
    98  	}
    99  
   100  	d.Set("app_id", p.App.ID)
   101  	d.Set("stage", p.Stage)
   102  	d.Set("pipeline", p.Pipeline.ID)
   103  
   104  	return nil
   105  }