-
Notifications
You must be signed in to change notification settings - Fork 47
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
refactor: move device resource and datasource to separate package #574
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package equinix | ||
|
||
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"} | ||
) | ||
|
||
// Deprecated: use the identical TestDeviceTerminationTime from internal/acceptance instead | ||
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[:], `","`))) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package equinix | ||
package device | ||
|
||
import ( | ||
"context" | ||
|
@@ -11,8 +11,8 @@ import ( | |
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceMetalDevices() *schema.Resource { | ||
dsmd := dataSourceMetalDevice() | ||
func ListDataSource() *schema.Resource { | ||
dsmd := DataSource() | ||
sch := dsmd.Schema | ||
Comment on lines
-14
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I opted to move the If that is agreed to be a good approach, then that raises the question: should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The pluralization of the resource names, as in multi-response datasources, was a subtle point that I missed on first glance of this comment. The AWS provider followed this pattern. Seems fine to me. |
||
for _, v := range sch { | ||
if v.Optional { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chose to duplicate this matcher instead of finding a place to put it. IMO we can clean that up after framework and equinix-sdk-go migrations are done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the validation this enforces is necessary in the Terraform provider.
terraform-provider-equinix/equinix/resource_metal_spot_market_request.go
Lines 274 to 277 in 21c9dcc
Perhaps this block would be better as a warning or left to the API to enforce. It's well enough to leave this validation alone since there are no known edge cases where the combination of an IPXE URL and a Custom IPXE Userdata are necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps the regexp could be moved closer to where it is used since it is not used elsewhere.