forked from sacloud/terraform-provider-sakuracloud
/
resource_sakuracloud_vpc_router_user.go
163 lines (135 loc) · 4.94 KB
/
resource_sakuracloud_vpc_router_user.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
package sakuracloud
import (
"fmt"
"bytes"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/sacloud/libsacloud/api"
"github.com/sacloud/libsacloud/sacloud"
)
func resourceSakuraCloudVPCRouterRemoteAccessUser() *schema.Resource {
return &schema.Resource{
Create: resourceSakuraCloudVPCRouterRemoteAccessUserCreate,
Read: resourceSakuraCloudVPCRouterRemoteAccessUserRead,
Delete: resourceSakuraCloudVPCRouterRemoteAccessUserDelete,
Schema: map[string]*schema.Schema{
"vpc_router_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateSakuracloudIDType,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateMaxLength(1, 20),
},
"password": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateMaxLength(1, 20),
},
"zone": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: "target SakuraCloud zone",
ValidateFunc: validateStringInWord([]string{"is1a", "is1b", "tk1a", "tk1v"}),
},
},
}
}
func resourceSakuraCloudVPCRouterRemoteAccessUserCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
zone, ok := d.GetOk("zone")
if ok {
client.Zone = zone.(string)
}
routerID := d.Get("vpc_router_id").(string)
sakuraMutexKV.Lock(routerID)
defer sakuraMutexKV.Unlock(routerID)
vpcRouter, err := client.VPCRouter.Read(toSakuraCloudID(routerID))
if err != nil {
return fmt.Errorf("Couldn't find SakuraCloud VPCRouter resource: %s", err)
}
remoteAccessUser := expandVPCRouterRemoteAccessUser(d)
if vpcRouter.Settings == nil {
vpcRouter.InitVPCRouterSetting()
}
vpcRouter.Settings.Router.AddRemoteAccessUser(remoteAccessUser.UserName, remoteAccessUser.Password)
vpcRouter, err = client.VPCRouter.UpdateSetting(toSakuraCloudID(routerID), vpcRouter)
if err != nil {
return fmt.Errorf("Failed to enable SakuraCloud VPCRouterRemoteAccessUser resource: %s", err)
}
_, err = client.VPCRouter.Config(toSakuraCloudID(routerID))
if err != nil {
return fmt.Errorf("Couldn'd apply SakuraCloud VPCRouter config: %s", err)
}
d.SetId(vpcRouterRemoteAccessUserIDHash(routerID, remoteAccessUser))
return resourceSakuraCloudVPCRouterRemoteAccessUserRead(d, meta)
}
func resourceSakuraCloudVPCRouterRemoteAccessUserRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
zone, ok := d.GetOk("zone")
if ok {
client.Zone = zone.(string)
}
routerID := d.Get("vpc_router_id").(string)
vpcRouter, err := client.VPCRouter.Read(toSakuraCloudID(routerID))
if err != nil {
return fmt.Errorf("Couldn't find SakuraCloud VPCRouter resource: %s", err)
}
remoteAccessUser := expandVPCRouterRemoteAccessUser(d)
if vpcRouter.Settings != nil && vpcRouter.Settings.Router != nil && vpcRouter.Settings.Router.RemoteAccessUsers != nil &&
vpcRouter.Settings.Router.FindRemoteAccessUser(remoteAccessUser.UserName, remoteAccessUser.Password) != nil {
d.Set("name", remoteAccessUser.UserName)
d.Set("password", remoteAccessUser.Password)
}
d.Set("zone", client.Zone)
return nil
}
func resourceSakuraCloudVPCRouterRemoteAccessUserDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
zone, ok := d.GetOk("zone")
if ok {
client.Zone = zone.(string)
}
routerID := d.Get("vpc_router_id").(string)
sakuraMutexKV.Lock(routerID)
defer sakuraMutexKV.Unlock(routerID)
vpcRouter, err := client.VPCRouter.Read(toSakuraCloudID(routerID))
if err != nil {
return fmt.Errorf("Couldn't find SakuraCloud VPCRouter resource: %s", err)
}
if vpcRouter.Settings.Router.RemoteAccessUsers != nil {
remoteAccessUser := expandVPCRouterRemoteAccessUser(d)
vpcRouter.Settings.Router.RemoveRemoteAccessUser(remoteAccessUser.UserName, remoteAccessUser.Password)
vpcRouter, err = client.VPCRouter.UpdateSetting(toSakuraCloudID(routerID), vpcRouter)
if err != nil {
return fmt.Errorf("Failed to delete SakuraCloud VPCRouterRemoteAccessUser resource: %s", err)
}
_, err = client.VPCRouter.Config(toSakuraCloudID(routerID))
if err != nil {
return fmt.Errorf("Couldn'd apply SakuraCloud VPCRouter config: %s", err)
}
}
d.SetId("")
return nil
}
func vpcRouterRemoteAccessUserIDHash(routerID string, s *sacloud.VPCRouterRemoteAccessUsersConfig) string {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("%s-", routerID))
buf.WriteString(fmt.Sprintf("%s-", s.UserName))
buf.WriteString(fmt.Sprintf("%s", s.Password))
return fmt.Sprintf("%d", hashcode.String(buf.String()))
}
func expandVPCRouterRemoteAccessUser(d *schema.ResourceData) *sacloud.VPCRouterRemoteAccessUsersConfig {
var remoteAccessUser = &sacloud.VPCRouterRemoteAccessUsersConfig{
UserName: d.Get("name").(string),
Password: d.Get("password").(string),
}
return remoteAccessUser
}