|
| 1 | +package infomaniak |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +const baseURL = "https://api.infomaniak.com/2" |
| 11 | + |
| 12 | +type dnssecRecord struct { |
| 13 | + IsEnabled bool `json:"is_enabled"` |
| 14 | +} |
| 15 | + |
| 16 | +type errorRecord struct { |
| 17 | + Code string `json:"code"` |
| 18 | + Description string `json:"description"` |
| 19 | +} |
| 20 | + |
| 21 | +type dnsZoneResponse struct { |
| 22 | + Result string `json:"result"` |
| 23 | + Data dnsZone `json:"data,omitempty"` |
| 24 | + Error errorRecord `json:"error,omitempty"` |
| 25 | +} |
| 26 | + |
| 27 | +type dnsRecordsResponse struct { |
| 28 | + Result string `json:"result"` |
| 29 | + Data []dnsRecord `json:"data,omitempty"` |
| 30 | + Error errorRecord `json:"error,omitempty"` |
| 31 | +} |
| 32 | + |
| 33 | +type dnsRecordResponse struct { |
| 34 | + Result string `json:"result"` |
| 35 | + Data dnsRecord `json:"data,omitempty"` |
| 36 | + Error errorRecord `json:"error,omitempty"` |
| 37 | +} |
| 38 | + |
| 39 | +type boolResponse struct { |
| 40 | + Result string `json:"result"` |
| 41 | + Data bool `json:"data,omitempty"` |
| 42 | + Error errorRecord `json:"error,omitempty"` |
| 43 | +} |
| 44 | +type dnsZone struct { |
| 45 | + ID int64 `json:"id,omitempty"` |
| 46 | + FQDN string `json:"fqdn,omitempty"` |
| 47 | + DNSSEC dnssecRecord `json:"dnssec,omitempty"` |
| 48 | + Nameservers []string `json:"nameservers,omitempty"` |
| 49 | +} |
| 50 | + |
| 51 | +type dnsRecord struct { |
| 52 | + ID int64 `json:"id,omitempty"` |
| 53 | + Source string `json:"source,omitempty"` |
| 54 | + Type string `json:"type,omitempty"` |
| 55 | + TTL int64 `json:"ttl,omitempty"` |
| 56 | + Target string `json:"target,omitempty"` |
| 57 | + UpdatedAt int64 `json:"updated_at,omitempty"` |
| 58 | +} |
| 59 | + |
| 60 | +type dnsRecordCreate struct { |
| 61 | + Source string `json:"source,omitempty"` |
| 62 | + Type string `json:"type,omitempty"` |
| 63 | + TTL int64 `json:"ttl,omitempty"` |
| 64 | + Target string `json:"target,omitempty"` |
| 65 | +} |
| 66 | + |
| 67 | +type dnsRecordUpdate struct { |
| 68 | + Target string `json:"target,omitempty"` |
| 69 | + TTL int64 `json:"ttl,omitempty"` |
| 70 | +} |
| 71 | + |
| 72 | +// Get zone information |
| 73 | +// See https://developer.infomaniak.com/docs/api/get/2/zones/%7Bzone%7D |
| 74 | +func (p *infomaniakProvider) getDNSZone(zone string) (*dnsZone, error) { |
| 75 | + reqURL := fmt.Sprintf("%s/zones/%s", baseURL, zone) |
| 76 | + |
| 77 | + req, err := http.NewRequest(http.MethodGet, reqURL, nil) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + req.Header.Add("Authorization", "Bearer "+p.apiToken) |
| 82 | + req.Header.Add("Content-Type", "application/json") |
| 83 | + |
| 84 | + res, err := http.DefaultClient.Do(req) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + defer res.Body.Close() |
| 89 | + |
| 90 | + response := &dnsZoneResponse{} |
| 91 | + err = json.NewDecoder(res.Body).Decode(response) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + |
| 96 | + return &response.Data, nil |
| 97 | +} |
| 98 | + |
| 99 | +// Retrieve all dns record for a given zone |
| 100 | +// See https://developer.infomaniak.com/docs/api/get/2/zones/%7Bzone%7D/records |
| 101 | +func (p *infomaniakProvider) getDNSRecords(zone string) ([]dnsRecord, error) { |
| 102 | + reqURL := fmt.Sprintf("%s/zones/%s/records", baseURL, zone) |
| 103 | + |
| 104 | + req, err := http.NewRequest(http.MethodGet, reqURL, nil) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + req.Header.Add("Authorization", "Bearer "+p.apiToken) |
| 109 | + req.Header.Add("Content-Type", "application/json") |
| 110 | + |
| 111 | + res, err := http.DefaultClient.Do(req) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + defer res.Body.Close() |
| 116 | + |
| 117 | + response := &dnsRecordsResponse{} |
| 118 | + err = json.NewDecoder(res.Body).Decode(response) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + |
| 123 | + return response.Data, nil |
| 124 | +} |
| 125 | + |
| 126 | +// Delete a dns record |
| 127 | +// See https://developer.infomaniak.com/docs/api/delete/2/zones/%7Bzone%7D/records/%7Brecord%7D |
| 128 | +func (p *infomaniakProvider) deleteDNSRecord(zone string, recordID string) error { |
| 129 | + reqURL := fmt.Sprintf("%s/zones/%s/records/%s", baseURL, zone, recordID) |
| 130 | + |
| 131 | + req, err := http.NewRequest(http.MethodDelete, reqURL, nil) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + req.Header.Add("Authorization", "Bearer "+p.apiToken) |
| 136 | + req.Header.Add("Content-Type", "application/json") |
| 137 | + |
| 138 | + res, err := http.DefaultClient.Do(req) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + defer res.Body.Close() |
| 143 | + |
| 144 | + response := &boolResponse{} |
| 145 | + err = json.NewDecoder(res.Body).Decode(response) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + if response.Result == "error" { |
| 151 | + return fmt.Errorf("failed to delete record %s in zone %s: %s", recordID, zone, response.Error.Description) |
| 152 | + } |
| 153 | + |
| 154 | + return nil |
| 155 | +} |
| 156 | + |
| 157 | +// Create a dns record in a given zone |
| 158 | +// See https://developer.infomaniak.com/docs/api/post/2/zones/%7Bzone%7D/records |
| 159 | +func (p *infomaniakProvider) createDNSRecord(zone string, rec *dnsRecordCreate) (*dnsRecord, error) { |
| 160 | + reqURL := fmt.Sprintf("%s/zones/%s/records", baseURL, zone) |
| 161 | + |
| 162 | + data, err := json.Marshal(rec) |
| 163 | + if err != nil { |
| 164 | + return nil, err |
| 165 | + } |
| 166 | + |
| 167 | + req, err := http.NewRequest(http.MethodPost, reqURL, bytes.NewReader(data)) |
| 168 | + if err != nil { |
| 169 | + return nil, err |
| 170 | + } |
| 171 | + req.Header.Add("Authorization", "Bearer "+p.apiToken) |
| 172 | + req.Header.Add("Content-Type", "application/json") |
| 173 | + |
| 174 | + res, err := http.DefaultClient.Do(req) |
| 175 | + if err != nil { |
| 176 | + return nil, err |
| 177 | + } |
| 178 | + defer res.Body.Close() |
| 179 | + |
| 180 | + response := &dnsRecordResponse{} |
| 181 | + err = json.NewDecoder(res.Body).Decode(response) |
| 182 | + if err != nil { |
| 183 | + return nil, err |
| 184 | + } |
| 185 | + |
| 186 | + if response.Result == "error" { |
| 187 | + return nil, fmt.Errorf("failed to create %s record in zone %s: %s", rec.Type, zone, response.Error.Description) |
| 188 | + } |
| 189 | + |
| 190 | + return &response.Data, nil |
| 191 | +} |
| 192 | + |
| 193 | +// Update a dns record in a given zone |
| 194 | +// See https://developer.infomaniak.com/docs/api/put/2/zones/%7Bzone%7D/records/%7Brecord%7D |
| 195 | +func (p *infomaniakProvider) updateDNSRecord(zone string, recordID string, rec *dnsRecordUpdate) (*dnsRecord, error) { |
| 196 | + reqURL := fmt.Sprintf("%s/zones/%s/records/%s", baseURL, zone, recordID) |
| 197 | + |
| 198 | + data, err := json.Marshal(rec) |
| 199 | + if err != nil { |
| 200 | + return nil, err |
| 201 | + } |
| 202 | + |
| 203 | + req, err := http.NewRequest(http.MethodPut, reqURL, bytes.NewReader(data)) |
| 204 | + if err != nil { |
| 205 | + return nil, err |
| 206 | + } |
| 207 | + req.Header.Add("Authorization", "Bearer "+p.apiToken) |
| 208 | + req.Header.Add("Content-Type", "application/json") |
| 209 | + |
| 210 | + res, err := http.DefaultClient.Do(req) |
| 211 | + if err != nil { |
| 212 | + return nil, err |
| 213 | + } |
| 214 | + defer res.Body.Close() |
| 215 | + |
| 216 | + response := &dnsRecordResponse{} |
| 217 | + err = json.NewDecoder(res.Body).Decode(response) |
| 218 | + if err != nil { |
| 219 | + return nil, err |
| 220 | + } |
| 221 | + |
| 222 | + if response.Result == "error" { |
| 223 | + return nil, fmt.Errorf("failed to update record %s in zone %s: %s", recordID, zone, response.Error.Description) |
| 224 | + } |
| 225 | + |
| 226 | + return &response.Data, nil |
| 227 | +} |
0 commit comments