forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_response.go
113 lines (89 loc) · 2.46 KB
/
agent_response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package http
import (
"encoding/json"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
)
type Response interface {
Unmarshal([]byte) error
GetException() exceptionResponse
}
type exceptionResponse struct {
Message string
}
func (r exceptionResponse) IsEmpty() bool {
return r == exceptionResponse{}
}
type SimpleTaskResponse struct {
Value string
Exception exceptionResponse
}
func (r *SimpleTaskResponse) GetException() exceptionResponse {
return r.Exception
}
func (r *SimpleTaskResponse) Unmarshal(message []byte) error {
return json.Unmarshal(message, r)
}
type ListResponse struct {
Value []string
Exception exceptionResponse
}
func (r *ListResponse) GetException() exceptionResponse {
return r.Exception
}
func (r *ListResponse) Unmarshal(message []byte) error {
return json.Unmarshal(message, r)
}
type StateResponse struct {
Value AgentState
Exception exceptionResponse
}
func (r *StateResponse) GetException() exceptionResponse {
return r.Exception
}
func (r *StateResponse) Unmarshal(message []byte) error {
return json.Unmarshal(message, r)
}
type AgentState struct {
JobState string `json:"job_state"`
}
type TaskResponse struct {
Value interface{}
Exception exceptionResponse
}
func (r *TaskResponse) GetException() exceptionResponse {
return r.Exception
}
func (r *TaskResponse) Unmarshal(message []byte) error {
return json.Unmarshal(message, r)
}
func (r *TaskResponse) TaskID() (string, error) {
complexResponse, ok := r.Value.(map[string]interface{})
if !ok {
return "", bosherr.Errorf("Failed to convert agent response to map %#v", r.Value)
}
agentTaskID, ok := complexResponse["agent_task_id"]
if !ok {
return "", bosherr.Errorf("Failed to parse task id from agent response %#v", r.Value)
}
return agentTaskID.(string), nil
}
// TaskState returns the state of the task reported by agent.
//
// Agent response to get_task can be in different format based on task state.
// If task state is running agent responds
// with value as { agent_task_id: "task-id", state: "running" }
// Otherwise the value is a string like "stopped".
func (r *TaskResponse) TaskState() (string, error) {
complexResponse, ok := r.Value.(map[string]interface{})
if ok {
_, ok := complexResponse["agent_task_id"]
if ok {
taskState, ok := complexResponse["state"]
if ok {
return taskState.(string), nil
}
return "", bosherr.Errorf("Failed to parse task state from agent response %#v", r.Value)
}
}
return "finished", nil
}