-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor provider code so that we can have a resources im subdirs
- Loading branch information
Showing
124 changed files
with
1,349 additions
and
939 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package acceptance | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/equinix/terraform-provider-equinix/equinix" | ||
"github.com/equinix/terraform-provider-equinix/equinix/internal" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
const ( | ||
// duplicated from equinix_sweeoer_test.go | ||
tstResourcePrefix = "tfacc" | ||
missingMetalToken = "To run acceptance tests of Equinix Metal Resources, you must set %s" | ||
) | ||
|
||
var ( | ||
TestAccProvider *schema.Provider | ||
TestAccProviders map[string]*schema.Provider | ||
TestAccProviderFactories map[string]func() (*schema.Provider, error) | ||
TestExternalProviders map[string]resource.ExternalProvider | ||
) | ||
|
||
func init() { | ||
TestAccProvider = equinix.Provider() | ||
TestAccProviders = map[string]*schema.Provider{ | ||
"equinix": TestAccProvider, | ||
} | ||
TestAccProviderFactories = map[string]func() (*schema.Provider, error){ | ||
"equinix": func() (*schema.Provider, error) { | ||
return TestAccProvider, nil | ||
}, | ||
} | ||
TestExternalProviders = map[string]resource.ExternalProvider{ | ||
"random": { | ||
Source: "hashicorp/random", | ||
}, | ||
} | ||
} | ||
|
||
func TestAccPreCheckMetal(t *testing.T) { | ||
if os.Getenv(equinix.MetalAuthTokenEnvVar) == "" { | ||
t.Fatalf(missingMetalToken, equinix.MetalAuthTokenEnvVar) | ||
} | ||
} | ||
|
||
func IsSweepableTestResource(namePrefix string) bool { | ||
return strings.HasPrefix(namePrefix, tstResourcePrefix) | ||
} | ||
|
||
func getFromEnvDefault(varName string, defaultValue string) string { | ||
if v := os.Getenv(varName); v != "" { | ||
return v | ||
} | ||
return defaultValue | ||
} | ||
|
||
func GetConfigForNonStandardMetalTest() (*internal.Config, error) { | ||
endpoint := getFromEnvDefault(equinix.EndpointEnvVar, internal.DefaultBaseURL) | ||
clientTimeout := getFromEnvDefault(equinix.ClientTimeoutEnvVar, strconv.Itoa(internal.DefaultTimeout)) | ||
clientTimeoutInt, err := strconv.Atoi(clientTimeout) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot convert value of '%s' env variable to int", equinix.ClientTimeoutEnvVar) | ||
} | ||
metalAuthToken := getFromEnvDefault(equinix.MetalAuthTokenEnvVar, "") | ||
|
||
if metalAuthToken == "" { | ||
return nil, fmt.Errorf(missingMetalToken, equinix.MetalAuthTokenEnvVar) | ||
} | ||
|
||
return &internal.Config{ | ||
AuthToken: metalAuthToken, | ||
BaseURL: endpoint, | ||
RequestTimeout: time.Duration(clientTimeoutInt) * time.Second, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package acceptance | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// list of plans and metros and os used as filter criteria to find available hardware to run tests | ||
var ( | ||
Preferable_plans = []string{"x1.small.x86", "t1.small.x86", "c2.medium.x86", "c3.small.x86", "c3.medium.x86", "m3.small.x86"} | ||
Preferable_metros = []string{"ch", "ny", "sv", "ty", "am"} | ||
Preferable_os = []string{"ubuntu_20_04"} | ||
) | ||
|
||
func TestDeviceTerminationTime() string { | ||
return time.Now().UTC().Add(60 * time.Minute).Format(time.RFC3339) | ||
} | ||
|
||
// This function should be used to find available plans in all test where a metal_device resource is needed. | ||
// | ||
// TODO consider adding a datasource for equinix_metal_operating_system and making the local.os conditional | ||
// | ||
// https://github.com/equinix/terraform-provider-equinix/pull/220#discussion_r915418418equinix_metal_operating_system | ||
// https://github.com/equinix/terraform-provider-equinix/discussions/221 | ||
func ConfAccMetalDevice_base(plans, metros, os []string) string { | ||
return fmt.Sprintf(` | ||
data "equinix_metal_plans" "test" { | ||
sort { | ||
attribute = "id" | ||
direction = "asc" | ||
} | ||
filter { | ||
attribute = "name" | ||
values = [%s] | ||
} | ||
filter { | ||
attribute = "available_in_metros" | ||
values = [%s] | ||
} | ||
filter { | ||
attribute = "deployment_types" | ||
values = ["on_demand", "spot_market"] | ||
} | ||
} | ||
// Select a metal plan randomly and lock it in | ||
// so that we don't pick a different one for | ||
// every subsequent terraform plan | ||
resource "random_integer" "plan_idx" { | ||
min = 0 | ||
max = length(data.equinix_metal_plans.test.plans) - 1 | ||
} | ||
resource "terraform_data" "plan" { | ||
input = data.equinix_metal_plans.test.plans[random_integer.plan_idx.result] | ||
lifecycle { | ||
ignore_changes = ["input"] | ||
} | ||
} | ||
resource "terraform_data" "facilities" { | ||
input = sort(tolist(setsubtract(terraform_data.plan.output.available_in, ["nrt1", "dfw2", "ewr1", "ams1", "sjc1", "ld7", "sy4", "ny6"]))) | ||
lifecycle { | ||
ignore_changes = ["input"] | ||
} | ||
} | ||
// Select a metal facility randomly and lock it in | ||
// so that we don't pick a different one for | ||
// every subsequent terraform plan | ||
resource "random_integer" "facility_idx" { | ||
min = 0 | ||
max = length(local.facilities) - 1 | ||
} | ||
resource "terraform_data" "facility" { | ||
input = local.facilities[random_integer.facility_idx.result] | ||
lifecycle { | ||
ignore_changes = ["input"] | ||
} | ||
} | ||
// Select a metal metro randomly and lock it in | ||
// so that we don't pick a different one for | ||
// every subsequent terraform plan | ||
resource "random_integer" "metro_idx" { | ||
min = 0 | ||
max = length(local.metros) - 1 | ||
} | ||
resource "terraform_data" "metro" { | ||
input = local.metros[random_integer.metro_idx.result] | ||
lifecycle { | ||
ignore_changes = ["input"] | ||
} | ||
} | ||
locals { | ||
// Select a random plan | ||
plan = terraform_data.plan.output.slug | ||
// Select a random facility from the facilities in which the selected plan is available, excluding decommed facilities | ||
facilities = terraform_data.facilities.output | ||
facility = terraform_data.facility.output | ||
// Select a random metro from the metros in which the selected plan is available | ||
metros = sort(tolist(terraform_data.plan.output.available_in_metros)) | ||
metro = terraform_data.metro.output | ||
os = [%s][0] | ||
} | ||
`, fmt.Sprintf("\"%s\"", strings.Join(plans[:], `","`)), fmt.Sprintf("\"%s\"", strings.Join(metros[:], `","`)), fmt.Sprintf("\"%s\"", strings.Join(os[:], `","`))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package acceptance | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/equinix/terraform-provider-equinix/equinix/internal" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
"github.com/packethost/packngo" | ||
) | ||
|
||
|
||
func TestAccCheckMetalSSHKeyExists(n string, key *packngo.SSHKey) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No Record ID is set") | ||
} | ||
|
||
client := TestAccProvider.Meta().(*internal.Config).Metal | ||
|
||
foundKey, _, err := client.SSHKeys.Get(rs.Primary.ID, nil) | ||
if err != nil { | ||
return err | ||
} | ||
if foundKey.ID != rs.Primary.ID { | ||
return fmt.Errorf("SSh Key not found: %v - %v", rs.Primary.ID, foundKey) | ||
} | ||
|
||
*key = *foundKey | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.