Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added resource & data source for object store credentials #144

Merged
merged 11 commits into from
Sep 14, 2022
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ builds:
- goos: windows
goarch: 'arm64'
binary: '{{ .ProjectName }}_v{{ .Version }}'
ldflags:
- -s -w -X github.com/civo/terraform-provider-civo/civo.ProviderVersion={{.Version}}
archives:
- format: zip
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}'
Expand Down
33 changes: 8 additions & 25 deletions civo/datasource_object_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/civo/civogo"
"github.com/civo/terraform-provider-civo/internal/utils"
"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 Down Expand Up @@ -38,28 +37,18 @@ func dataSourceObjectStore() *schema.Resource {
Optional: true,
Description: "The region of an existing Object Store",
},
"generated_name": {
Type: schema.TypeString,
Computed: true,
Description: "The generated name of the Object Store",
},
// Computed resource
"max_size_gb": {
Type: schema.TypeInt,
Optional: true,
Default: 500,
Computed: true,
Description: "The maximum size of the Object Store",
},
"access_key_id": {
Type: schema.TypeString,
Computed: true,
Description: "The access key id of the Object Store",
Description: "The access key ID from the Object Store credential. If this is not set, a new credential will be created.",
},
"secret_access_key": {
Type: schema.TypeString,
Computed: true,
Description: "The secret access key of the Object Store",
},
"endpoint": {
"bucket_url": {
Type: schema.TypeString,
Description: "The endpoint of the Object Store",
Computed: true,
Expand Down Expand Up @@ -103,18 +92,12 @@ func dataSourceObjectStoreRead(ctx context.Context, d *schema.ResourceData, m in
foundStore = store
}

maxSize, err := utils.StringToInt(foundStore.MaxSize)
if err != nil {
return diag.Errorf("[ERR] failed to convert the max size to int: %s", err)
}

d.SetId(foundStore.ID)
d.Set("name", foundStore.Name)
d.Set("generated_name", foundStore.GeneratedName)
d.Set("max_size_gb", maxSize)
d.Set("access_key_id", foundStore.AccessKeyID)
d.Set("secret_access_key", foundStore.SecretAccessKey)
d.Set("endpoint", foundStore.ObjectStoreEndpoint)
d.Set("region", apiClient.Region)
d.Set("max_size_gb", foundStore.MaxSize)
d.Set("access_key_id", foundStore.OwnerInfo.AccessKeyID)
d.Set("bucket_url", foundStore.BucketURL)
d.Set("status", foundStore.Status)

return nil
Expand Down
98 changes: 98 additions & 0 deletions civo/datasource_object_store_credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package civo

import (
"context"
"log"
"strings"

"github.com/civo/civogo"
"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"
)

// Data source to get from the api a specific ObjectStoreCredential
// using the id or the name
func dataSourceObjectStoreCredential() *schema.Resource {
return &schema.Resource{
Description: strings.Join([]string{
"Get information of an Object Store Credential for use in other resources. This data source provides all of the Object Store Credential's properties as configured on your Civo account.",
"Note: This data source returns a single Object Store Credential. When specifying a name, an error will be raised if more than one Object Store Credentials with the same name found.",
}, "\n\n"),
ReadContext: dataSourceObjectStoreCredentialRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Description: "The ID of the Object Store Credential",
},
"name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.NoZeroValues,
Description: "The name of the Object Store Credential",
},
"region": {
Type: schema.TypeString,
Optional: true,
Description: "The region of an existing Object Store",
},
// Computed values
"access_key_id": {
Type: schema.TypeString,
Computed: true,
Description: "The access key id of the Object Store Credential",
},
"secret_access_key": {
Type: schema.TypeString,
Computed: true,
Description: "The secret access key of the Object Store Credential",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The status of the Object Store Credential",
},
},
}
}

func dataSourceObjectStoreCredentialRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
apiClient := m.(*civogo.Client)

// overwrite the region if is define in the datasource
if region, ok := d.GetOk("region"); ok {
apiClient.Region = region.(string)
}

var foundStoreCredential *civogo.ObjectStoreCredential

if name, ok := d.GetOk("name"); ok {
log.Printf("[INFO] Getting the Object Store Credential by name")
storeCredential, err := apiClient.FindObjectStoreCredential(name.(string))
if err != nil {
return diag.Errorf("[ERR] failed to retrive Object Store Credential: %s", err)
}

foundStoreCredential = storeCredential
}

if id, ok := d.GetOk("id"); ok {
log.Printf("[INFO] Getting the Object Store Credential by name")
storeCredential, err := apiClient.FindObjectStoreCredential(id.(string))
if err != nil {
return diag.Errorf("[ERR] failed to retrive Object Store Credential: %s", err)
}

foundStoreCredential = storeCredential
}

d.SetId(foundStoreCredential.ID)
d.Set("name", foundStoreCredential.Name)
d.Set("region", apiClient.Region)
d.Set("access_key_id", foundStoreCredential.AccessKeyID)
d.Set("secret_access_key", foundStoreCredential.SecretAccessKeyID)
d.Set("status", foundStoreCredential.Status)

return nil
}
42 changes: 42 additions & 0 deletions civo/datasource_object_store_credentials_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package civo

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceCivoObjectStoreCredential_basic(t *testing.T) {
datasourceName := "data.civo_object_store_credential.foobar"
name := acctest.RandomWithPrefix("objectstorecredential")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceCivoObjectStoreCredentialConfig(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "name", name),
resource.TestCheckResourceAttrSet(datasourceName, "access_key_id"),
resource.TestCheckResourceAttrSet(datasourceName, "secret_access_key"),
resource.TestCheckResourceAttr(datasourceName, "status", "ready"),
),
},
},
})
}

func testAccDataSourceCivoObjectStoreCredentialConfig(name string) string {
return fmt.Sprintf(`
resource "civo_object_store_credential" "foobar" {
name = "%s"
}

data "civo_object_store_credential" "foobar" {
name = civo_object_store_credential.foobar.name
}
`, name)
}
5 changes: 3 additions & 2 deletions civo/datasource_object_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func TestAccDataSourceCivoObjectStore_basic(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "name", name),
resource.TestCheckResourceAttrSet(datasourceName, "max_size_gb"),
resource.TestCheckResourceAttrSet(datasourceName, "region"),
resource.TestCheckResourceAttrSet(datasourceName, "bucket_url"),
resource.TestCheckResourceAttrSet(datasourceName, "access_key_id"),
resource.TestCheckResourceAttrSet(datasourceName, "secret_access_key"),
resource.TestCheckResourceAttrSet(datasourceName, "endpoint"),
resource.TestCheckResourceAttr(datasourceName, "status", "ready"),
),
},
Expand All @@ -36,6 +36,7 @@ func testAccDataSourceCivoObjectStoreConfig(name string) string {
resource "civo_object_store" "foobar" {
name = "%s"
max_size_gb = 500
region = "FAKE"
}

data "civo_object_store" "foobar" {
Expand Down
51 changes: 32 additions & 19 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var (
// ProviderVersion is the version of the provider to set in the User-Agent header
ProviderVersion = "dev"
)

// Provider Civo cloud provider
func Provider() *schema.Provider {
return &schema.Provider{
Expand All @@ -28,23 +33,24 @@ func Provider() *schema.Provider {
},
DataSourcesMap: map[string]*schema.Resource{
// "civo_template": dataSourceTemplate(),
"civo_disk_image": dataSourceDiskImage(),
"civo_kubernetes_version": dataSourceKubernetesVersion(),
"civo_kubernetes_cluster": dataSourceKubernetesCluster(),
"civo_instances_size": dataSourceInstancesSize(),
"civo_size": dataSourceSize(),
"civo_instances": dataSourceInstances(),
"civo_instance": dataSourceInstance(),
"civo_dns_domain_name": dataSourceDNSDomainName(),
"civo_dns_domain_record": dataSourceDNSDomainRecord(),
"civo_network": dataSourceNetwork(),
"civo_volume": dataSourceVolume(),
"civo_firewall": dataSourceFirewall(),
"civo_loadbalancer": dataSourceLoadBalancer(),
"civo_ssh_key": dataSourceSSHKey(),
"civo_object_store": dataSourceObjectStore(),
"civo_region": dataSourceRegion(),
"civo_reserved_ip": dataSourceReservedIP(),
"civo_disk_image": dataSourceDiskImage(),
"civo_kubernetes_version": dataSourceKubernetesVersion(),
"civo_kubernetes_cluster": dataSourceKubernetesCluster(),
"civo_instances_size": dataSourceInstancesSize(),
"civo_size": dataSourceSize(),
"civo_instances": dataSourceInstances(),
"civo_instance": dataSourceInstance(),
"civo_dns_domain_name": dataSourceDNSDomainName(),
"civo_dns_domain_record": dataSourceDNSDomainRecord(),
"civo_network": dataSourceNetwork(),
"civo_volume": dataSourceVolume(),
"civo_firewall": dataSourceFirewall(),
"civo_loadbalancer": dataSourceLoadBalancer(),
"civo_ssh_key": dataSourceSSHKey(),
"civo_object_store": dataSourceObjectStore(),
"civo_object_store_credential": dataSourceObjectStoreCredential(),
"civo_region": dataSourceRegion(),
"civo_reserved_ip": dataSourceReservedIP(),
// "civo_snapshot": dataSourceSnapshot(),
},
ResourcesMap: map[string]*schema.Resource{
Expand All @@ -61,8 +67,9 @@ func Provider() *schema.Provider {
"civo_kubernetes_node_pool": resourceKubernetesClusterNodePool(),
"civo_reserved_ip": resourceReservedIP(),
"civo_instance_reserved_ip_assignment": resourceInstanceReservedIPAssignment(),
"civo_object_store": resourceObjectStore(),
"civo_object_store_credential": resourceObjectStoreCredential(),
// "civo_loadbalancer": resourceLoadBalancer(),
"civo_object_store": resourceObjectStore(),
// "civo_template": resourceTemplate(),
// "civo_snapshot": resourceSnapshot(),

Expand Down Expand Up @@ -98,11 +105,17 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return client, nil
}

userAgent := &civogo.Component{
Name: "terraform-provider-civo",
Version: ProviderVersion,
}

client, err = civogo.NewClient(tokenValue, regionValue)
if err != nil {
return nil, err
}
client.SetUserAgent(userAgent)

log.Printf("[DEBUG] Civo API URL: %s\n", "https://api.civo.com")
return client, nil

}
2 changes: 1 addition & 1 deletion civo/resource_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func resourceFirewallCreate(ctx context.Context, d *schema.ResourceData, m inter
if err != nil {
return diag.Errorf("[ERR] an error occurred while tring to build the firewall request, %s", err)
}

firewall, err := apiClient.NewFirewall(firewallConfig)

if err != nil {
return diag.Errorf("[ERR] failed to create a new firewall: %s", err)
}
Expand Down
Loading