terraform-provider-greenhost/digitalocean/resource_digitalocean_volum...

136 lines
3.4 KiB
Go
Raw Normal View History

package digitalocean
import (
"context"
"fmt"
"log"
"github.com/digitalocean/godo"
upgrade provider to use terraform-plugin-sdk v2 (#492) * upgrade terraform-plugin-sdk and `go mod vendor` * Update digitalocean/datasource_digitalocean_image_test.go Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * Update digitalocean/datasource_digitalocean_kubernetes_cluster_test.go Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * Update digitalocean/datasource_digitalocean_vpc_test.go Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * Update digitalocean/datasource_digitalocean_vpc_test.go Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> * go fmt * fix droplet_id to be of the right type * fix digitalocean_project resource * fix creation order in digitalocean_certificate test * fix digitalocean_container_registry data source tes * Port new changes to v2. * Port all tests to resource.ParallelTest * Fix KubernetesProviderInteroperability test. * Fix TestAccDigitalOceanKubernetesCluster_UpgradeVersion * Fix firewall panic s/create_at/created_at/ * Fix TestAccDigitalOceanDroplet_Basic: Droplets now have private networking by default. * Fix TestAccDataSourceDigitalOceanDomain_Basic * Fix TestAccDataSourceDigitalOceanDropletSnapshot tests. * Fix TestAccDataSourceDigitalOceanSSHKey_Basic * Fix TestAccDataSourceDigitalOceanVolumeSnapshot tests. * Fix TestAccDataSourceDigitalOceanVolume tests. * Fix TestAccDataSourceDigitalOceanRecord_Basic * Fix TestAccDataSourceDigitalOceanProject_NonDefaultProject * Fix TestAccDigitalOceanImage_PublicSlug * Fix TestAccDataSourceDigitalOceanImages_Basic via bug in imageSchema() * go mod tidy * Fix TestAccDataSourceDigitalOceanDroplet tests. * Fix TestAccDataSourceDigitalOceanVPC_ByName * Fix TestAccDataSourceDigitalOceanTag_Basic * Fix TestAccDataSourceDigitalOceanTags_Basic * Ensure versions are set in DBaaS tests. * Fix TestAccDataSourceDigitalOceanApp_Basic * Fix non-set related issues with TestAccDataSourceDigitalOceanLoadBalancer tests. * Fix TestAccDataSourceDigitalOceanKubernetesCluster_Basic * Remove testAccDigitalOceanKubernetesConfigWithEmptyNodePool: Empty node pools are no longer supported. * Fix TestAccDigitalOceanProject_WithManyResources. * Fix TestAccDigitalOceanProject_UpdateFromDropletToSpacesResource * vendor set helpers from AWS provider * Fix TestAccDigitalOceanFloatingIP_Droplet. * Fix CDN panic. * fix TestAccDigitalOceanSpacesBucket_LifecycleBasic using setutil helpers * vendor set helpers from AWS provider * fix TestAccDigitalOceanSpacesBucket_LifecycleBasic using setutil helpers * Fix load balancer tests using setutil helpers. * Fix K8s tests using setutil helpers. * Fix TestAccDigitalOceanApp_Envs using setutil helpers. * Fix TestAccDigitalOceanSpacesBucket_LifecycleExpireMarkerOnly using setutil helpers. * Fix TestAccDigitalOceanFloatingIPAssignment_createBeforeDestroy * fix remaining TypeSet tests using setutil * Registry test can not run in parallel. One per account. * Fix TestAccDigitalOceanProject_UpdateWithDropletResource * Fix replica tests. * go mod tidy Co-authored-by: Andrew Starr-Bochicchio <andrewsomething@users.noreply.github.com> Co-authored-by: Andrew Starr-Bochicchio <a.starr.b@gmail.com>
2020-10-16 19:50:20 +00:00
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceDigitalOceanVolumeSnapshot() *schema.Resource {
return &schema.Resource{
Create: resourceDigitalOceanVolumeSnapshotCreate,
Read: resourceDigitalOceanVolumeSnapshotRead,
Update: resourceDigitalOceanVolumeSnapshotUpdate,
Delete: resourceDigitalOceanVolumeSnapshotDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
"volume_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
"regions": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"size": {
Type: schema.TypeFloat,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"min_disk_size": {
Type: schema.TypeInt,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
func resourceDigitalOceanVolumeSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
opts := &godo.SnapshotCreateRequest{
Name: d.Get("name").(string),
VolumeID: d.Get("volume_id").(string),
Tags: expandTags(d.Get("tags").(*schema.Set).List()),
}
log.Printf("[DEBUG] Volume Snapshot create configuration: %#v", opts)
snapshot, _, err := client.Storage.CreateSnapshot(context.Background(), opts)
if err != nil {
return fmt.Errorf("Error creating Volume Snapshot: %s", err)
}
d.SetId(snapshot.ID)
log.Printf("[INFO] Volume Snapshot name: %s", snapshot.Name)
return resourceDigitalOceanVolumeSnapshotRead(d, meta)
}
func resourceDigitalOceanVolumeSnapshotUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
if d.HasChange("tags") {
err := setTags(client, d, godo.VolumeSnapshotResourceType)
if err != nil {
return fmt.Errorf("Error updating tags: %s", err)
}
}
return resourceDigitalOceanVolumeSnapshotRead(d, meta)
}
func resourceDigitalOceanVolumeSnapshotRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
snapshot, resp, err := client.Snapshots.Get(context.Background(), d.Id())
if err != nil {
// If the snapshot is somehow already destroyed, mark as
// successfully gone
if resp != nil && resp.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving volume snapshot: %s", err)
}
d.Set("name", snapshot.Name)
d.Set("volume_id", snapshot.ResourceID)
d.Set("regions", snapshot.Regions)
d.Set("size", snapshot.SizeGigaBytes)
d.Set("created_at", snapshot.Created)
d.Set("min_disk_size", snapshot.MinDiskSize)
d.Set("tags", flattenTags(snapshot.Tags))
return nil
}
func resourceDigitalOceanVolumeSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
log.Printf("[INFO] Deleting snaphot: %s", d.Id())
_, err := client.Snapshots.Delete(context.Background(), d.Id())
if err != nil {
return fmt.Errorf("Error deleting snapshot: %s", err)
}
d.SetId("")
return nil
}