github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/packet/resource_packet_project.go (about)

     1  package packet
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/packethost/packngo"
    10  )
    11  
    12  func resourcePacketProject() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourcePacketProjectCreate,
    15  		Read:   resourcePacketProjectRead,
    16  		Update: resourcePacketProjectUpdate,
    17  		Delete: resourcePacketProjectDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"id": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Computed: true,
    23  			},
    24  
    25  			"name": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  			},
    29  
    30  			"payment_method": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Optional: true,
    33  			},
    34  
    35  			"created": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Computed: true,
    38  			},
    39  
    40  			"updated": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Computed: true,
    43  			},
    44  		},
    45  	}
    46  }
    47  
    48  func resourcePacketProjectCreate(d *schema.ResourceData, meta interface{}) error {
    49  	client := meta.(*packngo.Client)
    50  
    51  	createRequest := &packngo.ProjectCreateRequest{
    52  		Name:          d.Get("name").(string),
    53  		PaymentMethod: d.Get("payment_method").(string),
    54  	}
    55  
    56  	log.Printf("[DEBUG] Project create configuration: %#v", createRequest)
    57  	project, _, err := client.Projects.Create(createRequest)
    58  	if err != nil {
    59  		return fmt.Errorf("Error creating Project: %s", err)
    60  	}
    61  
    62  	d.SetId(project.ID)
    63  	log.Printf("[INFO] Project created: %s", project.ID)
    64  
    65  	return resourcePacketProjectRead(d, meta)
    66  }
    67  
    68  func resourcePacketProjectRead(d *schema.ResourceData, meta interface{}) error {
    69  	client := meta.(*packngo.Client)
    70  
    71  	key, _, err := client.Projects.Get(d.Id())
    72  	if err != nil {
    73  		// If the project somehow already destroyed, mark as
    74  		// succesfully gone
    75  		if strings.Contains(err.Error(), "404") {
    76  			d.SetId("")
    77  			return nil
    78  		}
    79  
    80  		return fmt.Errorf("Error retrieving Project: %s", err)
    81  	}
    82  
    83  	d.Set("id", key.ID)
    84  	d.Set("name", key.Name)
    85  	d.Set("created", key.Created)
    86  	d.Set("updated", key.Updated)
    87  
    88  	return nil
    89  }
    90  
    91  func resourcePacketProjectUpdate(d *schema.ResourceData, meta interface{}) error {
    92  	client := meta.(*packngo.Client)
    93  
    94  	updateRequest := &packngo.ProjectUpdateRequest{
    95  		ID:   d.Get("id").(string),
    96  		Name: d.Get("name").(string),
    97  	}
    98  
    99  	if attr, ok := d.GetOk("payment_method"); ok {
   100  		updateRequest.PaymentMethod = attr.(string)
   101  	}
   102  
   103  	log.Printf("[DEBUG] Project update: %#v", d.Get("id"))
   104  	_, _, err := client.Projects.Update(updateRequest)
   105  	if err != nil {
   106  		return fmt.Errorf("Failed to update Project: %s", err)
   107  	}
   108  
   109  	return resourcePacketProjectRead(d, meta)
   110  }
   111  
   112  func resourcePacketProjectDelete(d *schema.ResourceData, meta interface{}) error {
   113  	client := meta.(*packngo.Client)
   114  
   115  	log.Printf("[INFO] Deleting Project: %s", d.Id())
   116  	_, err := client.Projects.Delete(d.Id())
   117  	if err != nil {
   118  		return fmt.Errorf("Error deleting SSH key: %s", err)
   119  	}
   120  
   121  	d.SetId("")
   122  	return nil
   123  }