terraform-provider-google/google/import.go
Dana Hoffman edf8918db6
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
2018-03-09 17:14:32 -08:00

67 lines
1.8 KiB
Go

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
}