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

Feat: implement domain zone redirection #36

Merged
merged 1 commit into from
May 24, 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
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