Skip to content

Commit

Permalink
Merge pull request #36 from dlecan/feat-domain-zone-redirection
Browse files Browse the repository at this point in the history
Feat: implement domain zone redirection
  • Loading branch information
yanndegat authored May 24, 2018
2 parents 6750160 + 5a398c4 commit d907401
Show file tree
Hide file tree
Showing 5 changed files with 495 additions and 2 deletions.
1 change: 1 addition & 0 deletions ovh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func Provider() terraform.ResourceProvider {
ResourcesMap: map[string]*schema.Resource{
"ovh_iploadbalancing_tcp_farm": resourceIpLoadbalancingTcpFarm(),
"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
180 changes: 180 additions & 0 deletions ovh/resource_ovh_domain_zone_redirection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package ovh

import (
"fmt"
"log"
"strconv"

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

type OvhDomainZoneRedirection struct {
Id int `json:"id,omitempty"`
Zone string `json:"zone,omitempty"`
Target string `json:"target,omitempty"`
SubDomain string `json:"subDomain"`
Type string `json:"type,omitempty"`
Description string `json:"description"`
Keyword string `json:"keyword"`
Title string `json:"title"`
}

func resourceOvhDomainZoneRedirection() *schema.Resource {
return &schema.Resource{
Create: resourceOvhDomainZoneRedirectionCreate,
Read: resourceOvhDomainZoneRedirectionRead,
Update: resourceOvhDomainZoneRedirectionUpdate,
Delete: resourceOvhDomainZoneRedirectionDelete,

Schema: map[string]*schema.Schema{
"zone": {
Type: schema.TypeString,
Required: true,
},
"target": {
Type: schema.TypeString,
Required: true,
},
"subdomain": {
Type: schema.TypeString,
Optional: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"keyword": {
Type: schema.TypeString,
Optional: true,
},
"title": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

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

// Create the new redirection
newRedirection := &OvhDomainZoneRedirection{
Type: d.Get("type").(string),
SubDomain: d.Get("subdomain").(string),
Target: d.Get("target").(string),
Description: d.Get("description").(string),
Keyword: d.Get("keyword").(string),
Title: d.Get("title").(string),
}

log.Printf("[DEBUG] OVH Redirection create configuration: %#v", newRedirection)

resultRedirection := OvhDomainZoneRedirection{}

err := provider.OVHClient.Post(
fmt.Sprintf("/domain/zone/%s/redirection", d.Get("zone").(string)),
newRedirection,
&resultRedirection,
)

if err != nil {
return fmt.Errorf("Failed to create OVH Redirection: %s", err)
}

d.SetId(strconv.Itoa(resultRedirection.Id))

log.Printf("[INFO] OVH Redirection ID: %s", d.Id())

OvhDomainZoneRefresh(d, meta)

return resourceOvhDomainZoneRedirectionRead(d, meta)
}

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

redirection := OvhDomainZoneRedirection{}
err := provider.OVHClient.Get(
fmt.Sprintf("/domain/zone/%s/redirection/%s", d.Get("zone").(string), d.Id()),
&redirection,
)

if err != nil {
d.SetId("")
return nil
}

d.Set("zone", redirection.Zone)
d.Set("type", redirection.Type)
d.Set("subdomain", redirection.SubDomain)
d.Set("description", redirection.Description)
d.Set("target", redirection.Target)
d.Set("keyword", redirection.Keyword)
d.Set("title", redirection.Title)

return nil
}

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

redirection := OvhDomainZoneRedirection{}

if attr, ok := d.GetOk("subdomain"); ok {
redirection.SubDomain = attr.(string)
}
if attr, ok := d.GetOk("type"); ok {
redirection.Type = attr.(string)
}
if attr, ok := d.GetOk("target"); ok {
redirection.Target = attr.(string)
}
if attr, ok := d.GetOk("description"); ok {
redirection.Description, _ = attr.(string)
}
if attr, ok := d.GetOk("keyword"); ok {
redirection.Keyword, _ = attr.(string)
}
if attr, ok := d.GetOk("title"); ok {
redirection.Title, _ = attr.(string)
}

log.Printf("[DEBUG] OVH Redirection update configuration: %#v", redirection)

err := provider.OVHClient.Put(
fmt.Sprintf("/domain/zone/%s/redirection/%s", d.Get("zone").(string), d.Id()),
redirection,
nil,
)
if err != nil {
return fmt.Errorf("Failed to update OVH Redirection: %s", err)
}

OvhDomainZoneRefresh(d, meta)

return resourceOvhDomainZoneRedirectionRead(d, meta)
}

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

log.Printf("[INFO] Deleting OVH Redirection: %s.%s, %s", d.Get("zone").(string), d.Get("subdomain").(string), d.Id())

err := provider.OVHClient.Delete(
fmt.Sprintf("/domain/zone/%s/redirection/%s", d.Get("zone").(string), d.Id()),
nil,
)

if err != nil {
return fmt.Errorf("Error deleting OVH Redirection: %s", err)
}

OvhDomainZoneRefresh(d, meta)

return nil
}
Loading

0 comments on commit d907401

Please sign in to comment.