Skip to content

Commit 313927a

Browse files
committed
Adding custom import
1 parent 5a7e1f2 commit 313927a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

mmv1/products/apigee/Space.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ update_mask: true
2727
import_format:
2828
- "{{org_id}}/spaces/{{space_id}}"
2929
- "{{org_id}}/{{space_id}}"
30+
custom_code:
31+
custom_import: "templates/terraform/custom_import/apigee_space.go.tmpl"
3032
autogen_async: true
3133
iam_policy:
3234
method_name_separator: ':'
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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/{{org_name}}/spaces/{{space_id}}`
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/{{org_name}}/{{space_id}}`
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

Comments
 (0)