/
resource_script.go
142 lines (124 loc) · 4.91 KB
/
resource_script.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
package mikrotik
import (
"context"
"github.com/ddelnano/terraform-provider-mikrotik/client"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
tftypes "github.com/hashicorp/terraform-plugin-framework/types"
)
type script struct {
client *client.Mikrotik
}
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &script{}
_ resource.ResourceWithConfigure = &script{}
_ resource.ResourceWithImportState = &script{}
)
// NewScriptResource is a helper function to simplify the provider implementation.
func NewScriptResource() resource.Resource {
return &script{}
}
func (r *script) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
r.client = req.ProviderData.(*client.Mikrotik)
}
// Metadata returns the resource type name.
func (r *script) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_script"
}
// Schema defines the schema for the resource.
func (s *script) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Creates a MikroTik Script.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Description: "ID of this resource.",
},
"name": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Description: "The name of script.",
},
"owner": schema.StringAttribute{
Required: true,
Description: "The owner of the script.",
},
"policy": schema.ListAttribute{
Required: true,
ElementType: tftypes.StringType,
Description: "What permissions the script has. This must be one of the following: ftp, reboot, read, write, policy, test, password, sniff, sensitive, romon.",
},
"dont_require_permissions": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
Description: "If the script requires permissions or not.",
},
"source": schema.StringAttribute{
Required: true,
Description: "The source code of the script. See the [MikroTik docs](https://wiki.mikrotik.com/wiki/Manual:Scripting) for the scripting language.",
},
},
}
}
// Create creates the resource and sets the initial Terraform state.
func (r *script) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan scriptModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
GenericCreateResource(&plan, &client.Script{}, r.client)(ctx, req, resp)
}
// Read refreshes the Terraform state with the latest data.
func (r *script) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state scriptModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
GenericReadResource(&state, &client.Script{}, r.client)(ctx, req, resp)
}
// Update updates the resource and sets the updated Terraform state on success.
func (r *script) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan scriptModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
GenericUpdateResource(&plan, &client.Script{}, r.client)(ctx, req, resp)
}
// Delete deletes the resource and removes the Terraform state on success.
func (r *script) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state scriptModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
GenericDeleteResource(&state, &client.Script{}, r.client)(ctx, req, resp)
}
func (r *script) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// Retrieve import ID and save to id attribute
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
}
type scriptModel struct {
Id tftypes.String `tfsdk:"id"`
Name tftypes.String `tfsdk:"name"`
Owner tftypes.String `tfsdk:"owner"`
Policy tftypes.List `tfsdk:"policy"`
DontRequirePermissions tftypes.Bool `tfsdk:"dont_require_permissions"`
Source tftypes.String `tfsdk:"source"`
}