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