Skip to content

Commit c69d192

Browse files
author
Ludovic Lamarche
committed
fix payload of get instance actions
1 parent 1931200 commit c69d192

2 files changed

Lines changed: 118 additions & 17 deletions

File tree

openstack/compute/v2/extensions/instanceactions/results.go

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,85 @@ func ExtractInstanceActions(r pagination.Page) ([]InstanceAction, error) {
5858
return resp, err
5959
}
6060

61+
// Event represents an event of instance action.
62+
type Event struct {
63+
Event string `json:"event"`
64+
// Host is the host of the event.
65+
// This requires microversion 2.62 or later.
66+
Host *string `json:"host"`
67+
// HostID is the host id of the event.
68+
// This requires microversion 2.62 or later.
69+
HostID *string `json:"hostId"`
70+
Result string `json:"result"`
71+
Traceback string `json:"traceback"`
72+
StartTime time.Time `json:"-"`
73+
FinishTime time.Time `json:"-"`
74+
}
75+
76+
// UnmarshalJSON converts our JSON API response into our instance action struct
77+
func (e *Event) UnmarshalJSON(b []byte) error {
78+
type tmp Event
79+
var s struct {
80+
tmp
81+
StartTime gophercloud.JSONRFC3339MilliNoZ `json:"start_time"`
82+
FinishTime gophercloud.JSONRFC3339MilliNoZ `json:"finish_time"`
83+
}
84+
err := json.Unmarshal(b, &s)
85+
if err != nil {
86+
return err
87+
}
88+
*e = Event(s.tmp)
89+
90+
e.StartTime = time.Time(s.StartTime)
91+
e.FinishTime = time.Time(s.FinishTime)
92+
93+
return err
94+
}
95+
96+
// InstanceActionDetail gives more details on instance action.
97+
type InstanceActionDetail struct {
98+
Action string `json:"action"`
99+
InstanceUUID string `json:"instance_uuid"`
100+
Message string `json:"message"`
101+
ProjectID string `json:"project_id"`
102+
RequestID string `json:"request_id"`
103+
UserID string `json:"user_id"`
104+
// Events is the list of events of the action.
105+
// This requires microversion 2.50 or later.
106+
Events *[]Event `json:"events"`
107+
// UpdatedAt last update date of the action.
108+
// This requires microversion 2.58 or later.
109+
UpdatedAt *time.Time `json:"-"`
110+
StartTime time.Time `json:"-"`
111+
}
112+
113+
// UnmarshalJSON converts our JSON API response into our instance action struct
114+
func (i *InstanceActionDetail) UnmarshalJSON(b []byte) error {
115+
type tmp InstanceActionDetail
116+
var s struct {
117+
tmp
118+
UpdatedAt *gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
119+
StartTime gophercloud.JSONRFC3339MilliNoZ `json:"start_time"`
120+
}
121+
err := json.Unmarshal(b, &s)
122+
if err != nil {
123+
return err
124+
}
125+
*i = InstanceActionDetail(s.tmp)
126+
127+
i.UpdatedAt = (*time.Time)(s.UpdatedAt)
128+
i.StartTime = time.Time(s.StartTime)
129+
return err
130+
}
131+
132+
// InstanceActionResult is the result handler of Get.
61133
type InstanceActionResult struct {
62134
gophercloud.Result
63135
}
64136

65-
// Extract interprets any instanceActionResult as an InstanceAction, if possible.
66-
func (r InstanceActionResult) Extract() (InstanceAction, error) {
67-
var s InstanceAction
137+
// Extract interprets any instanceActionResult as an InstanceActionDetail, if possible.
138+
func (r InstanceActionResult) Extract() (InstanceActionDetail, error) {
139+
var s InstanceActionDetail
68140
err := r.ExtractInto(&s)
69141
return s, err
70142
}

openstack/compute/v2/extensions/instanceactions/testing/fixtures.go

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var ListExpected = []instanceactions.InstanceAction{
3333
},
3434
}
3535

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

68+
var (
69+
expectedUpdateAt = time.Date(2018, 04, 25, 1, 26, 36, 0, time.UTC)
70+
expectedEventHost = "compute"
71+
expectedEventHostID = "2091634baaccdc4c5a1d57069c833e402921df696b7f970791b12ec6"
72+
expectedEvents = []instanceactions.Event{{
73+
Event: "compute_stop_instance",
74+
Host: &expectedEventHost,
75+
HostID: &expectedEventHostID,
76+
Result: "Success",
77+
StartTime: time.Date(2018, 04, 25, 1, 26, 36, 0, time.UTC),
78+
FinishTime: time.Date(2018, 04, 25, 1, 26, 36, 0, time.UTC),
79+
Traceback: "",
80+
}}
81+
)
82+
6883
// GetExpected represents an expected repsonse from a Get request.
69-
var GetExpected = instanceactions.InstanceAction{
84+
var GetExpected = instanceactions.InstanceActionDetail{
7085
Action: "stop",
71-
InstanceUUID: "fcd19ef2-b593-40b1-90a5-fc31063fa95c",
86+
InstanceUUID: "4bf3473b-d550-4b65-9409-292d44ab14a2",
7287
Message: "",
7388
ProjectID: "6f70656e737461636b20342065766572",
74-
RequestID: "req-f8a59f03-76dc-412f-92c2-21f8612be728",
75-
StartTime: time.Date(2018, 04, 25, 1, 26, 29, 000000, time.UTC),
89+
RequestID: "req-0d819d5c-1527-4669-bdf0-ffad31b5105b",
90+
StartTime: time.Date(2018, 04, 25, 1, 26, 36, 0, time.UTC),
91+
UpdatedAt: &expectedUpdateAt,
7692
UserID: "admin",
93+
Events: &expectedEvents,
7794
}
7895

7996
// HandleInstanceActionGetSuccessfully sets up the test server to respond to a Get request.
@@ -85,15 +102,27 @@ func HandleInstanceActionGetSuccessfully(t *testing.T) {
85102
w.Header().Add("Content-Type", "application/json")
86103
fmt.Fprintf(w, `{
87104
"instanceAction":
88-
{
89-
"action": "stop",
90-
"instance_uuid": "fcd19ef2-b593-40b1-90a5-fc31063fa95c",
91-
"message": null,
92-
"project_id": "6f70656e737461636b20342065766572",
93-
"request_id": "req-f8a59f03-76dc-412f-92c2-21f8612be728",
94-
"start_time": "2018-04-25T01:26:29.000000",
95-
"user_id": "admin"
96-
}
105+
{
106+
"action": "stop",
107+
"events": [
108+
{
109+
"event": "compute_stop_instance",
110+
"finish_time": "2018-04-25T01:26:36.00000",
111+
"host": "compute",
112+
"hostId": "2091634baaccdc4c5a1d57069c833e402921df696b7f970791b12ec6",
113+
"result": "Success",
114+
"start_time": "2018-04-25T01:26:36.00000",
115+
"traceback": null
116+
}
117+
],
118+
"instance_uuid": "4bf3473b-d550-4b65-9409-292d44ab14a2",
119+
"message": null,
120+
"project_id": "6f70656e737461636b20342065766572",
121+
"request_id": "req-0d819d5c-1527-4669-bdf0-ffad31b5105b",
122+
"start_time": "2018-04-25T01:26:36.00000",
123+
"updated_at": "2018-04-25T01:26:36.00000",
124+
"user_id": "admin"
125+
}
97126
}`)
98127
})
99128
}

0 commit comments

Comments
 (0)