@@ -12,6 +12,8 @@ import (
12
12
"github.com/morikuni/failure"
13
13
)
14
14
15
+ const DefaultSeparateKindCount = 30
16
+
15
17
type DatastoreExportRequest struct {
16
18
ProjectID string `json:"projectId"`
17
19
AllKinds bool `json:"allKinds"`
@@ -33,94 +35,62 @@ type DS2BQJobIDWithDatastoreExportJobID struct {
33
35
DatastoreExportJobID string `json:"datastoreExportJobId"`
34
36
}
35
37
36
- func HandleDatastoreExportAPI (w http.ResponseWriter , r * http.Request ) {
37
- queue , err := NewDatastoreExportJobCheckQueue (r .Host , TasksClient )
38
- if err != nil {
39
- msg := fmt .Sprintf ("failed NewDatastoreExportJobCheckQueue.err=%+v" , err )
40
- log .Println (msg )
41
- w .Header ().Set ("Content-Type" , "text/plain; charset=utf-8" )
42
- w .WriteHeader (http .StatusBadRequest )
43
- _ , err := w .Write ([]byte (msg ))
44
- if err != nil {
45
- log .Println (err )
46
- }
47
- return
38
+ type DatastoreExportAPI struct {
39
+ DatastoreExportJobCheckQueue * DatastoreExportJobCheckQueue
40
+ DSExportJobStore * DSExportJobStore
41
+ BQLoadJobStore * BQLoadJobStore
42
+ }
43
+
44
+ func NewDatastoreExportAPI (queue * DatastoreExportJobCheckQueue , dseJS * DSExportJobStore , bqlJS * BQLoadJobStore ) * DatastoreExportAPI {
45
+ return & DatastoreExportAPI {
46
+ queue , dseJS , bqlJS ,
48
47
}
48
+ }
49
49
50
+ func HandleDatastoreExportAPI (w http.ResponseWriter , r * http.Request ) {
50
51
body , err := ioutil .ReadAll (r .Body )
51
52
if err != nil {
52
- msg := fmt .Sprintf ("failed ioutil.Read(request.Body).err=%+v" , err )
53
- log .Println (msg )
54
- w .Header ().Set ("Content-Type" , "text/plain; charset=utf-8" )
55
- w .WriteHeader (http .StatusBadRequest )
56
- _ , err := w .Write ([]byte (msg ))
57
- if err != nil {
58
- log .Println (err )
59
- }
53
+ WriteError (w , http .StatusBadRequest , "failed ioutil.Read(request.Body)" , err )
60
54
return
61
55
}
62
56
63
57
form := & DatastoreExportRequest {}
64
58
if err := json .Unmarshal (body , form ); err != nil {
65
- msg := fmt .Sprintf ("failed json.Unmarshal(request.Body).err=%+v" , err )
66
- log .Println (msg )
67
- w .Header ().Set ("Content-Type" , "text/plain; charset=utf-8" )
68
- w .WriteHeader (http .StatusBadRequest )
69
- _ , err := w .Write ([]byte (msg ))
70
- if err != nil {
71
- log .Println (err )
72
- }
59
+ WriteError (w , http .StatusBadRequest , fmt .Sprintf ("failed json.Unmarshal(request.Body) body=%v" , string (body )), err )
73
60
return
74
61
}
75
62
76
63
log .Printf ("%s\n " , string (body ))
77
64
78
65
kinds , err := GetDatastoreKinds (r .Context (), form )
79
66
if err != nil {
80
- msg := fmt .Sprintf ("failed GetDatastoreKinds form=%+v.err=%+v" , form , err )
81
- log .Println (msg )
82
- w .WriteHeader (http .StatusInternalServerError )
83
- _ , err := w .Write ([]byte (msg ))
84
- if err != nil {
85
- log .Println (err )
86
- }
67
+ WriteError (w , http .StatusBadRequest , fmt .Sprintf ("failed GetDatastoreKinds form=%+v" , form ), err )
87
68
return
88
69
}
89
- efs , err := BuildEntityFilter (r .Context (), form .NamespaceIDs , kinds , 30 )
70
+ efs , err := BuildEntityFilter (r .Context (), form .NamespaceIDs , kinds , DefaultSeparateKindCount )
90
71
if err != nil {
91
- msg := fmt .Sprintf ("failed BuildEntityFilter form=%+v.err=%+v " , form , err )
92
- log . Println ( msg )
93
- w . WriteHeader ( http . StatusInternalServerError )
94
- _ , err := w . Write ([] byte ( msg ))
95
- if err != nil {
96
- log . Println ( err )
97
- }
72
+ WriteError ( w , http . StatusBadRequest , fmt .Sprintf ("failed BuildEntityFilter form=%+v" , form ) , err )
73
+ return
74
+ }
75
+
76
+ queue , err := NewDatastoreExportJobCheckQueue ( r . Host , TasksClient )
77
+ if err != nil {
78
+ WriteError ( w , http . StatusBadRequest , "failed NewDatastoreExportJobCheckQueue" , err )
98
79
return
99
80
}
100
81
101
82
dsexportJobStore , err := NewDSExportJobStore (r .Context (), DatastoreClient )
102
83
if err != nil {
103
- msg := fmt .Sprintf ("failed NewDSExportJobStore() form=%+v.err=%+v" , form , err )
104
- log .Println (msg )
105
- w .WriteHeader (http .StatusInternalServerError )
106
- _ , err := w .Write ([]byte (msg ))
107
- if err != nil {
108
- log .Println (err )
109
- }
84
+ WriteError (w , http .StatusInternalServerError , fmt .Sprintf ("failed NewDSExportJobStore() form=%+v" , form ), err )
110
85
return
111
86
}
112
87
113
88
bqloadJobStore , err := NewBQLoadJobStore (r .Context (), DatastoreClient )
114
89
if err != nil {
115
- msg := fmt .Sprintf ("failed NewBQLoadJobStore() form=%+v.err=%+v" , form , err )
116
- log .Println (msg )
117
- w .WriteHeader (http .StatusInternalServerError )
118
- _ , err := w .Write ([]byte (msg ))
119
- if err != nil {
120
- log .Println (err )
121
- }
90
+ WriteError (w , http .StatusInternalServerError , fmt .Sprintf ("failed NewBQLoadJobStore() form=%+v" , form ), err )
122
91
return
123
92
}
93
+ api := NewDatastoreExportAPI (queue , dsexportJobStore , bqloadJobStore )
124
94
125
95
res := & DatastoreExportResponse {
126
96
[]* DS2BQJobIDWithDatastoreExportJobID {},
@@ -129,7 +99,7 @@ func HandleDatastoreExportAPI(w http.ResponseWriter, r *http.Request) {
129
99
var dsExportJobID string
130
100
ds2bqJobID := dsexportJobStore .NewDS2BQJobID (r .Context ())
131
101
bqLoadKinds := BuildBQLoadKinds (ef , form .IgnoreBQLoadKinds )
132
- dsExportJobID , err := CreateDatastoreExportJob (r .Context (), dsexportJobStore , bqloadJobStore , queue , ds2bqJobID , string (body ), form , bqLoadKinds , ef )
102
+ dsExportJobID , err := api . StartDS2BQJob (r .Context (), ds2bqJobID , string (body ), form , form . NamespaceIDs , bqLoadKinds , ef )
133
103
if err != nil {
134
104
msg := fmt .Sprintf ("failed CreateDatastoreExportJob ds2bqJobID=%v.err=%+v" , ds2bqJobID , err )
135
105
log .Println (msg )
@@ -153,41 +123,45 @@ func HandleDatastoreExportAPI(w http.ResponseWriter, r *http.Request) {
153
123
}
154
124
}
155
125
156
- func CreateDatastoreExportJob ( ctx context.Context , dsexportJobStore * DSExportJobStore , bqloadJobStore * BQLoadJobStore , queue * DatastoreExportJobCheckQueue , ds2bqJobID string , body string , form * DatastoreExportRequest , kinds []string , ef * datastore.EntityFilter ) (string , error ) {
157
- _ , err := dsexportJobStore . Create (ctx , ds2bqJobID , body , kinds )
126
+ func ( api * DatastoreExportAPI ) StartDS2BQJob ( ctx context.Context , ds2bqJobID string , body string , form * DatastoreExportRequest , namespaceIDs [] string , kinds []string , ef * datastore.EntityFilter ) (string , error ) {
127
+ _ , err := api . DSExportJobStore . Create (ctx , ds2bqJobID , body , form . ProjectID , namespaceIDs , kinds )
158
128
if err != nil {
159
129
return "" , fmt .Errorf ("failed DSExportJobStore.Create() ds2bqJobID=%v.err=%+v" , ds2bqJobID , err )
160
130
}
161
131
162
- _ , err = bqloadJobStore .PutMulti (ctx , BuildBQLoadJobPutMultiForm (ds2bqJobID , kinds , form ))
132
+ _ , err = api . BQLoadJobStore .PutMulti (ctx , BuildBQLoadJobPutMultiForm (ds2bqJobID , kinds , form ))
163
133
if err != nil {
164
134
return "" , fmt .Errorf ("failed BQLoadJobStore.PutMulti() ds2bqJobID=%v,bqLoadKinds=%+v.err=%+v" , ds2bqJobID , kinds , err )
165
135
}
166
136
167
- ope , err := datastore .Export (ctx , form .ProjectID , form .OutputGCSFilePath , ef )
137
+ return api .CreateDatastoreExportJob (ctx , ds2bqJobID , form .ProjectID , form .OutputGCSFilePath , ef )
138
+ }
139
+
140
+ func (api * DatastoreExportAPI ) CreateDatastoreExportJob (ctx context.Context , ds2bqJobID string , projectID string , outputGCSFilePath string , ef * datastore.EntityFilter ) (string , error ) {
141
+ ope , err := datastore .Export (ctx , projectID , outputGCSFilePath , ef )
168
142
if err != nil {
169
- return "" , fmt .Errorf ("failed datastore.Export() form=%+v. err=%+v" , form , err )
143
+ return "" , fmt .Errorf ("failed datastore.Export() err=%+v" , err )
170
144
}
171
145
switch ope .HTTPStatusCode {
172
146
case http .StatusOK :
173
147
log .Printf ("%+v" , ope )
174
148
175
- if _ , err := dsexportJobStore . StartExportJob (ctx , ds2bqJobID , ope .Name ); err != nil {
149
+ if _ , err := api . DSExportJobStore . StartExportJob (ctx , ds2bqJobID , ope .Name , 0 ); err != nil {
176
150
return "" , fmt .Errorf ("failed DSExportJobStore.StartExportJob. ds2bqJobID=%v,jobName=%s.err=%+v" , ds2bqJobID , ope .Name , err )
177
151
}
178
152
179
- if err := queue .AddTask (ctx , & DatastoreExportJobCheckRequest {
153
+ if err := api . DatastoreExportJobCheckQueue .AddTask (ctx , & DatastoreExportJobCheckRequest {
180
154
DS2BQJobID : ds2bqJobID ,
181
155
DatastoreExportJobID : ope .Name ,
182
156
}); err != nil {
183
157
return "" , fmt .Errorf ("failed queue.AddTask. jobName=%s.err=%+v" , ope .Name , err )
184
158
}
185
159
return ope .Name , nil
186
160
default :
187
- if _ , err := dsexportJobStore . FinishExportJob (ctx , ds2bqJobID , DSExportJobStatusFailed , fmt .Sprintf ("failed DatastoreExportJob.INSERT(). Code=%v,Message=%v" , ope .Error .Code , ope .Error .Message )); err != nil {
161
+ if _ , err := api . DSExportJobStore . FinishExportJob (ctx , ds2bqJobID , DSExportJobStatusFailed , "" , fmt .Sprintf ("failed DatastoreExportJob.INSERT(). Code=%v,Message=%v" , ope .Error .Code , ope .Error .Message )); err != nil {
188
162
return "" , fmt .Errorf ("failed DSExportJobStore.FinishExportJob. ds2bqJobID=%v.err=%+v" , ds2bqJobID , err )
189
163
}
190
- return "" , fmt .Errorf ("failed DatastoreExportJob.INSERT(). form=%+v. ope.Error=%+v" , form , ope .Error )
164
+ return "" , fmt .Errorf ("failed DatastoreExportJob.INSERT(). ds2bqJobID=%v, ope.Error=%+v" , ds2bqJobID , ope .Error )
191
165
}
192
166
}
193
167
0 commit comments