|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform/helper/schema" |
| 7 | +) |
| 8 | + |
| 9 | +type IpLoadbalancingTcpFrontend struct { |
| 10 | + FrontendId int `json:"frontendId,omitempty"` |
| 11 | + Port string `json:"port"` |
| 12 | + Zone string `json:"zone"` |
| 13 | + AllowedSource []string `json:"allowedSource,omitempty"` |
| 14 | + DedicatedIpFo []string `json:"dedicatedIpfo,omitempty"` |
| 15 | + DefaultFarmId *int `json:"defaultFarmId,omitempty"` |
| 16 | + DefaultSslId *int `json:"defaultSslId,omitempty"` |
| 17 | + Disabled *bool `json:"disabled"` |
| 18 | + Ssl *bool `json:"ssl"` |
| 19 | + DisplayName string `json:"displayName,omitempty"` |
| 20 | +} |
| 21 | + |
| 22 | +func resourceIpLoadbalancingTcpFrontend() *schema.Resource { |
| 23 | + return &schema.Resource{ |
| 24 | + Create: resourceIpLoadbalancingTcpFrontendCreate, |
| 25 | + Read: resourceIpLoadbalancingTcpFrontendRead, |
| 26 | + Update: resourceIpLoadbalancingTcpFrontendUpdate, |
| 27 | + Delete: resourceIpLoadbalancingTcpFrontendDelete, |
| 28 | + |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "service_name": &schema.Schema{ |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + ForceNew: true, |
| 34 | + }, |
| 35 | + "port": &schema.Schema{ |
| 36 | + Type: schema.TypeString, |
| 37 | + Required: true, |
| 38 | + ForceNew: false, |
| 39 | + }, |
| 40 | + "zone": &schema.Schema{ |
| 41 | + Type: schema.TypeString, |
| 42 | + Required: true, |
| 43 | + ForceNew: false, |
| 44 | + }, |
| 45 | + "allowed_source": { |
| 46 | + Type: schema.TypeSet, |
| 47 | + Optional: true, |
| 48 | + Computed: true, |
| 49 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 50 | + Set: schema.HashString, |
| 51 | + }, |
| 52 | + "dedicated_ipfo": { |
| 53 | + Type: schema.TypeSet, |
| 54 | + Optional: true, |
| 55 | + Computed: true, |
| 56 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 57 | + Set: schema.HashString, |
| 58 | + }, |
| 59 | + "default_farm_id": &schema.Schema{ |
| 60 | + Type: schema.TypeInt, |
| 61 | + Optional: true, |
| 62 | + Computed: true, |
| 63 | + ForceNew: false, |
| 64 | + }, |
| 65 | + "default_ssl_id": &schema.Schema{ |
| 66 | + Type: schema.TypeInt, |
| 67 | + Computed: true, |
| 68 | + Optional: true, |
| 69 | + ForceNew: false, |
| 70 | + }, |
| 71 | + "disabled": &schema.Schema{ |
| 72 | + Type: schema.TypeBool, |
| 73 | + Default: false, |
| 74 | + Optional: true, |
| 75 | + ForceNew: false, |
| 76 | + }, |
| 77 | + "ssl": &schema.Schema{ |
| 78 | + Type: schema.TypeBool, |
| 79 | + Default: false, |
| 80 | + Optional: true, |
| 81 | + ForceNew: false, |
| 82 | + }, |
| 83 | + "display_name": &schema.Schema{ |
| 84 | + Type: schema.TypeString, |
| 85 | + Optional: true, |
| 86 | + ForceNew: false, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func resourceIpLoadbalancingTcpFrontendCreate(d *schema.ResourceData, meta interface{}) error { |
| 93 | + config := meta.(*Config) |
| 94 | + |
| 95 | + allowedSources := stringsFromSchema(d, "allowed_source") |
| 96 | + dedicatedIpFo := stringsFromSchema(d, "dedicated_ipfo") |
| 97 | + |
| 98 | + for _, s := range allowedSources { |
| 99 | + if err := validateIpBlock(s); err != nil { |
| 100 | + return fmt.Errorf("Error validating `allowed_source` value: %s", err) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + for _, s := range dedicatedIpFo { |
| 105 | + if err := validateIpBlock(s); err != nil { |
| 106 | + return fmt.Errorf("Error validating `dedicated_ipfo` value: %s", err) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + frontend := &IpLoadbalancingTcpFrontend{ |
| 111 | + Port: d.Get("port").(string), |
| 112 | + Zone: d.Get("zone").(string), |
| 113 | + AllowedSource: allowedSources, |
| 114 | + DedicatedIpFo: dedicatedIpFo, |
| 115 | + Disabled: getNilBoolPointer(d.Get("disabled").(bool)), |
| 116 | + Ssl: getNilBoolPointer(d.Get("ssl").(bool)), |
| 117 | + DisplayName: d.Get("display_name").(string), |
| 118 | + } |
| 119 | + |
| 120 | + if farmId, ok := d.GetOk("default_farm_id"); ok { |
| 121 | + frontend.DefaultFarmId = getNilIntPointer(farmId.(int)) |
| 122 | + } |
| 123 | + if sslId, ok := d.GetOk("default_ssl_id"); ok { |
| 124 | + frontend.DefaultSslId = getNilIntPointer(sslId.(int)) |
| 125 | + } |
| 126 | + |
| 127 | + service := d.Get("service_name").(string) |
| 128 | + frontend.DefaultFarmId = nil |
| 129 | + resp := &IpLoadbalancingTcpFrontend{} |
| 130 | + endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/frontend", service) |
| 131 | + |
| 132 | + err := config.OVHClient.Post(endpoint, frontend, resp) |
| 133 | + if err != nil { |
| 134 | + return fmt.Errorf("calling POST %s:\n\t %s", endpoint, err.Error()) |
| 135 | + } |
| 136 | + return readIpLoadbalancingTcpFrontend(resp, d) |
| 137 | + |
| 138 | +} |
| 139 | + |
| 140 | +func resourceIpLoadbalancingTcpFrontendRead(d *schema.ResourceData, meta interface{}) error { |
| 141 | + config := meta.(*Config) |
| 142 | + service := d.Get("service_name").(string) |
| 143 | + r := &IpLoadbalancingTcpFrontend{} |
| 144 | + endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/frontend/%s", service, d.Id()) |
| 145 | + |
| 146 | + err := config.OVHClient.Get(endpoint, &r) |
| 147 | + if err != nil { |
| 148 | + return fmt.Errorf("calling %s:\n\t %s", endpoint, err.Error()) |
| 149 | + } |
| 150 | + return readIpLoadbalancingTcpFrontend(r, d) |
| 151 | +} |
| 152 | + |
| 153 | +func resourceIpLoadbalancingTcpFrontendUpdate(d *schema.ResourceData, meta interface{}) error { |
| 154 | + config := meta.(*Config) |
| 155 | + service := d.Get("service_name").(string) |
| 156 | + endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/frontend/%s", service, d.Id()) |
| 157 | + |
| 158 | + allowedSources := stringsFromSchema(d, "allowed_source") |
| 159 | + dedicatedIpFo := stringsFromSchema(d, "dedicated_ipfo") |
| 160 | + |
| 161 | + for _, s := range allowedSources { |
| 162 | + if err := validateIpBlock(s); err != nil { |
| 163 | + return fmt.Errorf("Error validating `allowed_source` value: %s", err) |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + for _, s := range dedicatedIpFo { |
| 168 | + if err := validateIpBlock(s); err != nil { |
| 169 | + return fmt.Errorf("Error validating `dedicated_ipfo` value: %s", err) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + frontend := &IpLoadbalancingTcpFrontend{ |
| 174 | + Port: d.Get("port").(string), |
| 175 | + Zone: d.Get("zone").(string), |
| 176 | + AllowedSource: allowedSources, |
| 177 | + DedicatedIpFo: dedicatedIpFo, |
| 178 | + Disabled: getNilBoolPointer(d.Get("disabled").(bool)), |
| 179 | + Ssl: getNilBoolPointer(d.Get("ssl").(bool)), |
| 180 | + DisplayName: d.Get("display_name").(string), |
| 181 | + } |
| 182 | + |
| 183 | + if farmId, ok := d.GetOk("default_farm_id"); ok { |
| 184 | + frontend.DefaultFarmId = getNilIntPointer(farmId.(int)) |
| 185 | + } |
| 186 | + if sslId, ok := d.GetOk("default_ssl_id"); ok { |
| 187 | + frontend.DefaultSslId = getNilIntPointer(sslId.(int)) |
| 188 | + } |
| 189 | + |
| 190 | + err := config.OVHClient.Put(endpoint, frontend, nil) |
| 191 | + if err != nil { |
| 192 | + return fmt.Errorf("calling %s:\n\t %s", endpoint, err.Error()) |
| 193 | + } |
| 194 | + return nil |
| 195 | +} |
| 196 | + |
| 197 | +func readIpLoadbalancingTcpFrontend(r *IpLoadbalancingTcpFrontend, d *schema.ResourceData) error { |
| 198 | + d.Set("display_name", r.DisplayName) |
| 199 | + d.Set("port", r.Port) |
| 200 | + d.Set("zone", r.Zone) |
| 201 | + |
| 202 | + allowedSources := make([]string, 0) |
| 203 | + for _, s := range r.AllowedSource { |
| 204 | + allowedSources = append(allowedSources, s) |
| 205 | + } |
| 206 | + d.Set("allowed_source", allowedSources) |
| 207 | + |
| 208 | + dedicatedIpFos := make([]string, 0) |
| 209 | + for _, s := range r.DedicatedIpFo { |
| 210 | + dedicatedIpFos = append(dedicatedIpFos, s) |
| 211 | + } |
| 212 | + d.Set("dedicated_ipfo", dedicatedIpFos) |
| 213 | + |
| 214 | + if r.DefaultFarmId != nil { |
| 215 | + d.Set("default_farm_id", r.DefaultFarmId) |
| 216 | + } |
| 217 | + |
| 218 | + if r.DefaultSslId != nil { |
| 219 | + d.Set("default_ssl_id", r.DefaultSslId) |
| 220 | + } |
| 221 | + if r.Disabled != nil { |
| 222 | + d.Set("disabled", r.Disabled) |
| 223 | + } |
| 224 | + if r.Ssl != nil { |
| 225 | + d.Set("ssl", r.Ssl) |
| 226 | + } |
| 227 | + |
| 228 | + d.SetId(fmt.Sprintf("%d", r.FrontendId)) |
| 229 | + |
| 230 | + return nil |
| 231 | +} |
| 232 | + |
| 233 | +func resourceIpLoadbalancingTcpFrontendDelete(d *schema.ResourceData, meta interface{}) error { |
| 234 | + config := meta.(*Config) |
| 235 | + |
| 236 | + service := d.Get("service_name").(string) |
| 237 | + r := &IpLoadbalancingTcpFrontend{} |
| 238 | + endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/frontend/%s", service, d.Id()) |
| 239 | + |
| 240 | + err := config.OVHClient.Delete(endpoint, &r) |
| 241 | + if err != nil { |
| 242 | + return fmt.Errorf("Error calling %s: %s \n", endpoint, err.Error()) |
| 243 | + } |
| 244 | + |
| 245 | + return nil |
| 246 | +} |
0 commit comments