mhoppa commented on a change in pull request #3744: Rewrote jobs endpoints to go
URL: https://github.com/apache/trafficcontrol/pull/3744#discussion_r329240418
 
 

 ##########
 File path: 
traffic_ops/traffic_ops_golang/invalidationjobs/userinvalidationjobs.go
 ##########
 @@ -0,0 +1,202 @@
+package invalidationjobs
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import "encoding/json"
+import "fmt"
+import "net/http"
+import "strconv"
+import "time"
+
+import "github.com/apache/trafficcontrol/lib/go-tc"
+import "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+
+const userReadQuery = `
+SELECT job.agent,
+       job.asset_url,
+       job.asset_type,
+       (
+               SELECT tm_user.username
+               FROM tm_user
+               WHERE tm_user.id=$1
+       ) AS username,
+       (
+               SELECT deliveryservice.xml_id
+               FROM deliveryservice
+               WHERE deliveryservice.id=job.job_deliveryservice
+       ) AS deliveryservice,
+       job.entered_time,
+       job.id,
+       job.keyword,
+       job.object_name,
+       job.object_type,
+       job.parameters
+FROM job
+WHERE job.job_user=$1
+`
+
+type response struct {
+       Alerts []tc.Alert `json:"alerts"`
+       Response interface{} `json:"response"`
+}
+
+// Creates a new job for the current user (via POST request to 
`/user/current/jobs`)
+// this uses its own special format encoded in the tc.UserInvalidationJobInput 
structure
+func CreateUserJob(w http.ResponseWriter, r *http.Request) {
+       inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
+       if userErr != nil || sysErr != nil {
+               api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
+               return
+       }
+       defer inf.Close()
+
+       job := tc.UserInvalidationJobInput{}
+       if err := api.Parse(r.Body, inf.Tx.Tx, &job); err != nil {
+               api.HandleErr(w, r, inf.Tx.Tx, http.StatusBadRequest, err, 
fmt.Errorf("error parsing jobs POST body: %v", err))
+               return
+       }
+
+       if userErr, sysErr, errCode = IsUserAuthorizedToModifyDSID(inf, 
*job.DSID); userErr != nil || sysErr != nil {
+               api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
+               return
+       }
+
+       resultRow := inf.Tx.Tx.QueryRow(insertQuery,
+               job.DSID,
+               job.Regex,
+               time.Now(),
+               job.DSID,
+               inf.User.ID,
+               fmt.Sprintf("TTL:%dh", *job.TTL),
+               job.StartTime.Time)
+
+       result := tc.InvalidationJob{}
+       err := resultRow.Scan(&result.AssetURL,
+               &result.DeliveryService,
+               &result.ID,
+               &result.CreatedBy,
+               &result.Keyword,
+               &result.Parameters,
+               &result.StartTime)
+       if err != nil {
+               userErr, sysErr, code := api.ParseDBError(err)
+               api.HandleErr(w, r, inf.Tx.Tx, code, userErr, sysErr)
+               return
+       }
+
+       if err := setRevalFlags(*job.DSID, inf.Tx.Tx); err != nil {
+               api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, fmt.Errorf("setting reval flags: %v", err))
+       } else {
+               respObj := apiResponse{
+                       []tc.Alert{
+                               tc.Alert{
+                                       Level: tc.SuccessLevel.String(),
+                                       Text: "Invalidation Job creation was 
successful.",
+                               },
+                               api.DeprecationWarning("POST /jobs"),
+                       },
+                       result,
+               }
+               resp, err := json.Marshal(respObj)
+               if err != nil {
+                       api.HandleErr(w, r, inf.Tx.Tx, 
http.StatusInternalServerError, nil, fmt.Errorf("Marshaling JSON: %v", err))
 
 Review comment:
   return here?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to