mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-06 17:16:44 +00:00
![Nic Cope](/assets/img/avatar_default.png)
* Move AliasIpRange helpers into utils To reflect the fact they'll be used by multiple resources. * Pass Config to build helpers, not meta It's the only thing meta is used for. * Refactor getNetwork util methods to return early for the happy path. * Update compute APIs compute.Instance.MinCpuPlatform is now GA. * Fix panic in TestComputeInstanceMigrateState This seemed to be a pre-existing issue, i.e. I could repro it in master. --- FAIL: TestComputeInstanceMigrateState (0.00s) panic: interface conversion: interface {} is nil, not *google.Config [recovered] panic: interface conversion: interface {} is nil, not *google.Config goroutine 85 [running]: testing.tRunner.func1(0xc4205d60f0) /usr/local/Cellar/go/1.9.1/libexec/src/testing/testing.go:711 +0x2d2 panic(0x203acc0, 0xc4205d2080) /usr/local/Cellar/go/1.9.1/libexec/src/runtime/panic.go:491 +0x283 github.com/terraform-providers/terraform-provider-google/google.migrateStateV3toV4(0xc4205f2000, 0x0, 0x0, 0x0, 0x48, 0xc4205f2000) /Users/negz/control/go/src/github.com/terraform-providers/terraform-provider-google/google/resource_compute_instance_migrate.go:182 +0x2405 github.com/terraform-providers/terraform-provider-google/google.resourceComputeInstanceMigrateState(0x2, 0xc4205f2000, 0x0, 0x0, 0x0, 0x0, 0xe0000000000) /Users/negz/control/go/src/github.com/terraform-providers/terraform-provider-google/google/resource_compute_instance_migrate.go:48 +0x21a github.com/terraform-providers/terraform-provider-google/google.runInstanceMigrateTest(0xc4205d60f0, 0x2260816, 0x8, 0x227d23a, 0x20, 0x2, 0xc4205ec0f0, 0xc4205ec120, 0x0, 0x0) /Users/negz/control/go/src/github.com/terraform-providers/terraform-provider-google/google/resource_compute_instance_migrate_test.go:803 +0xc1 github.com/terraform-providers/terraform-provider-google/google.TestComputeInstanceMigrateState(0xc4205d60f0) /Users/negz/control/go/src/github.com/terraform-providers/terraform-provider-google/google/resource_compute_instance_migrate_test.go:71 +0xc84 testing.tRunner(0xc4205d60f0, 0x22d81c0) /usr/local/Cellar/go/1.9.1/libexec/src/testing/testing.go:746 +0xd0 created by testing.(*T).Run /usr/local/Cellar/go/1.9.1/libexec/src/testing/testing.go:789 +0x2de FAIL github.com/terraform-providers/terraform-provider-google/google 0.035s * Use only the v1 API for resource_compute_instance Alias IP ranges, Accelerators, and min CPU platform are now GA. * Move common instance code into utils.go Methods used by both resource_compute_instance and resource_compute_instance_template are currently spread between their respective files, and utils.go. This commit moves them all into utils.go for the sake of consistency. It may be worth considering an instance_common.go file or similar. * Unify compute_instance and compute_instance_template network_interface and service_account code This has the side effect of enabling Alias IP range support for compute_instance_templates. * Add tests for compute instance template Alias IP ranges * Mark instance template region as computed We compute it from the subnet its network interfaces are in. Note this is not new behaviour - I believe it was erroneously missing the computed flag. * Support guest accelerators for instance templates Since most of the code is already there. * Add a test for using 'address' rather than 'network_ip' for instance templates * Don't mark assigned_nat_ip as deprecated * Remove network_interface schema fields that don't make sense for a compute instance template * Add newline after count in instance template docs * Don't try to dedupe guest accelerator expansion code The API calls to Google to create guest accelerators take different values for instances and instance templates. Instance templates don't have a zone and can thus *only* be passed a guest accelerator name. * Use ParseNetworkFieldValue instead of getNetworkLink * Add support for parsing regional fields, and subnetworks specifically Currently unused because subnetworks may have a separate project from that of the instance using them, which complicates looking up the project field. * Fall back to provider region when parsing regional field values Also slightly refactors getXFromSchema field helper functions for readability. * Revert to assigned_nat_ip in compute instance docs * Add beta scaffolding to compute instance and compute instance template Note these resources don't currently use beta features - this is futureproofing. * Fix indentation in comment about instance template alias IP ranges * Consolidate metadata helper functions in metadata.go * Move compute instance (and template) related helpers into their own file
334 lines
12 KiB
Go
334 lines
12 KiB
Go
package google
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestParseGlobalFieldValue(t *testing.T) {
|
|
const resourceType = "networks"
|
|
cases := map[string]struct {
|
|
FieldValue string
|
|
ExpectedRelativeLink string
|
|
ExpectedError bool
|
|
IsEmptyValid bool
|
|
ProjectSchemaField string
|
|
ProjectSchemaValue string
|
|
Config *Config
|
|
}{
|
|
"network is a full self link": {
|
|
FieldValue: "https://www.googleapis.com/compute/v1/projects/myproject/global/networks/my-network",
|
|
ExpectedRelativeLink: "projects/myproject/global/networks/my-network",
|
|
},
|
|
"network is a relative self link": {
|
|
FieldValue: "projects/myproject/global/networks/my-network",
|
|
ExpectedRelativeLink: "projects/myproject/global/networks/my-network",
|
|
},
|
|
"network is a partial relative self link": {
|
|
FieldValue: "global/networks/my-network",
|
|
Config: &Config{Project: "default-project"},
|
|
ExpectedRelativeLink: "projects/default-project/global/networks/my-network",
|
|
},
|
|
"network is the name only": {
|
|
FieldValue: "my-network",
|
|
Config: &Config{Project: "default-project"},
|
|
ExpectedRelativeLink: "projects/default-project/global/networks/my-network",
|
|
},
|
|
"network is the name only and has a project set in schema": {
|
|
FieldValue: "my-network",
|
|
ProjectSchemaField: "project",
|
|
ProjectSchemaValue: "schema-project",
|
|
Config: &Config{Project: "default-project"},
|
|
ExpectedRelativeLink: "projects/schema-project/global/networks/my-network",
|
|
},
|
|
"network is the name only and has a project set in schema but the field is not specified.": {
|
|
FieldValue: "my-network",
|
|
ProjectSchemaValue: "schema-project",
|
|
Config: &Config{Project: "default-project"},
|
|
ExpectedRelativeLink: "projects/default-project/global/networks/my-network",
|
|
},
|
|
"network is empty and it is valid": {
|
|
FieldValue: "",
|
|
IsEmptyValid: true,
|
|
ExpectedRelativeLink: "",
|
|
},
|
|
"network is empty and it is not valid": {
|
|
FieldValue: "",
|
|
IsEmptyValid: false,
|
|
ExpectedError: true,
|
|
},
|
|
}
|
|
|
|
for tn, tc := range cases {
|
|
fieldsInSchema := make(map[string]interface{})
|
|
|
|
if len(tc.ProjectSchemaValue) > 0 && len(tc.ProjectSchemaField) > 0 {
|
|
fieldsInSchema[tc.ProjectSchemaField] = tc.ProjectSchemaValue
|
|
}
|
|
|
|
d := &ResourceDataMock{
|
|
FieldsInSchema: fieldsInSchema,
|
|
}
|
|
|
|
v, err := parseGlobalFieldValue(resourceType, tc.FieldValue, tc.ProjectSchemaField, d, tc.Config, tc.IsEmptyValid)
|
|
|
|
if err != nil {
|
|
if !tc.ExpectedError {
|
|
t.Errorf("bad: %s, did not expect an error. Error: %s", tn, err)
|
|
}
|
|
} else {
|
|
if v.RelativeLink() != tc.ExpectedRelativeLink {
|
|
t.Errorf("bad: %s, expected relative link to be '%s' but got '%s'", tn, tc.ExpectedRelativeLink, v.RelativeLink())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseZonalFieldValue(t *testing.T) {
|
|
const resourceType = "instances"
|
|
cases := map[string]struct {
|
|
FieldValue string
|
|
ExpectedRelativeLink string
|
|
ExpectedError bool
|
|
IsEmptyValid bool
|
|
ProjectSchemaField string
|
|
ProjectSchemaValue string
|
|
ZoneSchemaField string
|
|
ZoneSchemaValue string
|
|
Config *Config
|
|
}{
|
|
"instance is a full self link": {
|
|
FieldValue: "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-b/instances/my-instance",
|
|
ExpectedRelativeLink: "projects/myproject/zones/us-central1-b/instances/my-instance",
|
|
},
|
|
"instance is a relative self link": {
|
|
FieldValue: "projects/myproject/zones/us-central1-b/instances/my-instance",
|
|
ExpectedRelativeLink: "projects/myproject/zones/us-central1-b/instances/my-instance",
|
|
},
|
|
"instance is a partial relative self link": {
|
|
FieldValue: "zones/us-central1-b/instances/my-instance",
|
|
Config: &Config{Project: "default-project"},
|
|
ExpectedRelativeLink: "projects/default-project/zones/us-central1-b/instances/my-instance",
|
|
},
|
|
"instance is the name only": {
|
|
FieldValue: "my-instance",
|
|
ZoneSchemaField: "zone",
|
|
ZoneSchemaValue: "us-east1-a",
|
|
Config: &Config{Project: "default-project"},
|
|
ExpectedRelativeLink: "projects/default-project/zones/us-east1-a/instances/my-instance",
|
|
},
|
|
"instance is the name only and has a project set in schema": {
|
|
FieldValue: "my-instance",
|
|
ProjectSchemaField: "project",
|
|
ProjectSchemaValue: "schema-project",
|
|
ZoneSchemaField: "zone",
|
|
ZoneSchemaValue: "us-east1-a",
|
|
Config: &Config{Project: "default-project"},
|
|
ExpectedRelativeLink: "projects/schema-project/zones/us-east1-a/instances/my-instance",
|
|
},
|
|
"instance is the name only and has a project set in schema but the field is not specified.": {
|
|
FieldValue: "my-instance",
|
|
ProjectSchemaValue: "schema-project",
|
|
ZoneSchemaField: "zone",
|
|
ZoneSchemaValue: "us-east1-a",
|
|
Config: &Config{Project: "default-project"},
|
|
ExpectedRelativeLink: "projects/default-project/zones/us-east1-a/instances/my-instance",
|
|
},
|
|
"instance is the name only and no zone field is specified": {
|
|
FieldValue: "my-instance",
|
|
Config: &Config{Project: "default-project"},
|
|
ExpectedError: true,
|
|
},
|
|
"instance is the name only and no value for zone field is specified": {
|
|
FieldValue: "my-instance",
|
|
ZoneSchemaField: "zone",
|
|
Config: &Config{Project: "default-project"},
|
|
ExpectedError: true,
|
|
},
|
|
"instance is empty and it is valid": {
|
|
FieldValue: "",
|
|
IsEmptyValid: true,
|
|
ExpectedRelativeLink: "",
|
|
},
|
|
"instance is empty and it is not valid": {
|
|
FieldValue: "",
|
|
IsEmptyValid: false,
|
|
ExpectedError: true,
|
|
},
|
|
}
|
|
|
|
for tn, tc := range cases {
|
|
fieldsInSchema := make(map[string]interface{})
|
|
|
|
if len(tc.ProjectSchemaValue) > 0 && len(tc.ProjectSchemaField) > 0 {
|
|
fieldsInSchema[tc.ProjectSchemaField] = tc.ProjectSchemaValue
|
|
}
|
|
|
|
if len(tc.ZoneSchemaValue) > 0 && len(tc.ZoneSchemaField) > 0 {
|
|
fieldsInSchema[tc.ZoneSchemaField] = tc.ZoneSchemaValue
|
|
}
|
|
|
|
d := &ResourceDataMock{
|
|
FieldsInSchema: fieldsInSchema,
|
|
}
|
|
|
|
v, err := parseZonalFieldValue(resourceType, tc.FieldValue, tc.ProjectSchemaField, tc.ZoneSchemaField, d, tc.Config, tc.IsEmptyValid)
|
|
|
|
if err != nil {
|
|
if !tc.ExpectedError {
|
|
t.Errorf("bad: %s, did not expect an error. Error: %s", tn, err)
|
|
}
|
|
} else {
|
|
if v.RelativeLink() != tc.ExpectedRelativeLink {
|
|
t.Errorf("bad: %s, expected relative link to be '%s' but got '%s'", tn, tc.ExpectedRelativeLink, v.RelativeLink())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseOrganizationFieldValue(t *testing.T) {
|
|
const resourceType = "roles"
|
|
cases := map[string]struct {
|
|
FieldValue string
|
|
ExpectedRelativeLink string
|
|
ExpectedName string
|
|
ExpectedOrgId string
|
|
ExpectedError bool
|
|
IsEmptyValid bool
|
|
}{
|
|
"role is valid": {
|
|
FieldValue: "organizations/123/roles/custom",
|
|
ExpectedRelativeLink: "organizations/123/roles/custom",
|
|
ExpectedName: "custom",
|
|
ExpectedOrgId: "123",
|
|
},
|
|
"role is empty and it is valid": {
|
|
FieldValue: "",
|
|
IsEmptyValid: true,
|
|
ExpectedRelativeLink: "",
|
|
},
|
|
"role is empty and it is not valid": {
|
|
FieldValue: "",
|
|
IsEmptyValid: false,
|
|
ExpectedError: true,
|
|
},
|
|
}
|
|
|
|
for tn, tc := range cases {
|
|
v, err := parseOrganizationFieldValue(resourceType, tc.FieldValue, tc.IsEmptyValid)
|
|
|
|
if err != nil {
|
|
if !tc.ExpectedError {
|
|
t.Errorf("bad: %s, did not expect an error. Error: %s", tn, err)
|
|
}
|
|
} else {
|
|
if v.RelativeLink() != tc.ExpectedRelativeLink {
|
|
t.Errorf("bad: %s, expected relative link to be '%s' but got '%s'", tn, tc.ExpectedRelativeLink, v.RelativeLink())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseRegionalFieldValue(t *testing.T) {
|
|
const resourceType = "subnetworks"
|
|
cases := map[string]struct {
|
|
FieldValue string
|
|
ExpectedRelativeLink string
|
|
ExpectedError bool
|
|
IsEmptyValid bool
|
|
ProjectSchemaField string
|
|
ProjectSchemaValue string
|
|
RegionSchemaField string
|
|
RegionSchemaValue string
|
|
Config *Config
|
|
}{
|
|
"subnetwork is a full self link": {
|
|
FieldValue: "https://www.googleapis.com/compute/v1/projects/myproject/regions/us-central1/subnetworks/my-subnetwork",
|
|
ExpectedRelativeLink: "projects/myproject/regions/us-central1/subnetworks/my-subnetwork",
|
|
},
|
|
"subnetwork is a relative self link": {
|
|
FieldValue: "projects/myproject/regions/us-central1/subnetworks/my-subnetwork",
|
|
ExpectedRelativeLink: "projects/myproject/regions/us-central1/subnetworks/my-subnetwork",
|
|
},
|
|
"subnetwork is a partial relative self link": {
|
|
FieldValue: "regions/us-central1/subnetworks/my-subnetwork",
|
|
Config: &Config{Project: "default-project", Region: "default-region"},
|
|
ExpectedRelativeLink: "projects/default-project/regions/us-central1/subnetworks/my-subnetwork",
|
|
},
|
|
"subnetwork is the name only": {
|
|
FieldValue: "my-subnetwork",
|
|
RegionSchemaField: "region",
|
|
RegionSchemaValue: "us-east1",
|
|
Config: &Config{Project: "default-project"},
|
|
ExpectedRelativeLink: "projects/default-project/regions/us-east1/subnetworks/my-subnetwork",
|
|
},
|
|
"subnetwork is the name only and has a project set in schema": {
|
|
FieldValue: "my-subnetwork",
|
|
ProjectSchemaField: "project",
|
|
ProjectSchemaValue: "schema-project",
|
|
RegionSchemaField: "region",
|
|
RegionSchemaValue: "us-east1",
|
|
Config: &Config{Project: "default-project", Region: "default-region"},
|
|
ExpectedRelativeLink: "projects/schema-project/regions/us-east1/subnetworks/my-subnetwork",
|
|
},
|
|
"subnetwork is the name only and has a project set in schema but the field is not specified.": {
|
|
FieldValue: "my-subnetwork",
|
|
ProjectSchemaValue: "schema-project",
|
|
RegionSchemaField: "region",
|
|
RegionSchemaValue: "us-east1",
|
|
Config: &Config{Project: "default-project", Region: "default-region"},
|
|
ExpectedRelativeLink: "projects/default-project/regions/us-east1/subnetworks/my-subnetwork",
|
|
},
|
|
"subnetwork is the name only and no region field is specified": {
|
|
FieldValue: "my-subnetwork",
|
|
Config: &Config{Project: "default-project", Region: "default-region"},
|
|
ExpectedRelativeLink: "projects/default-project/regions/default-region/subnetworks/my-subnetwork",
|
|
},
|
|
"subnetwork is the name only and no value for region field is specified": {
|
|
FieldValue: "my-subnetwork",
|
|
RegionSchemaField: "region",
|
|
Config: &Config{Project: "default-project", Region: "default-region"},
|
|
ExpectedRelativeLink: "projects/default-project/regions/default-region/subnetworks/my-subnetwork",
|
|
},
|
|
"subnetwork is empty and it is valid": {
|
|
FieldValue: "",
|
|
IsEmptyValid: true,
|
|
ExpectedRelativeLink: "",
|
|
},
|
|
"subnetwork is empty and it is not valid": {
|
|
FieldValue: "",
|
|
IsEmptyValid: false,
|
|
ExpectedError: true,
|
|
},
|
|
}
|
|
|
|
for tn, tc := range cases {
|
|
t.Run(tn, func(t *testing.T) {
|
|
fieldsInSchema := make(map[string]interface{})
|
|
|
|
if len(tc.ProjectSchemaValue) > 0 && len(tc.ProjectSchemaField) > 0 {
|
|
fieldsInSchema[tc.ProjectSchemaField] = tc.ProjectSchemaValue
|
|
}
|
|
|
|
if len(tc.RegionSchemaValue) > 0 && len(tc.RegionSchemaField) > 0 {
|
|
fieldsInSchema[tc.RegionSchemaField] = tc.RegionSchemaValue
|
|
}
|
|
|
|
d := &ResourceDataMock{
|
|
FieldsInSchema: fieldsInSchema,
|
|
}
|
|
|
|
v, err := parseRegionalFieldValue(resourceType, tc.FieldValue, tc.ProjectSchemaField, tc.RegionSchemaField, d, tc.Config, tc.IsEmptyValid)
|
|
|
|
if err != nil {
|
|
if !tc.ExpectedError {
|
|
t.Errorf("bad: did not expect an error. Error: %s", err)
|
|
}
|
|
} else {
|
|
if v.RelativeLink() != tc.ExpectedRelativeLink {
|
|
t.Errorf("bad: expected relative link to be '%s' but got '%s'", tc.ExpectedRelativeLink, v.RelativeLink())
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|