|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5" |
| 12 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 13 | +) |
| 14 | + |
| 15 | +var _ tfprotov5.ProviderServerWithListResource = PrimeNumberProvider{} //nolint:staticcheck |
| 16 | + |
| 17 | +type PrimeNumberProvider struct { |
| 18 | +} |
| 19 | + |
| 20 | +func (p PrimeNumberProvider) GetMetadata(ctx context.Context, request *tfprotov5.GetMetadataRequest) (*tfprotov5.GetMetadataResponse, error) { |
| 21 | + return &tfprotov5.GetMetadataResponse{ |
| 22 | + ListResources: []tfprotov5.ListResourceMetadata{ |
| 23 | + { |
| 24 | + TypeName: "prime", |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, nil |
| 28 | +} |
| 29 | + |
| 30 | +func (p PrimeNumberProvider) GetResourceIdentitySchemas(ctx context.Context, request *tfprotov5.GetResourceIdentitySchemasRequest) (*tfprotov5.GetResourceIdentitySchemasResponse, error) { |
| 31 | + return &tfprotov5.GetResourceIdentitySchemasResponse{ |
| 32 | + 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 | + }, |
| 42 | + }, |
| 43 | + }, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (p PrimeNumberProvider) GetProviderSchema(ctx context.Context, request *tfprotov5.GetProviderSchemaRequest) (*tfprotov5.GetProviderSchemaResponse, error) { |
| 47 | + return &tfprotov5.GetProviderSchemaResponse{ |
| 48 | + Provider: &tfprotov5.Schema{ |
| 49 | + Block: &tfprotov5.SchemaBlock{ |
| 50 | + Description: "Prime Provider", |
| 51 | + DescriptionKind: tfprotov5.StringKindPlain, |
| 52 | + }, |
| 53 | + }, |
| 54 | + ResourceSchemas: map[string]*tfprotov5.Schema{ |
| 55 | + "prime": p.primeSchema(), |
| 56 | + }, |
| 57 | + ListResourceSchemas: map[string]*tfprotov5.Schema{ |
| 58 | + "prime": p.primeSchema(), |
| 59 | + }, |
| 60 | + }, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (p PrimeNumberProvider) primeSchema() *tfprotov5.Schema { |
| 64 | + return &tfprotov5.Schema{ |
| 65 | + Block: &tfprotov5.SchemaBlock{ |
| 66 | + Attributes: []*tfprotov5.SchemaAttribute{ |
| 67 | + { |
| 68 | + Name: "number", |
| 69 | + Type: tftypes.Number, |
| 70 | + Description: "The nth prime", |
| 71 | + DescriptionKind: tfprotov5.StringKindPlain, |
| 72 | + Optional: true, |
| 73 | + }, |
| 74 | + { |
| 75 | + Name: "ordinal", |
| 76 | + Type: tftypes.Number, |
| 77 | + Description: "n", |
| 78 | + DescriptionKind: tfprotov5.StringKindPlain, |
| 79 | + Optional: true, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func (p PrimeNumberProvider) PrepareProviderConfig(ctx context.Context, request *tfprotov5.PrepareProviderConfigRequest) (*tfprotov5.PrepareProviderConfigResponse, error) { |
| 87 | + return &tfprotov5.PrepareProviderConfigResponse{ |
| 88 | + PreparedConfig: request.Config, |
| 89 | + }, nil |
| 90 | +} |
| 91 | + |
| 92 | +func (p PrimeNumberProvider) ConfigureProvider(ctx context.Context, request *tfprotov5.ConfigureProviderRequest) (*tfprotov5.ConfigureProviderResponse, error) { |
| 93 | + return &tfprotov5.ConfigureProviderResponse{}, nil |
| 94 | +} |
| 95 | + |
| 96 | +func (p PrimeNumberProvider) convertToDynamicValue(number int, ordinal int) (tfprotov5.DynamicValue, error) { |
| 97 | + typ := p.primeSchema().ValueType() |
| 98 | + value := map[string]tftypes.Value{ |
| 99 | + "number": tftypes.NewValue(tftypes.Number, number), |
| 100 | + "ordinal": tftypes.NewValue(tftypes.Number, ordinal), |
| 101 | + } |
| 102 | + |
| 103 | + return tfprotov5.NewDynamicValue(typ, tftypes.NewValue(typ, value)) |
| 104 | +} |
| 105 | + |
| 106 | +func (p PrimeNumberProvider) ListResource(ctx context.Context, request *tfprotov5.ListResourceRequest) (*tfprotov5.ListResourceServerStream, error) { |
| 107 | + 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} |
| 108 | + |
| 109 | + results := func(push func(tfprotov5.ListResourceResult) bool) { |
| 110 | + for i, prime := range primes { |
| 111 | + ordinal := i + 1 |
| 112 | + displayName := fmt.Sprintf("primes[%d]: %d", ordinal, prime) |
| 113 | + |
| 114 | + resourceObject, err := p.convertToDynamicValue(prime, ordinal) |
| 115 | + if err != nil { |
| 116 | + panic(err) |
| 117 | + } |
| 118 | + |
| 119 | + protoEv := tfprotov5.ListResourceResult{ |
| 120 | + DisplayName: displayName, |
| 121 | + Resource: &resourceObject, |
| 122 | + } |
| 123 | + if !push(protoEv) { |
| 124 | + fmt.Println("let's stop here") |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + fmt.Println("1 second nap") |
| 129 | + time.Sleep(1 * time.Second) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return &tfprotov5.ListResourceServerStream{ |
| 134 | + Results: results, |
| 135 | + }, nil |
| 136 | +} |
| 137 | + |
| 138 | +func (p PrimeNumberProvider) ValidateListResourceConfig(ctx context.Context, request *tfprotov5.ValidateListResourceConfigRequest) (*tfprotov5.ValidateListResourceConfigResponse, error) { |
| 139 | + return &tfprotov5.ValidateListResourceConfigResponse{}, nil |
| 140 | +} |
| 141 | + |
| 142 | +func (p PrimeNumberProvider) StopProvider(ctx context.Context, request *tfprotov5.StopProviderRequest) (*tfprotov5.StopProviderResponse, error) { |
| 143 | + return &tfprotov5.StopProviderResponse{}, nil |
| 144 | +} |
0 commit comments