-
Notifications
You must be signed in to change notification settings - Fork 14
/
wrapper_resource.go
87 lines (71 loc) · 2.87 KB
/
wrapper_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
package frameworkwrapper
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/resource"
jsonschema "github.com/vk-cs/terraform-provider-vkcs/helpers/providerjson/schema"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/providerwrapper/framework/resource/customschema"
)
var (
_ resource.Resource = &ResourceWrapper{}
)
func NewResourceWrapper(resource resource.Resource, resourceJSON jsonschema.ResourceJSON) *ResourceWrapper {
return &ResourceWrapper{
resource: resource,
resourceJSON: resourceJSON,
}
}
type ResourceWrapper struct {
resource resource.Resource
resourceJSON jsonschema.ResourceJSON
}
func (rw *ResourceWrapper) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
rw.resource.Metadata(ctx, req, resp)
}
func (rw *ResourceWrapper) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
rw.resource.Schema(ctx, req, resp)
resp.Schema = customschema.CustomizeSchema(rw.resourceJSON, resp.Schema)
}
func (rw *ResourceWrapper) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
rw.resource.Create(ctx, req, resp)
}
func (rw *ResourceWrapper) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
rw.resource.Read(ctx, req, resp)
}
func (rw *ResourceWrapper) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
rw.resource.Update(ctx, req, resp)
}
func (rw *ResourceWrapper) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
rw.resource.Delete(ctx, req, resp)
}
func (rw *ResourceWrapper) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if rs, ok := rw.resource.(resource.ResourceWithConfigure); ok {
rs.Configure(ctx, req, resp)
}
}
func (rw *ResourceWrapper) ConfigValidators(ctx context.Context) []resource.ConfigValidator {
if rs, ok := rw.resource.(resource.ResourceWithConfigValidators); ok {
return rs.ConfigValidators(ctx)
}
return nil
}
func (rw *ResourceWrapper) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
if rs, ok := rw.resource.(resource.ResourceWithImportState); ok {
rs.ImportState(ctx, req, resp)
} else {
resp.Diagnostics.AddError(
"Resource Import Not Implemented",
"This resource does not support import.",
)
}
}
func (rw *ResourceWrapper) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
if rs, ok := rw.resource.(resource.ResourceWithModifyPlan); ok {
rs.ModifyPlan(ctx, req, resp)
}
}
func (rw *ResourceWrapper) UpgradeState(ctx context.Context) map[int64]resource.StateUpgrader {
if rs, ok := rw.resource.(resource.ResourceWithUpgradeState); ok {
return rs.UpgradeState(ctx)
}
return make(map[int64]resource.StateUpgrader, 0)
}