github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/opc/resource_storage_volume.go (about)

     1  package opc
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/hashicorp/go-oracle-terraform/compute"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/hashicorp/terraform/helper/validation"
    10  )
    11  
    12  func resourceOPCStorageVolume() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceOPCStorageVolumeCreate,
    15  		Read:   resourceOPCStorageVolumeRead,
    16  		Update: resourceOPCStorageVolumeUpdate,
    17  		Delete: resourceOPCStorageVolumeDelete,
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": {
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  			"description": {
    29  				Type:     schema.TypeString,
    30  				Optional: true,
    31  			},
    32  			"size": {
    33  				Type:         schema.TypeInt,
    34  				Required:     true,
    35  				ValidateFunc: validation.IntBetween(1, 2048),
    36  			},
    37  			"storage_type": {
    38  				Type:     schema.TypeString,
    39  				Optional: true,
    40  				ForceNew: true,
    41  				Default:  compute.StorageVolumeKindDefault,
    42  				ValidateFunc: validation.StringInSlice([]string{
    43  					string(compute.StorageVolumeKindDefault),
    44  					string(compute.StorageVolumeKindLatency),
    45  				}, true),
    46  			},
    47  
    48  			"snapshot": {
    49  				Type:     schema.TypeString,
    50  				Optional: true,
    51  				ForceNew: true,
    52  				Computed: true,
    53  			},
    54  
    55  			"snapshot_id": {
    56  				Type:     schema.TypeString,
    57  				Optional: true,
    58  				Computed: true,
    59  				ForceNew: true,
    60  			},
    61  
    62  			"snapshot_account": {
    63  				Type:     schema.TypeString,
    64  				Optional: true,
    65  				ForceNew: true,
    66  			},
    67  
    68  			"bootable": {
    69  				Type:     schema.TypeBool,
    70  				Optional: true,
    71  				Default:  false,
    72  				ForceNew: true,
    73  			},
    74  
    75  			"image_list": {
    76  				Type:     schema.TypeString,
    77  				Optional: true,
    78  				ForceNew: true,
    79  			},
    80  
    81  			"image_list_entry": {
    82  				Type:     schema.TypeInt,
    83  				Optional: true,
    84  				ForceNew: true,
    85  				Default:  -1,
    86  			},
    87  
    88  			"tags": tagsOptionalSchema(),
    89  
    90  			// Computed fields
    91  			"hypervisor": {
    92  				Type:     schema.TypeString,
    93  				Optional: true,
    94  				Computed: true,
    95  			},
    96  			"machine_image": {
    97  				Type:     schema.TypeString,
    98  				Optional: true,
    99  				Computed: true,
   100  			},
   101  			"managed": {
   102  				Type:     schema.TypeBool,
   103  				Optional: true,
   104  				Computed: true,
   105  			},
   106  			"platform": {
   107  				Type:     schema.TypeString,
   108  				Optional: true,
   109  				Computed: true,
   110  			},
   111  			"readonly": {
   112  				Type:     schema.TypeBool,
   113  				Optional: true,
   114  				Computed: true,
   115  			},
   116  			"status": {
   117  				Type:     schema.TypeString,
   118  				Optional: true,
   119  				Computed: true,
   120  			},
   121  			"storage_pool": {
   122  				Type:     schema.TypeString,
   123  				Optional: true,
   124  				Computed: true,
   125  			},
   126  			"uri": {
   127  				Type:     schema.TypeString,
   128  				Optional: true,
   129  				Computed: true,
   130  			},
   131  		},
   132  	}
   133  }
   134  
   135  func resourceOPCStorageVolumeCreate(d *schema.ResourceData, meta interface{}) error {
   136  	client := meta.(*compute.Client).StorageVolumes()
   137  
   138  	name := d.Get("name").(string)
   139  	description := d.Get("description").(string)
   140  	size := d.Get("size").(int)
   141  	storageType := d.Get("storage_type").(string)
   142  	bootable := d.Get("bootable").(bool)
   143  	imageList := d.Get("image_list").(string)
   144  	imageListEntry := d.Get("image_list_entry").(int)
   145  
   146  	if bootable == true {
   147  		if imageList == "" {
   148  			return fmt.Errorf("Error: A Bootable Volume must have an Image List!")
   149  		}
   150  
   151  		if imageListEntry == -1 {
   152  			return fmt.Errorf("Error: A Bootable Volume must have an Image List Entry!")
   153  		}
   154  	}
   155  
   156  	input := compute.CreateStorageVolumeInput{
   157  		Name:           name,
   158  		Description:    description,
   159  		Size:           strconv.Itoa(size),
   160  		Properties:     []string{storageType},
   161  		Bootable:       bootable,
   162  		ImageList:      imageList,
   163  		ImageListEntry: imageListEntry,
   164  		Tags:           getStringList(d, "tags"),
   165  	}
   166  
   167  	if v, ok := d.GetOk("snapshot"); ok {
   168  		input.Snapshot = v.(string)
   169  	}
   170  	if v, ok := d.GetOk("snapshot_account"); ok {
   171  		input.SnapshotAccount = v.(string)
   172  	}
   173  	if v, ok := d.GetOk("snapshot_id"); ok {
   174  		input.SnapshotID = v.(string)
   175  	}
   176  
   177  	info, err := client.CreateStorageVolume(&input)
   178  	if err != nil {
   179  		return fmt.Errorf("Error creating storage volume %s: %s", name, err)
   180  	}
   181  
   182  	d.SetId(info.Name)
   183  	return resourceOPCStorageVolumeRead(d, meta)
   184  }
   185  
   186  func resourceOPCStorageVolumeUpdate(d *schema.ResourceData, meta interface{}) error {
   187  	client := meta.(*compute.Client).StorageVolumes()
   188  
   189  	name := d.Id()
   190  	description := d.Get("description").(string)
   191  	size := d.Get("size").(int)
   192  	storageType := d.Get("storage_type").(string)
   193  	imageList := d.Get("image_list").(string)
   194  	imageListEntry := d.Get("image_list_entry").(int)
   195  
   196  	input := compute.UpdateStorageVolumeInput{
   197  		Name:           name,
   198  		Description:    description,
   199  		Size:           strconv.Itoa(size),
   200  		Properties:     []string{storageType},
   201  		ImageList:      imageList,
   202  		ImageListEntry: imageListEntry,
   203  		Tags:           getStringList(d, "tags"),
   204  	}
   205  	_, err := client.UpdateStorageVolume(&input)
   206  	if err != nil {
   207  		return fmt.Errorf("Error updating storage volume %s: %s", name, err)
   208  	}
   209  
   210  	return resourceOPCStorageVolumeRead(d, meta)
   211  }
   212  
   213  func resourceOPCStorageVolumeRead(d *schema.ResourceData, meta interface{}) error {
   214  	sv := meta.(*compute.Client).StorageVolumes()
   215  
   216  	name := d.Id()
   217  	input := compute.GetStorageVolumeInput{
   218  		Name: name,
   219  	}
   220  	result, err := sv.GetStorageVolume(&input)
   221  	if err != nil {
   222  		// Volume doesn't exist
   223  		if compute.WasNotFoundError(err) {
   224  			d.SetId("")
   225  			return nil
   226  		}
   227  		return fmt.Errorf("Error reading storage volume %s: %s", name, err)
   228  	}
   229  
   230  	if result == nil {
   231  		// Volume doesn't exist
   232  		d.SetId("")
   233  		return nil
   234  	}
   235  
   236  	d.Set("name", result.Name)
   237  	d.Set("description", result.Description)
   238  	d.Set("storage_type", result.Properties[0])
   239  	size, err := strconv.Atoi(result.Size)
   240  	if err != nil {
   241  		return err
   242  	}
   243  	d.Set("size", size)
   244  	d.Set("bootable", result.Bootable)
   245  	d.Set("image_list", result.ImageList)
   246  	d.Set("image_list_entry", result.ImageListEntry)
   247  
   248  	d.Set("snapshot", result.Snapshot)
   249  	d.Set("snapshot_id", result.SnapshotID)
   250  	d.Set("snapshot_account", result.SnapshotAccount)
   251  
   252  	if err := setStringList(d, "tags", result.Tags); err != nil {
   253  		return err
   254  	}
   255  
   256  	flattenOPCStorageVolumeComputedFields(d, result)
   257  
   258  	return nil
   259  }
   260  
   261  func resourceOPCStorageVolumeDelete(d *schema.ResourceData, meta interface{}) error {
   262  	client := meta.(*compute.Client).StorageVolumes()
   263  	name := d.Id()
   264  
   265  	input := compute.DeleteStorageVolumeInput{
   266  		Name: name,
   267  	}
   268  	err := client.DeleteStorageVolume(&input)
   269  	if err != nil {
   270  		return fmt.Errorf("Error deleting storage volume %s: %s", name, err)
   271  	}
   272  
   273  	return nil
   274  }
   275  
   276  func flattenOPCStorageVolumeComputedFields(d *schema.ResourceData, result *compute.StorageVolumeInfo) {
   277  	d.Set("hypervisor", result.Hypervisor)
   278  	d.Set("machine_image", result.MachineImage)
   279  	d.Set("managed", result.Managed)
   280  	d.Set("platform", result.Platform)
   281  	d.Set("readonly", result.ReadOnly)
   282  	d.Set("status", result.Status)
   283  	d.Set("storage_pool", result.StoragePool)
   284  	d.Set("uri", result.URI)
   285  }