github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/google/resource_storage_bucket_object.go (about)

     1  package google
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"log"
     8  	"os"
     9  
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  
    12  	"google.golang.org/api/googleapi"
    13  	"google.golang.org/api/storage/v1"
    14  )
    15  
    16  func resourceStorageBucketObject() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceStorageBucketObjectCreate,
    19  		Read:   resourceStorageBucketObjectRead,
    20  		Delete: resourceStorageBucketObjectDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"bucket": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"name": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  				ForceNew: true,
    33  			},
    34  
    35  			"content": &schema.Schema{
    36  				Type:          schema.TypeString,
    37  				Optional:      true,
    38  				ForceNew:      true,
    39  				ConflictsWith: []string{"source"},
    40  			},
    41  
    42  			"crc32c": &schema.Schema{
    43  				Type:     schema.TypeString,
    44  				Computed: true,
    45  			},
    46  
    47  			"md5hash": &schema.Schema{
    48  				Type:     schema.TypeString,
    49  				Computed: true,
    50  			},
    51  
    52  			"predefined_acl": &schema.Schema{
    53  				Type:       schema.TypeString,
    54  				Deprecated: "Please use resource \"storage_object_acl.predefined_acl\" instead.",
    55  				Optional:   true,
    56  				ForceNew:   true,
    57  			},
    58  
    59  			"source": &schema.Schema{
    60  				Type:          schema.TypeString,
    61  				Optional:      true,
    62  				ForceNew:      true,
    63  				ConflictsWith: []string{"content"},
    64  			},
    65  		},
    66  	}
    67  }
    68  
    69  func objectGetId(object *storage.Object) string {
    70  	return object.Bucket + "-" + object.Name
    71  }
    72  
    73  func resourceStorageBucketObjectCreate(d *schema.ResourceData, meta interface{}) error {
    74  	config := meta.(*Config)
    75  
    76  	bucket := d.Get("bucket").(string)
    77  	name := d.Get("name").(string)
    78  	var media io.Reader
    79  
    80  	if v, ok := d.GetOk("source"); ok {
    81  		err := error(nil)
    82  		media, err = os.Open(v.(string))
    83  		if err != nil {
    84  			return err
    85  		}
    86  	} else if v, ok := d.GetOk("content"); ok {
    87  		media = bytes.NewReader([]byte(v.(string)))
    88  	} else {
    89  		return fmt.Errorf("Error, either \"content\" or \"string\" must be specified")
    90  	}
    91  
    92  	objectsService := storage.NewObjectsService(config.clientStorage)
    93  	object := &storage.Object{Bucket: bucket}
    94  
    95  	insertCall := objectsService.Insert(bucket, object)
    96  	insertCall.Name(name)
    97  	insertCall.Media(media)
    98  	if v, ok := d.GetOk("predefined_acl"); ok {
    99  		insertCall.PredefinedAcl(v.(string))
   100  	}
   101  
   102  	_, err := insertCall.Do()
   103  
   104  	if err != nil {
   105  		return fmt.Errorf("Error uploading object %s: %s", name, err)
   106  	}
   107  
   108  	return resourceStorageBucketObjectRead(d, meta)
   109  }
   110  
   111  func resourceStorageBucketObjectRead(d *schema.ResourceData, meta interface{}) error {
   112  	config := meta.(*Config)
   113  
   114  	bucket := d.Get("bucket").(string)
   115  	name := d.Get("name").(string)
   116  
   117  	objectsService := storage.NewObjectsService(config.clientStorage)
   118  	getCall := objectsService.Get(bucket, name)
   119  
   120  	res, err := getCall.Do()
   121  
   122  	if err != nil {
   123  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   124  			log.Printf("[WARN] Removing Bucket Object %q because it's gone", d.Get("name").(string))
   125  			// The resource doesn't exist anymore
   126  			d.SetId("")
   127  
   128  			return nil
   129  		}
   130  
   131  		return fmt.Errorf("Error retrieving contents of object %s: %s", name, err)
   132  	}
   133  
   134  	d.Set("md5hash", res.Md5Hash)
   135  	d.Set("crc32c", res.Crc32c)
   136  
   137  	d.SetId(objectGetId(res))
   138  
   139  	return nil
   140  }
   141  
   142  func resourceStorageBucketObjectDelete(d *schema.ResourceData, meta interface{}) error {
   143  	config := meta.(*Config)
   144  
   145  	bucket := d.Get("bucket").(string)
   146  	name := d.Get("name").(string)
   147  
   148  	objectsService := storage.NewObjectsService(config.clientStorage)
   149  
   150  	DeleteCall := objectsService.Delete(bucket, name)
   151  	err := DeleteCall.Do()
   152  
   153  	if err != nil {
   154  		return fmt.Errorf("Error deleting contents of object %s: %s", name, err)
   155  	}
   156  
   157  	return nil
   158  }