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.

410 lines
11 KiB

  1. package hclouddns
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. )
  11. // GetZone retrieve one single zone by ID.
  12. // Accepts zone ID string.
  13. // Returns HCloudAnswerGetZone with HCloudZone and error
  14. func (d *HCloudClient) GetZone(ID string) (HCloudAnswerGetZone, error) {
  15. client := &http.Client{}
  16. req, err := http.NewRequest("GET", fmt.Sprintf("https://dns.hetzner.com/api/v1/zones/%v", ID), nil)
  17. if err != nil {
  18. return HCloudAnswerGetZone{}, err
  19. }
  20. req.Header.Add("Auth-API-Token", d.Token)
  21. resp, err := client.Do(req)
  22. if err != nil {
  23. return HCloudAnswerGetZone{}, err
  24. }
  25. respBody, err := ioutil.ReadAll(resp.Body)
  26. if err != nil {
  27. return HCloudAnswerGetZone{}, err
  28. }
  29. answer := HCloudAnswerGetZone{}
  30. err = json.Unmarshal([]byte(respBody), &answer)
  31. if err != nil {
  32. return HCloudAnswerGetZone{}, err
  33. }
  34. // parse error
  35. errorResult := HCloudAnswerError{}
  36. err = json.Unmarshal([]byte(respBody), &errorResult)
  37. if err != nil {
  38. //ok, non-standard error, try another form
  39. errorResultString := HCloudAnswerErrorString{}
  40. err = json.Unmarshal([]byte(respBody), &errorResultString)
  41. if err != nil {
  42. return HCloudAnswerGetZone{}, err
  43. }
  44. errorResult.Error.Message = errorResultString.Error
  45. errorResult.Error.Code = resp.StatusCode
  46. }
  47. answer.Error = errorResult.Error
  48. return answer, nil
  49. }
  50. // GetZones retrieve all zones of user.
  51. // Accepts exact name as string, search name with partial name.
  52. // Returns HCloudAnswerGetZones with array of HCloudZone, Meta and error.
  53. func (d *HCloudClient) GetZones(params HCloudGetZonesParams) (HCloudAnswerGetZones, error) {
  54. v := url.Values{}
  55. if params.Name != "" {
  56. v.Add("name", params.Name)
  57. }
  58. if params.SearchName != "" {
  59. v.Add("search_name", params.SearchName)
  60. }
  61. if params.Page != "" {
  62. v.Add("page", params.Page)
  63. }
  64. if params.PerPage != "" {
  65. v.Add("per_page", params.PerPage)
  66. }
  67. client := &http.Client{}
  68. req, err := http.NewRequest("GET", fmt.Sprintf("https://dns.hetzner.com/api/v1/zones?%v", v.Encode()), nil)
  69. if err != nil {
  70. return HCloudAnswerGetZones{}, err
  71. }
  72. req.Header.Add("Auth-API-Token", d.Token)
  73. parseFormErr := req.ParseForm()
  74. if parseFormErr != nil {
  75. return HCloudAnswerGetZones{}, parseFormErr
  76. }
  77. resp, err := client.Do(req)
  78. if err != nil {
  79. return HCloudAnswerGetZones{}, err
  80. }
  81. respBody, err := ioutil.ReadAll(resp.Body)
  82. if err != nil {
  83. return HCloudAnswerGetZones{}, err
  84. }
  85. answer := HCloudAnswerGetZones{}
  86. err = json.Unmarshal([]byte(respBody), &answer)
  87. if err != nil {
  88. return HCloudAnswerGetZones{}, err
  89. }
  90. // parse error
  91. errorResult := HCloudAnswerError{}
  92. err = json.Unmarshal([]byte(respBody), &errorResult)
  93. if err != nil {
  94. //ok, non-standard error, try another form
  95. errorResultString := HCloudAnswerErrorString{}
  96. err = json.Unmarshal([]byte(respBody), &errorResultString)
  97. if err != nil {
  98. return HCloudAnswerGetZones{}, err
  99. }
  100. errorResult.Error.Message = errorResultString.Error
  101. errorResult.Error.Code = resp.StatusCode
  102. }
  103. answer.Error = errorResult.Error
  104. return answer, nil
  105. }
  106. // UpdateZone makes update of single zone by ID.
  107. // Accepts HCloudZone with fullfilled fields.
  108. // Returns HCloudAnswerGetZone with HCloudZone and error.
  109. func (d *HCloudClient) UpdateZone(zone HCloudZone) (HCloudAnswerGetZone, error) {
  110. jsonZoneString, err := json.Marshal(zone)
  111. if err != nil {
  112. return HCloudAnswerGetZone{}, err
  113. }
  114. body := bytes.NewBuffer(jsonZoneString)
  115. client := &http.Client{}
  116. req, err := http.NewRequest("PUT", fmt.Sprintf("https://dns.hetzner.com/api/v1/zones/%v", zone.ID), body)
  117. if err != nil {
  118. return HCloudAnswerGetZone{}, err
  119. }
  120. req.Header.Add("Content-Type", "application/json")
  121. req.Header.Add("Auth-API-Token", d.Token)
  122. resp, err := client.Do(req)
  123. if err != nil {
  124. return HCloudAnswerGetZone{}, err
  125. }
  126. respBody, err := ioutil.ReadAll(resp.Body)
  127. if err != nil {
  128. return HCloudAnswerGetZone{}, err
  129. }
  130. answer := HCloudAnswerGetZone{}
  131. err = json.Unmarshal([]byte(respBody), &answer)
  132. if err != nil {
  133. return HCloudAnswerGetZone{}, err
  134. }
  135. // parse error
  136. errorResult := HCloudAnswerError{}
  137. err = json.Unmarshal([]byte(respBody), &errorResult)
  138. if err != nil {
  139. //ok, non-standard error, try another form
  140. errorResultString := HCloudAnswerErrorString{}
  141. err = json.Unmarshal([]byte(respBody), &errorResultString)
  142. if err != nil {
  143. return HCloudAnswerGetZone{}, err
  144. }
  145. errorResult.Error.Message = errorResultString.Error
  146. errorResult.Error.Code = resp.StatusCode
  147. }
  148. answer.Error = errorResult.Error
  149. return answer, nil
  150. }
  151. // DeleteZone remove zone by ID.
  152. // Accepts single ID string.
  153. // Returns HCloudAnswerDeleteZone with error.
  154. func (d *HCloudClient) DeleteZone(ID string) (HCloudAnswerDeleteZone, error) {
  155. client := &http.Client{}
  156. req, err := http.NewRequest("DELETE", fmt.Sprintf("https://dns.hetzner.com/api/v1/zones/%v", ID), nil)
  157. if err != nil {
  158. return HCloudAnswerDeleteZone{}, err
  159. }
  160. req.Header.Add("Auth-API-Token", d.Token)
  161. resp, err := client.Do(req)
  162. if err != nil {
  163. return HCloudAnswerDeleteZone{}, err
  164. }
  165. respBody, err := ioutil.ReadAll(resp.Body)
  166. if err != nil {
  167. return HCloudAnswerDeleteZone{}, err
  168. }
  169. answer := HCloudAnswerDeleteZone{}
  170. // parse error
  171. errorResult := HCloudAnswerError{}
  172. err = json.Unmarshal([]byte(respBody), &errorResult)
  173. if err != nil {
  174. //ok, non-standard error, try another form
  175. errorResultString := HCloudAnswerErrorString{}
  176. err = json.Unmarshal([]byte(respBody), &errorResultString)
  177. if err != nil {
  178. return HCloudAnswerDeleteZone{}, err
  179. }
  180. errorResult.Error.Message = errorResultString.Error
  181. errorResult.Error.Code = resp.StatusCode
  182. }
  183. answer.Error = errorResult.Error
  184. return answer, nil
  185. }
  186. // CreateZone creates new single zone.
  187. // Accepts HCloudZone with record to create, of cource no ID.
  188. // Returns HCloudAnswerGetZone with HCloudZone and error.
  189. func (d *HCloudClient) CreateZone(zone HCloudZone) (HCloudAnswerGetZone, error) {
  190. jsonZoneString, err := json.Marshal(zone)
  191. if err != nil {
  192. return HCloudAnswerGetZone{}, err
  193. }
  194. body := bytes.NewBuffer(jsonZoneString)
  195. client := &http.Client{}
  196. req, err := http.NewRequest("POST", fmt.Sprintf("https://dns.hetzner.com/api/v1/zones"), body)
  197. if err != nil {
  198. return HCloudAnswerGetZone{}, err
  199. }
  200. req.Header.Add("Content-Type", "application/json")
  201. req.Header.Add("Auth-API-Token", d.Token)
  202. resp, err := client.Do(req)
  203. if err != nil {
  204. return HCloudAnswerGetZone{}, err
  205. }
  206. respBody, err := ioutil.ReadAll(resp.Body)
  207. if err != nil {
  208. return HCloudAnswerGetZone{}, err
  209. }
  210. answer := HCloudAnswerGetZone{}
  211. err = json.Unmarshal([]byte(respBody), &answer)
  212. if err != nil {
  213. return HCloudAnswerGetZone{}, err
  214. }
  215. // parse error
  216. errorResult := HCloudAnswerError{}
  217. err = json.Unmarshal([]byte(respBody), &errorResult)
  218. if err != nil {
  219. //ok, non-standard error, try another form
  220. errorResultString := HCloudAnswerErrorString{}
  221. err = json.Unmarshal([]byte(respBody), &errorResultString)
  222. if err != nil {
  223. return HCloudAnswerGetZone{}, err
  224. }
  225. errorResult.Error.Message = errorResultString.Error
  226. errorResult.Error.Code = resp.StatusCode
  227. }
  228. answer.Error = errorResult.Error
  229. return answer, nil
  230. }
  231. // ImportZoneString imports single zone from imported text.
  232. // Accepts ID and zonePlainText strings.
  233. // Returns HCloudAnswerGetZone with HCloudZone and error.
  234. func (d *HCloudClient) ImportZoneString(zoneID string, zonePlainText string) (HCloudAnswerGetZone, error) {
  235. body := strings.NewReader(zonePlainText)
  236. client := &http.Client{}
  237. req, err := http.NewRequest("POST", fmt.Sprintf("https://dns.hetzner.com/api/v1/zones/%v/import", zoneID), body)
  238. if err != nil {
  239. return HCloudAnswerGetZone{}, err
  240. }
  241. req.Header.Add("Content-Type", "text/plain")
  242. req.Header.Add("Auth-API-Token", d.Token)
  243. resp, err := client.Do(req)
  244. if err != nil {
  245. return HCloudAnswerGetZone{}, err
  246. }
  247. respBody, err := ioutil.ReadAll(resp.Body)
  248. if err != nil {
  249. return HCloudAnswerGetZone{}, err
  250. }
  251. answer := HCloudAnswerGetZone{}
  252. err = json.Unmarshal([]byte(respBody), &answer)
  253. if err != nil {
  254. return HCloudAnswerGetZone{}, 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 HCloudAnswerGetZone{}, 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. // ExportZoneToString exports single zone from imported text.
  273. // Accepts ID and zonePlainText strings.
  274. // Returns HCloudAnswerGetZonePlainText with HCloudZone and error.
  275. func (d *HCloudClient) ExportZoneToString(zoneID string) (HCloudAnswerGetZonePlainText, error) {
  276. client := &http.Client{}
  277. req, err := http.NewRequest("GET", fmt.Sprintf("https://dns.hetzner.com/api/v1/zones/%v/export", zoneID), nil)
  278. if err != nil {
  279. return HCloudAnswerGetZonePlainText{}, err
  280. }
  281. req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
  282. req.Header.Add("Auth-API-Token", d.Token)
  283. resp, err := client.Do(req)
  284. if err != nil {
  285. return HCloudAnswerGetZonePlainText{}, err
  286. }
  287. respBody, err := ioutil.ReadAll(resp.Body)
  288. if err != nil {
  289. return HCloudAnswerGetZonePlainText{}, err
  290. }
  291. answer := HCloudAnswerGetZonePlainText{}
  292. answer.ZonePlainText = string(respBody)
  293. return answer, nil
  294. }
  295. // ValidateZoneString validate single zone from imported text.
  296. // Accepts ID and zonePlainText strings.
  297. // Returns HCloudAnswerZoneValidate with HCloudZone and error.
  298. func (d *HCloudClient) ValidateZoneString(zonePlainText string) (HCloudAnswerZoneValidate, error) {
  299. body := strings.NewReader(zonePlainText)
  300. client := &http.Client{}
  301. req, err := http.NewRequest("POST", fmt.Sprintf("https://dns.hetzner.com/api/v1/zones/file/validate"), body)
  302. if err != nil {
  303. return HCloudAnswerZoneValidate{}, err
  304. }
  305. req.Header.Add("Content-Type", "text/plain")
  306. req.Header.Add("Auth-API-Token", d.Token)
  307. resp, err := client.Do(req)
  308. if err != nil {
  309. return HCloudAnswerZoneValidate{}, err
  310. }
  311. respBody, err := ioutil.ReadAll(resp.Body)
  312. if err != nil {
  313. return HCloudAnswerZoneValidate{}, err
  314. }
  315. answer := HCloudAnswerZoneValidate{}
  316. err = json.Unmarshal([]byte(respBody), &answer)
  317. if err != nil {
  318. return HCloudAnswerZoneValidate{}, err
  319. }
  320. // parse error
  321. errorResult := HCloudAnswerError{}
  322. err = json.Unmarshal([]byte(respBody), &errorResult)
  323. if err != nil {
  324. //ok, non-standard error, try another form
  325. errorResultString := HCloudAnswerErrorString{}
  326. err = json.Unmarshal([]byte(respBody), &errorResultString)
  327. if err != nil {
  328. return HCloudAnswerZoneValidate{}, err
  329. }
  330. errorResult.Error.Message = errorResultString.Error
  331. errorResult.Error.Code = resp.StatusCode
  332. }
  333. answer.Error = errorResult.Error
  334. return answer, nil
  335. }