Skip to content

Instantly share code, notes, and snippets.

@andrewsomething

andrewsomething/tf.diff

Last active Nov 27, 2018
Embed
What would you like to do?
diff --git a/digitalocean/config.go b/digitalocean/config.go
index 96f6785..bc38c31 100644
--- a/digitalocean/config.go
+++ b/digitalocean/config.go
@@ -12,33 +12,63 @@ import (
"github.com/hashicorp/terraform/helper/logging"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
+ "github.com/minio/minio-go"
"golang.org/x/oauth2"
)
type Config struct {
Token string
+
+ AccessKey string
+ SecretKey string
+}
+
+type CombinedConfig struct {
+ client *godo.Client
+ accessKey string
+ secretKey string
+}
+
+func (c *CombinedConfig) godoClient() *godo.Client { return c.client }
+
+func (c *CombinedConfig) spacesClient(region string) (*minio.Client, error) {
+ client, err := minio.New(
+ fmt.Sprintf("%v.digitaloceanspaces.com", region),
+ c.accessKey,
+ c.secretKey,
+ true,
+ )
+ if err != nil {
+ return &minio.Client{}, err
+ }
+
+ return client, nil
}
// Client() returns a new client for accessing digital ocean.
-func (c *Config) Client() (*godo.Client, error) {
+func (c *Config) Client() (*CombinedConfig, error) {
tokenSrc := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: c.Token,
})
userAgent := fmt.Sprintf("Terraform/%s", terraform.VersionString())
- client, err := godo.New(oauth2.NewClient(oauth2.NoContext, tokenSrc), godo.SetUserAgent(userAgent))
+ do, err := godo.New(oauth2.NewClient(oauth2.NoContext, tokenSrc), godo.SetUserAgent(userAgent))
if err != nil {
return nil, err
}
if logging.IsDebugOrHigher() {
- client.OnRequestCompleted(logRequestAndResponse)
+ do.OnRequestCompleted(logRequestAndResponse)
}
- log.Printf("[INFO] DigitalOcean Client configured for URL: %s", client.BaseURL.String())
+ log.Printf("[INFO] DigitalOcean Client configured for URL: %s", do.BaseURL.String())
- return client, nil
+ return &CombinedConfig{
+ client: do,
+ accessKey: c.AccessKey,
+ secretKey: c.SecretKey,
+ }, nil
}
func logRequestAndResponse(req *http.Request, resp *http.Response) {
diff --git a/digitalocean/provider.go b/digitalocean/provider.go
index 4c944ee..114913d 100644
--- a/digitalocean/provider.go
+++ b/digitalocean/provider.go
@@ -15,6 +15,18 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("DIGITALOCEAN_TOKEN", nil),
Description: "The token key for API operations.",
},
+ "access_key": {
+ Type: schema.TypeString,
+ Optional: true,
+ DefaultFunc: schema.EnvDefaultFunc("SPACES_KEY", nil),
+ Description: "Spaces Access Key",
+ },
+ "secret_key": {
+ Type: schema.TypeString,
+ Optional: true,
+ DefaultFunc: schema.EnvDefaultFunc("SPACES_SECRET", nil),
+ Description: "Spaces Secret Key",
+ },
},
DataSourcesMap: map[string]*schema.Resource{
@@ -41,7 +53,9 @@ func Provider() terraform.ResourceProvider {
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{
- Token: d.Get("token").(string),
+ Token: d.Get("token").(string),
+ AccessKey: d.Get("access_key").(string),
+ SecretKey: d.Get("secret_key").(string),
}
return config.Client()
diff --git a/digitalocean/resource_digitalocean_bucket.go b/digitalocean/resource_digitalocean_bucket.go
index f56be89..784fdd1 100644
--- a/digitalocean/resource_digitalocean_bucket.go
+++ b/digitalocean/resource_digitalocean_bucket.go
@@ -5,7 +5,6 @@ import (
"log"
"github.com/hashicorp/terraform/helper/schema"
- "github.com/minio/minio-go"
)
func resourceDigitalOceanBucket() *schema.Resource {
@@ -30,31 +29,15 @@ func resourceDigitalOceanBucket() *schema.Resource {
ForceNew: true,
Description: "Bucket region",
},
- "access_key": {
- Type: schema.TypeString,
- Required: true,
- ForceNew: true,
- Description: "Spaces Access Key",
- },
- "secret_key": {
- Type: schema.TypeString,
- Required: true,
- ForceNew: true,
- Description: "Spaces Secret Key",
- },
},
}
}
func resourceDigitalOceanBucketCreate(d *schema.ResourceData, meta interface{}) error {
- client, err := minio.New(
- fmt.Sprintf("%v.digitaloceanspaces.com", d.Get("region").(string)),
- d.Get("access_key").(string),
- d.Get("secret_key").(string),
- true,
- )
+ region := d.Get("region").(string)
+ client, err := meta.(*CombinedConfig).spacesClient(region)
if err != nil {
- log.Fatal(err)
+ return fmt.Errorf("Error creating bucket: %s", err)
}
err = client.MakeBucket(d.Get("name").(string), "us-east-1")
@@ -68,14 +51,10 @@ func resourceDigitalOceanBucketCreate(d *schema.ResourceData, meta interface{})
}
func resourceDigitalOceanBucketRead(d *schema.ResourceData, meta interface{}) error {
- client, err := minio.New(
- fmt.Sprintf("%v.digitaloceanspaces.com", d.Get("region").(string)),
- d.Get("access_key").(string),
- d.Get("secret_key").(string),
- true,
- )
+ region := d.Get("region").(string)
+ client, err := meta.(*CombinedConfig).spacesClient(region)
if err != nil {
- log.Fatal(err)
+ return fmt.Errorf("Error creating bucket: %s", err)
}
found, err := client.BucketExists(d.Get("name").(string))
@@ -95,14 +74,10 @@ func resourceDigitalOceanBucketRead(d *schema.ResourceData, meta interface{}) er
}
func resourceDigitalOceanBucketDelete(d *schema.ResourceData, meta interface{}) error {
- client, err := minio.New(
- fmt.Sprintf("%v.digitaloceanspaces.com", d.Get("region").(string)),
- d.Get("access_key").(string),
- d.Get("secret_key").(string),
- true,
- )
+ region := d.Get("region").(string)
+ client, err := meta.(*CombinedConfig).spacesClient(region)
if err != nil {
- log.Fatal(err)
+ return fmt.Errorf("Error creating bucket: %s", err)
}
err = client.RemoveBucket(d.Get("name").(string))
diff --git a/digitalocean/resource_digitalocean_domain.go b/digitalocean/resource_digitalocean_domain.go
index a60f126..cf35ab3 100644
--- a/digitalocean/resource_digitalocean_domain.go
+++ b/digitalocean/resource_digitalocean_domain.go
@@ -35,7 +35,7 @@ func resourceDigitalOceanDomain() *schema.Resource {
}
func resourceDigitalOceanDomainCreate(d *schema.ResourceData, meta interface{}) error {
- client := meta.(*godo.Client)
+ client := meta.(*CombinedConfig).godoClient()
// Build up our creation options
@@ -57,7 +57,7 @@ func resourceDigitalOceanDomainCreate(d *schema.ResourceData, meta interface{})
}
func resourceDigitalOceanDomainRead(d *schema.ResourceData, meta interface{}) error {
- client := meta.(*godo.Client)
+ client := meta.(*CombinedConfig).godoClient()
domain, resp, err := client.Domains.Get(context.Background(), d.Id())
if err != nil {
@@ -77,7 +77,7 @@ func resourceDigitalOceanDomainRead(d *schema.ResourceData, meta interface{}) er
}
func resourceDigitalOceanDomainDelete(d *schema.ResourceData, meta interface{}) error {
- client := meta.(*godo.Client)
+ client := meta.(*CombinedConfig).godoClient()
log.Printf("[INFO] Deleting Domain: %s", d.Id())
_, err := client.Domains.Delete(context.Background(), d.Id())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.