Joe Topjian | 71b85bd | 2017-03-09 18:55:36 -0700 | [diff] [blame^] | 1 | package zones |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strconv" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/gophercloud/gophercloud" |
| 9 | "github.com/gophercloud/gophercloud/pagination" |
| 10 | ) |
| 11 | |
| 12 | type commonResult struct { |
| 13 | gophercloud.Result |
| 14 | } |
| 15 | |
| 16 | // Extract interprets a GetResult, CreateResult or UpdateResult as a concrete Zone. |
| 17 | // An error is returned if the original call or the extraction failed. |
| 18 | func (r commonResult) Extract() (*Zone, error) { |
| 19 | var s *Zone |
| 20 | err := r.ExtractInto(&s) |
| 21 | return s, err |
| 22 | } |
| 23 | |
| 24 | // GetResult is the deferred result of a Get call. |
| 25 | type GetResult struct { |
| 26 | commonResult |
| 27 | } |
| 28 | |
| 29 | // ZonePage is a single page of Zone results. |
| 30 | type ZonePage struct { |
| 31 | pagination.LinkedPageBase |
| 32 | } |
| 33 | |
| 34 | // IsEmpty returns true if the page contains no results. |
| 35 | func (r ZonePage) IsEmpty() (bool, error) { |
| 36 | s, err := ExtractZones(r) |
| 37 | return len(s) == 0, err |
| 38 | } |
| 39 | |
| 40 | // ExtractZones extracts a slice of Services from a Collection acquired from List. |
| 41 | func ExtractZones(r pagination.Page) ([]Zone, error) { |
| 42 | var s struct { |
| 43 | Zones []Zone `json:"zones"` |
| 44 | } |
| 45 | err := (r.(ZonePage)).ExtractInto(&s) |
| 46 | return s.Zones, err |
| 47 | } |
| 48 | |
| 49 | // Zone represents a DNS zone. |
| 50 | type Zone struct { |
| 51 | // ID uniquely identifies this zone amongst all other zones, including those not accessible to the current tenant. |
| 52 | ID string `json:"id"` |
| 53 | |
| 54 | // PoolID is the ID for the pool hosting this zone. |
| 55 | PoolID string `json:"pool_id"` |
| 56 | |
| 57 | // ProjectID identifies the project/tenant owning this resource. |
| 58 | ProjectID string `json:"project_id"` |
| 59 | |
| 60 | // Name is the DNS Name for the zone. |
| 61 | Name string `json:"name"` |
| 62 | |
| 63 | // Email for the zone. Used in SOA records for the zone. |
| 64 | Email string `json:"email"` |
| 65 | |
| 66 | // Description for this zone. |
| 67 | Description string `json:"description"` |
| 68 | |
| 69 | // TTL is the Time to Live for the zone. |
| 70 | TTL int `json:"ttl"` |
| 71 | |
| 72 | // Serial is the current serial number for the zone. |
| 73 | Serial int `json:"-"` |
| 74 | |
| 75 | // Status is the status of the resource. |
| 76 | Status string `json:"status"` |
| 77 | |
| 78 | // Action is the current action in progress on the resource. |
| 79 | Action string `json:"action"` |
| 80 | |
| 81 | // Version of the resource. |
| 82 | Version int `json:"version"` |
| 83 | |
| 84 | // Attributes for the zone. |
| 85 | Attributes map[string]string `json:"attributes"` |
| 86 | |
| 87 | // Type of zone. Primary is controlled by Designate. |
| 88 | // Secondary zones are slaved from another DNS Server. |
| 89 | // Defaults to Primary. |
| 90 | Type string `json:"type"` |
| 91 | |
| 92 | // Masters is the servers for slave servers to get DNS information from. |
| 93 | Masters []string `json:"masters"` |
| 94 | |
| 95 | // CreatedAt is the date when the zone was created. |
| 96 | CreatedAt time.Time `json:"-"` |
| 97 | |
| 98 | // UpdatedAt is the date when the last change was made to the zone. |
| 99 | UpdatedAt time.Time `json:"-"` |
| 100 | |
| 101 | // TransferredAt is the last time an update was retrieved from the master servers. |
| 102 | TransferredAt time.Time `json:"-"` |
| 103 | |
| 104 | // Links includes HTTP references to the itself, useful for passing along to other APIs that might want a server reference. |
| 105 | Links map[string]interface{} `json:"links"` |
| 106 | } |
| 107 | |
| 108 | func (r *Zone) UnmarshalJSON(b []byte) error { |
| 109 | type tmp Zone |
| 110 | var s struct { |
| 111 | tmp |
| 112 | CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"` |
| 113 | UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"` |
| 114 | TransferredAt gophercloud.JSONRFC3339MilliNoZ `json:"transferred_at"` |
| 115 | Serial interface{} `json:"serial"` |
| 116 | } |
| 117 | err := json.Unmarshal(b, &s) |
| 118 | if err != nil { |
| 119 | return err |
| 120 | } |
| 121 | *r = Zone(s.tmp) |
| 122 | |
| 123 | r.CreatedAt = time.Time(s.CreatedAt) |
| 124 | r.UpdatedAt = time.Time(s.UpdatedAt) |
| 125 | r.TransferredAt = time.Time(s.TransferredAt) |
| 126 | |
| 127 | switch t := s.Serial.(type) { |
| 128 | case float64: |
| 129 | r.Serial = int(t) |
| 130 | case string: |
| 131 | switch t { |
| 132 | case "": |
| 133 | r.Serial = 0 |
| 134 | default: |
| 135 | serial, err := strconv.ParseFloat(t, 64) |
| 136 | if err != nil { |
| 137 | return err |
| 138 | } |
| 139 | r.Serial = int(serial) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return err |
| 144 | } |