Skip to content

Commit

Permalink
Adding sandbox flag (#12)
Browse files Browse the repository at this point in the history
Allow to use sandbox setting the `sandbox` variable.
https://developer.dnsimple.com/sandbox/

Co-authored-by: Jonathan Derham <[email protected]>
  • Loading branch information
1 parent f7e479a commit f21961d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion dnsimple/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import (
"golang.org/x/oauth2"
)

const (
baseURLSandbox = "https://api.sandbox.dnsimple.com"
)

type Config struct {
Email string
Account string
Token string
Sandbox bool

terraformVersion string
}
Expand All @@ -31,13 +36,16 @@ func (c *Config) Client() (*Client, error) {

client := dnsimple.NewClient(tc)
client.SetUserAgent(httpclient.TerraformUserAgent(c.terraformVersion))
if c.Sandbox {
client.BaseURL = baseURLSandbox
}

provider := &Client{
client: client,
config: c,
}

log.Printf("[INFO] DNSimple Client configured for account: %s", c.Account)
log.Printf("[INFO] DNSimple Client configured for account: %s, sandbox: %v", c.Account, c.Sandbox)

return provider, nil
}
8 changes: 8 additions & 0 deletions dnsimple/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("DNSIMPLE_ACCOUNT", nil),
Description: "The account for API operations.",
},

"sandbox": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("DNSIMPLE_SANDBOX", nil),
Description: "Flag to enable the sandbox API.",
},
},

ResourcesMap: map[string]*schema.Resource{
Expand All @@ -46,6 +53,7 @@ func providerConfigure(d *schema.ResourceData, terraformVersion string) (interfa
config := Config{
Token: d.Get("token").(string),
Account: d.Get("account").(string),
Sandbox: d.Get("sandbox").(bool),
terraformVersion: terraformVersion,
}

Expand Down
13 changes: 13 additions & 0 deletions dnsimple/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ func TestProvider(t *testing.T) {
}
}

func TestProvider_sandbox(t *testing.T) {
if v := os.Getenv("DNSIMPLE_SANDBOX"); v != "" {
provider := testAccProvider.Meta().(*Client)
if provider.config.Sandbox != true {
t.Fatal("Config Sandbox Flag does not equal True!")
}

if provider.client.BaseURL != "https://api.sandbox.dnsimple.com" {
t.Fatalf("Client.BaseURL is not the expected sandbox URL! Currently set to: %s", provider.client.BaseURL)
}
}
}

func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}
Expand Down

0 comments on commit f21961d

Please sign in to comment.