Skip to content

Commit 4186bf4

Browse files
authored
Remove Schema attribute for identity in favor of SchemaFunc (#1461)
* Remove Schema attribute for identity in favor of SchemaFunc * fix: don't call function if it's nil, return nil instead
1 parent dee0dd6 commit 4186bf4

File tree

3 files changed

+306
-228
lines changed

3 files changed

+306
-228
lines changed

helper/schema/grpc_provider_test.go

Lines changed: 91 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3307,13 +3307,15 @@ func TestGRPCProviderServerGetResourceIdentitySchemas(t *testing.T) {
33073307
"test_resource1": {
33083308
Identity: &ResourceIdentity{
33093309
Version: 1,
3310-
Schema: map[string]*Schema{
3311-
"test": {
3312-
Type: TypeString,
3313-
RequiredForImport: true,
3314-
OptionalForImport: false,
3315-
Description: "test resource",
3316-
},
3310+
SchemaFunc: func() map[string]*Schema {
3311+
return map[string]*Schema{
3312+
"test": {
3313+
Type: TypeString,
3314+
RequiredForImport: true,
3315+
OptionalForImport: false,
3316+
Description: "test resource",
3317+
},
3318+
}
33173319
},
33183320
},
33193321
},
@@ -3392,15 +3394,17 @@ func TestGRPCProviderServerGetResourceIdentitySchemas(t *testing.T) {
33923394
ResourcesMap: map[string]*Resource{
33933395
"test_resource": {
33943396
Identity: &ResourceIdentity{
3395-
Schema: map[string]*Schema{
3396-
"bool_attr": {Type: TypeBool, Description: "Boolean attribute"},
3397-
"float_attr": {Type: TypeFloat, Description: "Float attribute"},
3398-
"int_attr": {Type: TypeInt, Description: "Int attribute"},
3399-
"list_bool_attr": {Type: TypeList, Elem: TypeBool, Description: "List Bool attribute"},
3400-
"list_float_attr": {Type: TypeList, Elem: TypeFloat, Description: "List Float attribute"},
3401-
"list_int_attr": {Type: TypeList, Elem: TypeInt, Description: "List Int attribute"},
3402-
"list_str_attr": {Type: TypeList, Elem: TypeString, Description: "List String attribute"},
3403-
"string_attr": {Type: TypeString, Description: "String attribute"},
3397+
SchemaFunc: func() map[string]*Schema {
3398+
return map[string]*Schema{
3399+
"bool_attr": {Type: TypeBool, Description: "Boolean attribute"},
3400+
"float_attr": {Type: TypeFloat, Description: "Float attribute"},
3401+
"int_attr": {Type: TypeInt, Description: "Int attribute"},
3402+
"list_bool_attr": {Type: TypeList, Elem: TypeBool, Description: "List Bool attribute"},
3403+
"list_float_attr": {Type: TypeList, Elem: TypeFloat, Description: "List Float attribute"},
3404+
"list_int_attr": {Type: TypeList, Elem: TypeInt, Description: "List Int attribute"},
3405+
"list_str_attr": {Type: TypeList, Elem: TypeString, Description: "List String attribute"},
3406+
"string_attr": {Type: TypeString, Description: "String attribute"},
3407+
}
34043408
},
34053409
},
34063410
},
@@ -3449,7 +3453,9 @@ func TestGRPCProviderServerGetResourceIdentitySchemas(t *testing.T) {
34493453
"test_resource1": {
34503454
Identity: &ResourceIdentity{
34513455
Version: 1,
3452-
Schema: map[string]*Schema{},
3456+
SchemaFunc: func() map[string]*Schema {
3457+
return map[string]*Schema{}
3458+
},
34533459
},
34543460
},
34553461
},
@@ -3501,13 +3507,15 @@ func TestUpgradeResourceIdentity_jsonState(t *testing.T) {
35013507
SchemaVersion: 1,
35023508
Identity: &ResourceIdentity{
35033509
Version: 1,
3504-
Schema: map[string]*Schema{
3505-
"id": {
3506-
Type: TypeString,
3507-
RequiredForImport: true,
3508-
OptionalForImport: false,
3509-
Description: "id of thing",
3510-
},
3510+
SchemaFunc: func() map[string]*Schema {
3511+
return map[string]*Schema{
3512+
"id": {
3513+
Type: TypeString,
3514+
RequiredForImport: true,
3515+
OptionalForImport: false,
3516+
Description: "id of thing",
3517+
},
3518+
}
35113519
},
35123520
IdentityUpgraders: []IdentityUpgrader{
35133521
{
@@ -3584,13 +3592,15 @@ func TestUpgradeResourceIdentity_removedAttr(t *testing.T) {
35843592
SchemaVersion: 1,
35853593
Identity: &ResourceIdentity{
35863594
Version: 1,
3587-
Schema: map[string]*Schema{
3588-
"id": {
3589-
Type: TypeString,
3590-
RequiredForImport: true,
3591-
OptionalForImport: false,
3592-
Description: "id of thing",
3593-
},
3595+
SchemaFunc: func() map[string]*Schema {
3596+
return map[string]*Schema{
3597+
"id": {
3598+
Type: TypeString,
3599+
RequiredForImport: true,
3600+
OptionalForImport: false,
3601+
Description: "id of thing",
3602+
},
3603+
}
35943604
},
35953605
IdentityUpgraders: []IdentityUpgrader{
35963606
{
@@ -3669,13 +3679,15 @@ func TestUpgradeResourceIdentity_jsonStateBigInt(t *testing.T) {
36693679
SchemaVersion: 1,
36703680
Identity: &ResourceIdentity{
36713681
Version: 1,
3672-
Schema: map[string]*Schema{
3673-
"int": {
3674-
Type: TypeInt,
3675-
RequiredForImport: true,
3676-
OptionalForImport: false,
3677-
Description: "",
3678-
},
3682+
SchemaFunc: func() map[string]*Schema {
3683+
return map[string]*Schema{
3684+
"int": {
3685+
Type: TypeInt,
3686+
RequiredForImport: true,
3687+
OptionalForImport: false,
3688+
Description: "",
3689+
},
3690+
}
36793691
},
36803692
},
36813693
}
@@ -5066,15 +5078,17 @@ func TestReadResource(t *testing.T) {
50665078
},
50675079
Identity: &ResourceIdentity{
50685080
Version: 1,
5069-
Schema: map[string]*Schema{
5070-
"instance_id": {
5071-
Type: TypeString,
5072-
RequiredForImport: true,
5073-
},
5074-
"region": {
5075-
Type: TypeString,
5076-
OptionalForImport: true,
5077-
},
5081+
SchemaFunc: func() map[string]*Schema {
5082+
return map[string]*Schema{
5083+
"instance_id": {
5084+
Type: TypeString,
5085+
RequiredForImport: true,
5086+
},
5087+
"region": {
5088+
Type: TypeString,
5089+
OptionalForImport: true,
5090+
},
5091+
}
50785092
},
50795093
},
50805094
ReadContext: func(ctx context.Context, d *ResourceData, meta interface{}) diag.Diagnostics {
@@ -5226,7 +5240,9 @@ func TestReadResource(t *testing.T) {
52265240
SchemaVersion: 1,
52275241
Identity: &ResourceIdentity{
52285242
Version: 1,
5229-
Schema: map[string]*Schema{},
5243+
SchemaFunc: func() map[string]*Schema {
5244+
return map[string]*Schema{}
5245+
},
52305246
},
52315247
},
52325248
},
@@ -5538,11 +5554,13 @@ func TestPlanResourceChange(t *testing.T) {
55385554
},
55395555
Identity: &ResourceIdentity{
55405556
Version: 1,
5541-
Schema: map[string]*Schema{
5542-
"name": {
5543-
Type: TypeString,
5544-
RequiredForImport: true,
5545-
},
5557+
SchemaFunc: func() map[string]*Schema {
5558+
return map[string]*Schema{
5559+
"name": {
5560+
Type: TypeString,
5561+
RequiredForImport: true,
5562+
},
5563+
}
55465564
},
55475565
},
55485566
},
@@ -5644,11 +5662,13 @@ func TestPlanResourceChange(t *testing.T) {
56445662
},
56455663
Identity: &ResourceIdentity{
56465664
Version: 1,
5647-
Schema: map[string]*Schema{
5648-
"name": {
5649-
Type: TypeString,
5650-
RequiredForImport: true,
5651-
},
5665+
SchemaFunc: func() map[string]*Schema {
5666+
return map[string]*Schema{
5667+
"name": {
5668+
Type: TypeString,
5669+
RequiredForImport: true,
5670+
},
5671+
}
56525672
},
56535673
},
56545674
CustomizeDiff: func(ctx context.Context, d *ResourceDiff, meta interface{}) error {
@@ -5827,7 +5847,9 @@ func TestPlanResourceChange(t *testing.T) {
58275847
},
58285848
Identity: &ResourceIdentity{
58295849
Version: 1,
5830-
Schema: map[string]*Schema{},
5850+
SchemaFunc: func() map[string]*Schema {
5851+
return map[string]*Schema{}
5852+
},
58315853
},
58325854
},
58335855
},
@@ -6833,11 +6855,13 @@ func TestApplyResourceChange(t *testing.T) {
68336855
Schema: map[string]*Schema{},
68346856
Identity: &ResourceIdentity{
68356857
Version: 1,
6836-
Schema: map[string]*Schema{
6837-
"ident": {
6838-
Type: TypeString,
6839-
RequiredForImport: true,
6840-
},
6858+
SchemaFunc: func() map[string]*Schema {
6859+
return map[string]*Schema{
6860+
"ident": {
6861+
Type: TypeString,
6862+
RequiredForImport: true,
6863+
},
6864+
}
68416865
},
68426866
},
68436867
},
@@ -6988,7 +7012,9 @@ func TestApplyResourceChange(t *testing.T) {
69887012
Schema: map[string]*Schema{},
69897013
Identity: &ResourceIdentity{
69907014
Version: 1,
6991-
Schema: map[string]*Schema{},
7015+
SchemaFunc: func() map[string]*Schema {
7016+
return map[string]*Schema{}
7017+
},
69927018
},
69937019
},
69947020
},

helper/schema/resource_identity.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ type ResourceIdentity struct {
3232
// Version is the identity schema version.
3333
Version int64
3434

35-
// Schema is the structure and type information for the identity.
36-
// The types allowed in this Schema will be more restricted than
37-
// previous resource schemas.
38-
Schema map[string]*Schema
39-
40-
// SchemaFunc is an optional function that returns the schema for the
41-
// identity. Use this field instead of Schema on resource identity
42-
// declarations to prevent storing all identity schema information in
43-
// memory for the lifecycle of a provider.
35+
// SchemaFunc is the function that returns the schema for the
36+
// identity. Using a function for this field allows to prevent
37+
// storing all identity schema information in memory for the
38+
// lifecycle of a provider.
39+
// The types of the schema values are restricted to the types:
40+
// - TypeBool
41+
// - TypeFloat
42+
// - TypeInt
43+
// - TypeString
44+
// - TypeList (of any of the above types)
4445
SchemaFunc func() map[string]*Schema
4546

4647
// New struct, will be similar to (Resource).StateUpgraders
@@ -77,17 +78,12 @@ type ResourceIdentity struct {
7778
// align to the typing mentioned above.
7879
type ResourceIdentityUpgradeFunc func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error)
7980

80-
// SchemaMap returns the schema information for this Resource whether it is
81-
// defined via the SchemaFunc field or Schema field. The SchemaFunc field, if
82-
// defined, takes precedence over the Schema field.
81+
// SchemaMap returns the schema information for this resource identity
82+
// defined via the SchemaFunc field.
8383
func (ri *ResourceIdentity) SchemaMap() map[string]*Schema {
84-
if ri == nil {
84+
if ri == nil || ri.SchemaFunc == nil {
8585
return nil
8686
}
8787

88-
if ri.SchemaFunc != nil {
89-
return ri.SchemaFunc()
90-
}
91-
92-
return ri.Schema
88+
return ri.SchemaFunc()
9389
}

0 commit comments

Comments
 (0)