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

     1  package google
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"log"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  
    10  	"google.golang.org/api/storage/v1"
    11  )
    12  
    13  func resourceStorageBucket() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceStorageBucketCreate,
    16  		Read:   resourceStorageBucketRead,
    17  		Update: resourceStorageBucketUpdate,
    18  		Delete: resourceStorageBucketDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"name": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  			"predefined_acl": &schema.Schema{
    27  				Type:       schema.TypeString,
    28  				Deprecated: "Please use resource \"storage_bucket_acl.predefined_acl\" instead.",
    29  				Optional:   true,
    30  				ForceNew:   true,
    31  			},
    32  			"location": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Default:  "US",
    35  				Optional: true,
    36  				ForceNew: true,
    37  			},
    38  			"force_destroy": &schema.Schema{
    39  				Type:     schema.TypeBool,
    40  				Optional: true,
    41  				Default:  false,
    42  			},
    43  			"website": &schema.Schema{
    44  				Type:     schema.TypeList,
    45  				Optional: true,
    46  				Elem: &schema.Resource{
    47  					Schema: map[string]*schema.Schema{
    48  						"main_page_suffix": &schema.Schema{
    49  							Type:     schema.TypeString,
    50  							Optional: true,
    51  						},
    52  						"not_found_page": &schema.Schema{
    53  							Type:     schema.TypeString,
    54  							Optional: true,
    55  						},
    56  					},
    57  				},
    58  			},
    59  			"self_link": &schema.Schema{
    60  				Type:     schema.TypeString,
    61  				Computed: true,
    62  			},
    63  		},
    64  	}
    65  }
    66  
    67  func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error {
    68  	config := meta.(*Config)
    69  
    70  	// Get the bucket and acl
    71  	bucket := d.Get("name").(string)
    72  	location := d.Get("location").(string)
    73  
    74  	// Create a bucket, setting the acl, location and name.
    75  	sb := &storage.Bucket{Name: bucket, Location: location}
    76  
    77  	if v, ok := d.GetOk("website"); ok {
    78  		websites := v.([]interface{})
    79  
    80  		if len(websites) > 1 {
    81  			return fmt.Errorf("At most one website block is allowed")
    82  		}
    83  
    84  		sb.Website = &storage.BucketWebsite{}
    85  
    86  		website := websites[0].(map[string]interface{})
    87  
    88  		if v, ok := website["not_found_page"]; ok {
    89  			sb.Website.NotFoundPage = v.(string)
    90  		}
    91  
    92  		if v, ok := website["main_page_suffix"]; ok {
    93  			sb.Website.MainPageSuffix = v.(string)
    94  		}
    95  	}
    96  
    97  	call := config.clientStorage.Buckets.Insert(config.Project, sb)
    98  	if v, ok := d.GetOk("predefined_acl"); ok {
    99  		call = call.PredefinedAcl(v.(string))
   100  	}
   101  
   102  	res, err := call.Do()
   103  
   104  	if err != nil {
   105  		fmt.Printf("Error creating bucket %s: %v", bucket, err)
   106  		return err
   107  	}
   108  
   109  	log.Printf("[DEBUG] Created bucket %v at location %v\n\n", res.Name, res.SelfLink)
   110  
   111  	// Assign the bucket ID as the resource ID
   112  	d.Set("self_link", res.SelfLink)
   113  	d.SetId(res.Id)
   114  
   115  	return nil
   116  }
   117  
   118  func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error {
   119  	config := meta.(*Config)
   120  
   121  	sb := &storage.Bucket{}
   122  
   123  	if d.HasChange("website") {
   124  		if v, ok := d.GetOk("website"); ok {
   125  			websites := v.([]interface{})
   126  
   127  			if len(websites) > 1 {
   128  				return fmt.Errorf("At most one website block is allowed")
   129  			}
   130  
   131  			// Setting fields to "" to be explicit that the PATCH call will
   132  			// delete this field.
   133  			if len(websites) == 0 {
   134  				sb.Website.NotFoundPage = ""
   135  				sb.Website.MainPageSuffix = ""
   136  			} else {
   137  				website := websites[0].(map[string]interface{})
   138  				sb.Website = &storage.BucketWebsite{}
   139  				if v, ok := website["not_found_page"]; ok {
   140  					sb.Website.NotFoundPage = v.(string)
   141  				} else {
   142  					sb.Website.NotFoundPage = ""
   143  				}
   144  
   145  				if v, ok := website["main_page_suffix"]; ok {
   146  					sb.Website.MainPageSuffix = v.(string)
   147  				} else {
   148  					sb.Website.MainPageSuffix = ""
   149  				}
   150  			}
   151  		}
   152  	}
   153  
   154  	res, err := config.clientStorage.Buckets.Patch(d.Get("name").(string), sb).Do()
   155  
   156  	if err != nil {
   157  		return err
   158  	}
   159  
   160  	log.Printf("[DEBUG] Patched bucket %v at location %v\n\n", res.Name, res.SelfLink)
   161  
   162  	// Assign the bucket ID as the resource ID
   163  	d.Set("self_link", res.SelfLink)
   164  	d.SetId(res.Id)
   165  
   166  	return nil
   167  }
   168  
   169  func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error {
   170  	config := meta.(*Config)
   171  
   172  	// Get the bucket and acl
   173  	bucket := d.Get("name").(string)
   174  	res, err := config.clientStorage.Buckets.Get(bucket).Do()
   175  
   176  	if err != nil {
   177  		fmt.Printf("Error reading bucket %s: %v", bucket, err)
   178  		return err
   179  	}
   180  
   181  	log.Printf("[DEBUG] Read bucket %v at location %v\n\n", res.Name, res.SelfLink)
   182  
   183  	// Update the bucket ID according to the resource ID
   184  	d.Set("self_link", res.SelfLink)
   185  	d.SetId(res.Id)
   186  
   187  	return nil
   188  }
   189  
   190  func resourceStorageBucketDelete(d *schema.ResourceData, meta interface{}) error {
   191  	config := meta.(*Config)
   192  
   193  	// Get the bucket
   194  	bucket := d.Get("name").(string)
   195  
   196  	for {
   197  		res, err := config.clientStorage.Objects.List(bucket).Do()
   198  		if err != nil {
   199  			fmt.Printf("Error Objects.List failed: %v", err)
   200  			return err
   201  		}
   202  
   203  		if len(res.Items) != 0 {
   204  			if d.Get("force_destroy").(bool) {
   205  				// purge the bucket...
   206  				log.Printf("[DEBUG] GCS Bucket attempting to forceDestroy\n\n")
   207  
   208  				for _, object := range res.Items {
   209  					log.Printf("[DEBUG] Found %s", object.Name)
   210  					if err := config.clientStorage.Objects.Delete(bucket, object.Name).Do(); err != nil {
   211  						log.Fatalf("Error trying to delete object: %s %s\n\n", object.Name, err)
   212  					} else {
   213  						log.Printf("Object deleted: %s \n\n", object.Name)
   214  					}
   215  				}
   216  
   217  			} else {
   218  				delete_err := errors.New("Error trying to delete a bucket containing objects without `force_destroy` set to true")
   219  				log.Printf("Error! %s : %s\n\n", bucket, delete_err)
   220  				return delete_err
   221  			}
   222  		} else {
   223  			break // 0 items, bucket empty
   224  		}
   225  	}
   226  
   227  	// remove empty bucket
   228  	err := config.clientStorage.Buckets.Delete(bucket).Do()
   229  	if err != nil {
   230  		fmt.Printf("Error deleting bucket %s: %v\n\n", bucket, err)
   231  		return err
   232  	}
   233  	log.Printf("[DEBUG] Deleted bucket %v\n\n", bucket)
   234  
   235  	return nil
   236  }