Skip to content

Commit

Permalink
fix payload of get instance actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludovic Lamarche committed Feb 14, 2020
1 parent b43ad7f commit 91020b3
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 16 deletions.
70 changes: 67 additions & 3 deletions openstack/compute/v2/extensions/instanceactions/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,77 @@ func ExtractInstanceActions(r pagination.Page) ([]InstanceAction, error) {
return resp, err
}

// Event represents an event of instance action.
type Event struct {
Event string `json:"event"`
Host string `json:"host"`
HostID string `json:"hostId"`
Result string `json:"result"`
Traceback string `json:"traceback"`
StartTime time.Time `json:"-"`
FinishTime time.Time `json:"-"`
}

// UnmarshalJSON converts our JSON API response into our instance action struct
func (e *Event) UnmarshalJSON(b []byte) error {
type tmp Event
var s struct {
tmp
StartTime gophercloud.JSONRFC3339MilliNoZ `json:"start_time"`
FinishTime gophercloud.JSONRFC3339MilliNoZ `json:"finish_time"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*e = Event(s.tmp)

e.StartTime = time.Time(s.StartTime)
e.FinishTime = time.Time(s.FinishTime)

return err
}

// InstanceActionDetail gives more details on instance action.
type InstanceActionDetail struct {
Action string `json:"action"`
InstanceUUID string `json:"instance_uuid"`
Message string `json:"message"`
ProjectID string `json:"project_id"`
RequestID string `json:"request_id"`
UserID string `json:"user_id"`
Events []Event `json:"events"`
UpdatedAt time.Time `json:"-"`
StartTime time.Time `json:"-"`
}

// UnmarshalJSON converts our JSON API response into our instance action struct
func (i *InstanceActionDetail) UnmarshalJSON(b []byte) error {
type tmp InstanceActionDetail
var s struct {
tmp
UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
StartTime gophercloud.JSONRFC3339MilliNoZ `json:"start_time"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*i = InstanceActionDetail(s.tmp)

i.UpdatedAt = time.Time(s.UpdatedAt)
i.StartTime = time.Time(s.StartTime)
return err
}

// InstanceActionResult is the result handler of Get.
type InstanceActionResult struct {
gophercloud.Result
}

// Extract interprets any instanceActionResult as an InstanceAction, if possible.
func (r InstanceActionResult) Extract() (InstanceAction, error) {
var s InstanceAction
// Extract interprets any instanceActionResult as an InstanceActionDetail, if possible.
func (r InstanceActionResult) Extract() (InstanceActionDetail, error) {
var s InstanceActionDetail
err := r.ExtractInto(&s)
return s, err
}
Expand Down
48 changes: 35 additions & 13 deletions openstack/compute/v2/extensions/instanceactions/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,24 @@ func HandleInstanceActionListSuccessfully(t *testing.T) {
}

// GetExpected represents an expected repsonse from a Get request.
var GetExpected = instanceactions.InstanceAction{
var GetExpected = instanceactions.InstanceActionDetail{
Action: "stop",
InstanceUUID: "fcd19ef2-b593-40b1-90a5-fc31063fa95c",
InstanceUUID: "4bf3473b-d550-4b65-9409-292d44ab14a2",
Message: "",
ProjectID: "6f70656e737461636b20342065766572",
RequestID: "req-f8a59f03-76dc-412f-92c2-21f8612be728",
StartTime: time.Date(2018, 04, 25, 1, 26, 29, 000000, time.UTC),
RequestID: "req-0d819d5c-1527-4669-bdf0-ffad31b5105b",
StartTime: time.Date(2018, 04, 25, 1, 26, 36, 0, time.UTC),
UpdatedAt: time.Date(2018, 04, 25, 1, 26, 36, 0, time.UTC),
UserID: "admin",
Events: []instanceactions.Event{{
Event: "compute_stop_instance",
Host: "compute",
HostID: "2091634baaccdc4c5a1d57069c833e402921df696b7f970791b12ec6",
Result: "Success",
StartTime: time.Date(2018, 04, 25, 1, 26, 36, 0, time.UTC),
FinishTime: time.Date(2018, 04, 25, 1, 26, 36, 0, time.UTC),
Traceback: "",
}},
}

// HandleInstanceActionGetSuccessfully sets up the test server to respond to a Get request.
Expand All @@ -85,15 +95,27 @@ func HandleInstanceActionGetSuccessfully(t *testing.T) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, `{
"instanceAction":
{
"action": "stop",
"instance_uuid": "fcd19ef2-b593-40b1-90a5-fc31063fa95c",
"message": null,
"project_id": "6f70656e737461636b20342065766572",
"request_id": "req-f8a59f03-76dc-412f-92c2-21f8612be728",
"start_time": "2018-04-25T01:26:29.000000",
"user_id": "admin"
}
{
"action": "stop",
"events": [
{
"event": "compute_stop_instance",
"finish_time": "2018-04-25T01:26:36.00000",
"host": "compute",
"hostId": "2091634baaccdc4c5a1d57069c833e402921df696b7f970791b12ec6",
"result": "Success",
"start_time": "2018-04-25T01:26:36.00000",
"traceback": null
}
],
"instance_uuid": "4bf3473b-d550-4b65-9409-292d44ab14a2",
"message": null,
"project_id": "6f70656e737461636b20342065766572",
"request_id": "req-0d819d5c-1527-4669-bdf0-ffad31b5105b",
"start_time": "2018-04-25T01:26:36.00000",
"updated_at": "2018-04-25T01:26:36.00000",
"user_id": "admin"
}
}`)
})
}

0 comments on commit 91020b3

Please sign in to comment.