This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
87 lines (72 loc) · 1.83 KB
/
job.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
package job
import (
"encoding/json"
"fmt"
"github.com/VeeamHub/martini-cli/core"
)
type MartiniJob struct {
Id string `json:"id"`
Name string `json:"name"`
LastRun string `json:"lastRun"`
LastStatus string `json:"lastStatus"`
}
type MartiniJobIdentification struct {
Id string `json:"id,omitempty"`
JobId string `json:"jobid,omitempty"`
Data string `json:"data,omitempty"`
}
func List(conn *core.Connection, instance string) ([]MartiniJob, error) {
var arr []MartiniJob
var err error
idjson, _ := json.Marshal(MartiniJobIdentification{Id: instance})
txt, sc, rerr := conn.Post("job/list", idjson)
if rerr == nil {
if sc != 200 {
rc := core.ReturnStatus{}
json.Unmarshal(txt, &rc)
err = fmt.Errorf("Not valid return code %d on job list; content [%s]", sc, rc.Status)
} else {
err = json.Unmarshal(txt, &arr)
if err != nil {
err = fmt.Errorf("Unexpected output server job list %s (%v)", txt, err)
}
}
} else {
err = rerr
}
return arr, err
}
func Resolve(conn *core.Connection, instance string, jobname string) (string, error) {
//job.Resolve(conn, id, jobname)
var err error
var id = "-1"
jobs, rerr := List(conn, instance)
if rerr == nil {
for _, j := range jobs {
if j.Name == jobname {
id = j.Id
}
}
} else {
err = rerr
}
if id == "-1" {
err = fmt.Errorf("Was not able to resolve id for %s", jobname)
}
return id, err
}
func Start(conn *core.Connection, instance string, jobid string) error {
var err error
idjson, _ := json.Marshal(MartiniJobIdentification{Id: instance, JobId: jobid})
txt, sc, rerr := conn.Post("job/start", idjson)
if rerr == nil {
if sc != 200 {
rc := core.ReturnStatus{}
json.Unmarshal(txt, &rc)
err = fmt.Errorf("Not valid return code %d on job start; content [%s]", sc, rc.Status)
}
} else {
err = rerr
}
return err
}