You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2019/12/13 21:43:15 UTC
[GitHub] [trafficcontrol] rawlinp commented on a change in pull request
#4204: Fixes #3813 implement `/profiles/name/{{name}}/copy/{{fromName}}` in
Go
rawlinp commented on a change in pull request #4204: Fixes #3813 implement `/profiles/name/{{name}}/copy/{{fromName}}` in Go
URL: https://github.com/apache/trafficcontrol/pull/4204#discussion_r357838567
##########
File path: traffic_ops/traffic_ops_golang/profile/copy.go
##########
@@ -0,0 +1,181 @@
+package profile
+
+/*
+ * 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 (
+ "fmt"
+ "net/http"
+
+ "github.com/apache/trafficcontrol/lib/go-log"
+ "github.com/apache/trafficcontrol/lib/go-tc"
+ "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+ "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/profileparameter"
+)
+
+type errorDetails struct {
+ userErr error
+ sysErr error
+ errCode int
+}
+
+// CopyProfileHandler creates a new profile and parameters from an existing profile.
+func CopyProfileHandler(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()
+
+ p := tc.ProfileCopyResponse{
+ Response: tc.ProfileCopy{
+ ExistingName: inf.Params["existing_profile"],
+ Name: inf.Params["new_profile"],
+ },
+ }
+
+ errs := copyProfile(inf, &p.Response)
+ if errs.userErr != nil || errs.sysErr != nil {
+ api.HandleErr(w, r, inf.Tx.Tx, errs.errCode, errs.userErr, errs.sysErr)
+ return
+ }
+
+ errs = copyParameters(inf, &p.Response)
+ if errs.userErr != nil || errs.sysErr != nil {
+ api.HandleErr(w, r, inf.Tx.Tx, errs.errCode, errs.userErr, errs.sysErr)
+ return
+ }
+
+ successMsg := fmt.Sprintf("created new profile [%s] from existing profile [%s]", p.Response.Name, p.Response.ExistingName)
+ api.CreateChangeLogRawTx(api.ApiChange, successMsg, inf.User, inf.Tx.Tx)
+ api.WriteRespAlertObj(w, r, tc.SuccessLevel, successMsg, p.Response)
+}
+
+func copyProfile(inf *api.APIInfo, p *tc.ProfileCopy) errorDetails {
+ // check if the newPrfoile already exists
+ ok, err := tc.ProfileExistsByName(p.Name, inf.Tx.Tx)
+ if ok {
+ return errorDetails{
+ userErr: fmt.Errorf("profile with name %s already exists", p.Name),
+ errCode: http.StatusBadRequest,
+ }
+ }
+ if err != nil {
+ return errorDetails{
+ userErr: err,
Review comment:
I think this was meant to be `sysErr` instead, since the errCode is internal server error?
----------------------------------------------------------------
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:
users@infra.apache.org
With regards,
Apache Git Services