Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get instance action fix #1851

Merged
merged 1 commit into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 75 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,85 @@ 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 is the host of the event.
// This requires microversion 2.62 or later.
Host *string `json:"host"`
// HostID is the host id of the event.
// This requires microversion 2.62 or later.
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 is the list of events of the action.
// This requires microversion 2.50 or later.
Events *[]Event `json:"events"`
// UpdatedAt last update date of the action.
// This requires microversion 2.58 or later.
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
57 changes: 43 additions & 14 deletions openstack/compute/v2/extensions/instanceactions/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var ListExpected = []instanceactions.InstanceAction{
},
}

// HandleAddressListSuccessfully sets up the test server to respond to a ListAddresses request.
// HandleInstanceActionListSuccessfully sets up the test server to respond to a ListAddresses request.
func HandleInstanceActionListSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/servers/asdfasdfasdf/os-instance-actions", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
Expand Down Expand Up @@ -65,15 +65,32 @@ func HandleInstanceActionListSuccessfully(t *testing.T) {
})
}

var (
expectedUpdateAt = time.Date(2018, 04, 25, 1, 26, 36, 0, time.UTC)
expectedEventHost = "compute"
expectedEventHostID = "2091634baaccdc4c5a1d57069c833e402921df696b7f970791b12ec6"
expectedEvents = []instanceactions.Event{{
Event: "compute_stop_instance",
Host: &expectedEventHost,
HostID: &expectedEventHostID,
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: "",
}}
)

// 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: &expectedUpdateAt,
UserID: "admin",
Events: &expectedEvents,
}

// HandleInstanceActionGetSuccessfully sets up the test server to respond to a Get request.
Expand All @@ -85,15 +102,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"
}
}`)
})
}