Skip to content

Commit

Permalink
Optional Api Url Parameter (#23)
Browse files Browse the repository at this point in the history
* added new default parameter for the url

* moved default url to parameter, optional env get method

* moved helper function to more appropriate location

* renamed helper function

* moved default config

* updated casing of the optional environment variable test function

Co-authored-by: Jordan <jordan.braiuka@instaclustr.com>
  • Loading branch information
jbraiuka and jordanbraiuka authored May 6, 2020
1 parent c73b401 commit 87450d9
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 12 deletions.
3 changes: 1 addition & 2 deletions instaclustr/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package instaclustr

const (
ApiHostname string = "https://api.instaclustr.com"
DefaultApiHostname string = "https://api.instaclustr.com"
)

type Config struct {
Expand All @@ -13,7 +13,6 @@ type Config struct {
}

func (c *Config) Init() {
c.apiServerHostname = ApiHostname
c.Client = new(APIClient)
c.Client.InitClient(c.apiServerHostname, c.Username, c.ApiKey)
}
Expand Down
10 changes: 8 additions & 2 deletions instaclustr/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func Provider() *schema.Provider {
Type: schema.TypeString,
Required: true,
},
"api_hostname": {
Type: schema.TypeString,
Optional: true,
Default: DefaultApiHostname,
},
},

ResourcesMap: map[string]*schema.Resource{
Expand All @@ -31,8 +36,9 @@ func Provider() *schema.Provider {

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{
Username: d.Get("username").(string),
ApiKey: d.Get("api_key").(string),
Username: d.Get("username").(string),
ApiKey: d.Get("api_key").(string),
apiServerHostname: d.Get("api_hostname").(string),
}

config.Init()
Expand Down
7 changes: 4 additions & 3 deletions test/resource_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/instaclustr/terraform-provider-instaclustr/instaclustr"
)


func TestAccCluster(t *testing.T) {
testAccProvider := instaclustr.Provider()
testAccProviders := map[string]terraform.ResourceProvider{
Expand All @@ -23,7 +24,7 @@ func TestAccCluster(t *testing.T) {
apiKey := os.Getenv("IC_API_KEY")
oriConfig := fmt.Sprintf(string(validConfig), username, apiKey)
updatedConfig := strings.Replace(oriConfig, "testcluster", "newcluster", 1)
hostname := instaclustr.ApiHostname
hostname := getOptionalEnv("IC_API_URL", instaclustr.DefaultApiHostname)
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
PreCheck: func() { AccTestEnvVarsCheck(t) },
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestAccClusterResize(t *testing.T) {
invalidResizeClassConfig := strings.Replace(oriConfig, "resizeable-small(r5-l)", "resizeable-large(r5-xl)", 1)
invalidResizeConfig := strings.Replace(oriConfig, "resizeable-small(r5-l)", "t3.medium", 1)

hostname := instaclustr.ApiHostname
hostname := getOptionalEnv("IC_API_URL", instaclustr.DefaultApiHostname)
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
PreCheck: func() { AccTestEnvVarsCheck(t) },
Expand Down Expand Up @@ -142,7 +143,7 @@ func TestAccClusterCustomVPC(t *testing.T) {
providerAccountName := os.Getenv("IC_PROV_ACC_NAME")
providerVpcId := os.Getenv("IC_PROV_VPC_ID")
oriConfig := fmt.Sprintf(string(validConfig), username, apiKey, providerAccountName, providerVpcId)
hostname := instaclustr.ApiHostname
hostname := getOptionalEnv("IC_API_URL", instaclustr.DefaultApiHostname)
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
PreCheck: func() { AccTestEnvVarsCheck(t) },
Expand Down
2 changes: 1 addition & 1 deletion test/resource_encryption_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestAccEBSKey(t *testing.T) {
apiKey := os.Getenv("IC_API_KEY")
kmsArn := os.Getenv("KMS_ARN")
oriConfig := fmt.Sprintf(string(validConfig), username, apiKey, kmsArn)
hostname := instaclustr.ApiHostname
hostname := getOptionalEnv("IC_API_URL", instaclustr.DefaultApiHostname)
resource.Test(t, resource.TestCase{
Providers: testAccEBSKeyProviders,
PreCheck: func() { AccTestEnvVarsCheck(t) },
Expand Down
2 changes: 1 addition & 1 deletion test/resource_firewall_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAccFirewallRuleResource(t *testing.T) {
apiKey := os.Getenv("IC_API_KEY")
config := fmt.Sprintf(string(tfFile), username, apiKey)

hostname := instaclustr.ApiHostname
hostname := getOptionalEnv("IC_API_URL", instaclustr.DefaultApiHostname)
resource.Test(t, resource.TestCase{
Providers: testProviders,
CheckDestroy: checkFirewallRuleDeleted(hostname, username, apiKey),
Expand Down
4 changes: 2 additions & 2 deletions test/resource_pci_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAccPCICluster(t *testing.T) {
apiKey := os.Getenv("IC_API_KEY")
oriConfig := fmt.Sprintf(string(validConfig), username, apiKey)
updatedConfig := strings.Replace(oriConfig, "testcluster", "newcluster", 1)
hostname := instaclustr.ApiHostname
hostname := getOptionalEnv("IC_API_URL", instaclustr.DefaultApiHostname)
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
PreCheck: func() { AccTestEnvVarsCheck(t) },
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestAccPCIClusterResize(t *testing.T) {
invalidResizeClassConfig := strings.Replace(oriConfig, "resizeable-small(r5-l)", "resizeable-large(r5-xl)", 1)
invalidResizeConfig := strings.Replace(oriConfig, "resizeable-small(r5-l)", "t3.medium", 1)

hostname := instaclustr.ApiHostname
hostname := getOptionalEnv("IC_API_URL", instaclustr.DefaultApiHostname)
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
PreCheck: func() { AccTestEnvVarsCheck(t) },
Expand Down
2 changes: 1 addition & 1 deletion test/resource_vpc_peering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAccVpcPeeringResource(t *testing.T) {
apiKey := os.Getenv("IC_API_KEY")
config := fmt.Sprintf(string(tfFile), username, apiKey)

hostname := instaclustr.ApiHostname
hostname := getOptionalEnv("IC_API_URL", instaclustr.DefaultApiHostname)
resource.Test(t, resource.TestCase{
Providers: testProviders,
CheckDestroy: checkVpcPeeringDeleted(hostname, username, apiKey),
Expand Down
9 changes: 9 additions & 0 deletions test/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ func AccTestEnvVarsCheck(t *testing.T) {
t.Fatal("IC_PROV_VPC_ID for provisioning API must be set for acceptance tests")
}
}


func getOptionalEnv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}

0 comments on commit 87450d9

Please sign in to comment.