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

     1  package docker
     2  
     3  import (
     4  	"fmt"
     5  
     6  	dc "github.com/fsouza/go-dockerclient"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourceDockerVolume() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourceDockerVolumeCreate,
    13  		Read:   resourceDockerVolumeRead,
    14  		Delete: resourceDockerVolumeDelete,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"name": &schema.Schema{
    18  				Type:     schema.TypeString,
    19  				Optional: true,
    20  				Computed: true,
    21  				ForceNew: true,
    22  			},
    23  			"driver": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Optional: true,
    26  				Computed: true,
    27  				ForceNew: true,
    28  			},
    29  			"driver_opts": &schema.Schema{
    30  				Type:     schema.TypeMap,
    31  				Optional: true,
    32  				ForceNew: true,
    33  			},
    34  			"mountpoint": &schema.Schema{
    35  				Type:     schema.TypeString,
    36  				Computed: true,
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  func resourceDockerVolumeCreate(d *schema.ResourceData, meta interface{}) error {
    43  	client := meta.(*dc.Client)
    44  
    45  	createOpts := dc.CreateVolumeOptions{}
    46  	if v, ok := d.GetOk("name"); ok {
    47  		createOpts.Name = v.(string)
    48  	}
    49  	if v, ok := d.GetOk("driver"); ok {
    50  		createOpts.Driver = v.(string)
    51  	}
    52  	if v, ok := d.GetOk("driver_opts"); ok {
    53  		createOpts.DriverOpts = mapTypeMapValsToString(v.(map[string]interface{}))
    54  	}
    55  
    56  	var err error
    57  	var retVolume *dc.Volume
    58  	if retVolume, err = client.CreateVolume(createOpts); err != nil {
    59  		return fmt.Errorf("Unable to create volume: %s", err)
    60  	}
    61  	if retVolume == nil {
    62  		return fmt.Errorf("Returned volume is nil")
    63  	}
    64  
    65  	d.SetId(retVolume.Name)
    66  	d.Set("name", retVolume.Name)
    67  	d.Set("driver", retVolume.Driver)
    68  	d.Set("mountpoint", retVolume.Mountpoint)
    69  
    70  	return nil
    71  }
    72  
    73  func resourceDockerVolumeRead(d *schema.ResourceData, meta interface{}) error {
    74  	client := meta.(*dc.Client)
    75  
    76  	var err error
    77  	var retVolume *dc.Volume
    78  	if retVolume, err = client.InspectVolume(d.Id()); err != nil && err != dc.ErrNoSuchVolume {
    79  		return fmt.Errorf("Unable to inspect volume: %s", err)
    80  	}
    81  	if retVolume == nil {
    82  		d.SetId("")
    83  		return nil
    84  	}
    85  
    86  	d.Set("name", retVolume.Name)
    87  	d.Set("driver", retVolume.Driver)
    88  	d.Set("mountpoint", retVolume.Mountpoint)
    89  
    90  	return nil
    91  }
    92  
    93  func resourceDockerVolumeDelete(d *schema.ResourceData, meta interface{}) error {
    94  	client := meta.(*dc.Client)
    95  
    96  	if err := client.RemoveVolume(d.Id()); err != nil && err != dc.ErrNoSuchVolume {
    97  		return fmt.Errorf("Error deleting volume %s: %s", d.Id(), err)
    98  	}
    99  
   100  	d.SetId("")
   101  	return nil
   102  }