github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/packet/resource_packet_project.go (about)

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