Skip to content

Commit

Permalink
Compute v2: Get Hypervisor Uptime (#724)
Browse files Browse the repository at this point in the history
* Add support to get hypervisor uptime specified by ID

* Add unit test

* Add acceptance test
  • Loading branch information
dstdfx authored and jtopjian committed Jan 19, 2018
1 parent 8a6dfa8 commit 8ca2d18
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 1 deletion.
21 changes: 20 additions & 1 deletion acceptance/openstack/compute/v2/hypervisors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestHypervisorsGet(t *testing.T) {
tools.PrintResource(t, hypervisor)
}

func TestHypervisorsStatistics(t *testing.T) {
func TestHypervisorsGetStatistics(t *testing.T) {
client, err := clients.NewComputeV2Client()
if err != nil {
t.Fatalf("Unable to create a compute client: %v", err)
Expand All @@ -66,6 +66,25 @@ func TestHypervisorsStatistics(t *testing.T) {
tools.PrintResource(t, hypervisorsStats)
}

func TestHypervisorsGetUptime(t *testing.T) {
client, err := clients.NewComputeV2Client()
if err != nil {
t.Fatalf("Unable to create a compute client: %v", err)
}

hypervisorID, err := getHypervisorID(t, client)
if err != nil {
t.Fatal(err)
}

hypervisor, err := hypervisors.GetUptime(client, hypervisorID).Extract()
if err != nil {
t.Fatalf("Unable to hypervisor uptime: %v", err)
}

tools.PrintResource(t, hypervisor)
}

func getHypervisorID(t *testing.T, client *gophercloud.ServiceClient) (int, error) {
allPages, err := hypervisors.List(client).AllPages()
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions openstack/compute/v2/extensions/hypervisors/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,15 @@ Example of Show Hypervisor Statistics
fmt.Printf("%+v\n", hypervisorsStatistics)
Example of Show Hypervisor Uptime
hypervisorID := 42
hypervisorUptime, err := hypervisors.GetUptime(computeClient, hypervisorID).Extract()
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", hypervisorUptime)
*/
package hypervisors
9 changes: 9 additions & 0 deletions openstack/compute/v2/extensions/hypervisors/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ func Get(client *gophercloud.ServiceClient, hypervisorID int) (r HypervisorResul
})
return
}

// GetUptime makes a request against the API to get uptime for specific hypervisor.
func GetUptime(client *gophercloud.ServiceClient, hypervisorID int) (r UptimeResult) {
v := strconv.Itoa(hypervisorID)
_, r.Err = client.Get(hypervisorsUptimeURL(client, v), &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return
}
32 changes: 32 additions & 0 deletions openstack/compute/v2/extensions/hypervisors/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,35 @@ func (r StatisticsResult) Extract() (*Statistics, error) {
err := r.ExtractInto(&s)
return &s.Stats, err
}

// Uptime represents uptime and additional info for a specific hypervisor.
type Uptime struct {
// The hypervisor host name provided by the Nova virt driver.
// For the Ironic driver, it is the Ironic node uuid.
HypervisorHostname string `json:"hypervisor_hostname"`

// The id of the hypervisor.
ID int `json:"id"`

// The state of the hypervisor. One of up or down.
State string `json:"state"`

// The status of the hypervisor. One of enabled or disabled.
Status string `json:"status"`

// The total uptime of the hypervisor and information about average load.
Uptime string `json:"uptime"`
}

type UptimeResult struct {
gophercloud.Result
}

// Extract interprets any UptimeResult as a Uptime, if possible.
func (r UptimeResult) Extract() (*Uptime, error) {
var s struct {
Uptime Uptime `json:"hypervisor"`
}
err := r.ExtractInto(&s)
return &s.Uptime, err
}
30 changes: 30 additions & 0 deletions openstack/compute/v2/extensions/hypervisors/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ const HypervisorGetBody = `
}
`

const HypervisorUptimeBody = `
{
"hypervisor": {
"hypervisor_hostname": "fake-mini",
"id": 1,
"state": "up",
"status": "enabled",
"uptime": " 08:32:11 up 93 days, 18:25, 12 users, load average: 0.20, 0.12, 0.14"
}
}
`

var (
HypervisorFake = hypervisors.Hypervisor{
CPUInfo: hypervisors.CPUInfo{
Expand Down Expand Up @@ -201,6 +213,13 @@ var (
VCPUs: 2,
VCPUsUsed: 0,
}
HypervisorUptimeExpected = hypervisors.Uptime{
HypervisorHostname: "fake-mini",
ID: 1,
State: "up",
Status: "enabled",
Uptime: " 08:32:11 up 93 days, 18:25, 12 users, load average: 0.20, 0.12, 0.14",
}
)

func HandleHypervisorsStatisticsSuccessfully(t *testing.T) {
Expand Down Expand Up @@ -233,3 +252,14 @@ func HandleHypervisorGetSuccessfully(t *testing.T) {
fmt.Fprintf(w, HypervisorGetBody)
})
}

func HandleHypervisorUptimeSuccessfully(t *testing.T) {
v := strconv.Itoa(HypervisorFake.ID)
testhelper.Mux.HandleFunc("/os-hypervisors/"+v+"/uptime", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "GET")
testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID)

w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, HypervisorUptimeBody)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,15 @@ func TestGetHypervisor(t *testing.T) {
testhelper.AssertNoErr(t, err)
testhelper.CheckDeepEquals(t, &expected, actual)
}

func TestHypervisorsUptime(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
HandleHypervisorUptimeSuccessfully(t)

expected := HypervisorUptimeExpected

actual, err := hypervisors.GetUptime(client.ServiceClient(), HypervisorFake.ID).Extract()
testhelper.AssertNoErr(t, err)
testhelper.CheckDeepEquals(t, &expected, actual)
}
4 changes: 4 additions & 0 deletions openstack/compute/v2/extensions/hypervisors/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ func hypervisorsStatisticsURL(c *gophercloud.ServiceClient) string {
func hypervisorsGetURL(c *gophercloud.ServiceClient, hypervisorID string) string {
return c.ServiceURL("os-hypervisors", hypervisorID)
}

func hypervisorsUptimeURL(c *gophercloud.ServiceClient, hypervisorID string) string {
return c.ServiceURL("os-hypervisors", hypervisorID, "uptime")
}

0 comments on commit 8ca2d18

Please sign in to comment.