/
schema_resource.go
147 lines (125 loc) · 4.25 KB
/
schema_resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package framework
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var _ resource.Resource = SchemaResource{}
func NewSchemaResource() resource.Resource {
return &SchemaResource{}
}
// SchemaResource is for testing all schema types, excluding dynamic schema types. (see `DynamicSchemaResource`)
type SchemaResource struct{}
func (r SchemaResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_schema"
}
func (r SchemaResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"bool_attribute": schema.BoolAttribute{
Optional: true,
},
"float64_attribute": schema.Float64Attribute{
Optional: true,
},
"int64_attribute": schema.Int64Attribute{
Optional: true,
},
"list_attribute": schema.ListAttribute{
ElementType: types.StringType,
Optional: true,
},
"map_attribute": schema.MapAttribute{
ElementType: types.StringType,
Optional: true,
},
"number_attribute": schema.NumberAttribute{
Optional: true,
},
"object_attribute": schema.ObjectAttribute{
AttributeTypes: map[string]attr.Type{
"object_attribute_attribute": types.StringType,
},
Optional: true,
},
"set_attribute": schema.SetAttribute{
ElementType: types.StringType,
Optional: true,
},
"string_attribute": schema.StringAttribute{
Optional: true,
},
},
Blocks: map[string]schema.Block{
"list_nested_block": schema.ListNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"list_nested_block_attribute": schema.StringAttribute{
Optional: true,
},
},
},
},
"set_nested_block": schema.SetNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"set_nested_block_attribute": schema.StringAttribute{
Optional: true,
},
},
},
},
"single_nested_block": schema.SingleNestedBlock{
Attributes: map[string]schema.Attribute{
"single_nested_block_attribute": schema.StringAttribute{
Optional: true,
},
},
},
},
}
}
func (r SchemaResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data SchemaResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r SchemaResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data SchemaResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r SchemaResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data SchemaResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r SchemaResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
}
type SchemaResourceModel struct {
BoolAttribute types.Bool `tfsdk:"bool_attribute"`
Float64Attribute types.Float64 `tfsdk:"float64_attribute"`
Int64Attribute types.Int64 `tfsdk:"int64_attribute"`
ListAttribute types.List `tfsdk:"list_attribute"`
ListNestedBlock types.List `tfsdk:"list_nested_block"`
MapAttribute types.Map `tfsdk:"map_attribute"`
NumberAttribute types.Number `tfsdk:"number_attribute"`
ObjectAttribute types.Object `tfsdk:"object_attribute"`
SetAttribute types.Set `tfsdk:"set_attribute"`
SetNestedBlock types.Set `tfsdk:"set_nested_block"`
SingleNestedBlock types.Object `tfsdk:"single_nested_block"`
StringAttribute types.String `tfsdk:"string_attribute"`
}