Joe Topjian | 0f64da0 | 2017-03-09 18:59:53 -0700 | [diff] [blame] | 1 | package availabilityzones |
| 2 | |
Ildar Svetlov | b35f361 | 2020-01-27 13:51:00 +0400 | [diff] [blame] | 3 | import ( |
| 4 | "encoding/json" |
| 5 | "time" |
| 6 | |
Ildar Svetlov | 82d4291 | 2020-01-27 14:47:43 +0400 | [diff] [blame] | 7 | "gerrit.mcp.mirantis.net/debian/gophercloud.git" |
| 8 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" |
Ildar Svetlov | b35f361 | 2020-01-27 13:51:00 +0400 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | // ServerAvailabilityZoneExt is an extension to the base Server object. |
| 12 | type ServerAvailabilityZoneExt struct { |
Joe Topjian | 0f64da0 | 2017-03-09 18:59:53 -0700 | [diff] [blame] | 13 | // AvailabilityZone is the availabilty zone the server is in. |
| 14 | AvailabilityZone string `json:"OS-EXT-AZ:availability_zone"` |
| 15 | } |
| 16 | |
Ildar Svetlov | b35f361 | 2020-01-27 13:51:00 +0400 | [diff] [blame] | 17 | // ServiceState represents the state of a service in an AvailabilityZone. |
| 18 | type ServiceState struct { |
| 19 | Active bool `json:"active"` |
| 20 | Available bool `json:"available"` |
| 21 | UpdatedAt time.Time `json:"-"` |
| 22 | } |
| 23 | |
Joe Topjian | 0f64da0 | 2017-03-09 18:59:53 -0700 | [diff] [blame] | 24 | // UnmarshalJSON to override default |
Ildar Svetlov | b35f361 | 2020-01-27 13:51:00 +0400 | [diff] [blame] | 25 | func (r *ServiceState) UnmarshalJSON(b []byte) error { |
| 26 | type tmp ServiceState |
| 27 | var s struct { |
| 28 | tmp |
| 29 | UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"` |
| 30 | } |
| 31 | err := json.Unmarshal(b, &s) |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | *r = ServiceState(s.tmp) |
| 36 | |
| 37 | r.UpdatedAt = time.Time(s.UpdatedAt) |
| 38 | |
Joe Topjian | 0f64da0 | 2017-03-09 18:59:53 -0700 | [diff] [blame] | 39 | return nil |
| 40 | } |
Ildar Svetlov | b35f361 | 2020-01-27 13:51:00 +0400 | [diff] [blame] | 41 | |
| 42 | // Services is a map of services contained in an AvailabilityZone. |
| 43 | type Services map[string]ServiceState |
| 44 | |
| 45 | // Hosts is map of hosts/nodes contained in an AvailabilityZone. |
| 46 | // Each host can have multiple services. |
| 47 | type Hosts map[string]Services |
| 48 | |
| 49 | // ZoneState represents the current state of the availability zone. |
| 50 | type ZoneState struct { |
| 51 | // Returns true if the availability zone is available |
| 52 | Available bool `json:"available"` |
| 53 | } |
| 54 | |
| 55 | // AvailabilityZone contains all the information associated with an OpenStack |
| 56 | // AvailabilityZone. |
| 57 | type AvailabilityZone struct { |
| 58 | Hosts Hosts `json:"hosts"` |
| 59 | // The availability zone name |
| 60 | ZoneName string `json:"zoneName"` |
| 61 | ZoneState ZoneState `json:"zoneState"` |
| 62 | } |
| 63 | |
| 64 | type AvailabilityZonePage struct { |
| 65 | pagination.SinglePageBase |
| 66 | } |
| 67 | |
| 68 | // ExtractAvailabilityZones returns a slice of AvailabilityZones contained in a |
| 69 | // single page of results. |
| 70 | func ExtractAvailabilityZones(r pagination.Page) ([]AvailabilityZone, error) { |
| 71 | var s struct { |
| 72 | AvailabilityZoneInfo []AvailabilityZone `json:"availabilityZoneInfo"` |
| 73 | } |
| 74 | err := (r.(AvailabilityZonePage)).ExtractInto(&s) |
| 75 | return s.AvailabilityZoneInfo, err |
| 76 | } |