Skip to content

Commit

Permalink
Merge pull request #33 from goblain/iploadbalancing_tcp_farm_server
Browse files Browse the repository at this point in the history
adds ovh_iploadbalancing_tcp_farm_server resource
  • Loading branch information
yanndegat authored May 28, 2018
2 parents d907401 + 8c5dfb4 commit 4631eb4
Show file tree
Hide file tree
Showing 5 changed files with 599 additions and 4 deletions.
50 changes: 49 additions & 1 deletion ovh/helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ovh

import "fmt"
import (
"bytes"
"fmt"
)

func validateStringEnum(value string, enum []string) error {
missing := true
Expand All @@ -14,3 +17,48 @@ func validateStringEnum(value string, enum []string) error {
}
return nil
}

func getNilBoolPointer(val interface{}) *bool {
if val == nil {
return nil
}
value := val.(bool)
return &value
}

func getNilStringPointer(val interface{}) *string {
if val == nil {
return nil
}
value := val.(string)
if len(value) == 0 {
return nil
}
return &value
}

func getNilIntPointer(val interface{}) *int {
if val == nil {
return nil
}
value := val.(int)
return &value
}

func conditionalAttributeInt(buff *bytes.Buffer, name string, val *int) {
if val != nil {
buff.WriteString(fmt.Sprintf(" %s = %d\n", name, *val))
}
}

func conditionalAttributeString(buff *bytes.Buffer, name string, val *string) {
if val != nil {
buff.WriteString(fmt.Sprintf(" %s = \"%s\"\n", name, *val))
}
}

func conditionalAttributeBool(buff *bytes.Buffer, name string, val *bool) {
if val != nil {
buff.WriteString(fmt.Sprintf(" %s = %v\n", name, *val))
}
}
7 changes: 4 additions & 3 deletions ovh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ func Provider() terraform.ResourceProvider {
},

ResourcesMap: map[string]*schema.Resource{
"ovh_iploadbalancing_tcp_farm": resourceIpLoadbalancingTcpFarm(),
"ovh_domain_zone_record": resourceOvhDomainZoneRecord(),
"ovh_domain_zone_redirection": resourceOvhDomainZoneRedirection(),
"ovh_iploadbalancing_tcp_farm": resourceIpLoadbalancingTcpFarm(),
"ovh_iploadbalancing_tcp_farm_server": resourceIpLoadbalancingTcpFarmServer(),
"ovh_domain_zone_record": resourceOvhDomainZoneRecord(),
"ovh_domain_zone_redirection": resourceOvhDomainZoneRedirection(),
// New naming schema (issue #23)
"ovh_cloud_network_private": resourcePublicCloudPrivateNetwork(),
"ovh_cloud_network_private_subnet": resourcePublicCloudPrivateNetworkSubnet(),
Expand Down
234 changes: 234 additions & 0 deletions ovh/resource_ovh_iploadbalancing_tcp_farm_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
package ovh

import (
"encoding/json"
"fmt"
"log"
"net"

"github.com/hashicorp/terraform/helper/schema"
)

type IpLoadbalancingTcpFarmServer struct {
BackendId int `json:"backendId,omitempty"`
ServerId int `json:"serverId,omitempty"`
FarmId int `json:"farmId,omitempty"`
DisplayName *string `json:"displayName,omitempty"`
Address *string `json:"address"`
Cookie *string `json:"cookie,omitempty"`
Port *int `json:"port"`
ProxyProtocolVersion *string `json:"proxyProtocolVersion"`
Chain *string `json:"chain"`
Weight *int `json:"weight"`
Probe *bool `json:"probe"`
Ssl *bool `json:"ssl"`
Backup *bool `json:"backup"`
Status *string `json:"status"`
}

func resourceIpLoadbalancingTcpFarmServer() *schema.Resource {
return &schema.Resource{
Create: resourceIpLoadbalancingTcpFarmServerCreate,
Read: resourceIpLoadbalancingTcpFarmServerRead,
Update: resourceIpLoadbalancingTcpFarmServerUpdate,
Delete: resourceIpLoadbalancingTcpFarmServerDelete,
Schema: map[string]*schema.Schema{
"service_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"farm_id": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"display_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"address": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
ip := v.(string)
if net.ParseIP(ip).To4() == nil {
errors = append(errors, fmt.Errorf("Address %s is not an IPv4", ip))
}
return
},
},
"ssl": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"cookie": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"port": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"proxy_protocol_version": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := validateStringEnum(v.(string), []string{"v1", "v2", "v2-ssl", "v2-ssl-cn"})
if err != nil {
errors = append(errors, err)
}
return
},
},
"chain": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"weight": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 1,
},
"probe": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"backup": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"status": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := validateStringEnum(v.(string), []string{"active", "inactive"})
if err != nil {
errors = append(errors, err)
}
return
},
},
},
}
}

func resourceIpLoadbalancingTcpFarmServerCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

newBackendServer := &IpLoadbalancingTcpFarmServer{
DisplayName: getNilStringPointer(d.Get("display_name").(string)),
Address: getNilStringPointer(d.Get("address").(string)),
Port: getNilIntPointer(d.Get("port").(int)),
ProxyProtocolVersion: getNilStringPointer(d.Get("proxy_protocol_version").(string)),
Chain: getNilStringPointer(d.Get("chain").(string)),
Weight: getNilIntPointer(d.Get("weight").(int)),
Probe: getNilBoolPointer(d.Get("probe").(bool)),
Ssl: getNilBoolPointer(d.Get("ssl").(bool)),
Backup: getNilBoolPointer(d.Get("backup").(bool)),
Status: getNilStringPointer(d.Get("status").(string)),
}

service := d.Get("service_name").(string)
farmid := d.Get("farm_id").(int)
r := &IpLoadbalancingTcpFarmServer{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server", service, farmid)

err := config.OVHClient.Post(endpoint, newBackendServer, r)
if err != nil {
return fmt.Errorf("calling %s with %d:\n\t %s", endpoint, farmid, err.Error())
}

//set id
d.SetId(fmt.Sprintf("%d", r.ServerId))

return nil
}

func resourceIpLoadbalancingTcpFarmServerRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

service := d.Get("service_name").(string)
farmid := d.Get("farm_id").(int)
r := &IpLoadbalancingTcpFarmServer{}

endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server/%s", service, farmid, d.Id())

err := config.OVHClient.Get(endpoint, r)
if err != nil {
return fmt.Errorf("calling %s :\n\t %q", endpoint, err)
}
log.Printf("[DEBUG] Response object from OVH : %v", r)

d.Set("probe", *r.Probe)
d.Set("ssl", *r.Ssl)
d.Set("backup", *r.Backup)
d.Set("address", *r.Address)
if r.DisplayName != nil {
d.Set("display_name", *r.DisplayName)
}
if r.Cookie != nil {
d.Set("cookie", *r.Cookie)
}
d.Set("port", *r.Port)
if r.ProxyProtocolVersion != nil {
d.Set("proxy_protocol_version", *r.ProxyProtocolVersion)
}
if r.Chain != nil {
d.Set("chain", *r.Chain)
}
d.Set("weight", *r.Weight)
d.Set("status", *r.Status)

return nil
}

func resourceIpLoadbalancingTcpFarmServerUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

update := &IpLoadbalancingTcpFarmServer{
DisplayName: getNilStringPointer(d.Get("display_name").(string)),
Address: getNilStringPointer(d.Get("address").(string)),
Port: getNilIntPointer(d.Get("port").(int)),
ProxyProtocolVersion: getNilStringPointer(d.Get("proxy_protocol_version").(string)),
Chain: getNilStringPointer(d.Get("chain").(string)),
Weight: getNilIntPointer(d.Get("weight").(int)),
Probe: getNilBoolPointer(d.Get("probe").(bool)),
Ssl: getNilBoolPointer(d.Get("ssl").(bool)),
Backup: getNilBoolPointer(d.Get("backup").(bool)),
Status: getNilStringPointer(d.Get("status").(string)),
}

service := d.Get("service_name").(string)
farmid := d.Get("farm_id").(int)
r := &IpLoadbalancingTcpFarmServer{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server/%s", service, farmid, d.Id())
js, _ := json.Marshal(update)
log.Printf("[DEBUG] PUT %s : %v", endpoint, string(js))
err := config.OVHClient.Put(endpoint, update, r)
if err != nil {
return fmt.Errorf("calling %s with %d:\n\t %s", endpoint, farmid, err.Error())
}
return nil
}

func resourceIpLoadbalancingTcpFarmServerDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

service := d.Get("service_name").(string)
farmid := d.Get("farm_id").(int)

r := &IpLoadbalancingTcpFarmServer{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/farm/%d/server/%s", service, farmid, d.Id())

err := config.OVHClient.Delete(endpoint, r)
if err != nil {
return fmt.Errorf("calling %s :\n\t %s", endpoint, err.Error())
}

return nil
}
Loading

0 comments on commit 4631eb4

Please sign in to comment.