Skip to content

Commit

Permalink
Merge pull request #35 from eliecharra/add_http_route
Browse files Browse the repository at this point in the history
Add ovh_iploadbalancing_http_route resource
  • Loading branch information
yanndegat authored Jun 14, 2018
2 parents 445de09 + d6f2873 commit e0580ac
Show file tree
Hide file tree
Showing 9 changed files with 704 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ovh/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package ovh
import (
"bytes"
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/ovh/go-ovh/ovh"
)

func validateStringEnum(value string, enum []string) error {
Expand Down Expand Up @@ -62,3 +65,14 @@ func conditionalAttributeBool(buff *bytes.Buffer, name string, val *bool) {
buff.WriteString(fmt.Sprintf(" %s = %v\n", name, *val))
}
}

// CheckDeleted checks the error to see if it's a 404 (Not Found) and, if so,
// sets the resource ID to the empty string instead of throwing an error.
func CheckDeleted(d *schema.ResourceData, err error, endpoint string) error {
if err.(*ovh.APIError).Code == 404 {
d.SetId("")
return nil
}

return fmt.Errorf("calling %s:\n\t %s", endpoint, err.Error())
}
2 changes: 2 additions & 0 deletions ovh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func Provider() terraform.ResourceProvider {
ResourcesMap: map[string]*schema.Resource{
"ovh_iploadbalancing_tcp_farm": resourceIpLoadbalancingTcpFarm(),
"ovh_iploadbalancing_tcp_farm_server": resourceIpLoadbalancingTcpFarmServer(),
"ovh_iploadbalancing_http_route": resourceIPLoadbalancingRouteHTTP(),
"ovh_iploadbalancing_http_route_rule": resourceIPLoadbalancingRouteHTTPRule(),
"ovh_domain_zone_record": resourceOvhDomainZoneRecord(),
"ovh_domain_zone_redirection": resourceOvhDomainZoneRedirection(),
// New naming schema (issue #23)
Expand Down
26 changes: 26 additions & 0 deletions ovh/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func testAccPreCheck(t *testing.T) {
t.Fatal("OVH_ZONE must be set for acceptance tests")
}

v = os.Getenv("OVH_IPLB_SERVICE")
if v == "" {
t.Fatal("OVH_IPLB_SERVICE must be set for acceptance tests")
}

if testAccOVHClient == nil {
config := Config{
Endpoint: os.Getenv("OVH_ENDPOINT"),
Expand Down Expand Up @@ -123,6 +128,26 @@ func testAccCheckPublicCloudExists(t *testing.T) {

}

func testAccCheckIpLoadbalancingExists(t *testing.T) {
type iplbResponse struct {
ServiceName string `json:"serviceName"`
State string `json:"state"`
}

r := iplbResponse{}

endpoint := fmt.Sprintf("/ipLoadbalancing/%s", os.Getenv("OVH_IPLB_SERVICE"))


err := testAccOVHClient.Get(endpoint, &r)
if err != nil {
t.Fatalf("Error: %q\n", err)
}
t.Logf("Read IPLB service %s -> state: '%s', serviceName: '%s'", endpoint, r.State, r.ServiceName)

}


func testAccCheckDomainZoneExists(t *testing.T) {
type domainZoneResponse struct {
NameServers []string `json:"nameServers"`
Expand All @@ -136,6 +161,7 @@ func testAccCheckDomainZoneExists(t *testing.T) {
if err != nil {
t.Fatalf("Error: %q\n", err)
}

t.Logf("Read Domain Zone %s -> nameservers: '%v'", endpoint, r.NameServers)

}
167 changes: 167 additions & 0 deletions ovh/resource_ovh_iploadbalancing_http_route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package ovh

import (
"fmt"

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

func resourceIPLoadbalancingRouteHTTP() *schema.Resource {
return &schema.Resource{
Create: resourceIPLoadbalancingRouteHTTPCreate,
Read: resourceIPLoadbalancingRouteHTTPRead,
Update: resourceIPLoadbalancingRouteHTTPUpdate,
Delete: resourceIPLoadbalancingRouteHTTPDelete,

Schema: map[string]*schema.Schema{
"service_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"action": &schema.Schema{
Type: schema.TypeSet,
Required: true,
ForceNew: false,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"target": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
"display_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"frontend_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"weight": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
},
}
}

// IPLoadbalancingRouteHTTPAction Action triggered when all rules match
type IPLoadbalancingRouteHTTPAction struct {
Target string `json:"target,omitempty"` // Farm ID for "farm" action type or URL template for "redirect" action. You may use ${uri}, ${protocol}, ${host}, ${port} and ${path} variables in redirect target
Status int `json:"status,omitempty"` // HTTP status code for "redirect" and "reject" actions
Type string `json:"type,omitempty"` // Action to trigger if all the rules of this route matches
}

//IPLoadbalancingRouteHTTP HTTP Route
type IPLoadbalancingRouteHTTP struct {
Status string `json:"status,omitempty"` //Route status. Routes in "ok" state are ready to operate
Weight int `json:"weight,omitempty"` //Route priority ([0..255]). 0 if null. Highest priority routes are evaluated first. Only the first matching route will trigger an action
Action *IPLoadbalancingRouteHTTPAction `json:"action,omitempty"` //Action triggered when all rules match
RouteID int `json:"routeId,omitempty"` //Id of your route
DisplayName string `json:"displayName,omitempty"` //Human readable name for your route, this field is for you
FrontendID int `json:"frontendId,omitempty"` //Route traffic for this frontend
}

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

action := &IPLoadbalancingRouteHTTPAction{}
actionSet := d.Get("action").(*schema.Set).List()[0].(map[string]interface{})

action.Status = actionSet["status"].(int)
action.Target = actionSet["target"].(string)
action.Type = actionSet["type"].(string)

route := &IPLoadbalancingRouteHTTP{
Action: action,
DisplayName: d.Get("display_name").(string),
FrontendID: d.Get("frontend_id").(int),
Weight: d.Get("weight").(int),
}

service := d.Get("service_name").(string)
resp := &IPLoadbalancingRouteHTTP{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/http/route", service)

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

d.SetId(fmt.Sprintf("%d", resp.RouteID))

return resourceIPLoadbalancingRouteHTTPRead(d, meta)
}

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

err := config.OVHClient.Get(endpoint, &r)
if err != nil {
return CheckDeleted(d, err, endpoint)
}

d.Set("status", r.Status)
d.Set("weight", r.Weight)
d.Set("display_name", r.DisplayName)
d.Set("frontend_id", r.FrontendID)

return nil
}

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

action := &IPLoadbalancingRouteHTTPAction{}
actionSet := d.Get("action").(*schema.Set).List()[0].(map[string]interface{})

action.Status = actionSet["status"].(int)
action.Target = actionSet["target"].(string)
action.Type = actionSet["type"].(string)

route := &IPLoadbalancingRouteHTTP{
Action: action,
DisplayName: d.Get("display_name").(string),
FrontendID: d.Get("frontend_id").(int),
Weight: d.Get("weight").(int),
}

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

return resourceIPLoadbalancingRouteHTTPRead(d, meta)
}

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

service := d.Get("service_name").(string)
r := &IPLoadbalancingRouteHTTP{}
endpoint := fmt.Sprintf("/ipLoadbalancing/%s/http/route/%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

0 comments on commit e0580ac

Please sign in to comment.