forked from ExalDraen/terraform-provider-solace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_clientusername.go
226 lines (196 loc) · 6.37 KB
/
resource_clientusername.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package solace
import (
"fmt"
"log"
"strings"
"github.com/PatrickDelancy/semp-client/client/all"
"github.com/PatrickDelancy/semp-client/models"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceClientUsername() *schema.Resource {
return &schema.Resource{
Create: resourceClientUsernameCreate,
Read: resourceClientUsernameRead,
Update: resourceClientUsernameUpdate,
Delete: resourceClientUsernameDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The name of the Client. Used as a unique identifier.",
Required: true,
ForceNew: true,
},
// Each Client username must belong to a VPN, but optionally we use the provider set default,
// and bail if neither is set. Thus the parameter is optional.
"msg_vpn": {
Type: schema.TypeString,
Description: "The name of the MSG VPN. If unset the provider default is used.",
Optional: true,
Computed: true,
ForceNew: true,
},
"enabled": {
Type: schema.TypeBool,
Description: "Enables or disables the Client Username. When disabled all clients currently connected as the Client Username are disconnected.",
Optional: true,
},
"acl": {
Type: schema.TypeString,
Description: "The ACL Profile of the Client Username. The default value is \"default\".",
Optional: true,
},
"password": {
Type: schema.TypeString,
Description: "The password of this Client Username for internal Authentication. The default is to have no password.",
Optional: true,
},
"profile": {
Type: schema.TypeString,
Description: "The Client Profile of the Client Username. The default value is \"default\".",
Optional: true,
Default: "default",
},
},
Importer: &schema.ResourceImporter{
State: resourceClientUsernameImport,
},
}
}
func resourceClientUsernameCreate(d *schema.ResourceData, m interface{}) error {
log.Print("[DEBUG] Creating Client username ...")
// Get our Solace client
c := m.(*Config)
client := c.Client
auth := c.Auth
// Extract config data from resource data and prepare new VPN object
name := d.Get("name").(string)
vpn, err := getMsgVPN(d, c)
if err != nil {
return err
}
user := models.MsgVpnClientUsername{
ClientUsername: name,
MsgVpnName: vpn,
}
// Only set these if they're actually set (not their default value)
if v, ok := d.GetOk("enabled"); ok {
val := v.(bool)
user.Enabled = val
}
if v, ok := d.GetOk("acl"); ok {
user.ACLProfileName = v.(string)
}
if v, ok := d.GetOk("password"); ok {
val := v.(string)
user.Password = val
}
if v, ok := d.GetOk("profile"); ok {
user.ClientProfileName = v.(string)
}
params := all.NewCreateMsgVpnClientUsernameParams()
params.MsgVpnName = vpn
params.Body = &user
resp, err := client.All.CreateMsgVpnClientUsername(params, auth)
if err != nil {
sempErr := err.(*all.CreateMsgVpnClientUsernameDefault).Payload.Meta.Error
return fmt.Errorf("[ERROR] Unable to create Client username %q on vpn %q: %v", name, vpn, formatError(sempErr))
}
d.SetId(resp.Payload.Data.ClientUsername)
log.Printf("[DEBUG] Finished creating Client username %q on VPN %q", name, vpn)
return resourceClientUsernameRead(d, m)
}
func resourceClientUsernameRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] Reading Client username %q ...", d.Id())
c := m.(*Config)
client := c.Client
auth := c.Auth
params := all.NewGetMsgVpnClientUsernameParams()
vpn, err := getMsgVPN(d, c)
if err != nil {
return err
}
params.ClientUsername = d.Id()
params.MsgVpnName = vpn
resp, err := client.All.GetMsgVpnClientUsername(params, auth)
if err != nil {
log.Printf("[WARN] No Client username found: %s", d.Id())
d.SetId("")
return nil
}
d.Set("name", resp.Payload.Data.ClientUsername)
d.Set("msg_vpn", resp.Payload.Data.MsgVpnName)
d.Set("enabled", resp.Payload.Data.Enabled)
d.Set("acl", resp.Payload.Data.ACLProfileName)
d.Set("password", resp.Payload.Data.Password)
d.Set("profile", resp.Payload.Data.ClientProfileName)
return nil
}
func resourceClientUsernameUpdate(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] Updating Client username %q ...", d.Id())
c := m.(*Config)
client := c.Client
auth := c.Auth
params := all.NewUpdateMsgVpnClientUsernameParams()
vpn, err := getMsgVPN(d, c)
if err != nil {
return err
}
params.ClientUsername = d.Id()
params.MsgVpnName = vpn
user := models.MsgVpnClientUsername{}
user.MsgVpnName = vpn
// Only include changed values; anything we don't specify does not get updated
if d.HasChange("enabled") {
val := d.Get("enabled").(bool)
user.Enabled = val
}
if d.HasChange("acl") {
user.ACLProfileName = d.Get("acl").(string)
}
if d.HasChange("password") {
val := d.Get("password").(string)
user.Password = val
}
if d.HasChange("profile") {
user.ClientProfileName = d.Get("profile").(string)
}
params.Body = &user
_, err = client.All.UpdateMsgVpnClientUsername(params, auth)
if err != nil {
sempErr := err.(*all.UpdateMsgVpnClientUsernameDefault).Payload.Meta.Error
return fmt.Errorf("[ERROR] Unable to update Client username %q: %v", params.ClientUsername, formatError(sempErr))
}
return resourceClientUsernameRead(d, m)
}
func resourceClientUsernameDelete(d *schema.ResourceData, m interface{}) error {
c := m.(*Config)
client := c.Client
auth := c.Auth
params := all.NewDeleteMsgVpnClientUsernameParams()
vpn, err := getMsgVPN(d, c)
if err != nil {
return err
}
params.ClientUsername = d.Id()
params.MsgVpnName = vpn
_, err = client.All.DeleteMsgVpnClientUsername(params, auth)
if err != nil {
sempErr := err.(*all.DeleteMsgVpnClientUsernameDefault).Payload.Meta.Error
return fmt.Errorf("[ERROR] Unable to delete Client username %q: %v", params.MsgVpnName, formatError(sempErr))
}
// d.SetId("") is automatically called assuming delete returns no errors, but
// it is added here for explicitness.
d.SetId("")
return nil
}
func resourceClientUsernameImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "/")
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return nil, fmt.Errorf("unexpected format of ID (%q), expected MSG-VPN/CLIENT-USERNAME", d.Id())
}
vpn := idParts[0]
user := idParts[1]
d.Set("msg_vpn", vpn)
d.SetId(user)
return []*schema.ResourceData{d}, nil
}