|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +type IpLoadbalancingTcpFarmServer struct { |
| 13 | + BackendId int `json:"backendId,omitempty"` |
| 14 | + ServerId int `json:"serverId,omitempty"` |
| 15 | + FarmId int `json:"farmId,omitempty"` |
| 16 | + DisplayName *string `json:"displayName,omitempty"` |
| 17 | + Address *string `json:"address"` |
| 18 | + Cookie *string `json:"cookie,omitempty"` |
| 19 | + Port *int `json:"port"` |
| 20 | + ProxyProtocolVersion *string `json:"proxyProtocolVersion"` |
| 21 | + Chain *string `json:"chain"` |
| 22 | + Weight *int `json:"weight"` |
| 23 | + Probe *bool `json:"probe"` |
| 24 | + Ssl *bool `json:"ssl"` |
| 25 | + Backup *bool `json:"backup"` |
| 26 | + Status *string `json:"status"` |
| 27 | +} |
| 28 | + |
| 29 | +func resourceIpLoadbalancingTcpFarmServer() *schema.Resource { |
| 30 | + return &schema.Resource{ |
| 31 | + Create: resourceIpLoadbalancingTcpFarmServerCreate, |
| 32 | + Read: resourceIpLoadbalancingTcpFarmServerRead, |
| 33 | + Update: resourceIpLoadbalancingTcpFarmServerUpdate, |
| 34 | + Delete: resourceIpLoadbalancingTcpFarmServerDelete, |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "service_name": &schema.Schema{ |
| 37 | + Type: schema.TypeString, |
| 38 | + Required: true, |
| 39 | + ForceNew: true, |
| 40 | + }, |
| 41 | + "farm_id": &schema.Schema{ |
| 42 | + Type: schema.TypeInt, |
| 43 | + Required: true, |
| 44 | + ForceNew: true, |
| 45 | + }, |
| 46 | + "display_name": &schema.Schema{ |
| 47 | + Type: schema.TypeString, |
| 48 | + Optional: true, |
| 49 | + }, |
| 50 | + "address": &schema.Schema{ |
| 51 | + Type: schema.TypeString, |
| 52 | + Required: true, |
| 53 | + ForceNew: true, |
| 54 | + ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { |
| 55 | + ip := v.(string) |
| 56 | + if net.ParseIP(ip).To4() == nil { |
| 57 | + errors = append(errors, fmt.Errorf("Address %s is not an IPv4", ip)) |
| 58 | + } |
| 59 | + return |
| 60 | + }, |
| 61 | + }, |
| 62 | + "ssl": &schema.Schema{ |
| 63 | + Type: schema.TypeBool, |
| 64 | + Optional: true, |
| 65 | + Default: false, |
| 66 | + }, |
| 67 | + "cookie": &schema.Schema{ |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + }, |
| 71 | + "port": &schema.Schema{ |
| 72 | + Type: schema.TypeInt, |
| 73 | + Optional: true, |
| 74 | + }, |
| 75 | + "proxy_protocol_version": &schema.Schema{ |
| 76 | + Type: schema.TypeString, |
| 77 | + Optional: true, |
| 78 | + ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { |
| 79 | + err := validateStringEnum(v.(string), []string{"v1", "v2", "v2-ssl", "v2-ssl-cn"}) |
| 80 | + if err != nil { |
| 81 | + errors = append(errors, err) |
| 82 | + } |
| 83 | + return |
| 84 | + }, |
| 85 | + }, |
| 86 | + "chain": &schema.Schema{ |
| 87 | + Type: schema.TypeString, |
| 88 | + Optional: true, |
| 89 | + }, |
| 90 | + "weight": &schema.Schema{ |
| 91 | + Type: schema.TypeInt, |
| 92 | + Optional: true, |
| 93 | + Default: 1, |
| 94 | + }, |
| 95 | + "probe": &schema.Schema{ |
| 96 | + Type: schema.TypeBool, |
| 97 | + Optional: true, |
| 98 | + Default: false, |
| 99 | + }, |
| 100 | + "backup": &schema.Schema{ |
| 101 | + Type: schema.TypeBool, |
| 102 | + Optional: true, |
| 103 | + Default: false, |
| 104 | + }, |
| 105 | + "status": &schema.Schema{ |
| 106 | + Type: schema.TypeString, |
| 107 | + Required: true, |
| 108 | + ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { |
| 109 | + err := validateStringEnum(v.(string), []string{"active", "inactive"}) |
| 110 | + if err != nil { |
| 111 | + errors = append(errors, err) |
| 112 | + } |
| 113 | + return |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func resourceIpLoadbalancingTcpFarmServerCreate(d *schema.ResourceData, meta interface{}) error { |
| 121 | + config := meta.(*Config) |
| 122 | + |
| 123 | + newBackendServer := &IpLoadbalancingTcpFarmServer{ |
| 124 | + DisplayName: getNilStringPointer(d.Get("display_name").(string)), |
| 125 | + Address: getNilStringPointer(d.Get("address").(string)), |
| 126 | + Port: getNilIntPointer(d.Get("port").(int)), |
| 127 | + ProxyProtocolVersion: getNilStringPointer(d.Get("proxy_protocol_version").(string)), |
| 128 | + Chain: getNilStringPointer(d.Get("chain").(string)), |
| 129 | + Weight: getNilIntPointer(d.Get("weight").(int)), |
| 130 | + Probe: getNilBoolPointer(d.Get("probe").(bool)), |
| 131 | + Ssl: getNilBoolPointer(d.Get("ssl").(bool)), |
| 132 | + Backup: getNilBoolPointer(d.Get("backup").(bool)), |
| 133 | + Status: getNilStringPointer(d.Get("status").(string)), |
| 134 | + } |
| 135 | + |
| 136 | + service := d.Get("service_name").(string) |
| 137 | + farmid := d.Get("farm_id").(int) |
| 138 | + r := &IpLoadbalancingTcpFarmServer{} |
| 139 | + endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server", service, farmid) |
| 140 | + |
| 141 | + err := config.OVHClient.Post(endpoint, newBackendServer, r) |
| 142 | + if err != nil { |
| 143 | + return fmt.Errorf("calling %s with %d:\n\t %s", endpoint, farmid, err.Error()) |
| 144 | + } |
| 145 | + |
| 146 | + //set id |
| 147 | + d.SetId(fmt.Sprintf("%d", r.ServerId)) |
| 148 | + |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +func resourceIpLoadbalancingTcpFarmServerRead(d *schema.ResourceData, meta interface{}) error { |
| 153 | + config := meta.(*Config) |
| 154 | + |
| 155 | + service := d.Get("service_name").(string) |
| 156 | + farmid := d.Get("farm_id").(int) |
| 157 | + r := &IpLoadbalancingTcpFarmServer{} |
| 158 | + |
| 159 | + endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server/%s", service, farmid, d.Id()) |
| 160 | + |
| 161 | + err := config.OVHClient.Get(endpoint, r) |
| 162 | + if err != nil { |
| 163 | + return fmt.Errorf("calling %s :\n\t %q", endpoint, err) |
| 164 | + } |
| 165 | + log.Printf("[DEBUG] Response object from OVH : %v", r) |
| 166 | + |
| 167 | + d.Set("probe", *r.Probe) |
| 168 | + d.Set("ssl", *r.Ssl) |
| 169 | + d.Set("backup", *r.Backup) |
| 170 | + d.Set("address", *r.Address) |
| 171 | + if r.DisplayName != nil { |
| 172 | + d.Set("display_name", *r.DisplayName) |
| 173 | + } |
| 174 | + if r.Cookie != nil { |
| 175 | + d.Set("cookie", *r.Cookie) |
| 176 | + } |
| 177 | + d.Set("port", *r.Port) |
| 178 | + if r.ProxyProtocolVersion != nil { |
| 179 | + d.Set("proxy_protocol_version", *r.ProxyProtocolVersion) |
| 180 | + } |
| 181 | + if r.Chain != nil { |
| 182 | + d.Set("chain", *r.Chain) |
| 183 | + } |
| 184 | + d.Set("weight", *r.Weight) |
| 185 | + d.Set("status", *r.Status) |
| 186 | + |
| 187 | + return nil |
| 188 | +} |
| 189 | + |
| 190 | +func resourceIpLoadbalancingTcpFarmServerUpdate(d *schema.ResourceData, meta interface{}) error { |
| 191 | + config := meta.(*Config) |
| 192 | + |
| 193 | + update := &IpLoadbalancingTcpFarmServer{ |
| 194 | + DisplayName: getNilStringPointer(d.Get("display_name").(string)), |
| 195 | + Address: getNilStringPointer(d.Get("address").(string)), |
| 196 | + Port: getNilIntPointer(d.Get("port").(int)), |
| 197 | + ProxyProtocolVersion: getNilStringPointer(d.Get("proxy_protocol_version").(string)), |
| 198 | + Chain: getNilStringPointer(d.Get("chain").(string)), |
| 199 | + Weight: getNilIntPointer(d.Get("weight").(int)), |
| 200 | + Probe: getNilBoolPointer(d.Get("probe").(bool)), |
| 201 | + Ssl: getNilBoolPointer(d.Get("ssl").(bool)), |
| 202 | + Backup: getNilBoolPointer(d.Get("backup").(bool)), |
| 203 | + Status: getNilStringPointer(d.Get("status").(string)), |
| 204 | + } |
| 205 | + |
| 206 | + service := d.Get("service_name").(string) |
| 207 | + farmid := d.Get("farm_id").(int) |
| 208 | + r := &IpLoadbalancingTcpFarmServer{} |
| 209 | + endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server/%s", service, farmid, d.Id()) |
| 210 | + js, _ := json.Marshal(update) |
| 211 | + log.Printf("[DEBUG] PUT %s : %v", endpoint, string(js)) |
| 212 | + err := config.OVHClient.Put(endpoint, update, r) |
| 213 | + if err != nil { |
| 214 | + return fmt.Errorf("calling %s with %d:\n\t %s", endpoint, farmid, err.Error()) |
| 215 | + } |
| 216 | + return nil |
| 217 | +} |
| 218 | + |
| 219 | +func resourceIpLoadbalancingTcpFarmServerDelete(d *schema.ResourceData, meta interface{}) error { |
| 220 | + config := meta.(*Config) |
| 221 | + |
| 222 | + service := d.Get("service_name").(string) |
| 223 | + farmid := d.Get("farm_id").(int) |
| 224 | + |
| 225 | + r := &IpLoadbalancingTcpFarmServer{} |
| 226 | + endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server/%s", service, farmid, d.Id()) |
| 227 | + |
| 228 | + err := config.OVHClient.Delete(endpoint, r) |
| 229 | + if err != nil { |
| 230 | + return fmt.Errorf("calling %s :\n\t %s", endpoint, err.Error()) |
| 231 | + } |
| 232 | + |
| 233 | + return nil |
| 234 | +} |
0 commit comments