terraform-provider-greenhost/digitalocean/droplets.go

207 lines
5.6 KiB
Go
Raw Normal View History

package digitalocean
import (
"context"
"fmt"
"strconv"
"strings"
"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"
)
func dropletSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Description: "id of the Droplet",
},
"name": {
Type: schema.TypeString,
Description: "name of the Droplet",
},
"created_at": {
Type: schema.TypeString,
Description: "the creation date for the Droplet",
},
"urn": {
Type: schema.TypeString,
Description: "the uniform resource name for the Droplet",
},
"region": {
Type: schema.TypeString,
Description: "the region that the Droplet instance is deployed in",
},
"image": {
Type: schema.TypeString,
Description: "the image id or slug of the Droplet",
},
"size": {
Type: schema.TypeString,
Description: "the current size of the Droplet",
},
"disk": {
Type: schema.TypeInt,
Description: "the size of the Droplets disk in gigabytes",
},
"vcpus": {
Type: schema.TypeInt,
Description: "the number of virtual cpus",
},
"memory": {
Type: schema.TypeInt,
Description: "memory of the Droplet in megabytes",
},
"price_hourly": {
Type: schema.TypeFloat,
Description: "the Droplets hourly price",
},
"price_monthly": {
Type: schema.TypeFloat,
Description: "the Droplets monthly price",
},
"status": {
Type: schema.TypeString,
Description: "state of the Droplet instance",
},
"locked": {
Type: schema.TypeBool,
Description: "whether the Droplet has been locked",
},
"ipv4_address": {
Type: schema.TypeString,
Description: "the Droplets public ipv4 address",
},
"ipv4_address_private": {
Type: schema.TypeString,
Description: "the Droplets private ipv4 address",
},
"ipv6_address": {
Type: schema.TypeString,
Description: "the Droplets public ipv6 address",
},
"ipv6_address_private": {
Type: schema.TypeString,
Description: "the Droplets private ipv4 address",
},
"backups": {
Type: schema.TypeBool,
Description: "whether the Droplet has backups enabled",
},
"ipv6": {
Type: schema.TypeBool,
Description: "whether the Droplet has ipv6 enabled",
},
"private_networking": {
Type: schema.TypeBool,
Description: "whether the Droplet has private networking enabled",
},
"monitoring": {
Type: schema.TypeBool,
Description: "whether the Droplet has monitoring enabled",
},
"volume_ids": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "list of volumes attached to the Droplet",
},
"tags": tagsDataSourceSchema(),
"vpc_uuid": {
Type: schema.TypeString,
Description: "UUID of the VPC in which the Droplet is located",
},
}
}
func getDigitalOceanDroplets(meta interface{}) ([]interface{}, error) {
client := meta.(*CombinedConfig).godoClient()
opts := &godo.ListOptions{
Page: 1,
PerPage: 200,
}
var dropletList []interface{}
for {
droplets, resp, err := client.Droplets.List(context.Background(), opts)
if err != nil {
return nil, fmt.Errorf("Error retrieving droplets: %s", err)
}
for _, droplet := range droplets {
dropletList = append(dropletList, droplet)
}
if resp.Links == nil || resp.Links.IsLastPage() {
break
}
page, err := resp.Links.CurrentPage()
if err != nil {
return nil, fmt.Errorf("Error retrieving droplets: %s", err)
}
opts.Page = page + 1
}
return dropletList, nil
}
func flattenDigitalOceanDroplet(rawDroplet, meta interface{}) (map[string]interface{}, error) {
droplet := rawDroplet.(godo.Droplet)
flattenedDroplet := map[string]interface{}{
"id": droplet.ID,
"name": droplet.Name,
"urn": droplet.URN(),
"region": droplet.Region.Slug,
"size": droplet.Size.Slug,
"price_hourly": droplet.Size.PriceHourly,
"price_monthly": droplet.Size.PriceMonthly,
"disk": droplet.Disk,
"vcpus": droplet.Vcpus,
"memory": droplet.Memory,
"status": droplet.Status,
"locked": droplet.Locked,
"created_at": droplet.Created,
"vpc_uuid": droplet.VPCUUID,
}
if droplet.Image.Slug == "" {
flattenedDroplet["image"] = strconv.Itoa(droplet.Image.ID)
} else {
flattenedDroplet["image"] = droplet.Image.Slug
}
if publicIPv4 := findIPv4AddrByType(&droplet, "public"); publicIPv4 != "" {
flattenedDroplet["ipv4_address"] = publicIPv4
}
if privateIPv4 := findIPv4AddrByType(&droplet, "private"); privateIPv4 != "" {
flattenedDroplet["ipv4_address_private"] = privateIPv4
}
if publicIPv6 := findIPv6AddrByType(&droplet, "public"); publicIPv6 != "" {
flattenedDroplet["ipv6_address"] = strings.ToLower(publicIPv6)
}
if privateIPv6 := findIPv6AddrByType(&droplet, "private"); privateIPv6 != "" {
flattenedDroplet["ipv6_address_private"] = strings.ToLower(privateIPv6)
}
if features := droplet.Features; features != nil {
flattenedDroplet["backups"] = containsDigitalOceanDropletFeature(features, "backups")
flattenedDroplet["ipv6"] = containsDigitalOceanDropletFeature(features, "ipv6")
flattenedDroplet["private_networking"] = containsDigitalOceanDropletFeature(features, "private_networking")
flattenedDroplet["monitoring"] = containsDigitalOceanDropletFeature(features, "monitoring")
}
flattenedDroplet["volume_ids"] = flattenDigitalOceanDropletVolumeIds(droplet.VolumeIDs)
flattenedDroplet["tags"] = flattenTags(droplet.Tags)
return flattenedDroplet, nil
}