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

provider/ns1: Ensure provider checks for credentials #12920

Merged
merged 3 commits into from
Mar 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions builtin/providers/ns1/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ns1

import (
"crypto/tls"
"errors"
"log"
"net/http"

ns1 "gopkg.in/ns1/ns1-go.v2/rest"
)

type Config struct {
Key string
Endpoint string
IgnoreSSL bool
}

// Client() returns a new NS1 client.
func (c *Config) Client() (*ns1.Client, error) {
httpClient := &http.Client{}
decos := []func(*ns1.Client){}

if c.Key == "" {
return nil, errors.New(`No valid credential sources found for NS1 Provider.
Please see https://terraform.io/docs/providers/ns1/index.html for more information on
providing credentials for the NS1 Provider`)
}

decos = append(decos, ns1.SetAPIKey(c.Key))
if c.Endpoint != "" {
decos = append(decos, ns1.SetEndpoint(c.Endpoint))
}
if c.IgnoreSSL {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpClient.Transport = tr
}

client := ns1.NewClient(httpClient, decos...)
client.RateLimitStrategySleep()

log.Printf("[INFO] NS1 Client configured for Endpoint: %s", client.Endpoint.String())

return client, nil
}
28 changes: 7 additions & 21 deletions builtin/providers/ns1/provider.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package ns1

import (
"crypto/tls"
"net/http"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"

ns1 "gopkg.in/ns1/ns1-go.v2/rest"
)

// Provider returns a terraform.ResourceProvider.
Expand All @@ -23,13 +18,13 @@ func Provider() terraform.ResourceProvider {
"endpoint": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NS1_ENDPOINT", nil),
DefaultFunc: schema.EnvDefaultFunc("NS1_ENDPOINT", "https://api.nsone.net/v1/"),
Description: descriptions["endpoint"],
},
"ignore_ssl": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NS1_IGNORE_SSL", nil),
DefaultFunc: schema.EnvDefaultFunc("NS1_IGNORE_SSL", false),
Description: descriptions["ignore_ssl"],
},
},
Expand All @@ -49,22 +44,13 @@ func Provider() terraform.ResourceProvider {
}

func ns1Configure(d *schema.ResourceData) (interface{}, error) {
httpClient := &http.Client{}
decos := []func(*ns1.Client){}
decos = append(decos, ns1.SetAPIKey(d.Get("apikey").(string)))
if v, ok := d.GetOk("endpoint"); ok {
decos = append(decos, ns1.SetEndpoint(v.(string)))
}
if _, ok := d.GetOk("ignore_ssl"); ok {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpClient.Transport = tr
config := Config{
Key: d.Get("apikey").(string),
Endpoint: d.Get("endpoint").(string),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should follow the pattern of

if v, ok := d.GetOk("endpoint"); ok {
  config.Endpoint = v.(string)
}

when we are adding optional parameters to the config block

Sound ok?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point, i wasnt thinking when moving away from GetOk. Making fix now.

IgnoreSSL: d.Get("ignore_ssl").(bool),
}

n := ns1.NewClient(httpClient, decos...)
n.RateLimitStrategySleep()
return n, nil
return config.Client()
}

var descriptions map[string]string
Expand Down