forked from ForceCLI/force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulk.go
329 lines (268 loc) · 8.03 KB
/
bulk.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
/*
bulk command
force bulk insert mydata.csv
The load process involves these steps
1. Create a job
https://instance_name—api.salesforce.com/services/async/APIversion/job
payload:
<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<operation>insert</operation>
<object>Account</object>
<contentType>CSV</contentType>
</jobInfo>
2. Add batches to the created job
https://instance_name—api.salesforce.com/services/async/APIversion/job/jobid/batch
payload:
<sObjects xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<sObject>
<description>Created from Bulk API on Tue Apr 14 11:15:59 PDT 2009</description>
<name>[Bulk API] Account 0 (batch 0)</name>
</sObject>
<sObject>
<description>Created from Bulk API on Tue Apr 14 11:15:59 PDT 2009</description>
<name>[Bulk API] Account 1 (batch 0)</name>
</sObject>
</sObjects>
3. Close job (I assume this submits the job???)
https://instance_name—api.salesforce.com/services/async/APIversion/job/jobId
payload:
<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<state>Closed</state>
</jobInfo>
Jobs and batches can be monitored.
bulk command
force bulk job <jobId>
bulk command
force bulk batches <jobId>
bulk command
force bulk batch <batchId>
*/
import (
"encoding/xml"
"fmt"
"io/ioutil"
)
var cmdBulk = &Command{
Run: runBulk,
Usage: "bulk insert Account [csv file]",
Short: "Load csv file use Bulk API",
Long: `
Load csv file use Bulk API
Examples:
force bulk insert Account [csv file]
force bulk update Account [csv file]
force bulk job [job id]
force bulk batches [job id]
force bulk batch [job id] [batch id]
force bulk batch retrieve [job id] [batch id]
force bulk query Account [SOQL]
force bulk query retrieve [job id] [batch id]
`,
}
func runBulk(cmd *Command, args []string) {
if len(args) == 1 {
ErrorAndExit("Invalid command")
} else if len(args) == 2 {
if args[0] == "insert" {
ErrorAndExit("Missing argument for insert")
} else if args[0] == "job" {
showJobDetails(args[1])
} else if args[0] == "batches" {
listBatches(args[1])
} else {
ErrorAndExit("Invalid command")
}
} else if len(args) == 3 {
if args[0] == "insert" {
createBulkInsertJob(args[2], args[1], "CSV")
} else if args[0] == "update" {
createBulkUpdateJob(args[2], args[1], "CSV")
} else if args[0] == "batch" {
showBatchDetails(args[1], args[2])
} else if args[0] == "query" {
if args[1] == "retrieve" {
ErrorAndExit("Query retrieve requires a job id and a batch id")
} else {
doBulkQuery(args[1], args[2], "CSV")
}
}
} else if len(args) == 4 {
if args[0] == "insert" {
createBulkInsertJob(args[2], args[1], args[3])
} else if args[0] == "update" {
createBulkUpdateJob(args[2], args[1], args[3])
} else if args[0] == "batch" {
getBatchResults(args[2], args[3])
} else if args[0] == "query" {
if args[1] == "retrieve" {
fmt.Println(string(getBulkQueryResults(args[2], args[3])))
} else if args[1] == "status" {
DisplayBatchInfo(getBatchDetails(args[2], args[3]))
} else {
doBulkQuery(args[1], args[2], args[3])
}
}
}
}
func doBulkQuery(objectType string, soql string, contenttype string) {
jobInfo, err := createBulkJob(objectType, "query", contenttype)
force, _ := ActiveForce()
result, err := force.BulkQuery(soql, jobInfo.Id, contenttype)
if err != nil {
closeBulkJob(jobInfo.Id)
ErrorAndExit(err.Error())
}
fmt.Println("Query Submitted")
fmt.Printf("To retrieve query status use\nforce bulk query status %s %s\n\n", jobInfo.Id, result.Id)
fmt.Printf("To retrieve query data use\nforce bulk query retrieve %s %s\n\n", jobInfo.Id, result.Id)
closeBulkJob(jobInfo.Id)
}
func getBulkQueryResults(jobId string, batchId string) (data []byte) {
resultIds := retrieveBulkQuery(jobId, batchId)
hasMultipleResultFiles := len(resultIds) > 1
for _, resultId := range resultIds {
//since this is going to stdOut, simply add header to separate "files"
//if it's all in the same file, don't print this separator.
if hasMultipleResultFiles {
resultHeader := fmt.Sprint("ResultId: ", resultId, "\n")
data = append(data[:], []byte(resultHeader)...)
}
//get next file, and append
var newData []byte = retrieveBulkQueryResults(jobId, batchId, resultId)
data = append(data[:], newData...)
}
return
}
func retrieveBulkQuery(jobId string, batchId string) (resultIds []string) {
force, _ := ActiveForce()
jobInfo, err := force.RetrieveBulkQuery(jobId, batchId)
if err != nil {
ErrorAndExit(err.Error())
}
var resultList struct {
Results []string `xml:"result"`
}
xml.Unmarshal(jobInfo, &resultList)
resultIds = resultList.Results
return
}
func retrieveBulkQueryResults(jobId string, batchId string, resultId string) (data []byte) {
force, _ := ActiveForce()
data, err := force.RetrieveBulkQueryResults(jobId, batchId, resultId)
if err != nil {
ErrorAndExit(err.Error())
}
return
}
func showJobDetails(jobId string) {
jobInfo := getJobDetails(jobId)
DisplayJobInfo(jobInfo)
}
func listBatches(jobId string) {
batchInfos := getBatches(jobId)
DisplayBatchList(batchInfos)
}
func showBatchDetails(jobId string, batchId string) {
batchInfo := getBatchDetails(jobId, batchId)
DisplayBatchInfo(batchInfo)
}
func getBatchResults(jobId string, batchId string) {
force, _ := ActiveForce()
data, err := force.RetrieveBulkBatchResults(jobId, batchId)
fmt.Println(data)
if err != nil {
ErrorAndExit(err.Error())
}
return
}
func getJobDetails(jobId string) (jobInfo JobInfo) {
force, _ := ActiveForce()
jobInfo, err := force.GetJobInfo(jobId)
if err != nil {
ErrorAndExit(err.Error())
}
return
}
func getBatches(jobId string) (batchInfos []BatchInfo) {
force, _ := ActiveForce()
batchInfos, err := force.GetBatches(jobId)
if err != nil {
ErrorAndExit(err.Error())
}
return
}
func getBatchDetails(jobId string, batchId string) (batchInfo BatchInfo) {
force, _ := ActiveForce()
batchInfo, err := force.GetBatchInfo(jobId, batchId)
if err != nil {
ErrorAndExit(err.Error())
}
return
}
func createBulkInsertJob(csvFilePath string, objectType string, format string) {
jobInfo, err := createBulkJob(objectType, "insert", format)
if err != nil {
ErrorAndExit(err.Error())
} else {
batchInfo, err := addBatchToJob(csvFilePath, jobInfo.Id)
if err != nil {
closeBulkJob(jobInfo.Id)
ErrorAndExit(err.Error())
} else {
closeBulkJob(jobInfo.Id)
fmt.Printf("Job created ( %s ) - for job status use\n force bulk batch %s %s\n", jobInfo.Id, jobInfo.Id, batchInfo.Id)
}
}
}
func createBulkUpdateJob(csvFilePath string, objectType string, format string) {
jobInfo, err := createBulkJob(objectType, "update", format)
if err != nil {
ErrorAndExit(err.Error())
} else {
_, err := addBatchToJob(csvFilePath, jobInfo.Id)
if err != nil {
closeBulkJob(jobInfo.Id)
ErrorAndExit(err.Error())
} else {
closeBulkJob(jobInfo.Id)
}
}
}
func addBatchToJob(csvFilePath string, jobId string) (result BatchInfo, err error) {
force, _ := ActiveForce()
filedata, err := ioutil.ReadFile(csvFilePath)
result, err = force.AddBatchToJob(string(filedata), jobId)
return
}
func getBatchInfo(jobId string, batchId string) (batchInfo BatchInfo, err error) {
force, _ := ActiveForce()
batchInfo, err = force.GetBatchInfo(jobId, batchId)
return
}
func createBulkJob(objectType string, operation string, fileFormat string) (jobInfo JobInfo, err error) {
force, _ := ActiveForce()
xml := `
<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<operation>%s</operation>
<object>%s</object>
<contentType>%s</contentType>
</jobInfo>
`
data := fmt.Sprintf(xml, operation, objectType, fileFormat)
jobInfo, err = force.CreateBulkJob(data)
return
}
func closeBulkJob(jobId string) (jobInfo JobInfo, err error) {
force, _ := ActiveForce()
xml := `
<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<state>Closed</state>
</jobInfo>
`
jobInfo, err = force.CloseBulkJob(jobId, xml)
if err != nil {
ErrorAndExit(err.Error())
}
return
}