Skip to content

Commit 3f3e439

Browse files
committed
Remove links to source code and make examples generic in migrating - resources - overview (#418)
1 parent a19d742 commit 3f3e439

File tree

1 file changed

+16
-35
lines changed
  • website/docs/plugin/framework/migrating/resources

1 file changed

+16
-35
lines changed

website/docs/plugin/framework/migrating/resources/index.mdx

+16-35
Original file line numberDiff line numberDiff line change
@@ -145,48 +145,34 @@ CRUD operations.
145145

146146
## Example
147147

148-
The following examples show how to migrate portions of the
149-
[tls](https://github.com/hashicorp/terraform-provider-tls) provider.
150-
151-
For a complete example,
152-
clone the `terraform-provider-tls` repository and compare the `resource_private_key.go` file in
153-
[v3.4.0](https://github.com/hashicorp/terraform-provider-tls/blob/v3.4.0/internal/provider/resource_private_key.go) with the file
154-
[after the migration](https://github.com/hashicorp/terraform-provider-tls/blob/4dafb105818e45a88532f917e7b170ee2a9bb092/internal/provider/resource_private_key.go).
155-
156148
### SDKv2
157149

158150
In SDKv2, the `ResourcesMap` field on the `schema.Provider` struct holds a `map[string]*schemaResource`. A typical
159151
pattern is to implement a function that returns `schema.Resource`.
160152

161-
The following example from the `provider.go` file defines a `tls_private_key` resource within the provider schema.
162-
163153
```go
164154
func New() (*schema.Provider, error) {
165155
return &schema.Provider {
166156
ResourcesMap: map[string]*schema.Resource {
167-
"tls_private_key": resourcePrivateKey(),
157+
"example_resource": exampleResource(),
168158
/* ... */
169159
```
170160
171-
The following example from the `resource_private_key.go` file defines the resource schema.
161+
This code defines the `example_resource` resource by mapping the resource name to the `exampleResource` struct.
172162
173163
```go
174-
func resourcePrivateKey() *schema.Resource {
164+
func exampleResource() *schema.Resource {
175165
return &schema.Resource{
176-
CreateContext: createResourcePrivateKey,
177-
DeleteContext: deleteResourcePrivateKey,
178-
ReadContext: readResourcePrivateKey,
179-
180-
Description: "Creates a PEM /* ... */",
166+
CreateContext: createResource,
167+
DeleteContext: deleteResource,
168+
ReadContext: readResource,
181169

182170
Schema: map[string]*schema.Schema{
183-
"algorithm": {
171+
"attribute": {
184172
Type: schema.TypeString,
185173
Required: true,
186174
ForceNew: true,
187-
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(SupportedAlgorithmsStr(), false)),
188-
Description: "Name of the algorithm to use when generating the private key. " +
189-
"Currently-supported values are `RSA`, `ECDSA` and `ED25519`.",
175+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{'a', 'b'}, false)),
190176
},
191177
/* ... */
192178
```
@@ -195,41 +181,36 @@ func resourcePrivateKey() *schema.Resource {
195181
196182
The following shows the same section of provider code after the migration.
197183
198-
This code defines the `tls_private_key` resource by mapping the resource name to the `privateKeyResourceType` struct.
199-
200184
```go
201-
func (p *TlsProvider) Resources(_ context.Context) []func() resource.Resource {
185+
func (p *exampleProvider) Resources(_ context.Context) []func() resource.Resource {
202186
return []func() resource.Resource{
203187
func() resource.Resource {
204-
return &privateKeyResource{}
188+
return &exampleResource{}
205189
},
206190
/* ... */
207191
}
208192
}
209193
```
210194
211-
This code defines the `Schema` and `Metadata` methods for the `privateKeyResource`.
195+
This code defines the `Schema` and `Metadata` methods for the `Resource`.
212196
213197
```go
214-
func (r *privateKeyResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
215-
resp.TypeName = "tls_private_key"
198+
func (r *exampleResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
199+
resp.TypeName = "example_resource"
216200
}
217201

218-
func (r *privateKeyResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
202+
func (r *exampleResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
219203
resp.Schema = schema.Schema{
220204
Attributes: map[string]schema.Attribute{
221205
// Required attributes
222-
"algorithm": schema.StringAttribute{
206+
"attribute": schema.StringAttribute{
223207
Required: true,
224208
PlanModifiers: []planmodifier.String{
225209
stringplanmodifier.RequiresReplace(),
226210
},
227211
Validators: []validator.String{
228-
stringvalidator.OneOf(supportedAlgorithmsAttrValue()...),
212+
stringvalidator.OneOf([]string{'a', 'b'}...),
229213
},
230-
Description: "Name of the algorithm to use when generating the private key. " +
231-
fmt.Sprintf("Currently-supported values are: `%s`. ", strings.Join(supportedAlgorithmsStr(), "`, `")),
232-
},
233214
/* ... */
234215
},
235216
}

0 commit comments

Comments
 (0)