Skip to content

Commit

Permalink
Support Spaces Key API
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-aaron committed Feb 20, 2025
1 parent 4ec164a commit 8e33402
Show file tree
Hide file tree
Showing 7 changed files with 627 additions and 3 deletions.
2 changes: 2 additions & 0 deletions digitalocean/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func Provider() *schema.Provider {
"digitalocean_spaces_buckets": spaces.DataSourceDigitalOceanSpacesBuckets(),
"digitalocean_spaces_bucket_object": spaces.DataSourceDigitalOceanSpacesBucketObject(),
"digitalocean_spaces_bucket_objects": spaces.DataSourceDigitalOceanSpacesBucketObjects(),
"digitalocean_spaces_key": spaces.DataSourceDigitalOceanSpacesKey(),
"digitalocean_ssh_key": sshkey.DataSourceDigitalOceanSSHKey(),
"digitalocean_ssh_keys": sshkey.DataSourceDigitalOceanSSHKeys(),
"digitalocean_tag": tag.DataSourceDigitalOceanTag(),
Expand Down Expand Up @@ -185,6 +186,7 @@ func Provider() *schema.Provider {
"digitalocean_spaces_bucket_cors_configuration": spaces.ResourceDigitalOceanBucketCorsConfiguration(),
"digitalocean_spaces_bucket_object": spaces.ResourceDigitalOceanSpacesBucketObject(),
"digitalocean_spaces_bucket_policy": spaces.ResourceDigitalOceanSpacesBucketPolicy(),
"digitalocean_spaces_key": spaces.ResourceDigitalOceanSpacesKey(),
"digitalocean_ssh_key": sshkey.ResourceDigitalOceanSSHKey(),
"digitalocean_tag": tag.ResourceDigitalOceanTag(),
"digitalocean_uptime_check": uptime.ResourceDigitalOceanUptimeCheck(),
Expand Down
69 changes: 69 additions & 0 deletions digitalocean/spaces/datasource_spaces_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package spaces

import (
"context"
"log"

"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config"

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

func DataSourceDigitalOceanSpacesKey() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceSpacesKeyRead,

Schema: spacesKeySchema(),
}
}

func dataSourceSpacesKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*config.CombinedConfig).GodoClient()

opts := &godo.ListOptions{
Page: 1,
PerPage: 200,
}

var key *godo.SpacesKey

for {
keys, resp, err := client.SpacesKeys.List(ctx, opts)
if err != nil {
return diag.Errorf("Error reading Spaces key: %s", err)
}

for _, k := range keys {
if k.Name == d.Get("name").(string) {
key = k
break
}
}

if resp.Links == nil || resp.Links.IsLastPage() {
break
}

page, err := resp.Links.CurrentPage()
if err != nil {
return diag.Errorf("Error reading Spaces key: %s", err)
}

opts.Page = page + 1
}

if key == nil {
log.Printf("[WARN] Key not found: %s", d.Id())
d.SetId("")
return nil
}

d.SetId(key.AccessKey)
d.Set("name", key.Name)
d.Set("access_key", key.AccessKey)
d.Set("grant", flattenGrants(key.Grants))
d.Set("created_at", key.CreatedAt)
return nil
}
119 changes: 119 additions & 0 deletions digitalocean/spaces/datasource_spaces_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package spaces_test

import (
"context"
"fmt"
"testing"

"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/acceptance"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config"

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

func TestAccDataSourceDigitalOceanSpacesKey_basic(t *testing.T) {
name := acceptance.RandomTestName()
resources, both := testAccDataSourceDigitalOceanSpacesKeyConfig_basic(name)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: testAccCheckDigitalOceanSpacesKeyDestroy,
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Config: resources,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("digitalocean_spaces_key.key", "name", name),
resource.TestCheckResourceAttr("digitalocean_spaces_key.key", "grant.0.bucket", "my-bucket"),
resource.TestCheckResourceAttr("digitalocean_spaces_key.key", "grant.0.permission", "read"),
),
},
{
Config: both,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.digitalocean_spaces_key.key", "name", name),
resource.TestCheckResourceAttr("data.digitalocean_spaces_key.key", "grant.0.bucket", "my-bucket"),
resource.TestCheckResourceAttr("data.digitalocean_spaces_key.key", "grant.0.permission", "read"),
),
},
},
})
}

func testAccDataSourceDigitalOceanSpacesKeyConfig_basic(name string) (string, string) {
resources := fmt.Sprintf(`
resource "digitalocean_spaces_key" "key" {
name = "%s"
grant {
bucket = "my-bucket"
permission = "read"
}
grant {
bucket = "my-bucket2"
permission = "readwrite"
}
}
`, name)

both := fmt.Sprintf(`%s
data "digitalocean_spaces_key" "key" {
name = "%s"
}
`, resources, name)

return resources, both
}

func testAccCheckDigitalOceanSpacesKeyDestroy(s *terraform.State) error {
return testAccCheckDigitalOceanSpacesKeyDestroyWithProvider(s, acceptance.TestAccProvider)
}

func testAccCheckDigitalOceanSpacesKeyDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "digitalocean_spaces_key" {
continue
}

if rs.Primary.ID == "" {
return fmt.Errorf("No Record ID is not set")
}

client := provider.Meta().(*config.CombinedConfig).GodoClient()

opts := &godo.ListOptions{
Page: 1,
PerPage: 200,
}

for {
keys, resp, err := client.SpacesKeys.List(context.Background(), opts)
if err != nil {
return fmt.Errorf("Error listing Spaces keys: %s", err)
}

for _, key := range keys {
if key.AccessKey == rs.Primary.ID {
return fmt.Errorf("Key still exists")
}
}

if resp.Links == nil || resp.Links.IsLastPage() {
break
}

page, err := resp.Links.CurrentPage()
if err != nil {
return fmt.Errorf("Error reading Spaces key: %s", err)
}

opts.Page = page + 1
}

return nil
}
return nil
}
5 changes: 3 additions & 2 deletions digitalocean/spaces/resource_spaces_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"strings"
"time"

"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/util"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/util"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
Expand Down
Loading

0 comments on commit 8e33402

Please sign in to comment.