1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
package recordsets
import (
"encoding/json"
"time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
type commonResult struct {
gophercloud.Result
}
// Extract interprets a GetResult, CreateResult or UpdateResult as a RecordSet.
// An error is returned if the original call or the extraction failed.
func (r commonResult) Extract() (*RecordSet, error) {
var s *RecordSet
err := r.ExtractInto(&s)
return s, err
}
// CreateResult is the result of a Create operation. Call its Extract method to
// interpret the result as a RecordSet.
type CreateResult struct {
commonResult
}
// GetResult is the result of a Get operation. Call its Extract method to
// interpret the result as a RecordSet.
type GetResult struct {
commonResult
}
// RecordSetPage is a single page of RecordSet results.
type RecordSetPage struct {
pagination.LinkedPageBase
}
// UpdateResult is result of an Update operation. Call its Extract method to
// interpret the result as a RecordSet.
type UpdateResult struct {
commonResult
}
// DeleteResult is result of a Delete operation. Call its ExtractErr method to
// determine if the operation succeeded or failed.
type DeleteResult struct {
gophercloud.ErrResult
}
// IsEmpty returns true if the page contains no results.
func (r RecordSetPage) IsEmpty() (bool, error) {
if r.StatusCode == 204 {
return true, nil
}
s, err := ExtractRecordSets(r)
return len(s) == 0, err
}
// ExtractRecordSets extracts a slice of RecordSets from a List result.
func ExtractRecordSets(r pagination.Page) ([]RecordSet, error) {
var s struct {
RecordSets []RecordSet `json:"recordsets"`
}
err := (r.(RecordSetPage)).ExtractInto(&s)
return s.RecordSets, err
}
// RecordSet represents a DNS Record Set.
type RecordSet struct {
// ID is the unique ID of the recordset
ID string `json:"id"`
// ZoneID is the ID of the zone the recordset belongs to.
ZoneID string `json:"zone_id"`
// ProjectID is the ID of the project that owns the recordset.
ProjectID string `json:"project_id"`
// Name is the name of the recordset.
Name string `json:"name"`
// ZoneName is the name of the zone the recordset belongs to.
ZoneName string `json:"zone_name"`
// Type is the RRTYPE of the recordset.
Type string `json:"type"`
// Records are the DNS records of the recordset.
Records []string `json:"records"`
// TTL is the time to live of the recordset.
TTL int `json:"ttl"`
// Status is the status of the recordset.
Status string `json:"status"`
// Action is the current action in progress of the recordset.
Action string `json:"action"`
// Description is the description of the recordset.
Description string `json:"description"`
// Version is the revision of the recordset.
Version int `json:"version"`
// CreatedAt is the date when the recordset was created.
CreatedAt time.Time `json:"-"`
// UpdatedAt is the date when the recordset was updated.
UpdatedAt time.Time `json:"-"`
// Links includes HTTP references to the itself,
// useful for passing along to other APIs that might want a recordset
// reference.
Links []gophercloud.Link `json:"-"`
// Metadata contains the total_count of resources matching the filter
Metadata struct {
TotalCount int `json:"total_count"`
} `json:"metadata"`
}
func (r *RecordSet) UnmarshalJSON(b []byte) error {
type tmp RecordSet
var s struct {
tmp
CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
Links map[string]interface{} `json:"links"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = RecordSet(s.tmp)
r.CreatedAt = time.Time(s.CreatedAt)
r.UpdatedAt = time.Time(s.UpdatedAt)
if s.Links != nil {
for rel, href := range s.Links {
if v, ok := href.(string); ok {
link := gophercloud.Link{
Rel: rel,
Href: v,
}
r.Links = append(r.Links, link)
}
}
}
return err
}
|