-
Notifications
You must be signed in to change notification settings - Fork 8
/
operations.go
77 lines (62 loc) · 2.06 KB
/
operations.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
package scalingo
import (
"context"
"time"
"gopkg.in/errgo.v1"
httpclient "github.com/Scalingo/go-scalingo/v7/http"
)
type OperationsService interface {
OperationsShow(ctx context.Context, app, opID string) (*Operation, error)
}
var _ OperationsService = (*Client)(nil)
type OperationStatus string
const (
OperationStatusPending OperationStatus = "pending"
OperationStatusDone OperationStatus = "done"
OperationStatusRunning OperationStatus = "running"
OperationStatusError OperationStatus = "error"
)
type OperationType string
const (
OperationTypeScale OperationType = "scale"
OperationTypeStart OperationType = "start"
OperationTypeStartOneOff OperationType = "start-one-off"
)
type OperationResponse struct {
Op Operation `json:"operation"`
}
type OperationStartOneOffData struct {
AttachURL string `json:"attach_url"`
ContainerID string `json:"container_id"`
}
type Operation struct {
ID string `json:"id"`
AppID string `json:"app_id"`
CreatedAt time.Time `json:"created_at"`
FinishedAt time.Time `json:"finished_at"`
Status OperationStatus `json:"status"`
Type OperationType `json:"type"`
Error string `json:"error"`
StartOneOffData OperationStartOneOffData `json:"start_one_off_data"`
}
func (op *Operation) ElapsedDuration() float64 {
return op.FinishedAt.Sub(op.CreatedAt).Seconds()
}
func (c *Client) OperationsShowFromURL(ctx context.Context, url string) (*Operation, error) {
var opRes OperationResponse
err := c.ScalingoAPI().DoRequest(ctx, &httpclient.APIRequest{
Method: "GET", URL: url,
}, &opRes)
if err != nil {
return nil, errgo.Mask(err)
}
return &opRes.Op, nil
}
func (c *Client) OperationsShow(ctx context.Context, app, opID string) (*Operation, error) {
var opRes OperationResponse
err := c.ScalingoAPI().SubresourceGet(ctx, "apps", app, "operations", opID, nil, &opRes)
if err != nil {
return nil, errgo.Mask(err)
}
return &opRes.Op, nil
}