github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/openstack/resource_openstack_objectstorage_container_v1.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourceObjectStorageContainerV1() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceObjectStorageContainerV1Create,
    14  		Read:   resourceObjectStorageContainerV1Read,
    15  		Update: resourceObjectStorageContainerV1Update,
    16  		Delete: resourceObjectStorageContainerV1Delete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"region": &schema.Schema{
    20  				Type:        schema.TypeString,
    21  				Required:    true,
    22  				ForceNew:    true,
    23  				DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""),
    24  			},
    25  			"name": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: false,
    29  			},
    30  			"container_read": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Optional: true,
    33  				ForceNew: false,
    34  			},
    35  			"container_sync_to": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Optional: true,
    38  				ForceNew: false,
    39  			},
    40  			"container_sync_key": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Optional: true,
    43  				ForceNew: false,
    44  			},
    45  			"container_write": &schema.Schema{
    46  				Type:     schema.TypeString,
    47  				Optional: true,
    48  				ForceNew: false,
    49  			},
    50  			"content_type": &schema.Schema{
    51  				Type:     schema.TypeString,
    52  				Optional: true,
    53  				ForceNew: false,
    54  			},
    55  			"metadata": &schema.Schema{
    56  				Type:     schema.TypeMap,
    57  				Optional: true,
    58  				ForceNew: false,
    59  			},
    60  		},
    61  	}
    62  }
    63  
    64  func resourceObjectStorageContainerV1Create(d *schema.ResourceData, meta interface{}) error {
    65  	config := meta.(*Config)
    66  	objectStorageClient, err := config.objectStorageV1Client(GetRegion(d))
    67  	if err != nil {
    68  		return fmt.Errorf("Error creating OpenStack object storage client: %s", err)
    69  	}
    70  
    71  	cn := d.Get("name").(string)
    72  
    73  	createOpts := &containers.CreateOpts{
    74  		ContainerRead:    d.Get("container_read").(string),
    75  		ContainerSyncTo:  d.Get("container_sync_to").(string),
    76  		ContainerSyncKey: d.Get("container_sync_key").(string),
    77  		ContainerWrite:   d.Get("container_write").(string),
    78  		ContentType:      d.Get("content_type").(string),
    79  		Metadata:         resourceContainerMetadataV2(d),
    80  	}
    81  
    82  	log.Printf("[DEBUG] Create Options: %#v", createOpts)
    83  	_, err = containers.Create(objectStorageClient, cn, createOpts).Extract()
    84  	if err != nil {
    85  		return fmt.Errorf("Error creating OpenStack container: %s", err)
    86  	}
    87  	log.Printf("[INFO] Container ID: %s", cn)
    88  
    89  	// Store the ID now
    90  	d.SetId(cn)
    91  
    92  	return resourceObjectStorageContainerV1Read(d, meta)
    93  }
    94  
    95  func resourceObjectStorageContainerV1Read(d *schema.ResourceData, meta interface{}) error {
    96  	return nil
    97  }
    98  
    99  func resourceObjectStorageContainerV1Update(d *schema.ResourceData, meta interface{}) error {
   100  	config := meta.(*Config)
   101  	objectStorageClient, err := config.objectStorageV1Client(GetRegion(d))
   102  	if err != nil {
   103  		return fmt.Errorf("Error creating OpenStack object storage client: %s", err)
   104  	}
   105  
   106  	updateOpts := containers.UpdateOpts{
   107  		ContainerRead:    d.Get("container_read").(string),
   108  		ContainerSyncTo:  d.Get("container_sync_to").(string),
   109  		ContainerSyncKey: d.Get("container_sync_key").(string),
   110  		ContainerWrite:   d.Get("container_write").(string),
   111  		ContentType:      d.Get("content_type").(string),
   112  	}
   113  
   114  	if d.HasChange("metadata") {
   115  		updateOpts.Metadata = resourceContainerMetadataV2(d)
   116  	}
   117  
   118  	_, err = containers.Update(objectStorageClient, d.Id(), updateOpts).Extract()
   119  	if err != nil {
   120  		return fmt.Errorf("Error updating OpenStack container: %s", err)
   121  	}
   122  
   123  	return resourceObjectStorageContainerV1Read(d, meta)
   124  }
   125  
   126  func resourceObjectStorageContainerV1Delete(d *schema.ResourceData, meta interface{}) error {
   127  	config := meta.(*Config)
   128  	objectStorageClient, err := config.objectStorageV1Client(GetRegion(d))
   129  	if err != nil {
   130  		return fmt.Errorf("Error creating OpenStack object storage client: %s", err)
   131  	}
   132  
   133  	_, err = containers.Delete(objectStorageClient, d.Id()).Extract()
   134  	if err != nil {
   135  		return fmt.Errorf("Error deleting OpenStack container: %s", err)
   136  	}
   137  
   138  	d.SetId("")
   139  	return nil
   140  }
   141  
   142  func resourceContainerMetadataV2(d *schema.ResourceData) map[string]string {
   143  	m := make(map[string]string)
   144  	for key, val := range d.Get("metadata").(map[string]interface{}) {
   145  		m[key] = val.(string)
   146  	}
   147  	return m
   148  }