xml.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Package api speaks the AutoDNS XML gateway that Schlundtech exposes at
  2. // gateway.schlundtech.de. It is the only place in this program that performs
  3. // HTTP.
  4. //
  5. // The gateway answers HTTP 200 even when a task fails, so every response is
  6. // parsed and judged on result/status/type and result/msg — never on the HTTP
  7. // status code. A non-2xx status from this host is an nginx 404 for a wrong
  8. // path, not an application error.
  9. package api
  10. import "encoding/xml"
  11. // Request is the AutoDNS request envelope: one auth block plus one or more
  12. // task blocks.
  13. type Request struct {
  14. XMLName xml.Name `xml:"request"`
  15. Auth Auth `xml:"auth"`
  16. Language string `xml:"language,omitempty"`
  17. Task []Task `xml:"task"`
  18. }
  19. // Auth carries the gateway credentials. Context is the project the records
  20. // belong to; for Schlundtech it is 10.
  21. type Auth struct {
  22. User string `xml:"user"`
  23. Password string `xml:"password"`
  24. Context string `xml:"context"`
  25. // Token is the optional second-factor token, when the account has 2FA on.
  26. Token string `xml:"token,omitempty"`
  27. }
  28. // Task is a single task block. The API is task-specific below the code
  29. // element, so only the sub-structures a task can carry are modelled and the
  30. // caller populates the one it needs.
  31. type Task struct {
  32. Code string `xml:"code"`
  33. // Zone identifies the zone for a zone info or zone update task. Its Name
  34. // is empty for a zone list.
  35. Zone *ZoneRef `xml:"zone,omitempty"`
  36. // View, Where and Key belong to a list query.
  37. View *View `xml:"view,omitempty"`
  38. Where *Where `xml:"where,omitempty"`
  39. Key []string `xml:"key,omitempty"`
  40. // Default is the payload of a zone update.
  41. Default *Update `xml:"default,omitempty"`
  42. }
  43. // ZoneRef names a zone and the name server that serves it. SystemNS is
  44. // mandatory on updates — the gateway refuses the task without it.
  45. type ZoneRef struct {
  46. Name string `xml:"name"`
  47. SystemNS string `xml:"system_ns"`
  48. VirtualNS string `xml:"virtual_name_server,omitempty"`
  49. }
  50. // View bounds a list query.
  51. type View struct {
  52. Offset int `xml:"offset"`
  53. Limit int `xml:"limit"`
  54. Children int `xml:"children,omitempty"`
  55. }
  56. // Where filters a list query.
  57. type Where struct {
  58. Key string `xml:"key"`
  59. Operator string `xml:"operator"`
  60. Value string `xml:"value"`
  61. }
  62. // Update is the payload of a zone update. rr_add and rr_rem are repeated
  63. // elements: one sibling per record, not one element with children.
  64. type Update struct {
  65. RRAdd []Record `xml:"rr_add"`
  66. RRRem []Record `xml:"rr_rem"`
  67. }
  68. // Record is one resource record. Name is relative to the zone; the zone apex
  69. // is the empty string.
  70. type Record struct {
  71. Name string `xml:"name"`
  72. Type string `xml:"type"`
  73. Value string `xml:"value"`
  74. TTL int `xml:"ttl,omitempty"`
  75. Pref int `xml:"pref,omitempty"`
  76. }
  77. // Response is the AutoDNS response envelope. Result is repeated because a
  78. // multi-zone task answers once per zone.
  79. type Response struct {
  80. XMLName xml.Name `xml:"response"`
  81. Result []Result `xml:"result"`
  82. Stid string `xml:"stid"`
  83. }
  84. // Result is the per-task outcome.
  85. type Result struct {
  86. Data *Data `xml:"data,omitempty"`
  87. Status Status `xml:"status"`
  88. Msg []Msg `xml:"msg,omitempty"`
  89. }
  90. // Status is the overall task status. Type is "success", "error" or
  91. // "notification".
  92. type Status struct {
  93. Code string `xml:"code"`
  94. Type string `xml:"type"`
  95. Text string `xml:"text"`
  96. }
  97. // Msg is a system message. On failure this is where the actionable code and
  98. // text live; status stays generic (E00000).
  99. type Msg struct {
  100. Code string `xml:"code"`
  101. Type string `xml:"type"`
  102. Text string `xml:"text"`
  103. Object *Object `xml:"object,omitempty"`
  104. }
  105. // Object names what a Msg refers to.
  106. type Object struct {
  107. Type string `xml:"type"`
  108. Value string `xml:"value"`
  109. }
  110. // Data is the payload of an info or list task.
  111. type Data struct {
  112. Summary int `xml:"summary"`
  113. Zone []Zone `xml:"zone"`
  114. }
  115. // Zone is a DNS zone. The list task returns the summary fields; an info task
  116. // additionally returns every record in RRs.
  117. type Zone struct {
  118. Name string `xml:"name"`
  119. Origin string `xml:"origin,omitempty"`
  120. SystemNS string `xml:"system_ns"`
  121. SOA *SOA `xml:"soa,omitempty"`
  122. RRs []Record `xml:"rr"`
  123. }
  124. // SOA is a zone's start-of-authority record.
  125. type SOA struct {
  126. TTL int `xml:"ttl,omitempty"`
  127. Refresh int `xml:"refresh,omitempty"`
  128. Retry int `xml:"retry,omitempty"`
  129. Expire int `xml:"expire,omitempty"`
  130. Email string `xml:"email,omitempty"`
  131. }