|
| 1 | +config := meta.(*transport_tpg.Config) |
| 2 | + |
| 3 | +// current import_formats cannot import fields with forward slashes in their value |
| 4 | +if err := tpgresource.ParseImportId([]string{"(?P<name>.+)"}, d, config); err != nil { |
| 5 | + return nil, err |
| 6 | +} |
| 7 | + |
| 8 | +nameParts := strings.Split(d.Get("name").(string), "/") |
| 9 | +if len(nameParts) == 4 { |
| 10 | + // `organizations/%s/spaces/%s` |
| 11 | + orgId := fmt.Sprintf("organizations/%s", nameParts[1]) |
| 12 | + if err := d.Set("org_id", orgId); err != nil { |
| 13 | + return nil, fmt.Errorf("Error setting org_id: %s", err) |
| 14 | + } |
| 15 | + if err := d.Set("space_id", nameParts[3]); err != nil { |
| 16 | + return nil, fmt.Errorf("Error setting space_id: %s", err) |
| 17 | + } |
| 18 | +} else if len(nameParts) == 3 { |
| 19 | + // `organizations/%s/%s` |
| 20 | + orgId := fmt.Sprintf("organizations/%s", nameParts[1]) |
| 21 | + if err := d.Set("org_id", orgId); err != nil { |
| 22 | + return nil, fmt.Errorf("Error setting org_id: %s", err) |
| 23 | + } |
| 24 | + if err := d.Set("space_id", nameParts[2]); err != nil { |
| 25 | + return nil, fmt.Errorf("Error setting space_id: %s", err) |
| 26 | + } |
| 27 | +} else { |
| 28 | + return nil, fmt.Errorf("Saw %s when the name is expected to have shape %s or %s", d.Get("name"), "organizations/{org_name}/spaces/{space_id}", "organizations/{org_name}/{space_id}") |
| 29 | +} |
| 30 | + |
| 31 | +// Replace import id for the resource id |
| 32 | +id, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}org_id{{"}}"}}/spaces/{{"{{"}}space_id{{"}}"}}") |
| 33 | +if err != nil { |
| 34 | + return nil, fmt.Errorf("Error constructing id: %s", err) |
| 35 | +} |
| 36 | +d.SetId(id) |
| 37 | + |
| 38 | +return []*schema.ResourceData{d}, nil |
0 commit comments