-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generated resources for http(s)_health_check, updates to backend_…
…bucket, and import helpers (#1177) * add import helpers for generated code * Updates to backend bucket and transport.go from MM * add generated http(s)_health_check resources * name is required; transport import style * update docs with new fields/timeouts * fixes
- Loading branch information
1 parent
1d9a14d
commit edf8918
Showing
9 changed files
with
762 additions
and
259 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,66 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// Parse an import id extracting field values using the given list of regexes. | ||
// They are applied in order. The first in the list is tried first. | ||
// | ||
// e.g: | ||
// - projects/(?P<project>[^/]+)/regions/(?P<region>[^/]+)/subnetworks/(?P<name>[^/]+) (applied first) | ||
// - (?P<project>[^/]+)/(?P<region>[^/]+)/(?P<name>[^/]+), | ||
// - (?P<name>[^/]+) (applied last) | ||
func parseImportId(idRegexes []string, d TerraformResourceData, config *Config) error { | ||
for _, idFormat := range idRegexes { | ||
re, err := regexp.Compile(idFormat) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Import is not supported. Invalid regex formats.") | ||
} | ||
|
||
if fieldValues := re.FindStringSubmatch(d.Id()); fieldValues != nil { | ||
// Starting at index 1, the first match is the full string. | ||
for i := 1; i < len(fieldValues); i++ { | ||
fieldName := re.SubexpNames()[i] | ||
d.Set(fieldName, fieldValues[i]) | ||
} | ||
|
||
// The first id format is applied first and contains all the fields. | ||
err := setDefaultValues(idRegexes[0], d, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
return fmt.Errorf("Import id %q doesn't match any of the accepted formats: %v", d.Id(), idRegexes) | ||
} | ||
|
||
func setDefaultValues(idRegex string, d TerraformResourceData, config *Config) error { | ||
if _, ok := d.GetOk("project"); !ok && strings.Contains(idRegex, "?P<project>") { | ||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
d.Set("project", project) | ||
} | ||
if _, ok := d.GetOk("region"); !ok && strings.Contains(idRegex, "?P<region>") { | ||
region, err := getRegion(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
d.Set("region", region) | ||
} | ||
if _, ok := d.GetOk("zone"); !ok && strings.Contains(idRegex, "?P<zone>") { | ||
zone, err := getZone(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
d.Set("zone", zone) | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package google | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestParseImportId(t *testing.T) { | ||
regionalIdRegexes := []string{ | ||
"projects/(?P<project>[^/]+)/regions/(?P<region>[^/]+)/subnetworks/(?P<name>[^/]+)", | ||
"(?P<project>[^/]+)/(?P<region>[^/]+)/(?P<name>[^/]+)", | ||
"(?P<name>[^/]+)", | ||
} | ||
zonalIdRegexes := []string{ | ||
"projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/instances/(?P<name>[^/]+)", | ||
"(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<name>[^/]+)", | ||
"(?P<name>[^/]+)", | ||
} | ||
multipleNondefaultIdRegexes := []string{ | ||
"projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/clusters/(?P<cluster>[^/]+)/nodePools/(?P<name>[^/]+)", | ||
"(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<cluster>[^/]+)/(?P<name>[^/]+)", | ||
"(?P<cluster>[^/]+)/(?P<name>[^/]+)", | ||
} | ||
|
||
cases := map[string]struct { | ||
ImportId string | ||
IdRegexes []string | ||
Config *Config | ||
ExpectedSchemaValues map[string]interface{} | ||
ExpectError bool | ||
}{ | ||
"full self_link": { | ||
ImportId: "https://www.googleapis.com/compute/v1/projects/my-project/regions/my-region/subnetworks/my-subnetwork", | ||
IdRegexes: regionalIdRegexes, | ||
ExpectedSchemaValues: map[string]interface{}{ | ||
"project": "my-project", | ||
"region": "my-region", | ||
"name": "my-subnetwork", | ||
}, | ||
}, | ||
"relative self_link": { | ||
ImportId: "projects/my-project/regions/my-region/subnetworks/my-subnetwork", | ||
IdRegexes: regionalIdRegexes, | ||
ExpectedSchemaValues: map[string]interface{}{ | ||
"project": "my-project", | ||
"region": "my-region", | ||
"name": "my-subnetwork", | ||
}, | ||
}, | ||
"short id": { | ||
ImportId: "my-project/my-region/my-subnetwork", | ||
IdRegexes: regionalIdRegexes, | ||
ExpectedSchemaValues: map[string]interface{}{ | ||
"project": "my-project", | ||
"region": "my-region", | ||
"name": "my-subnetwork", | ||
}, | ||
}, | ||
"short id with default project and region": { | ||
ImportId: "my-subnetwork", | ||
Config: &Config{ | ||
Project: "default-project", | ||
Region: "default-region", | ||
}, | ||
IdRegexes: regionalIdRegexes, | ||
ExpectedSchemaValues: map[string]interface{}{ | ||
"project": "default-project", | ||
"region": "default-region", | ||
"name": "my-subnetwork", | ||
}, | ||
}, | ||
"short id with default project and zone": { | ||
ImportId: "my-instance", | ||
Config: &Config{ | ||
Project: "default-project", | ||
Zone: "default-zone", | ||
}, | ||
IdRegexes: zonalIdRegexes, | ||
ExpectedSchemaValues: map[string]interface{}{ | ||
"project": "default-project", | ||
"zone": "default-zone", | ||
"name": "my-instance", | ||
}, | ||
}, | ||
"short id with two nondefault fields with default project and zone": { | ||
ImportId: "my-cluster/my-node-pool", | ||
Config: &Config{ | ||
Project: "default-project", | ||
Zone: "default-zone", | ||
}, | ||
IdRegexes: multipleNondefaultIdRegexes, | ||
ExpectedSchemaValues: map[string]interface{}{ | ||
"project": "default-project", | ||
"zone": "default-zone", | ||
"cluster": "my-cluster", | ||
"name": "my-node-pool", | ||
}, | ||
}, | ||
"short id with default project and region inferred from default zone": { | ||
ImportId: "my-subnetwork", | ||
Config: &Config{ | ||
Project: "default-project", | ||
Zone: "us-east1-a", | ||
}, | ||
IdRegexes: regionalIdRegexes, | ||
ExpectedSchemaValues: map[string]interface{}{ | ||
"project": "default-project", | ||
"region": "us-east1", | ||
"name": "my-subnetwork", | ||
}, | ||
}, | ||
"invalid import id": { | ||
ImportId: "i/n/v/a/l/i/d", | ||
IdRegexes: regionalIdRegexes, | ||
ExpectError: true, | ||
}, | ||
"provider-level defaults not set": { | ||
ImportId: "my-subnetwork", | ||
IdRegexes: regionalIdRegexes, | ||
ExpectError: true, | ||
}, | ||
} | ||
|
||
for tn, tc := range cases { | ||
d := &ResourceDataMock{ | ||
FieldsInSchema: make(map[string]interface{}), | ||
id: tc.ImportId, | ||
} | ||
config := tc.Config | ||
if config == nil { | ||
config = &Config{} | ||
} | ||
|
||
if err := parseImportId(tc.IdRegexes, d, config); err == nil { | ||
for k, expectedValue := range tc.ExpectedSchemaValues { | ||
if v, ok := d.GetOk(k); ok { | ||
if v != expectedValue { | ||
t.Errorf("%s failed; Expected value %q for field %q, got %q", tn, expectedValue, k, v) | ||
} | ||
} else { | ||
t.Errorf("%s failed; Expected a value for field %q", tn, k) | ||
} | ||
} | ||
} else if !tc.ExpectError { | ||
t.Errorf("%s failed; unexpected error: %s", tn, err) | ||
} | ||
} | ||
} |
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.