|
| 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