-
Notifications
You must be signed in to change notification settings - Fork 131
/
resource_domain_zone_redirection.go
184 lines (151 loc) · 4.88 KB
/
resource_domain_zone_redirection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package ovh
import (
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
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"`
Keywords string `json:"keywords"`
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,
ForceNew: true,
Optional: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"keywords": {
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),
Keywords: d.Get("keywords").(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())
if err := ovhDomainZoneRefresh(d, meta); err != nil {
log.Printf("[WARN] OVH Domain zone refresh after redirection creation failed: %s", err)
}
return resourceOvhDomainZoneRedirectionRead(d, meta)
}
func resourceOvhDomainZoneRedirectionRead(d *schema.ResourceData, meta interface{}) error {
provider := meta.(*Config)
redirection := OvhDomainZoneRedirection{}
endpoint := fmt.Sprintf("/domain/zone/%s/redirection/%s", d.Get("zone").(string), d.Id())
if err := provider.OVHClient.Get(endpoint, &redirection); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
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("keywords", redirection.Keywords)
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("keywords"); ok {
redirection.Keywords, _ = 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)
}
if err := ovhDomainZoneRefresh(d, meta); err != nil {
log.Printf("[WARN] OVH Domain zone refresh after redirection update failed: %s", err)
}
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)
}
if err := ovhDomainZoneRefresh(d, meta); err != nil {
log.Printf("[WARN] OVH Domain zone refresh after redirection deletion failed: %s", err)
}
return nil
}