diff --git a/ovh/provider.go b/ovh/provider.go index 977e45d93..5d930fdf4 100644 --- a/ovh/provider.go +++ b/ovh/provider.go @@ -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(), diff --git a/ovh/resource_ovh_domain_zone_redirection.go b/ovh/resource_ovh_domain_zone_redirection.go new file mode 100644 index 000000000..d259578e1 --- /dev/null +++ b/ovh/resource_ovh_domain_zone_redirection.go @@ -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 +} diff --git a/ovh/resource_ovh_domain_zone_redirection_test.go b/ovh/resource_ovh_domain_zone_redirection_test.go new file mode 100644 index 000000000..d46188cfb --- /dev/null +++ b/ovh/resource_ovh_domain_zone_redirection_test.go @@ -0,0 +1,258 @@ +package ovh + +import ( + "fmt" + "os" + "strconv" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccOvhDomainZoneRedirection_Basic(t *testing.T) { + var redirection OvhDomainZoneRedirection + zone := os.Getenv("OVH_ZONE") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckOvhDomainZoneRedirectionDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_basic, zone), + Check: resource.ComposeTestCheckFunc( + testAccCheckOvhDomainZoneRedirectionExists("ovh_domain_zone_redirection.foobar", &redirection), + testAccCheckOvhDomainZoneRedirectionAttributes(&redirection), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "subdomain", "terraform"), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "zone", zone), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "target", "https://terraform.net"), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "type", "visible"), + ), + }, + }, + }) +} + +func TestAccOvhDomainZoneRedirection_Updated(t *testing.T) { + redirection := OvhDomainZoneRedirection{} + zone := os.Getenv("OVH_ZONE") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckOvhDomainZoneRedirectionDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_basic, zone), + Check: resource.ComposeTestCheckFunc( + testAccCheckOvhDomainZoneRedirectionExists("ovh_domain_zone_redirection.foobar", &redirection), + testAccCheckOvhDomainZoneRedirectionAttributes(&redirection), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "subdomain", "terraform"), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "zone", zone), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "target", "https://terraform.net"), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "ttl", "3600"), + ), + }, + resource.TestStep{ + Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_new_value_1, zone), + Check: resource.ComposeTestCheckFunc( + testAccCheckOvhDomainZoneRedirectionExists("ovh_domain_zone_redirection.foobar", &redirection), + testAccCheckOvhDomainZoneRedirectionAttributesUpdated_1(&redirection), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "subdomain", "terraform"), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "zone", zone), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "target", "https://terraform.io"), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "ttl", "3600"), + ), + }, + resource.TestStep{ + Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_new_value_2, zone), + Check: resource.ComposeTestCheckFunc( + testAccCheckOvhDomainZoneRedirectionExists("ovh_domain_zone_redirection.foobar", &redirection), + testAccCheckOvhDomainZoneRedirectionAttributesUpdated_2(&redirection), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "subdomain", "terraform2"), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "zone", zone), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "target", "https://terraform.io"), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "ttl", "3600"), + ), + }, + resource.TestStep{ + Config: fmt.Sprintf(testAccCheckOvhDomainZoneRedirectionConfig_new_value_3, zone), + Check: resource.ComposeTestCheckFunc( + testAccCheckOvhDomainZoneRedirectionExists("ovh_domain_zone_redirection.foobar", &redirection), + testAccCheckOvhDomainZoneRedirectionAttributesUpdated_3(&redirection), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "subdomain", "terraform3"), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "zone", zone), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "target", "https://terraform.com"), + resource.TestCheckResourceAttr( + "ovh_domain_zone_redirection.foobar", "ttl", "3604"), + ), + }, + }, + }) +} + +func testAccCheckOvhDomainZoneRedirectionDestroy(s *terraform.State) error { + provider := testAccProvider.Meta().(*Config) + zone := os.Getenv("OVH_ZONE") + + for _, rs := range s.RootModule().Resources { + if rs.Type != "ovh_domain_zone_redirection" { + continue + } + + resultRedirection := OvhDomainZoneRedirection{} + err := provider.OVHClient.Get( + fmt.Sprintf("/domain/zone/%s/redirection/%s", zone, rs.Primary.ID), + &resultRedirection, + ) + + if err == nil { + return fmt.Errorf("Redirection still exists") + } + } + + return nil +} + +func testAccCheckOvhDomainZoneRedirectionExists(n string, redirection *OvhDomainZoneRedirection) resource.TestCheckFunc { + zone := os.Getenv("OVH_ZONE") + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No Redirection ID is set") + } + + provider := testAccProvider.Meta().(*Config) + + err := provider.OVHClient.Get( + fmt.Sprintf("/domain/zone/%s/redirection/%s", zone, rs.Primary.ID), + redirection, + ) + + if err != nil { + return err + } + + if strconv.Itoa(redirection.Id) != rs.Primary.ID { + return fmt.Errorf("Redirection not found") + } + + return nil + } +} + +func testAccCheckOvhDomainZoneRedirectionAttributes(redirection *OvhDomainZoneRedirection) resource.TestCheckFunc { + return func(s *terraform.State) error { + + if redirection.Target != "https://terraform.com" { + return fmt.Errorf("Bad content: %#v", redirection) + } + + return nil + } +} + +func testAccCheckOvhDomainZoneRedirectionAttributesUpdated_1(redirection *OvhDomainZoneRedirection) resource.TestCheckFunc { + return func(s *terraform.State) error { + + if redirection.Target != "https://terraform.io" { + return fmt.Errorf("Bad content: %#v", redirection) + } + + return nil + } +} + +func testAccCheckOvhDomainZoneRedirectionAttributesUpdated_2(redirection *OvhDomainZoneRedirection) resource.TestCheckFunc { + return func(s *terraform.State) error { + + if redirection.Target != "https://terraform.io" { + return fmt.Errorf("Bad content: %#v", redirection) + } + + if redirection.SubDomain != "terraform2" { + return fmt.Errorf("Bad content: %#v", redirection) + } + + return nil + } +} + +func testAccCheckOvhDomainZoneRedirectionAttributesUpdated_3(redirection *OvhDomainZoneRedirection) resource.TestCheckFunc { + return func(s *terraform.State) error { + + if redirection.Target != "https://terraform.com" { + return fmt.Errorf("Bad content: %#v", redirection) + } + + if redirection.SubDomain != "terraform3" { + return fmt.Errorf("Bad content: %#v", redirection) + } + + if redirection.Type != "visible" { + return fmt.Errorf("Bad content: %#v", redirection) + } + return nil + } +} + +const testAccCheckOvhDomainZoneRedirectionConfig_basic = ` +resource "ovh_domain_zone_redirection" "foobar" { + zone = "%s" + subdomain = "terraform" + target = "https://terraform.net" + type = "visible" + ttl = 3600 +}` + +const testAccCheckOvhDomainZoneRedirectionConfig_new_value_1 = ` +resource "ovh_domain_zone_redirection" "foobar" { + zone = "%s" + subdomain = "terraform" + target = "https://terraform.io" + type = "visible" + ttl = 3600 +} +` +const testAccCheckOvhDomainZoneRedirectionConfig_new_value_2 = ` +resource "ovh_domain_zone_redirection" "foobar" { + zone = "%s" + subdomain = "terraform2" + target = "https://terraform.io" + type = "visible" + ttl = 3600 +} +` +const testAccCheckOvhDomainZoneRedirectionConfig_new_value_3 = ` +resource "ovh_domain_zone_redirection" "foobar" { + zone = "%s" + subdomain = "terraform3" + target = "https://terraform.com" + type = "visible" + ttl = 3604 +}` diff --git a/website/docs/r/ovh_domain_zone_redirection.html.markdown b/website/docs/r/ovh_domain_zone_redirection.html.markdown new file mode 100644 index 000000000..6ba2b3a3c --- /dev/null +++ b/website/docs/r/ovh_domain_zone_redirection.html.markdown @@ -0,0 +1,51 @@ +--- +layout: "ovh" +page_title: "OVH: ovh_domain_zone_redirection" +sidebar_current: "docs-ovh-resource-domain-zone-redirection" +description: |- + Provides a OVH domain zone resource. +--- + +# ovh_domain_zone_redirection + +Provides a OVH domain zone redirection. + +## Example Usage + +```hcl +# Add a redirection to a sub-domain +resource "ovh_domain_zone_redirection" "test" { + zone = "testdemo.ovh" + subdomain = "test" + type = "visiblePermanent" + target = "http://www.ovh" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `zone` - (Required) The domain to add the redirection to +* `subdomain` - (Optional) The name of the redirection +* `target` - (Required) The value of the redirection +* `type` - (Required) The type of the redirection, with values: + * `visible` -> Redirection by http code 302 + * `visiblePermanent` -> Redirection by http code 301 + * `invisible` -> Redirection by html frame +* `description` - (Optional) A description of this redirection +* `keywords` - (Optional) Keywords to describe this redirection +* `title` - (Optional) Title of this redirection + +## Attributes Reference + +The following attributes are exported: + +* `id` - The redirection ID +* `zone` - The domain to add the redirection to +* `subDomain` - The name of the redirection +* `target` - The value of the redirection +* `type` - The type of the redirection +* `description` - The description of the redirection +* `keywords` - Keywords of the redirection +* `title` - The title of the redirection diff --git a/website/ovh.erb b/website/ovh.erb index 7b01af99d..33684eafe 100644 --- a/website/ovh.erb +++ b/website/ovh.erb @@ -9,7 +9,7 @@