/
job.go
174 lines (158 loc) · 5.72 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"strconv"
"github.com/apache/trafficcontrol/lib/go-tc"
)
// Creates a new Content Invalidation Job
func (to *Session) CreateInvalidationJob(job tc.InvalidationJobInput) (tc.Alerts, ReqInf, error) {
remoteAddr := (net.Addr)(nil)
reqBody, err := json.Marshal(job)
reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
if err != nil {
return tc.Alerts{}, reqInf, err
}
resp, remoteAddr, err := to.request(http.MethodPost, apiBase+`/jobs`, reqBody)
if err != nil {
return tc.Alerts{}, reqInf, err
}
defer resp.Body.Close()
var alerts tc.Alerts
err = json.NewDecoder(resp.Body).Decode(&alerts)
return alerts, reqInf, err
}
// GetJobs returns a list of Jobs.
// If deliveryServiceID or userID are not nil, only jobs for that delivery service or belonging to
// that user are returned. Both deliveryServiceID and userID may be nil.
//
// Deprecated, use GetInvalidationJobs instead
func (to *Session) GetJobs(deliveryServiceID *int, userID *int) ([]tc.Job, ReqInf, error) {
path := apiBase + "/jobs"
if deliveryServiceID != nil || userID != nil {
path += "?"
if deliveryServiceID != nil {
path += "dsId=" + strconv.Itoa(*deliveryServiceID)
if userID != nil {
path += "&"
}
}
if userID != nil {
path += "userId=" + strconv.Itoa(*userID)
}
}
resp, remoteAddr, err := to.request(http.MethodGet, path, nil)
reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
if err != nil {
return nil, reqInf, err
}
defer resp.Body.Close()
data := struct {
Response []tc.Job `json:"response"`
}{}
err = json.NewDecoder(resp.Body).Decode(&data)
return data.Response, reqInf, err
}
// Returns a list of Content Invalidation Jobs visible to your Tenant, filtered according to
// ds and user
//
// Either or both of ds and user may be nil, but when they are not they cause filtering of the
// returned jobs by Delivery Service and Traffic Ops user, respectively.
//
// ds may be a uint, int, or float64 indicating the integral, unique identifier of the desired
// Delivery Service (in the case of a float64 the fractional part is dropped, e.g. 3.45 -> 3), or
// it may be a string, in which case it should be the xml_id of the desired Delivery Service, or it
// may be an actual tc.DeliveryService or tc.DeliveryServiceNullable structure.
//
// Likewise, user may be a uint, int or float64 indicating the integral, unique identifier of the
// desired user (in the case of a float64 the fractional part is dropped, e.g. 3.45 -> 3), or it may
// be a string, in which case it should be the username of the desired user, or it may be an actual
// tc.User or tc.UserCurrent structure.
func (to *Session) GetInvalidationJobs(ds *interface{}, user *interface{}) ([]tc.InvalidationJob, ReqInf, error) {
path := apiBase + "/jobs"
if ds != nil || user != nil {
path += "?"
if ds != nil {
d := *ds
switch t := d.(type) {
case uint:
path += "dsId=" + strconv.FormatUint(uint64(d.(uint)), 10)
case float64:
path += "dsId=" + strconv.FormatInt(int64(d.(float64)), 10)
case int:
path += "dsId=" + strconv.FormatInt(int64(d.(int)), 10)
case string:
path += "deliveryService=" + d.(string)
case tc.DeliveryServiceNullable:
if d.(tc.DeliveryServiceNullable).XMLID != nil {
path += "deliveryService=" + *d.(tc.DeliveryServiceNullable).XMLID
} else if d.(tc.DeliveryServiceNullable).ID != nil {
path += "dsId=" + strconv.FormatInt(int64(*d.(tc.DeliveryServiceNullable).ID), 10)
} else {
return nil, ReqInf{}, errors.New("No non-nil identifier on passed Delivery Service!")
}
default:
return nil, ReqInf{}, fmt.Errorf("Invalid type for argument 'ds': %T*", t)
}
if user != nil {
path += "&"
}
}
if user != nil {
u := *user
switch t := u.(type) {
case uint:
path += "userId=" + strconv.FormatUint(uint64(u.(uint)), 10)
case float64:
path += "userId=" + strconv.FormatInt(int64(u.(float64)), 10)
case int:
path += "userId=" + strconv.FormatInt(int64(u.(int64)), 10)
case string:
path += "createdBy=" + u.(string)
case tc.User:
if u.(tc.User).Username != nil {
path += "createdBy=" + *u.(tc.User).Username
} else if u.(tc.User).ID != nil {
path += "userId=" + strconv.FormatInt(int64(*u.(tc.User).ID), 10)
} else {
return nil, ReqInf{}, errors.New("No non-nil identifier on passed User!")
}
case tc.UserCurrent:
if u.(tc.UserCurrent).UserName != nil {
path += "createdBy=" + *u.(tc.UserCurrent).UserName
} else if u.(tc.UserCurrent).ID != nil {
path += "userId=" + strconv.FormatInt(int64(*u.(tc.UserCurrent).ID), 10)
} else {
return nil, ReqInf{}, errors.New("No non-nil identifier on passed UserCurrent!")
}
default:
return nil, ReqInf{}, fmt.Errorf("Invalid type for argument 'user': %T*", t)
}
}
}
resp, remoteAddr, err := to.request(http.MethodGet, path, nil)
reqInf := ReqInf{CacheHitStatusMiss, remoteAddr}
if err != nil {
return nil, reqInf, err
}
defer resp.Body.Close()
data := struct {
Response []tc.InvalidationJob `json:"response"`
}{}
err = json.NewDecoder(resp.Body).Decode(&data)
return data.Response, reqInf, err
}