|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +type OvhDomainZoneRedirection struct { |
| 12 | + Id int `json:"id,omitempty"` |
| 13 | + Zone string `json:"zone,omitempty"` |
| 14 | + Target string `json:"target,omitempty"` |
| 15 | + SubDomain string `json:"subDomain"` |
| 16 | + Type string `json:"type,omitempty"` |
| 17 | + Description string `json:"description"` |
| 18 | + Keyword string `json:"keyword"` |
| 19 | + Title string `json:"title"` |
| 20 | +} |
| 21 | + |
| 22 | +func resourceOvhDomainZoneRedirection() *schema.Resource { |
| 23 | + return &schema.Resource{ |
| 24 | + Create: resourceOvhDomainZoneRedirectionCreate, |
| 25 | + Read: resourceOvhDomainZoneRedirectionRead, |
| 26 | + Update: resourceOvhDomainZoneRedirectionUpdate, |
| 27 | + Delete: resourceOvhDomainZoneRedirectionDelete, |
| 28 | + |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "zone": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + }, |
| 34 | + "target": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + }, |
| 38 | + "subdomain": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + }, |
| 42 | + "type": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Required: true, |
| 45 | + }, |
| 46 | + "description": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Optional: true, |
| 49 | + }, |
| 50 | + "keyword": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Optional: true, |
| 53 | + }, |
| 54 | + "title": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Optional: true, |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func resourceOvhDomainZoneRedirectionCreate(d *schema.ResourceData, meta interface{}) error { |
| 63 | + provider := meta.(*Config) |
| 64 | + |
| 65 | + // Create the new redirection |
| 66 | + newRedirection := &OvhDomainZoneRedirection{ |
| 67 | + Type: d.Get("type").(string), |
| 68 | + SubDomain: d.Get("subdomain").(string), |
| 69 | + Target: d.Get("target").(string), |
| 70 | + Description: d.Get("description").(string), |
| 71 | + Keyword: d.Get("keyword").(string), |
| 72 | + Title: d.Get("title").(string), |
| 73 | + } |
| 74 | + |
| 75 | + log.Printf("[DEBUG] OVH Redirection create configuration: %#v", newRedirection) |
| 76 | + |
| 77 | + resultRedirection := OvhDomainZoneRedirection{} |
| 78 | + |
| 79 | + err := provider.OVHClient.Post( |
| 80 | + fmt.Sprintf("/domain/zone/%s/redirection", d.Get("zone").(string)), |
| 81 | + newRedirection, |
| 82 | + &resultRedirection, |
| 83 | + ) |
| 84 | + |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("Failed to create OVH Redirection: %s", err) |
| 87 | + } |
| 88 | + |
| 89 | + d.SetId(strconv.Itoa(resultRedirection.Id)) |
| 90 | + |
| 91 | + log.Printf("[INFO] OVH Redirection ID: %s", d.Id()) |
| 92 | + |
| 93 | + OvhDomainZoneRefresh(d, meta) |
| 94 | + |
| 95 | + return resourceOvhDomainZoneRedirectionRead(d, meta) |
| 96 | +} |
| 97 | + |
| 98 | +func resourceOvhDomainZoneRedirectionRead(d *schema.ResourceData, meta interface{}) error { |
| 99 | + provider := meta.(*Config) |
| 100 | + |
| 101 | + redirection := OvhDomainZoneRedirection{} |
| 102 | + err := provider.OVHClient.Get( |
| 103 | + fmt.Sprintf("/domain/zone/%s/redirection/%s", d.Get("zone").(string), d.Id()), |
| 104 | + &redirection, |
| 105 | + ) |
| 106 | + |
| 107 | + if err != nil { |
| 108 | + d.SetId("") |
| 109 | + return nil |
| 110 | + } |
| 111 | + |
| 112 | + d.Set("zone", redirection.Zone) |
| 113 | + d.Set("type", redirection.Type) |
| 114 | + d.Set("subdomain", redirection.SubDomain) |
| 115 | + d.Set("description", redirection.Description) |
| 116 | + d.Set("target", redirection.Target) |
| 117 | + d.Set("keyword", redirection.Keyword) |
| 118 | + d.Set("title", redirection.Title) |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func resourceOvhDomainZoneRedirectionUpdate(d *schema.ResourceData, meta interface{}) error { |
| 124 | + provider := meta.(*Config) |
| 125 | + |
| 126 | + redirection := OvhDomainZoneRedirection{} |
| 127 | + |
| 128 | + if attr, ok := d.GetOk("subdomain"); ok { |
| 129 | + redirection.SubDomain = attr.(string) |
| 130 | + } |
| 131 | + if attr, ok := d.GetOk("type"); ok { |
| 132 | + redirection.Type = attr.(string) |
| 133 | + } |
| 134 | + if attr, ok := d.GetOk("target"); ok { |
| 135 | + redirection.Target = attr.(string) |
| 136 | + } |
| 137 | + if attr, ok := d.GetOk("description"); ok { |
| 138 | + redirection.Description, _ = attr.(string) |
| 139 | + } |
| 140 | + if attr, ok := d.GetOk("keyword"); ok { |
| 141 | + redirection.Keyword, _ = attr.(string) |
| 142 | + } |
| 143 | + if attr, ok := d.GetOk("title"); ok { |
| 144 | + redirection.Title, _ = attr.(string) |
| 145 | + } |
| 146 | + |
| 147 | + log.Printf("[DEBUG] OVH Redirection update configuration: %#v", redirection) |
| 148 | + |
| 149 | + err := provider.OVHClient.Put( |
| 150 | + fmt.Sprintf("/domain/zone/%s/redirection/%s", d.Get("zone").(string), d.Id()), |
| 151 | + redirection, |
| 152 | + nil, |
| 153 | + ) |
| 154 | + if err != nil { |
| 155 | + return fmt.Errorf("Failed to update OVH Redirection: %s", err) |
| 156 | + } |
| 157 | + |
| 158 | + OvhDomainZoneRefresh(d, meta) |
| 159 | + |
| 160 | + return resourceOvhDomainZoneRedirectionRead(d, meta) |
| 161 | +} |
| 162 | + |
| 163 | +func resourceOvhDomainZoneRedirectionDelete(d *schema.ResourceData, meta interface{}) error { |
| 164 | + provider := meta.(*Config) |
| 165 | + |
| 166 | + log.Printf("[INFO] Deleting OVH Redirection: %s.%s, %s", d.Get("zone").(string), d.Get("subdomain").(string), d.Id()) |
| 167 | + |
| 168 | + err := provider.OVHClient.Delete( |
| 169 | + fmt.Sprintf("/domain/zone/%s/redirection/%s", d.Get("zone").(string), d.Id()), |
| 170 | + nil, |
| 171 | + ) |
| 172 | + |
| 173 | + if err != nil { |
| 174 | + return fmt.Errorf("Error deleting OVH Redirection: %s", err) |
| 175 | + } |
| 176 | + |
| 177 | + OvhDomainZoneRefresh(d, meta) |
| 178 | + |
| 179 | + return nil |
| 180 | +} |
0 commit comments