github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/dns/v2/zones/results.go (about) 1 package zones 2 3 import ( 4 "encoding/json" 5 "strconv" 6 "time" 7 8 "github.com/huaweicloud/golangsdk" 9 "github.com/huaweicloud/golangsdk/pagination" 10 ) 11 12 type commonResult struct { 13 golangsdk.Result 14 } 15 16 // Extract interprets a GetResult, CreateResult or UpdateResult as a 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 // CreateResult is the result of a Create request. Call its Extract method 25 // to interpret the result as a Zone. 26 type CreateResult struct { 27 commonResult 28 } 29 30 // GetResult is the result of a Get request. Call its Extract method 31 // to interpret the result as a Zone. 32 type GetResult struct { 33 commonResult 34 } 35 36 // UpdateResult is the result of an Update request. Call its Extract method 37 // to interpret the result as a Zone. 38 type UpdateResult struct { 39 commonResult 40 } 41 42 // DeleteResult is the result of a Delete request. Call its ExtractErr method 43 // to determine if the request succeeded or failed. 44 type DeleteResult struct { 45 commonResult 46 } 47 48 // ZonePage is a single page of Zone results. 49 type ZonePage struct { 50 pagination.LinkedPageBase 51 } 52 53 // IsEmpty returns true if the page contains no results. 54 func (r ZonePage) IsEmpty() (bool, error) { 55 s, err := ExtractZones(r) 56 return len(s) == 0, err 57 } 58 59 // ExtractZones extracts a slice of Zones from a List result. 60 func ExtractZones(r pagination.Page) ([]Zone, error) { 61 var s struct { 62 Zones []Zone `json:"zones"` 63 } 64 err := (r.(ZonePage)).ExtractInto(&s) 65 return s.Zones, err 66 } 67 68 // Zone represents a DNS zone. 69 type Zone struct { 70 // ID uniquely identifies this zone amongst all other zones, including those 71 // not accessible to the current tenant. 72 ID string `json:"id"` 73 74 // PoolID is the ID for the pool hosting this zone. 75 PoolID string `json:"pool_id"` 76 77 // ProjectID identifies the project/tenant owning this resource. 78 ProjectID string `json:"project_id"` 79 80 // Name is the DNS Name for the zone. 81 Name string `json:"name"` 82 83 // Email for the zone. Used in SOA records for the zone. 84 Email string `json:"email"` 85 86 // Description for this zone. 87 Description string `json:"description"` 88 89 // TTL is the Time to Live for the zone. 90 TTL int `json:"ttl"` 91 92 // Serial is the current serial number for the zone. 93 Serial int `json:"-"` 94 95 // Status is the status of the resource. 96 Status string `json:"status"` 97 98 // Action is the current action in progress on the resource. 99 Action string `json:"action"` 100 101 // Version of the resource. 102 Version int `json:"version"` 103 104 // Attributes for the zone. 105 Attributes map[string]string `json:"attributes"` 106 107 // Type of zone. Primary is controlled by Designate. 108 // Secondary zones are slaved from another DNS Server. 109 // Defaults to Primary. 110 Type string `json:"type"` 111 ZoneType string `json:"zone_type"` 112 113 // Masters is the servers for slave servers to get DNS information from. 114 Masters []string `json:"masters"` 115 116 // CreatedAt is the date when the zone was created. 117 CreatedAt time.Time `json:"-"` 118 119 // UpdatedAt is the date when the last change was made to the zone. 120 UpdatedAt time.Time `json:"-"` 121 122 // TransferredAt is the last time an update was retrieved from the 123 // master servers. 124 TransferredAt time.Time `json:"-"` 125 126 // Links includes HTTP references to the itself, useful for passing along 127 // to other APIs that might want a server reference. 128 Links map[string]interface{} `json:"links"` 129 130 // Routers associate with the Zone 131 Routers []RouterResult `json:"routers"` 132 133 // Enterprise project id 134 EnterpriseProjectID string `json:"enterprise_project_id"` 135 } 136 137 type RouterResult struct { 138 RouterID string `json:"router_id"` 139 RouterRegion string `json:"router_region"` 140 Status string `json:"status"` 141 } 142 143 func (r *Zone) UnmarshalJSON(b []byte) error { 144 type tmp Zone 145 var s struct { 146 tmp 147 CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"` 148 UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"` 149 TransferredAt golangsdk.JSONRFC3339MilliNoZ `json:"transferred_at"` 150 Serial interface{} `json:"serial"` 151 } 152 err := json.Unmarshal(b, &s) 153 if err != nil { 154 return err 155 } 156 *r = Zone(s.tmp) 157 158 r.CreatedAt = time.Time(s.CreatedAt) 159 r.UpdatedAt = time.Time(s.UpdatedAt) 160 r.TransferredAt = time.Time(s.TransferredAt) 161 162 switch t := s.Serial.(type) { 163 case float64: 164 r.Serial = int(t) 165 case string: 166 switch t { 167 case "": 168 r.Serial = 0 169 default: 170 serial, err := strconv.ParseFloat(t, 64) 171 if err != nil { 172 return err 173 } 174 r.Serial = int(serial) 175 } 176 } 177 178 return err 179 } 180 181 // AssociateResult is the response from AssociateZone 182 type AssociateResult struct { 183 commonResult 184 } 185 186 // DisassociateResult is the response from DisassociateZone 187 type DisassociateResult struct { 188 commonResult 189 }