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

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  
     9  	"google.golang.org/api/storage/v1"
    10  )
    11  
    12  func resourceStorageBucketObject() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceStorageBucketObjectCreate,
    15  		Read:   resourceStorageBucketObjectRead,
    16  		Delete: resourceStorageBucketObjectDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"bucket": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  			"name": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  			"source": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  				ForceNew: true,
    33  			},
    34  			"predefined_acl": &schema.Schema{
    35  				Type:       schema.TypeString,
    36  				Deprecated: "Please use resource \"storage_object_acl.predefined_acl\" instead.",
    37  				Optional:   true,
    38  				ForceNew:   true,
    39  			},
    40  			"md5hash": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Computed: true,
    43  			},
    44  			"crc32c": &schema.Schema{
    45  				Type:     schema.TypeString,
    46  				Computed: true,
    47  			},
    48  		},
    49  	}
    50  }
    51  
    52  func objectGetId(object *storage.Object) string {
    53  	return object.Bucket + "-" + object.Name
    54  }
    55  
    56  func resourceStorageBucketObjectCreate(d *schema.ResourceData, meta interface{}) error {
    57  	config := meta.(*Config)
    58  
    59  	bucket := d.Get("bucket").(string)
    60  	name := d.Get("name").(string)
    61  	source := d.Get("source").(string)
    62  
    63  	file, err := os.Open(source)
    64  	if err != nil {
    65  		return fmt.Errorf("Error opening %s: %s", source, err)
    66  	}
    67  
    68  	objectsService := storage.NewObjectsService(config.clientStorage)
    69  	object := &storage.Object{Bucket: bucket}
    70  
    71  	insertCall := objectsService.Insert(bucket, object)
    72  	insertCall.Name(name)
    73  	insertCall.Media(file)
    74  	if v, ok := d.GetOk("predefined_acl"); ok {
    75  		insertCall.PredefinedAcl(v.(string))
    76  	}
    77  
    78  	_, err = insertCall.Do()
    79  
    80  	if err != nil {
    81  		return fmt.Errorf("Error uploading contents of object %s from %s: %s", name, source, err)
    82  	}
    83  
    84  	return resourceStorageBucketObjectRead(d, meta)
    85  }
    86  
    87  func resourceStorageBucketObjectRead(d *schema.ResourceData, meta interface{}) error {
    88  	config := meta.(*Config)
    89  
    90  	bucket := d.Get("bucket").(string)
    91  	name := d.Get("name").(string)
    92  
    93  	objectsService := storage.NewObjectsService(config.clientStorage)
    94  	getCall := objectsService.Get(bucket, name)
    95  
    96  	res, err := getCall.Do()
    97  
    98  	if err != nil {
    99  		return fmt.Errorf("Error retrieving contents of object %s: %s", name, err)
   100  	}
   101  
   102  	d.Set("md5hash", res.Md5Hash)
   103  	d.Set("crc32c", res.Crc32c)
   104  
   105  	d.SetId(objectGetId(res))
   106  
   107  	return nil
   108  }
   109  
   110  func resourceStorageBucketObjectDelete(d *schema.ResourceData, meta interface{}) error {
   111  	config := meta.(*Config)
   112  
   113  	bucket := d.Get("bucket").(string)
   114  	name := d.Get("name").(string)
   115  
   116  	objectsService := storage.NewObjectsService(config.clientStorage)
   117  
   118  	DeleteCall := objectsService.Delete(bucket, name)
   119  	err := DeleteCall.Do()
   120  
   121  	if err != nil {
   122  		return fmt.Errorf("Error deleting contents of object %s: %s", name, err)
   123  	}
   124  
   125  	return nil
   126  }