Skip to content

Commit 78c0a3d

Browse files
authored
Merge branch 'FEATURE-BRANCH-resource-identity' into sync-main-oct-20-FEATURE-BRANCH-resource-identity
2 parents bd6a648 + be07d5b commit 78c0a3d

File tree

5 files changed

+105
-9
lines changed

5 files changed

+105
-9
lines changed

mmv1/third_party/terraform/tpgresource/import.go

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strconv"
88
"strings"
99

10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
1112
)
1213

@@ -20,12 +21,14 @@ import (
2021
func ParseImportId(idRegexes []string, d TerraformResourceData, config *transport_tpg.Config) error {
2122
for _, idFormat := range idRegexes {
2223
re, err := regexp.Compile(idFormat)
23-
2424
if err != nil {
2525
log.Printf("[DEBUG] Could not compile %s.", idFormat)
2626
return fmt.Errorf("Import is not supported. Invalid regex formats.")
2727
}
28-
28+
identity, err := d.Identity()
29+
if identity == nil {
30+
log.Printf("[DEBUG] identity not set: %s", err)
31+
}
2932
if fieldValues := re.FindStringSubmatch(d.Id()); fieldValues != nil {
3033
log.Printf("[DEBUG] matching ID %s to regex %s.", d.Id(), idFormat)
3134
// Starting at index 1, the first match is the full string.
@@ -47,13 +50,23 @@ func ParseImportId(idRegexes []string, d TerraformResourceData, config *transpor
4750
if err = d.Set(fieldName, fieldValue); err != nil {
4851
return err
4952
}
53+
if identity != nil {
54+
if err = identity.Set(fieldName, fieldValue); err != nil {
55+
return err
56+
}
57+
}
5058
} else if _, ok := val.(int); ok {
5159
if intVal, atoiErr := strconv.Atoi(fieldValue); atoiErr == nil {
5260
// If the value can be parsed as an integer, we try to set the
5361
// value as an integer.
5462
if err = d.Set(fieldName, intVal); err != nil {
5563
return err
5664
}
65+
if identity != nil {
66+
if err = identity.Set(fieldName, intVal); err != nil {
67+
return err
68+
}
69+
}
5770
} else {
5871
return fmt.Errorf("%s appears to be an integer, but %v cannot be parsed as an int", fieldName, fieldValue)
5972
}
@@ -64,18 +77,56 @@ func ParseImportId(idRegexes []string, d TerraformResourceData, config *transpor
6477
}
6578

6679
// The first id format is applied first and contains all the fields.
67-
err := setDefaultValues(idRegexes[0], d, config)
80+
err := setDefaultValues(idRegexes[0], nil, d, config)
81+
if err != nil {
82+
return err
83+
}
84+
85+
err = setDefaultValues(idRegexes[0], identity, d, config)
6886
if err != nil {
6987
return err
7088
}
7189

90+
return nil
91+
} else if d.Id() == "" {
92+
if err := identityImport(re, identity, d); err != nil {
93+
return err
94+
}
95+
err = setDefaultValues(idRegexes[0], identity, d, config)
96+
if err != nil {
97+
return err
98+
}
7299
return nil
73100
}
74101
}
75102
return fmt.Errorf("Import id %q doesn't match any of the accepted formats: %v", d.Id(), idRegexes)
76103
}
77104

78-
func setDefaultValues(idRegex string, d TerraformResourceData, config *transport_tpg.Config) error {
105+
func identityImport(re *regexp.Regexp, identity *schema.IdentityData, d TerraformResourceData) error {
106+
if identity == nil {
107+
return nil
108+
}
109+
log.Print("[DEBUG] Using IdentitySchema to import resource")
110+
namedGroups := re.SubexpNames()
111+
for _, group := range namedGroups {
112+
if val, ok := d.GetOk(group); ok && group != "" {
113+
log.Printf("[DEBUG] Group %s = %s Identity Group", group, val)
114+
identity.Set(group, val)
115+
}
116+
if identityValue, identityExists := identity.GetOk(group); identityExists && group != "" {
117+
log.Printf("[DEBUG] identity Importing %s = %s", group, identityValue)
118+
d.Set(group, identityValue)
119+
} else if group == "" {
120+
continue
121+
} else {
122+
log.Printf("[DEBUG] No value was found for %s in identity import block", group)
123+
}
124+
}
125+
126+
return nil
127+
}
128+
129+
func setDefaultValues(idRegex string, identity *schema.IdentityData, d TerraformResourceData, config *transport_tpg.Config) error {
79130
if _, ok := d.GetOk("project"); !ok && strings.Contains(idRegex, "?P<project>") {
80131
project, err := GetProject(d, config)
81132
if err != nil {
@@ -84,6 +135,11 @@ func setDefaultValues(idRegex string, d TerraformResourceData, config *transport
84135
if err := d.Set("project", project); err != nil {
85136
return fmt.Errorf("Error setting project: %s", err)
86137
}
138+
if identity != nil {
139+
if err := identity.Set("project", project); err != nil {
140+
return fmt.Errorf("Error setting project: %s", err)
141+
}
142+
}
87143
}
88144
if _, ok := d.GetOk("region"); !ok && strings.Contains(idRegex, "?P<region>") {
89145
region, err := GetRegion(d, config)
@@ -93,6 +149,11 @@ func setDefaultValues(idRegex string, d TerraformResourceData, config *transport
93149
if err := d.Set("region", region); err != nil {
94150
return fmt.Errorf("Error setting region: %s", err)
95151
}
152+
if identity != nil {
153+
if err := identity.Set("region", region); err != nil {
154+
return fmt.Errorf("Error setting region: %s", err)
155+
}
156+
}
96157
}
97158
if _, ok := d.GetOk("zone"); !ok && strings.Contains(idRegex, "?P<zone>") {
98159
zone, err := GetZone(d, config)
@@ -102,6 +163,11 @@ func setDefaultValues(idRegex string, d TerraformResourceData, config *transport
102163
if err := d.Set("zone", zone); err != nil {
103164
return fmt.Errorf("Error setting zone: %s", err)
104165
}
166+
if identity != nil {
167+
if err := identity.Set("zone", zone); err != nil {
168+
return fmt.Errorf("Error setting zone: %s", err)
169+
}
170+
}
105171
}
106172
return nil
107173
}

mmv1/third_party/terraform/tpgresource/resource_test_utils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ import (
88
"testing"
99
"time"
1010

11+
"github.com/hashicorp/go-cty/cty"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1213
"github.com/hashicorp/terraform-plugin-testing/terraform"
14+
1315
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
1416
)
1517

1618
type ResourceDataMock struct {
1719
FieldsInSchema map[string]interface{}
1820
FieldsWithHasChange []string
1921
id string
22+
identity *schema.IdentityData
2023
}
2124

2225
func (d *ResourceDataMock) HasChange(key string) bool {
@@ -71,6 +74,10 @@ func (d *ResourceDataMock) GetProviderMeta(dst interface{}) error {
7174
return nil
7275
}
7376

77+
func (d *ResourceDataMock) Identity() (*schema.IdentityData, error) {
78+
return d.identity, nil
79+
}
80+
7481
func (d *ResourceDataMock) Timeout(key string) time.Duration {
7582
return time.Duration(1)
7683
}
@@ -185,6 +192,10 @@ func ReplaceVarsForTest(config *transport_tpg.Config, rs *terraform.ResourceStat
185192
return re.ReplaceAllStringFunc(linkTmpl, replaceFunc), nil
186193
}
187194

195+
// These methods are required by some mappers but we don't actually have (or need)
196+
// implementations for them.
197+
func (d *ResourceDataMock) GetRawConfig() cty.Value { return cty.NullVal(cty.String) }
198+
188199
// Used to create populated schema.ResourceData structs in tests.
189200
// Pass in a schema and a config map containing the fields and values you wish to set
190201
// The returned schema.ResourceData can represent a configured resource, data source or provider.

mmv1/third_party/terraform/tpgresource/utils.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ type TerraformResourceData interface {
4040
GetOkExists(string) (interface{}, bool)
4141
GetOk(string) (interface{}, bool)
4242
Get(string) interface{}
43+
GetRawConfig() cty.Value
4344
Set(string, interface{}) error
4445
SetId(string)
4546
Id() string
47+
Identity() (*schema.IdentityData, error)
4648
GetProviderMeta(interface{}) error
4749
Timeout(key string) time.Duration
4850
}

mmv1/third_party/tgc/tfdata/fake_resource_data.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525
"time"
2626

27+
"github.com/hashicorp/go-cty/cty"
2728
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2829
)
2930

@@ -41,16 +42,21 @@ type getResult struct {
4142

4243
// Compare to https://github.com/hashicorp/terraform-plugin-sdk/blob/97b4465/helper/schema/resource_data.go#L15
4344
type FakeResourceData struct {
44-
reader schema.FieldReader
45-
kind string
46-
schema map[string]*schema.Schema
45+
reader schema.FieldReader
46+
kind string
47+
schema map[string]*schema.Schema
48+
identity *schema.IdentityData
4749
}
4850

4951
// Kind returns the type of resource (i.e. "google_storage_bucket").
5052
func (d *FakeResourceData) Kind() string {
5153
return d.kind
5254
}
5355

56+
func (d *FakeResourceData) Identity() (*schema.IdentityData, error) {
57+
return d.identity, nil
58+
}
59+
5460
// Id returns the ID of the resource from state.
5561
func (d *FakeResourceData) Id() string {
5662
return ""
@@ -126,6 +132,7 @@ func (d *FakeResourceData) Set(string, interface{}) error { return nil }
126132
func (d *FakeResourceData) SetId(string) {}
127133
func (d *FakeResourceData) GetProviderMeta(interface{}) error { return nil }
128134
func (d *FakeResourceData) Timeout(key string) time.Duration { return time.Duration(1) }
135+
func (d *FakeResourceData) GetRawConfig() cty.Value { return cty.NullVal(cty.String) }
129136

130137
func NewFakeResourceData(kind string, resourceSchema map[string]*schema.Schema, values map[string]interface{}) *FakeResourceData {
131138
state := map[string]string{}

mmv1/third_party/tgc_next/pkg/tfplan2cai/models/fake_resource_data.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525
"time"
2626

27+
"github.com/hashicorp/go-cty/cty"
2728
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2829
)
2930

@@ -41,15 +42,20 @@ type getResult struct {
4142

4243
// Compare to https://github.com/hashicorp/terraform-plugin-sdk/blob/97b4465/helper/schema/resource_data.go#L15
4344
type FakeResourceData struct {
44-
reader schema.FieldReader
45-
schema map[string]*schema.Schema
45+
reader schema.FieldReader
46+
schema map[string]*schema.Schema
47+
identity *schema.IdentityData
4648
}
4749

4850
// Id returns the ID of the resource from state.
4951
func (d *FakeResourceData) Id() string {
5052
return ""
5153
}
5254

55+
func (d *FakeResourceData) Identity() (*schema.IdentityData, error) {
56+
return d.identity, nil
57+
}
58+
5359
func (d *FakeResourceData) getRaw(key string) getResult {
5460
var parts []string
5561
if key != "" {
@@ -107,6 +113,10 @@ func (d *FakeResourceData) GetOk(name string) (interface{}, bool) {
107113
return r.Value, exists
108114
}
109115

116+
func (d *FakeResourceData) GetRawConfig() cty.Value {
117+
return cty.NullVal(cty.String)
118+
}
119+
110120
func (d *FakeResourceData) GetOkExists(key string) (interface{}, bool) {
111121
r := d.getRaw(key)
112122
exists := r.Exists && !r.Computed

0 commit comments

Comments
 (0)