Skip to content

Commit f86d5e3

Browse files
authored
Update state and provider JSON with identity fields (#155)
1 parent 4d6dac0 commit f86d5e3

File tree

6 files changed

+75
-16
lines changed

6 files changed

+75
-16
lines changed

schemas.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ type ProviderSchema struct {
9292

9393
// The definitions for any functions in this provider.
9494
Functions map[string]*FunctionSignature `json:"functions,omitempty"`
95+
96+
// The schemas for resources identities in this provider.
97+
ResourceIdentitySchemas map[string]*IdentitySchema `json:"resource_identity_schemas,omitempty"`
9598
}
9699

97100
// Schema is the JSON representation of a particular schema
@@ -294,3 +297,31 @@ type SchemaNestedAttributeType struct {
294297
// of this attribute type (not applicable to single nesting mode).
295298
MaxItems uint64 `json:"max_items,omitempty"`
296299
}
300+
301+
// IdentitySchema is the JSON representation of a particular
302+
// resource identity schema
303+
type IdentitySchema struct {
304+
// The version of the particular resource identity schema.
305+
Version uint64 `json:"version"`
306+
307+
// Map of identity attributes
308+
Attributes map[string]*IdentityAttribute `json:"attributes,omitempty"`
309+
}
310+
311+
// IdentityAttribute describes an identity attribute
312+
type IdentityAttribute struct {
313+
// The identity attribute type
314+
IdentityType cty.Type `json:"type,omitempty"`
315+
316+
// The description of the identity attribute
317+
Description string `json:"description,omitempty"`
318+
319+
// RequiredForImport when enabled signifies that this attribute must be
320+
// specified in the configuration during import
321+
RequiredForImport bool `json:"required_for_import,omitempty"`
322+
323+
// OptionalForImport when enabled signifies that this attribute is not
324+
// required to be specified during import, because it can be supplied by the
325+
// provider
326+
OptionalForImport bool `json:"optional_for_import,omitempty"`
327+
}

schemas_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func TestProviderSchemasValidate(t *testing.T) {
2525
"a provider schema including a resource with write-only attribute(s) is validated": {
2626
testDataPath: "testdata/write_only_attribute_on_resource/schemas.json",
2727
},
28+
"a provider schema including resource identity schemas is validated": {
29+
testDataPath: "testdata/identity/schemas.json",
30+
},
2831
}
2932

3033
for tn, tc := range cases {

state.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ type StateResource struct {
173173
// DeposedKey is set if the resource instance has been marked Deposed and
174174
// will be destroyed on the next apply.
175175
DeposedKey string `json:"deposed_key,omitempty"`
176+
177+
// The version of the resource identity schema the "identity" property
178+
// conforms to.
179+
IdentitySchemaVersion *uint64 `json:"identity_schema_version,omitempty"`
180+
181+
// The JSON representation of the resource identity, whose structure
182+
// depends on the resource identity schema.
183+
IdentityValues map[string]interface{} `json:"identity,omitempty"`
176184
}
177185

178186
// StateOutput represents an output value in a common state

state_test.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,40 @@ package tfjson
55

66
import (
77
"encoding/json"
8-
"io/ioutil"
8+
"io"
99
"os"
1010
"testing"
1111
)
1212

1313
func TestStateValidate_raw(t *testing.T) {
14-
f, err := os.Open("testdata/no_changes/state.json")
15-
if err != nil {
16-
t.Fatal(err)
17-
}
18-
defer f.Close()
19-
20-
var state State
21-
if err := json.NewDecoder(f).Decode(&state); err != nil {
22-
t.Fatal(err)
23-
}
24-
25-
if err := state.Validate(); err != nil {
26-
t.Fatal(err)
14+
cases := map[string]struct {
15+
statePath string
16+
}{
17+
"basic state": {
18+
statePath: "testdata/no_changes/state.json",
19+
},
20+
"state with identity": {
21+
statePath: "testdata/identity/state.json",
22+
},
23+
}
24+
25+
for tn, tc := range cases {
26+
t.Run(tn, func(t *testing.T) {
27+
f, err := os.Open(tc.statePath)
28+
if err != nil {
29+
t.Fatal(err)
30+
}
31+
defer f.Close()
32+
33+
var state State
34+
if err := json.NewDecoder(f).Decode(&state); err != nil {
35+
t.Fatal(err)
36+
}
37+
38+
if err := state.Validate(); err != nil {
39+
t.Fatal(err)
40+
}
41+
})
2742
}
2843
}
2944

@@ -34,7 +49,7 @@ func TestStateUnmarshal_valid(t *testing.T) {
3449
}
3550
defer f.Close()
3651

37-
b, err := ioutil.ReadAll(f)
52+
b, err := io.ReadAll(f)
3853
if err != nil {
3954
t.Fatal(err)
4055
}
@@ -53,7 +68,7 @@ func TestStateUnmarshal_internalState(t *testing.T) {
5368
}
5469
defer f.Close()
5570

56-
b, err := ioutil.ReadAll(f)
71+
b, err := io.ReadAll(f)
5772
if err != nil {
5873
t.Fatal(err)
5974
}

testdata/identity/schemas.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"format_version":"1.0","provider_schemas":{"example":{"provider":{"version":0,"block":{"attributes":{"example":{"type":"string","description_kind":"plain","optional":true}},"description_kind":"plain"}},"resource_schemas":{"framework_example":{"version":0,"block":{"attributes":{"id":{"type":"string","description":"Example identifier","description_kind":"markdown","computed":true}},"description":"Example resource","description_kind":"markdown"}}},"data_source_schemas":{"framework_example":{"version":0,"block":{"attributes":{"id":{"type":"string","description":"Example identifier","description_kind":"markdown","computed":true}},"description":"Example data source","description_kind":"markdown"}}},"functions":{"example":{"description":"Echoes given argument as result","summary":"Example function","return_type":"string","parameters":[{"name":"input","description":"String to echo","type":"string"}]}},"resource_identity_schemas":{"framework_example":{"version":0,"attributes":{"number":{"type":"number","description":"A specific number","optional_for_import":true},"string":{"type":"string","required_for_import":true}}}}}}}

testdata/identity/state.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"format_version":"1.0","terraform_version":"1.12.0","values":{"root_module":{"resources":[{"address":"corner_bigint.number","mode":"managed","type":"corner_bigint","name":"number","provider_name":"registry.terraform.io/hashicorp/corner","schema_version":0,"values":{"id":"5","int64":5,"number":5},"sensitive_values":{}},{"address":"corner_user.user","mode":"managed","type":"corner_user","name":"user","provider_name":"registry.terraform.io/hashicorp/corner","schema_version":0,"values":{"age":999,"email":"[email protected]","id":"[email protected]","name":"test"},"sensitive_values":{},"identity_schema_version":0,"identity":{"age":999,"email":"[email protected]","name":"test"}}]}}}

0 commit comments

Comments
 (0)