Skip to content

Commit e5d6e8f

Browse files
authored
primes: include resource identity data in list results (#537)
1 parent a8477a1 commit e5d6e8f

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

examples/terraform-provider-primes/provider.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,7 @@ func (p PrimeNumberProvider) GetMetadata(ctx context.Context, request *tfprotov5
3030
func (p PrimeNumberProvider) GetResourceIdentitySchemas(ctx context.Context, request *tfprotov5.GetResourceIdentitySchemasRequest) (*tfprotov5.GetResourceIdentitySchemasResponse, error) {
3131
return &tfprotov5.GetResourceIdentitySchemasResponse{
3232
IdentitySchemas: map[string]*tfprotov5.ResourceIdentitySchema{
33-
"prime": {
34-
IdentityAttributes: []*tfprotov5.ResourceIdentitySchemaAttribute{
35-
{
36-
Name: "number",
37-
Type: tftypes.Number,
38-
RequiredForImport: true,
39-
},
40-
},
41-
},
33+
"prime": p.primeIdentitySchema(),
4234
},
4335
}, nil
4436
}
@@ -82,6 +74,17 @@ func (p PrimeNumberProvider) primeSchema() *tfprotov5.Schema {
8274
},
8375
}
8476
}
77+
func (p PrimeNumberProvider) primeIdentitySchema() *tfprotov5.ResourceIdentitySchema {
78+
return &tfprotov5.ResourceIdentitySchema{
79+
IdentityAttributes: []*tfprotov5.ResourceIdentitySchemaAttribute{
80+
{
81+
Name: "number",
82+
Type: tftypes.Number,
83+
RequiredForImport: true,
84+
},
85+
},
86+
}
87+
}
8588

8689
func (p PrimeNumberProvider) PrepareProviderConfig(ctx context.Context, request *tfprotov5.PrepareProviderConfigRequest) (*tfprotov5.PrepareProviderConfigResponse, error) {
8790
return &tfprotov5.PrepareProviderConfigResponse{
@@ -103,6 +106,22 @@ func (p PrimeNumberProvider) convertToDynamicValue(number int, ordinal int) (tfp
103106
return tfprotov5.NewDynamicValue(typ, tftypes.NewValue(typ, value))
104107
}
105108

109+
func (p PrimeNumberProvider) convertToResourceIdentity(number int) (tfprotov5.ResourceIdentityData, error) {
110+
typ := p.primeIdentitySchema().ValueType()
111+
value := map[string]tftypes.Value{
112+
"number": tftypes.NewValue(tftypes.Number, number),
113+
}
114+
115+
dynamicValue, err := tfprotov5.NewDynamicValue(typ, tftypes.NewValue(typ, value))
116+
if err != nil {
117+
return tfprotov5.ResourceIdentityData{}, fmt.Errorf("failed to create dynamic value: %w", err)
118+
}
119+
120+
return tfprotov5.ResourceIdentityData{
121+
IdentityData: &dynamicValue,
122+
}, nil
123+
}
124+
106125
func (p PrimeNumberProvider) ListResource(ctx context.Context, request *tfprotov5.ListResourceRequest) (*tfprotov5.ListResourceServerStream, error) {
107126
primes := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}
108127

@@ -116,9 +135,15 @@ func (p PrimeNumberProvider) ListResource(ctx context.Context, request *tfprotov
116135
panic(err)
117136
}
118137

138+
resourceIdentity, err := p.convertToResourceIdentity(prime)
139+
if err != nil {
140+
panic(err)
141+
}
142+
119143
protoEv := tfprotov5.ListResourceResult{
120144
DisplayName: displayName,
121145
Resource: &resourceObject,
146+
Identity: &resourceIdentity,
122147
}
123148
if !push(protoEv) {
124149
fmt.Println("let's stop here")

0 commit comments

Comments
 (0)