github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/dns/v2/zones/results.go (about) 1 package zones 2 3 import ( 4 "encoding/json" 5 "strconv" 6 "time" 7 8 "github.com/chnsz/golangsdk" 9 "github.com/chnsz/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 RecordNum int `json:"record_num"` 99 100 ZoneType string `json:"zone_type"` 101 102 // Masters is the servers for slave servers to get DNS information from. 103 Masters []string `json:"masters"` 104 105 // CreatedAt is the date when the zone was created. 106 CreatedAt time.Time `json:"-"` 107 108 // UpdatedAt is the date when the last change was made to the zone. 109 UpdatedAt time.Time `json:"-"` 110 111 // Links includes HTTP references to the itself, useful for passing along 112 // to other APIs that might want a server reference. 113 Links map[string]interface{} `json:"links"` 114 115 // Routers associate with the Zone 116 Routers []RouterResult `json:"routers"` 117 118 // Enterprise project id 119 EnterpriseProjectID string `json:"enterprise_project_id"` 120 121 // Deprecated 122 // Action is the current action in progress on the resource. 123 Action string `json:"action"` 124 125 // Deprecated 126 // Attributes for the zone. 127 Attributes map[string]string `json:"attributes"` 128 129 // Deprecated 130 // TransferredAt is the last time an update was retrieved from the 131 // master servers. 132 TransferredAt time.Time `json:"-"` 133 134 // Deprecated 135 // Type of zone. Primary is controlled by Designate. 136 // Secondary zones are slaved from another DNS Server. 137 // Defaults to Primary. 138 Type string `json:"type"` 139 140 // Deprecated 141 // Version of the resource. 142 Version int `json:"version"` 143 } 144 145 type RouterResult struct { 146 RouterID string `json:"router_id"` 147 RouterRegion string `json:"router_region"` 148 Status string `json:"status"` 149 } 150 151 func (r *Zone) UnmarshalJSON(b []byte) error { 152 type tmp Zone 153 var s struct { 154 tmp 155 CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"` 156 UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"` 157 TransferredAt golangsdk.JSONRFC3339MilliNoZ `json:"transferred_at"` 158 Serial interface{} `json:"serial"` 159 } 160 err := json.Unmarshal(b, &s) 161 if err != nil { 162 return err 163 } 164 *r = Zone(s.tmp) 165 166 r.CreatedAt = time.Time(s.CreatedAt) 167 r.UpdatedAt = time.Time(s.UpdatedAt) 168 r.TransferredAt = time.Time(s.TransferredAt) 169 170 switch t := s.Serial.(type) { 171 case float64: 172 r.Serial = int(t) 173 case string: 174 switch t { 175 case "": 176 r.Serial = 0 177 default: 178 serial, err := strconv.ParseFloat(t, 64) 179 if err != nil { 180 return err 181 } 182 r.Serial = int(serial) 183 } 184 } 185 186 return err 187 } 188 189 // AssociateResult is the response from AssociateZone 190 type AssociateResult struct { 191 commonResult 192 } 193 194 // DisassociateResult is the response from DisassociateZone 195 type DisassociateResult struct { 196 commonResult 197 }