Library for working with Hetzner DNS API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

386 lines
10 KiB

  1. package hclouddns
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. )
  10. // GetRecord retrieve one single record by ID.
  11. // Accepts single ID of record.
  12. // Returns HCloudAnswerGetRecord with HCloudRecord and error.
  13. func (d *HCloudClient) GetRecord(ID string) (HCloudAnswerGetRecord, error) {
  14. client := &http.Client{}
  15. req, err := http.NewRequest("GET", fmt.Sprintf("https://dns.hetzner.com/api/v1/records/%v", ID), nil)
  16. if err != nil {
  17. return HCloudAnswerGetRecord{}, err
  18. }
  19. req.Header.Add("Auth-API-Token", d.Token)
  20. resp, err := client.Do(req)
  21. if err != nil {
  22. return HCloudAnswerGetRecord{}, err
  23. }
  24. respBody, err := ioutil.ReadAll(resp.Body)
  25. if err != nil {
  26. return HCloudAnswerGetRecord{}, err
  27. }
  28. answer := HCloudAnswerGetRecord{}
  29. err = json.Unmarshal([]byte(respBody), &answer)
  30. if err != nil {
  31. return HCloudAnswerGetRecord{}, err
  32. }
  33. // parse error
  34. errorResult := HCloudAnswerError{}
  35. err = json.Unmarshal([]byte(respBody), &errorResult)
  36. if err != nil {
  37. //ok, non-standard error, try another form
  38. errorResultString := HCloudAnswerErrorString{}
  39. err = json.Unmarshal([]byte(respBody), &errorResultString)
  40. if err != nil {
  41. return HCloudAnswerGetRecord{}, err
  42. }
  43. errorResult.Error.Message = errorResultString.Error
  44. errorResult.Error.Code = resp.StatusCode
  45. }
  46. answer.Error = errorResult.Error
  47. return answer, nil
  48. }
  49. // GetRecords retrieve all records of user.
  50. // Accepts HCloudGetRecordsParams struct
  51. // Returns HCloudAnswerGetRecords with array of HCloudRecord, Meta and error.
  52. func (d *HCloudClient) GetRecords(params HCloudGetRecordsParams) (HCloudAnswerGetRecords, error) {
  53. v := url.Values{}
  54. if params.Page != "" {
  55. v.Add("page", params.Page)
  56. }
  57. if params.PerPage != "" {
  58. v.Add("per_page", params.PerPage)
  59. }
  60. if params.ZoneID != "" {
  61. v.Add("zone_id", params.ZoneID)
  62. }
  63. client := &http.Client{}
  64. req, err := http.NewRequest("GET", fmt.Sprintf("https://dns.hetzner.com/api/v1/records?%v", v.Encode()), nil)
  65. if err != nil {
  66. return HCloudAnswerGetRecords{}, err
  67. }
  68. req.Header.Add("Auth-API-Token", d.Token)
  69. parseFormErr := req.ParseForm()
  70. if parseFormErr != nil {
  71. return HCloudAnswerGetRecords{}, parseFormErr
  72. }
  73. resp, err := client.Do(req)
  74. if err != nil {
  75. return HCloudAnswerGetRecords{}, err
  76. }
  77. respBody, err := ioutil.ReadAll(resp.Body)
  78. if err != nil {
  79. return HCloudAnswerGetRecords{}, err
  80. }
  81. answer := HCloudAnswerGetRecords{}
  82. err = json.Unmarshal([]byte(respBody), &answer)
  83. if err != nil {
  84. return HCloudAnswerGetRecords{}, err
  85. }
  86. // parse error
  87. errorResult := HCloudAnswerError{}
  88. err = json.Unmarshal([]byte(respBody), &errorResult)
  89. if err != nil {
  90. //ok, non-standard error, try another form
  91. errorResultString := HCloudAnswerErrorString{}
  92. err = json.Unmarshal([]byte(respBody), &errorResultString)
  93. if err != nil {
  94. return HCloudAnswerGetRecords{}, err
  95. }
  96. errorResult.Error.Message = errorResultString.Error
  97. errorResult.Error.Code = resp.StatusCode
  98. }
  99. answer.Error = errorResult.Error
  100. return answer, nil
  101. }
  102. // UpdateRecord makes update of single record by ID.
  103. // Accepts HCloudRecord with fullfilled fields.
  104. // Returns HCloudAnswerGetRecord with HCloudRecord and error.
  105. func (d *HCloudClient) UpdateRecord(record HCloudRecord) (HCloudAnswerGetRecord, error) {
  106. jsonRecordString, err := json.Marshal(record)
  107. if err != nil {
  108. return HCloudAnswerGetRecord{}, err
  109. }
  110. body := bytes.NewBuffer(jsonRecordString)
  111. client := &http.Client{}
  112. req, err := http.NewRequest("PUT", fmt.Sprintf("https://dns.hetzner.com/api/v1/records/%v", record.ID), body)
  113. if err != nil {
  114. return HCloudAnswerGetRecord{}, err
  115. }
  116. req.Header.Add("Content-Type", "application/json")
  117. req.Header.Add("Auth-API-Token", d.Token)
  118. resp, err := client.Do(req)
  119. if err != nil {
  120. return HCloudAnswerGetRecord{}, err
  121. }
  122. respBody, err := ioutil.ReadAll(resp.Body)
  123. if err != nil {
  124. return HCloudAnswerGetRecord{}, err
  125. }
  126. answer := HCloudAnswerGetRecord{}
  127. err = json.Unmarshal([]byte(respBody), &answer)
  128. if err != nil {
  129. return HCloudAnswerGetRecord{}, err
  130. }
  131. // parse error
  132. errorResult := HCloudAnswerError{}
  133. err = json.Unmarshal([]byte(respBody), &errorResult)
  134. if err != nil {
  135. //ok, non-standard error, try another form
  136. errorResultString := HCloudAnswerErrorString{}
  137. err = json.Unmarshal([]byte(respBody), &errorResultString)
  138. if err != nil {
  139. return HCloudAnswerGetRecord{}, err
  140. }
  141. errorResult.Error.Message = errorResultString.Error
  142. errorResult.Error.Code = resp.StatusCode
  143. }
  144. answer.Error = errorResult.Error
  145. return answer, nil
  146. }
  147. // DeleteRecord remove record by ID.
  148. // Accepts single ID string.
  149. // Returns HCloudAnswerDeleteRecord and error.
  150. func (d *HCloudClient) DeleteRecord(ID string) (HCloudAnswerDeleteRecord, error) {
  151. client := &http.Client{}
  152. req, err := http.NewRequest("DELETE", fmt.Sprintf("https://dns.hetzner.com/api/v1/records/%v", ID), nil)
  153. if err != nil {
  154. return HCloudAnswerDeleteRecord{}, err
  155. }
  156. req.Header.Add("Auth-API-Token", d.Token)
  157. resp, err := client.Do(req)
  158. if err != nil {
  159. return HCloudAnswerDeleteRecord{}, err
  160. }
  161. respBody, err := ioutil.ReadAll(resp.Body)
  162. if err != nil {
  163. return HCloudAnswerDeleteRecord{}, err
  164. }
  165. answer := HCloudAnswerDeleteRecord{}
  166. // parse error
  167. errorResult := HCloudAnswerError{}
  168. err = json.Unmarshal([]byte(respBody), &errorResult)
  169. if err != nil {
  170. //ok, non-standard error, try another form
  171. errorResultString := HCloudAnswerErrorString{}
  172. err = json.Unmarshal([]byte(respBody), &errorResultString)
  173. if err != nil {
  174. return HCloudAnswerDeleteRecord{}, err
  175. }
  176. errorResult.Error.Message = errorResultString.Error
  177. errorResult.Error.Code = resp.StatusCode
  178. }
  179. answer.Error = errorResult.Error
  180. return answer, nil
  181. }
  182. // CreateRecord creates new single record.
  183. // Accepts HCloudRecord with record to create, of cource no ID.
  184. // Returns HCloudAnswerGetRecord with HCloudRecord and error.
  185. func (d *HCloudClient) CreateRecord(record HCloudRecord) (HCloudAnswerGetRecord, error) {
  186. jsonRecordString, err := json.Marshal(record)
  187. if err != nil {
  188. return HCloudAnswerGetRecord{}, err
  189. }
  190. body := bytes.NewBuffer(jsonRecordString)
  191. client := &http.Client{}
  192. req, err := http.NewRequest("POST", fmt.Sprintf("https://dns.hetzner.com/api/v1/records"), body)
  193. if err != nil {
  194. return HCloudAnswerGetRecord{}, err
  195. }
  196. req.Header.Add("Content-Type", "application/json")
  197. req.Header.Add("Auth-API-Token", d.Token)
  198. resp, err := client.Do(req)
  199. if err != nil {
  200. return HCloudAnswerGetRecord{}, err
  201. }
  202. respBody, err := ioutil.ReadAll(resp.Body)
  203. if err != nil {
  204. return HCloudAnswerGetRecord{}, err
  205. }
  206. answer := HCloudAnswerGetRecord{}
  207. err = json.Unmarshal([]byte(respBody), &answer)
  208. if err != nil {
  209. return HCloudAnswerGetRecord{}, err
  210. }
  211. // parse error
  212. errorResult := HCloudAnswerError{}
  213. err = json.Unmarshal([]byte(respBody), &errorResult)
  214. if err != nil {
  215. //ok, non-standard error, try another form
  216. errorResultString := HCloudAnswerErrorString{}
  217. err = json.Unmarshal([]byte(respBody), &errorResultString)
  218. if err != nil {
  219. return HCloudAnswerGetRecord{}, err
  220. }
  221. errorResult.Error.Message = errorResultString.Error
  222. errorResult.Error.Code = resp.StatusCode
  223. }
  224. answer.Error = errorResult.Error
  225. return answer, nil
  226. }
  227. // CreateRecordBulk creates many records at once.
  228. // Accepts array of HCloudRecord, converts it to json and makes POST to Hetzner.
  229. // Returns HCloudAnswerCreateRecords with arrays of HCloudRecord with whole list, valid and invalid, error.
  230. func (d *HCloudClient) CreateRecordBulk(record []HCloudRecord) (HCloudAnswerCreateRecords, error) {
  231. jsonRecordString, err := json.Marshal(record)
  232. if err != nil {
  233. return HCloudAnswerCreateRecords{}, err
  234. }
  235. body := bytes.NewBuffer(jsonRecordString)
  236. client := &http.Client{}
  237. req, err := http.NewRequest("POST", "https://dns.hetzner.com/api/v1/api/v1/records/bulk", body)
  238. if err != nil {
  239. return HCloudAnswerCreateRecords{}, err
  240. }
  241. req.Header.Add("Content-Type", "application/json")
  242. req.Header.Add("Auth-API-Token", d.Token)
  243. resp, err := client.Do(req)
  244. if err != nil {
  245. return HCloudAnswerCreateRecords{}, err
  246. }
  247. respBody, err := ioutil.ReadAll(resp.Body)
  248. if err != nil {
  249. return HCloudAnswerCreateRecords{}, err
  250. }
  251. answer := HCloudAnswerCreateRecords{}
  252. err = json.Unmarshal([]byte(respBody), &answer)
  253. if err != nil {
  254. return HCloudAnswerCreateRecords{}, err
  255. }
  256. // parse error
  257. errorResult := HCloudAnswerError{}
  258. err = json.Unmarshal([]byte(respBody), &errorResult)
  259. if err != nil {
  260. //ok, non-standard error, try another form
  261. errorResultString := HCloudAnswerErrorString{}
  262. err = json.Unmarshal([]byte(respBody), &errorResultString)
  263. if err != nil {
  264. return HCloudAnswerCreateRecords{}, err
  265. }
  266. errorResult.Error.Message = errorResultString.Error
  267. errorResult.Error.Code = resp.StatusCode
  268. }
  269. answer.Error = errorResult.Error
  270. return answer, nil
  271. }
  272. // UpdateRecordBulk updates many records at once.
  273. // Accepts array of HCloudRecord, converting to json and makes PUT to Hetzner.
  274. // Returns HCloudAnswerUpdateRecords with arrays of HCloudRecord with updated and failed, error.
  275. func (d *HCloudClient) UpdateRecordBulk(record []HCloudRecord) (HCloudAnswerUpdateRecords, error) {
  276. jsonRecordString, err := json.Marshal(record)
  277. if err != nil {
  278. return HCloudAnswerUpdateRecords{}, err
  279. }
  280. body := bytes.NewBuffer(jsonRecordString)
  281. client := &http.Client{}
  282. req, err := http.NewRequest("PUT", "https://dns.hetzner.com/api/v1/api/v1/records/bulk", body)
  283. if err != nil {
  284. return HCloudAnswerUpdateRecords{}, err
  285. }
  286. req.Header.Add("Content-Type", "application/json")
  287. req.Header.Add("Auth-API-Token", d.Token)
  288. resp, err := client.Do(req)
  289. if err != nil {
  290. return HCloudAnswerUpdateRecords{}, err
  291. }
  292. respBody, err := ioutil.ReadAll(resp.Body)
  293. if err != nil {
  294. return HCloudAnswerUpdateRecords{}, err
  295. }
  296. answer := HCloudAnswerUpdateRecords{}
  297. err = json.Unmarshal([]byte(respBody), &answer)
  298. if err != nil {
  299. return HCloudAnswerUpdateRecords{}, err
  300. }
  301. // parse error
  302. errorResult := HCloudAnswerError{}
  303. err = json.Unmarshal([]byte(respBody), &errorResult)
  304. if err != nil {
  305. //ok, non-standard error, try another form
  306. errorResultString := HCloudAnswerErrorString{}
  307. err = json.Unmarshal([]byte(respBody), &errorResultString)
  308. if err != nil {
  309. return HCloudAnswerUpdateRecords{}, err
  310. }
  311. errorResult.Error.Message = errorResultString.Error
  312. errorResult.Error.Code = resp.StatusCode
  313. }
  314. answer.Error = errorResult.Error
  315. return answer, nil
  316. }