Skip to content

Commit

Permalink
port data sources and resources to use the context-related methods (#503
Browse files Browse the repository at this point in the history
)

* add context arg and use context attribute

* fix up error types

* fix up error types in resources

* port datalist to ReadContext
  • Loading branch information
tdyas authored Oct 19, 2020
1 parent f50c276 commit 410de8b
Show file tree
Hide file tree
Showing 56 changed files with 794 additions and 734 deletions.
8 changes: 4 additions & 4 deletions digitalocean/datasource_digitalocean_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package digitalocean

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceDigitalOceanAccount() *schema.Resource {
return &schema.Resource{
Read: dataSourceDigitalOceanAccountRead,
ReadContext: dataSourceDigitalOceanAccountRead,
Schema: map[string]*schema.Schema{
"droplet_limit": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -50,12 +50,12 @@ func dataSourceDigitalOceanAccount() *schema.Resource {
}
}

func dataSourceDigitalOceanAccountRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceDigitalOceanAccountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*CombinedConfig).godoClient()

account, _, err := client.Account.Get(context.Background())
if err != nil {
return fmt.Errorf("Error retrieving account: %s", err)
return diag.Errorf("Error retrieving account: %s", err)
}

d.SetId(account.UUID)
Expand Down
9 changes: 6 additions & 3 deletions digitalocean/datasource_digitalocean_app.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package digitalocean

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceDigitalOceanApp() *schema.Resource {
return &schema.Resource{
Read: dataSourceDigitalOceanAppRead,
ReadContext: dataSourceDigitalOceanAppRead,
Schema: map[string]*schema.Schema{
"app_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -48,8 +51,8 @@ func dataSourceDigitalOceanApp() *schema.Resource {
}
}

func dataSourceDigitalOceanAppRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceDigitalOceanAppRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
d.SetId(d.Get("app_id").(string))

return resourceDigitalOceanAppRead(d, meta)
return resourceDigitalOceanAppRead(ctx, d, meta)
}
9 changes: 5 additions & 4 deletions digitalocean/datasource_digitalocean_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"net/http"

"github.com/digitalocean/godo"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceDigitalOceanCertificate() *schema.Resource {
return &schema.Resource{
Read: dataSourceDigitalOceanCertificateRead,
ReadContext: dataSourceDigitalOceanCertificateRead,
Schema: map[string]*schema.Schema{

"name": {
Expand Down Expand Up @@ -63,7 +64,7 @@ func dataSourceDigitalOceanCertificate() *schema.Resource {
}
}

func dataSourceDigitalOceanCertificateRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceDigitalOceanCertificateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*CombinedConfig).godoClient()

// When the certificate type is lets_encrypt, the certificate
Expand All @@ -72,7 +73,7 @@ func dataSourceDigitalOceanCertificateRead(d *schema.ResourceData, meta interfac
name := d.Get("name").(string)
cert, err := findCertificateByName(client, name)
if err != nil {
return err
return diag.FromErr(err)
}

d.SetId(cert.Name)
Expand All @@ -84,7 +85,7 @@ func dataSourceDigitalOceanCertificateRead(d *schema.ResourceData, meta interfac
d.Set("sha1_fingerprint", cert.SHA1Fingerprint)

if err := d.Set("domains", flattenDigitalOceanCertificateDomains(cert.DNSNames)); err != nil {
return fmt.Errorf("Error setting `domain`: %+v", err)
return diag.Errorf("Error setting `domain`: %+v", err)
}

return nil
Expand Down
9 changes: 5 additions & 4 deletions digitalocean/datasource_digitalocean_container_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
Expand All @@ -12,7 +13,7 @@ const RegistryHostname = "registry.digitalocean.com"

func dataSourceDigitalOceanContainerRegistry() *schema.Resource {
return &schema.Resource{
Read: dataSourceDigitalOceanContainerRegistryRead,
ReadContext: dataSourceDigitalOceanContainerRegistryRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand All @@ -32,16 +33,16 @@ func dataSourceDigitalOceanContainerRegistry() *schema.Resource {
}
}

func dataSourceDigitalOceanContainerRegistryRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceDigitalOceanContainerRegistryRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*CombinedConfig).godoClient()

reg, response, err := client.Registry.Get(context.Background())

if err != nil {
if response != nil && response.StatusCode == 404 {
return fmt.Errorf("registry not found: %s", err)
return diag.Errorf("registry not found: %s", err)
}
return fmt.Errorf("Error retrieving registry: %s", err)
return diag.Errorf("Error retrieving registry: %s", err)
}

d.SetId(reg.Name)
Expand Down
14 changes: 7 additions & 7 deletions digitalocean/datasource_digitalocean_database_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package digitalocean

import (
"context"
"fmt"

"github.com/digitalocean/godo"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceDigitalOceanDatabaseCluster() *schema.Resource {
return &schema.Resource{
Read: dataSourceDigitalOceanDatabaseClusterRead,
ReadContext: dataSourceDigitalOceanDatabaseClusterRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -120,7 +120,7 @@ func dataSourceDigitalOceanDatabaseCluster() *schema.Resource {
}
}

func dataSourceDigitalOceanDatabaseClusterRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceDigitalOceanDatabaseClusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*CombinedConfig).godoClient()

name := d.Get("name").(string)
Expand All @@ -135,7 +135,7 @@ func dataSourceDigitalOceanDatabaseClusterRead(d *schema.ResourceData, meta inte
for {
databases, resp, err := client.Databases.List(context.Background(), opts)
if err != nil {
return fmt.Errorf("Error retrieving DatabaseClusters: %s", err)
return diag.Errorf("Error retrieving DatabaseClusters: %s", err)
}

for _, d := range databases {
Expand All @@ -148,14 +148,14 @@ func dataSourceDigitalOceanDatabaseClusterRead(d *schema.ResourceData, meta inte

page, err := resp.Links.CurrentPage()
if err != nil {
return fmt.Errorf("Error retrieving DatabaseClusters: %s", err)
return diag.Errorf("Error retrieving DatabaseClusters: %s", err)
}

opts.Page = page + 1
}

if len(databaseList) == 0 {
return fmt.Errorf("Unable to find any database clusters")
return diag.Errorf("Unable to find any database clusters")
}

for _, db := range databaseList {
Expand All @@ -172,7 +172,7 @@ func dataSourceDigitalOceanDatabaseClusterRead(d *schema.ResourceData, meta inte

if _, ok := d.GetOk("maintenance_window"); ok {
if err := d.Set("maintenance_window", flattenMaintWindowOpts(*db.MaintenanceWindow)); err != nil {
return fmt.Errorf("[DEBUG] Error setting maintenance_window - error: %#v", err)
return diag.Errorf("[DEBUG] Error setting maintenance_window - error: %#v", err)
}
}

Expand Down
10 changes: 5 additions & 5 deletions digitalocean/datasource_digitalocean_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package digitalocean

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceDigitalOceanDomain() *schema.Resource {
return &schema.Resource{
Read: dataSourceDigitalOceanDomainRead,
ReadContext: dataSourceDigitalOceanDomainRead,
Schema: map[string]*schema.Schema{

"name": {
Expand Down Expand Up @@ -39,17 +39,17 @@ func dataSourceDigitalOceanDomain() *schema.Resource {
}
}

func dataSourceDigitalOceanDomainRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceDigitalOceanDomainRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*CombinedConfig).godoClient()

name := d.Get("name").(string)

domain, resp, err := client.Domains.Get(context.Background(), name)
if err != nil {
if resp != nil && resp.StatusCode == 404 {
return fmt.Errorf("domain not found: %s", err)
return diag.Errorf("domain not found: %s", err)
}
return fmt.Errorf("Error retrieving domain: %s", err)
return diag.Errorf("Error retrieving domain: %s", err)
}

d.SetId(domain.Name)
Expand Down
23 changes: 12 additions & 11 deletions digitalocean/datasource_digitalocean_droplet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"

"github.com/digitalocean/godo"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
Expand All @@ -31,59 +32,59 @@ func dataSourceDigitalOceanDroplet() *schema.Resource {
}

return &schema.Resource{
Read: dataSourceDigitalOceanDropletRead,
Schema: recordSchema,
ReadContext: dataSourceDigitalOceanDropletRead,
Schema: recordSchema,
}
}

func dataSourceDigitalOceanDropletRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceDigitalOceanDropletRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*CombinedConfig).godoClient()

var foundDroplet godo.Droplet

if id, ok := d.GetOk("id"); ok {
droplet, _, err := client.Droplets.Get(context.Background(), id.(int))
if err != nil {
return err
return diag.FromErr(err)
}

foundDroplet = *droplet
} else if v, ok := d.GetOk("tag"); ok {
dropletList, err := getDigitalOceanDroplets(meta)
if err != nil {
return err
return diag.FromErr(err)
}

droplet, err := findDropletByTag(dropletList, v.(string))
if err != nil {
return err
return diag.FromErr(err)
}

foundDroplet = *droplet
} else if v, ok := d.GetOk("name"); ok {
dropletList, err := getDigitalOceanDroplets(meta)
if err != nil {
return err
return diag.FromErr(err)
}

droplet, err := findDropletByName(dropletList, v.(string))

if err != nil {
return err
return diag.FromErr(err)
}

foundDroplet = *droplet
} else {
return fmt.Errorf("Error: specify either a name, tag, or id to use to look up the droplet")
return diag.Errorf("Error: specify either a name, tag, or id to use to look up the droplet")
}

flattenedDroplet, err := flattenDigitalOceanDroplet(foundDroplet, meta)
if err != nil {
return err
return diag.FromErr(err)
}

if err := setResourceDataFromMap(d, flattenedDroplet); err != nil {
return err
return diag.FromErr(err)
}

d.SetId(strconv.Itoa(foundDroplet.ID))
Expand Down
16 changes: 8 additions & 8 deletions digitalocean/datasource_digitalocean_droplet_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package digitalocean

import (
"context"
"fmt"
"log"
"strings"

"github.com/digitalocean/godo"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceDigitalOceanDropletSnapshot() *schema.Resource {
return &schema.Resource{
Read: dataSourceDigitalOceanDropletSnapshotRead,
ReadContext: dataSourceDigitalOceanDropletSnapshotRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -67,15 +67,15 @@ func dataSourceDigitalOceanDropletSnapshot() *schema.Resource {
}

// dataSourceDoSnapshotRead performs the Snapshot lookup.
func dataSourceDigitalOceanDropletSnapshotRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceDigitalOceanDropletSnapshotRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*CombinedConfig).godoClient()

name, hasName := d.GetOk("name")
nameRegex, hasNameRegex := d.GetOk("name_regex")
region, hasRegion := d.GetOk("region")

if !hasName && !hasNameRegex {
return fmt.Errorf("One of `name` or `name_regex` must be assigned")
return diag.Errorf("One of `name` or `name_regex` must be assigned")
}

opts := &godo.ListOptions{
Expand All @@ -89,7 +89,7 @@ func dataSourceDigitalOceanDropletSnapshotRead(d *schema.ResourceData, meta inte
snapshots, resp, err := client.Snapshots.ListDroplet(context.Background(), opts)

if err != nil {
return fmt.Errorf("Error retrieving Droplet snapshots: %s", err)
return diag.Errorf("Error retrieving Droplet snapshots: %s", err)
}

for _, s := range snapshots {
Expand All @@ -102,7 +102,7 @@ func dataSourceDigitalOceanDropletSnapshotRead(d *schema.ResourceData, meta inte

page, err := resp.Links.CurrentPage()
if err != nil {
return fmt.Errorf("Error retrieving Droplet snapshots: %s", err)
return diag.Errorf("Error retrieving Droplet snapshots: %s", err)
}

opts.Page = page + 1
Expand All @@ -121,14 +121,14 @@ func dataSourceDigitalOceanDropletSnapshotRead(d *schema.ResourceData, meta inte
// Get the queried snapshot or fail if it can't be determined
var snapshot *godo.Snapshot
if len(snapshotList) == 0 {
return fmt.Errorf("No DROPLET snapshot found with name %s", name)
return diag.Errorf("No DROPLET snapshot found with name %s", name)
}
if len(snapshotList) > 1 {
recent := d.Get("most_recent").(bool)
if recent {
snapshot = findMostRecentSnapshot(snapshotList)
} else {
return fmt.Errorf("too many Droplet snapshots found with name %s (found %d, expected 1)", name, len(snapshotList))
return diag.Errorf("too many Droplet snapshots found with name %s (found %d, expected 1)", name, len(snapshotList))
}
} else {
snapshot = &snapshotList[0]
Expand Down
Loading

0 comments on commit 410de8b

Please sign in to comment.