-
Notifications
You must be signed in to change notification settings - Fork 29
/
resource_script.go
146 lines (123 loc) · 3.26 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
143
144
145
146
package mikrotik
import (
"context"
"github.com/ddelnano/terraform-provider-mikrotik/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceScript() *schema.Resource {
return &schema.Resource{
CreateContext: resourceScriptCreate,
ReadContext: resourceScriptRead,
UpdateContext: resourceScriptUpdate,
DeleteContext: resourceScriptDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"owner": {
Type: schema.TypeString,
Required: true,
},
"source": {
Type: schema.TypeString,
Required: true,
},
"policy": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"dont_require_permissions": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func resourceScriptCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
name := d.Get("name").(string)
owner := d.Get("owner").(string)
source := d.Get("source").(string)
policy := d.Get("policy").([]interface{})
policies := []string{}
for _, p := range policy {
policies = append(policies, p.(string))
}
dontReqPerms := d.Get("dont_require_permissions").(bool)
c := m.(*client.Mikrotik)
script, err := c.CreateScript(
name,
owner,
source,
policies,
dontReqPerms,
)
if err != nil {
return diag.FromErr(err)
}
return scriptToData(script, d)
}
func scriptToData(s *client.Script, d *schema.ResourceData) diag.Diagnostics {
values := map[string]interface{}{
"name": s.Name,
"owner": s.Owner,
"source": s.Source,
"policy": s.Policy(),
"dont_require_permissions": s.DontRequirePermissions,
}
d.SetId(s.Name)
var diags diag.Diagnostics
for key, value := range values {
if err := d.Set(key, value); err != nil {
diags = append(diags, diag.Errorf("failed to set %s: %v", key, err)...)
}
}
return diags
}
func resourceScriptRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*client.Mikrotik)
script, err := c.FindScript(d.Id())
if err != nil {
d.SetId("")
return nil
}
return scriptToData(script, d)
}
func resourceScriptUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
name := d.Get("name").(string)
owner := d.Get("owner").(string)
source := d.Get("source").(string)
dontReqPerms := d.Get("dont_require_permissions").(bool)
policy := d.Get("policy").([]interface{})
policies := []string{}
for _, p := range policy {
str, ok := p.(string)
if ok {
policies = append(policies, str)
}
}
c := m.(*client.Mikrotik)
script, err := c.UpdateScript(name, owner, source, policies, dontReqPerms)
if err != nil {
return diag.FromErr(err)
}
return scriptToData(script, d)
}
func resourceScriptDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
name := d.Id()
c := m.(*client.Mikrotik)
err := c.DeleteScript(name)
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
}