Skip to content

Commit 394fe28

Browse files
committed
feat(resource): add website resource for CRD operations
1 parent 316ddce commit 394fe28

File tree

4 files changed

+187
-1
lines changed

4 files changed

+187
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ go 1.15
44

55
require (
66
github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.4
7-
github.com/maxjoehnk/microsoft-iis-administration v0.0.0-20201007080603-30daafe5102f
7+
github.com/maxjoehnk/microsoft-iis-administration v0.0.0-20201113093929-54ddb6f5a56f
88
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx
237237
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
238238
github.com/maxjoehnk/microsoft-iis-administration v0.0.0-20201007080603-30daafe5102f h1:VkXJbWEnu5I+51zkFPOEXkaIgJYIShRW0O3l1i1Ojww=
239239
github.com/maxjoehnk/microsoft-iis-administration v0.0.0-20201007080603-30daafe5102f/go.mod h1:RvSyx6SPV+AkzVjVK08vD1VvJ4Hgas/o31yCOesK+hA=
240+
github.com/maxjoehnk/microsoft-iis-administration v0.0.0-20201113080704-34c54058f44a h1:7QfGYtMEvuZV4yRKANeKVHZlbSAgzUBVuDjFZKCZqtE=
241+
github.com/maxjoehnk/microsoft-iis-administration v0.0.0-20201113080704-34c54058f44a/go.mod h1:RvSyx6SPV+AkzVjVK08vD1VvJ4Hgas/o31yCOesK+hA=
242+
github.com/maxjoehnk/microsoft-iis-administration v0.0.0-20201113093929-54ddb6f5a56f h1:4qI2ThQ2PtiDkI9TusLC6i3DOATmccnpjiDgcLox9UI=
243+
github.com/maxjoehnk/microsoft-iis-administration v0.0.0-20201113093929-54ddb6f5a56f/go.mod h1:RvSyx6SPV+AkzVjVK08vD1VvJ4Hgas/o31yCOesK+hA=
240244
github.com/mitchellh/cli v1.1.1/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI=
241245
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
242246
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=

iis/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func Provider() *schema.Provider {
2323
"iis_application_pool": resourceApplicationPool(),
2424
"iis_application": resourceApplication(),
2525
"iis_authentication": resourceAuthentication(),
26+
"iis_website": resourceWebsite(),
2627
},
2728
DataSourcesMap: map[string]*schema.Resource{
2829
"iis_website": dataSourceIisWebsite(),

iis/resource_website.go

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package iis
2+
3+
import (
4+
"context"
5+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"github.com/maxjoehnk/microsoft-iis-administration"
8+
)
9+
10+
const nameKey = "name"
11+
const physicalPathKey = "physical_path"
12+
const bindingsKey = "binding"
13+
const appPoolKey = "application_pool"
14+
15+
const bindingProtocolKey = "protocol"
16+
const bindingPortKey = "port"
17+
const bindingAddressKey = "ip_address"
18+
const bindingHostKey = "hostname"
19+
20+
func resourceWebsite() *schema.Resource {
21+
return &schema.Resource{
22+
CreateContext: resourceWebsiteCreate,
23+
ReadContext: resourceWebsiteRead,
24+
UpdateContext: resourceWebsiteUpdate,
25+
DeleteContext: resourceWebsiteDelete,
26+
27+
Schema: map[string]*schema.Schema{
28+
nameKey: {
29+
Type: schema.TypeString,
30+
Required: true,
31+
},
32+
physicalPathKey: {
33+
Type: schema.TypeString,
34+
Required: true,
35+
},
36+
appPoolKey: {
37+
Type: schema.TypeString,
38+
Optional: true,
39+
},
40+
bindingsKey: {
41+
Type: schema.TypeSet,
42+
Required: true,
43+
ForceNew: true,
44+
Elem: bindingSchema,
45+
},
46+
},
47+
}
48+
}
49+
50+
var bindingSchema = &schema.Resource{
51+
Schema: map[string]*schema.Schema{
52+
bindingProtocolKey: {
53+
Type: schema.TypeString,
54+
Default: "http",
55+
Optional: true,
56+
},
57+
bindingPortKey: {
58+
Type: schema.TypeInt,
59+
Default: 80,
60+
Optional: true,
61+
},
62+
bindingAddressKey: {
63+
Type: schema.TypeString,
64+
Default: "*",
65+
Optional: true,
66+
},
67+
bindingHostKey: {
68+
Type: schema.TypeString,
69+
Optional: true,
70+
},
71+
},
72+
}
73+
74+
func resourceWebsiteCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
75+
client := m.(*iis.Client)
76+
request := createWebsiteRequest(d)
77+
site, err := client.CreateWebsite(ctx, request)
78+
if err != nil {
79+
return diag.FromErr(err)
80+
}
81+
d.SetId(site.ID)
82+
return nil
83+
}
84+
85+
func resourceWebsiteRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
86+
client := m.(*iis.Client)
87+
site, err := client.ReadWebsite(ctx, d.Id())
88+
if err != nil {
89+
d.SetId("")
90+
return diag.FromErr(err)
91+
}
92+
if err = d.Set(nameKey, site.Name); err != nil {
93+
return diag.FromErr(err)
94+
}
95+
if err = d.Set(physicalPathKey, site.PhysicalPath); err != nil {
96+
return diag.FromErr(err)
97+
}
98+
if err = d.Set(appPoolKey, site.ApplicationPool.ID); err != nil {
99+
return diag.FromErr(err)
100+
}
101+
if err = d.Set(bindingsKey, mapBindingsToSet(site)); err != nil {
102+
return diag.FromErr(err)
103+
}
104+
return nil
105+
}
106+
107+
func resourceWebsiteUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
108+
return nil
109+
}
110+
111+
func resourceWebsiteDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
112+
client := m.(*iis.Client)
113+
id := d.Id()
114+
err := client.DeleteWebsite(ctx, id)
115+
if err != nil {
116+
return diag.FromErr(err)
117+
}
118+
return nil
119+
}
120+
121+
func createWebsiteRequest(d *schema.ResourceData) iis.CreateWebsiteRequest {
122+
name := d.Get(nameKey).(string)
123+
physicalPath := d.Get(physicalPathKey).(string)
124+
bindings := d.Get(bindingsKey).(*schema.Set)
125+
request := iis.CreateWebsiteRequest{
126+
Name: name,
127+
PhysicalPath: physicalPath,
128+
Bindings: getBindings(bindings),
129+
}
130+
appPool := d.Get(appPoolKey)
131+
if appPool != nil {
132+
request.ApplicationPool = iis.ApplicationReference{
133+
ID: appPool.(string),
134+
}
135+
}
136+
return request
137+
}
138+
139+
func getBindings(b *schema.Set) []iis.WebsiteBinding {
140+
bindings := make([]iis.WebsiteBinding, b.Len())
141+
for i, entry := range b.List() {
142+
binding := entry.(map[string]interface{})
143+
protocol := binding[bindingProtocolKey].(string)
144+
port := binding[bindingPortKey].(int64)
145+
ipAddress := binding[bindingAddressKey].(string)
146+
hostname := binding[bindingHostKey].(string)
147+
148+
bindings[i] = iis.WebsiteBinding{
149+
Port: port,
150+
IPAddress: ipAddress,
151+
Hostname: hostname,
152+
Protocol: protocol,
153+
}
154+
}
155+
156+
return bindings
157+
}
158+
159+
func mapBindingsToSet(site *iis.Website) *schema.Set {
160+
var bindings []interface{}
161+
for _, binding := range site.Bindings {
162+
bindings = append(bindings, map[string]interface{}{
163+
bindingProtocolKey: binding.Protocol,
164+
bindingAddressKey: binding.IPAddress,
165+
bindingPortKey: binding.Port,
166+
bindingHostKey: binding.Hostname,
167+
})
168+
}
169+
set := schema.NewSet(hashBinding, bindings)
170+
return set
171+
}
172+
173+
func hashBinding(v interface{}) int {
174+
bindingMap := v.(map[string]interface{})
175+
address := schema.HashString(bindingMap[bindingAddressKey].(string))
176+
protocol := schema.HashString(bindingMap[bindingProtocolKey].(string))
177+
port := schema.HashInt(bindingMap[bindingPortKey].(int64))
178+
hostname := schema.HashString(bindingMap[bindingHostKey].(string))
179+
180+
return address + protocol + port + hostname
181+
}

0 commit comments

Comments
 (0)