terraform-provider-greenhost/digitalocean/datasource_digitalocean_siz...

117 lines
3.4 KiB
Go
Raw Normal View History

package digitalocean
import (
"context"
"fmt"
"github.com/digitalocean/godo"
"github.com/digitalocean/terraform-provider-digitalocean/internal/datalist"
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 dataSourceDigitalOceanSizes() *schema.Resource {
dataListConfig := &datalist.ResourceConfig{
RecordSchema: map[string]*schema.Schema{
"slug": {
Type: schema.TypeString,
Description: "A human-readable string that is used to uniquely identify each size.",
},
"available": {
Type: schema.TypeBool,
Description: "This represents whether new Droplets can be created with this size.",
},
"transfer": {
Type: schema.TypeFloat,
Description: "The amount of transfer bandwidth that is available for Droplets created in this size. This only counts traffic on the public interface. The value is given in terabytes.",
},
"price_monthly": {
Type: schema.TypeFloat,
Description: "The monthly cost of Droplets created in this size if they are kept for an entire month. The value is measured in US dollars.",
},
"price_hourly": {
Type: schema.TypeFloat,
Description: "The hourly cost of Droplets created in this size as measured hourly. The value is measured in US dollars.",
},
"memory": {
Type: schema.TypeInt,
Description: "The amount of RAM allocated to Droplets created of this size. The value is measured in megabytes.",
},
"vcpus": {
Type: schema.TypeInt,
Description: "The number of CPUs allocated to Droplets of this size.",
},
"disk": {
Type: schema.TypeInt,
Description: "The amount of disk space set aside for Droplets of this size. The value is measured in gigabytes.",
},
"regions": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "List of region slugs where Droplets can be created in this size.",
},
},
ResultAttributeName: "sizes",
FlattenRecord: flattenDigitalOceanSize,
GetRecords: getDigitalOceanSizes,
}
return datalist.NewResource(dataListConfig)
}
func getDigitalOceanSizes(meta interface{}) ([]interface{}, error) {
client := meta.(*CombinedConfig).godoClient()
sizes := []interface{}{}
opts := &godo.ListOptions{
Page: 1,
PerPage: 200,
}
for {
partialSizes, resp, err := client.Sizes.List(context.Background(), opts)
if err != nil {
return nil, fmt.Errorf("Error retrieving sizes: %s", err)
}
for _, partialSize := range partialSizes {
sizes = append(sizes, partialSize)
}
if resp.Links == nil || resp.Links.IsLastPage() {
break
}
page, err := resp.Links.CurrentPage()
if err != nil {
return nil, fmt.Errorf("Error retrieving sizes: %s", err)
}
opts.Page = page + 1
}
return sizes, nil
}
func flattenDigitalOceanSize(size, meta interface{}) (map[string]interface{}, error) {
s := size.(godo.Size)
flattenedSize := map[string]interface{}{}
flattenedSize["slug"] = s.Slug
flattenedSize["available"] = s.Available
flattenedSize["transfer"] = s.Transfer
flattenedSize["price_monthly"] = s.PriceMonthly
flattenedSize["price_hourly"] = s.PriceHourly
flattenedSize["memory"] = s.Memory
flattenedSize["vcpus"] = s.Vcpus
flattenedSize["disk"] = s.Disk
flattenedRegions := schema.NewSet(schema.HashString, []interface{}{})
for _, r := range s.Regions {
flattenedRegions.Add(r)
}
flattenedSize["regions"] = flattenedRegions
return flattenedSize, nil
}