Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/ovh_iploadbalancing_tcp_frontend: new resource #58

Merged
merged 1 commit into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ovh/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,16 @@ func CheckDeleted(d *schema.ResourceData, err error, endpoint string) error {

return fmt.Errorf("calling %s:\n\t %s", endpoint, err.Error())
}

func stringsFromSchema(d *schema.ResourceData, id string) []string {
var xs []string
if v := d.Get(id); v != nil {
rs := v.(*schema.Set).List()
if len(rs) > 0 {
for _, v := range v.(*schema.Set).List() {
xs = append(xs, v.(string))
}
}
}
return xs
}
1 change: 1 addition & 0 deletions ovh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func Provider() terraform.ResourceProvider {
ResourcesMap: map[string]*schema.Resource{
"ovh_iploadbalancing_tcp_farm": resourceIpLoadbalancingTcpFarm(),
"ovh_iploadbalancing_tcp_farm_server": resourceIpLoadbalancingTcpFarmServer(),
"ovh_iploadbalancing_tcp_frontend": resourceIpLoadbalancingTcpFrontend(),
"ovh_iploadbalancing_http_route": resourceIPLoadbalancingRouteHTTP(),
"ovh_iploadbalancing_http_route_rule": resourceIPLoadbalancingRouteHTTPRule(),
"ovh_domain_zone_record": resourceOvhDomainZoneRecord(),
Expand Down
246 changes: 246 additions & 0 deletions ovh/resource_ovh_iploadbalancing_tcp_frontend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
package ovh

import (
"fmt"

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

type IpLoadbalancingTcpFrontend struct {
FrontendId int `json:"frontendId,omitempty"`
Port string `json:"port"`
Zone string `json:"zone"`
AllowedSource []string `json:"allowedSource,omitempty"`
DedicatedIpFo []string `json:"dedicatedIpfo,omitempty"`
DefaultFarmId *int `json:"defaultFarmId,omitempty"`
DefaultSslId *int `json:"defaultSslId,omitempty"`
Disabled *bool `json:"disabled"`
Ssl *bool `json:"ssl"`
DisplayName string `json:"displayName,omitempty"`
}

func resourceIpLoadbalancingTcpFrontend() *schema.Resource {
return &schema.Resource{
Create: resourceIpLoadbalancingTcpFrontendCreate,
Read: resourceIpLoadbalancingTcpFrontendRead,
Update: resourceIpLoadbalancingTcpFrontendUpdate,
Delete: resourceIpLoadbalancingTcpFrontendDelete,

Schema: map[string]*schema.Schema{
"service_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"port": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"zone": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"allowed_source": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"dedicated_ipfo": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"default_farm_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: false,
},
"default_ssl_id": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Optional: true,
ForceNew: false,
},
"disabled": &schema.Schema{
Type: schema.TypeBool,
Default: false,
Optional: true,
ForceNew: false,
},
"ssl": &schema.Schema{
Type: schema.TypeBool,
Default: false,
Optional: true,
ForceNew: false,
},
"display_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
},
}
}

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

allowedSources := stringsFromSchema(d, "allowed_source")
dedicatedIpFo := stringsFromSchema(d, "dedicated_ipfo")

for _, s := range allowedSources {
if err := validateIpBlock(s); err != nil {
return fmt.Errorf("Error validating `allowed_source` value: %s", err)
}
}

for _, s := range dedicatedIpFo {
if err := validateIpBlock(s); err != nil {
return fmt.Errorf("Error validating `dedicated_ipfo` value: %s", err)
}
}

frontend := &IpLoadbalancingTcpFrontend{
Port: d.Get("port").(string),
Zone: d.Get("zone").(string),
AllowedSource: allowedSources,
DedicatedIpFo: dedicatedIpFo,
Disabled: getNilBoolPointer(d.Get("disabled").(bool)),
Ssl: getNilBoolPointer(d.Get("ssl").(bool)),
DisplayName: d.Get("display_name").(string),
}

if farmId, ok := d.GetOk("default_farm_id"); ok {
frontend.DefaultFarmId = getNilIntPointer(farmId.(int))
}
if sslId, ok := d.GetOk("default_ssl_id"); ok {
frontend.DefaultSslId = getNilIntPointer(sslId.(int))
}

service := d.Get("service_name").(string)
frontend.DefaultFarmId = nil
resp := &IpLoadbalancingTcpFrontend{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/frontend", service)

err := config.OVHClient.Post(endpoint, frontend, resp)
if err != nil {
return fmt.Errorf("calling POST %s:\n\t %s", endpoint, err.Error())
}
return readIpLoadbalancingTcpFrontend(resp, d)

}

func resourceIpLoadbalancingTcpFrontendRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
service := d.Get("service_name").(string)
r := &IpLoadbalancingTcpFrontend{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/frontend/%s", service, d.Id())

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

func resourceIpLoadbalancingTcpFrontendUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
service := d.Get("service_name").(string)
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/frontend/%s", service, d.Id())

allowedSources := stringsFromSchema(d, "allowed_source")
dedicatedIpFo := stringsFromSchema(d, "dedicated_ipfo")

for _, s := range allowedSources {
if err := validateIpBlock(s); err != nil {
return fmt.Errorf("Error validating `allowed_source` value: %s", err)
}
}

for _, s := range dedicatedIpFo {
if err := validateIpBlock(s); err != nil {
return fmt.Errorf("Error validating `dedicated_ipfo` value: %s", err)
}
}

frontend := &IpLoadbalancingTcpFrontend{
Port: d.Get("port").(string),
Zone: d.Get("zone").(string),
AllowedSource: allowedSources,
DedicatedIpFo: dedicatedIpFo,
Disabled: getNilBoolPointer(d.Get("disabled").(bool)),
Ssl: getNilBoolPointer(d.Get("ssl").(bool)),
DisplayName: d.Get("display_name").(string),
}

if farmId, ok := d.GetOk("default_farm_id"); ok {
frontend.DefaultFarmId = getNilIntPointer(farmId.(int))
}
if sslId, ok := d.GetOk("default_ssl_id"); ok {
frontend.DefaultSslId = getNilIntPointer(sslId.(int))
}

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

func readIpLoadbalancingTcpFrontend(r *IpLoadbalancingTcpFrontend, d *schema.ResourceData) error {
d.Set("display_name", r.DisplayName)
d.Set("port", r.Port)
d.Set("zone", r.Zone)

allowedSources := make([]string, 0)
for _, s := range r.AllowedSource {
allowedSources = append(allowedSources, s)
}
d.Set("allowed_source", allowedSources)

dedicatedIpFos := make([]string, 0)
for _, s := range r.DedicatedIpFo {
dedicatedIpFos = append(dedicatedIpFos, s)
}
d.Set("dedicated_ipfo", dedicatedIpFos)

if r.DefaultFarmId != nil {
d.Set("default_farm_id", r.DefaultFarmId)
}

if r.DefaultSslId != nil {
d.Set("default_ssl_id", r.DefaultSslId)
}
if r.Disabled != nil {
d.Set("disabled", r.Disabled)
}
if r.Ssl != nil {
d.Set("ssl", r.Ssl)
}

d.SetId(fmt.Sprintf("%d", r.FrontendId))

return nil
}

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

service := d.Get("service_name").(string)
r := &IpLoadbalancingTcpFrontend{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/tcp/frontend/%s", service, d.Id())

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

return nil
}
Loading