forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_response.go
144 lines (117 loc) · 3.17 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package http
import (
"encoding/json"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type Response interface {
Unmarshal([]byte) error
ServerError() error
}
type exception struct {
Message string
}
type SimpleTaskResponse struct {
Value string
Exception *exception
}
func (r *SimpleTaskResponse) ServerError() error {
if r.Exception != nil {
return bosherr.Errorf("Agent responded with error: %s", r.Exception.Message)
}
return nil
}
func (r *SimpleTaskResponse) Unmarshal(message []byte) error {
return json.Unmarshal(message, r)
}
type ListResponse struct {
Value []string
Exception *exception
}
func (r *ListResponse) ServerError() error {
if r.Exception != nil {
return bosherr.Errorf("Agent responded with error: %s", r.Exception.Message)
}
return nil
}
func (r *ListResponse) Unmarshal(message []byte) error {
return json.Unmarshal(message, r)
}
type BlobRef struct {
Name string `json:"name"`
Version string `json:"version"`
SHA1 string `json:"sha1"`
BlobstoreID string `json:"blobstore_id"`
}
type BlobResponse struct {
Value map[string]string
Exception *exception
}
func (r *BlobResponse) ServerError() error {
if r.Exception != nil {
return bosherr.Errorf("Agent responded with error: %s", r.Exception.Message)
}
return nil
}
func (r *BlobResponse) Unmarshal(message []byte) error {
return json.Unmarshal(message, r)
}
type StateResponse struct {
Value AgentState
Exception *exception
}
func (r *StateResponse) ServerError() error {
if r.Exception != nil {
return bosherr.Errorf("Agent responded with error: %s", r.Exception.Message)
}
return nil
}
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 *exception
}
func (r *TaskResponse) ServerError() error {
if r.Exception != nil {
return bosherr.Errorf("Agent responded with error: %s", r.Exception.Message)
}
return nil
}
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
}