This repository was archived by the owner on Nov 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 356
Add TO Go put deliveryservices/id/safe #2455
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package v14 | ||
|
|
||
| /* | ||
|
|
||
| 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. | ||
| */ | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "testing" | ||
|
|
||
| "github.com/apache/trafficcontrol/lib/go-tc" | ||
| "github.com/apache/trafficcontrol/lib/go-util" | ||
| ) | ||
|
|
||
| func TestPutDeliveryServiceSafe(t *testing.T) { | ||
| WithObjs(t, []TCObj{CDNs, Types, Tenants, Users, Parameters, Profiles, Statuses, Divisions, Regions, PhysLocations, CacheGroups, Servers, DeliveryServices}, func() { | ||
| UpdateTestDeliveryServiceSafe(t) | ||
| }) | ||
| } | ||
|
|
||
| func UpdateTestDeliveryServiceSafe(t *testing.T) { | ||
| firstDS := testData.DeliveryServices[0] | ||
|
|
||
| dses, _, err := TOSession.GetDeliveryServices() | ||
| if err != nil { | ||
| t.Fatalf("cannot GET Delivery Services: %v\n", err) | ||
| } | ||
|
|
||
| remoteDS := tc.DeliveryService{} | ||
| found := false | ||
| for _, ds := range dses { | ||
| if ds.XMLID == firstDS.XMLID { | ||
| found = true | ||
| remoteDS = ds | ||
| break | ||
| } | ||
| } | ||
| if !found { | ||
| t.Fatalf("GET Delivery Services missing: %v\n", firstDS.XMLID) | ||
| } | ||
|
|
||
| req := tc.DeliveryServiceSafeUpdate{ | ||
| DisplayName: util.StrPtr("safe update display name"), | ||
| InfoURL: util.StrPtr("safe update info URL"), | ||
| LongDesc: util.StrPtr("safe update long desc"), | ||
| LongDesc1: util.StrPtr("safe update long desc one"), | ||
| } | ||
|
|
||
| if _, err := TOSession.UpdateDeliveryServiceSafe(remoteDS.ID, &req); err != nil { | ||
| t.Fatalf("cannot UPDATE DeliveryService safe: %v\n", err) | ||
| } | ||
|
|
||
| updatedDS, _, err := TOSession.GetDeliveryService(strconv.Itoa(remoteDS.ID)) | ||
| if err != nil { | ||
| t.Fatalf("cannot GET Delivery Service by ID: id %v xmlid '%v' - %v\n", remoteDS.ID, remoteDS.XMLID, err) | ||
| } | ||
| if updatedDS == nil { | ||
| t.Fatalf("GET Delivery Service by ID returned nil err, but nil DS: %v - nil\n", remoteDS.XMLID) | ||
| } | ||
|
|
||
| if *req.DisplayName != updatedDS.DisplayName || *req.InfoURL != updatedDS.InfoURL || *req.LongDesc != updatedDS.LongDesc || *req.LongDesc1 != updatedDS.LongDesc1 { | ||
| t.Fatalf("ds safe update succeeded, but get delivery service didn't match. expected: {'%v' '%v' '%v' '%v'} actual: {'%v' '%v' '%v' '%v'}\n", *req.DisplayName, *req.InfoURL, *req.LongDesc, *req.LongDesc1, updatedDS.DisplayName, updatedDS.InfoURL, updatedDS.LongDesc, updatedDS.LongDesc1) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package deliveryservice | ||
|
|
||
| /* | ||
| * 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 ( | ||
| "database/sql" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "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/tenant" | ||
| ) | ||
|
|
||
| func UpdateSafeV14(w http.ResponseWriter, r *http.Request) { | ||
| ds, ok := UpdateSafe(w, r) | ||
| if !ok { | ||
| return | ||
| } | ||
| api.WriteRespAlertObj(w, r, tc.SuccessLevel, "Deliveryservice safe update was successful.", []tc.DeliveryServiceNullableV14{tc.DeliveryServiceNullableV14(ds)}) | ||
| } | ||
|
|
||
| func UpdateSafeV13(w http.ResponseWriter, r *http.Request) { | ||
|
ocket8888 marked this conversation as resolved.
Outdated
|
||
| ds, ok := UpdateSafe(w, r) | ||
| if !ok { | ||
| return | ||
| } | ||
| api.WriteRespAlertObj(w, r, tc.SuccessLevel, "Deliveryservice safe update was successful.", []tc.DeliveryServiceNullableV13{ds.DeliveryServiceNullableV13}) | ||
| } | ||
|
|
||
| func UpdateSafeV12(w http.ResponseWriter, r *http.Request) { | ||
| ds, ok := UpdateSafe(w, r) | ||
| if !ok { | ||
| return | ||
| } | ||
| api.WriteRespAlertObj(w, r, tc.SuccessLevel, "Deliveryservice safe update was successful.", []tc.DeliveryServiceNullableV12{ds.DeliveryServiceNullableV12}) | ||
| } | ||
|
|
||
| // UpdateSafe updates the delivery service, writing any errors. Returns true on success, or false on error. If an error occured, it will be written to the client and logged appropriately, and the caller shouldn't write anything further. On success, the caller should write the delivery service response to the client. | ||
| func UpdateSafe(w http.ResponseWriter, r *http.Request) (tc.DeliveryServiceNullable, bool) { | ||
| inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"id"}, []string{"id"}) | ||
| if userErr != nil || sysErr != nil { | ||
| api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr) | ||
| return tc.DeliveryServiceNullable{}, false | ||
| } | ||
| defer inf.Close() | ||
|
|
||
| dsID := inf.IntParams["id"] | ||
|
|
||
| userErr, sysErr, errCode = tenant.CheckID(inf.Tx.Tx, inf.User, dsID) | ||
| if userErr != nil || sysErr != nil { | ||
| api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr) | ||
| return tc.DeliveryServiceNullable{}, false | ||
| } | ||
|
|
||
| ds := tc.DeliveryServiceSafeUpdate{} | ||
| if err := api.Parse(r.Body, inf.Tx.Tx, &ds); err != nil { | ||
| api.HandleErr(w, r, inf.Tx.Tx, http.StatusBadRequest, errors.New("decoding: "+err.Error()), nil) | ||
| return tc.DeliveryServiceNullable{}, false | ||
| } | ||
|
|
||
| ok, err := updateDSSafe(inf.Tx.Tx, dsID, ds) | ||
| if err != nil { | ||
| api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("updating delivery service safe: "+err.Error())) | ||
| return tc.DeliveryServiceNullable{}, false | ||
| } | ||
| if !ok { | ||
| api.HandleErr(w, r, inf.Tx.Tx, http.StatusNotFound, nil, nil) | ||
| return tc.DeliveryServiceNullable{}, false | ||
| } | ||
|
|
||
| dses, userErr, sysErr, errCode := readGetDeliveryServices(inf.Params, inf.Tx, inf.User) | ||
| if userErr != nil || sysErr != nil { | ||
| api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr) | ||
| return tc.DeliveryServiceNullable{}, false | ||
| } | ||
| if len(dses) != 1 { | ||
| api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, fmt.Errorf("delivery service safe update, read expected 1 delivery service, got %v", len(dses))) | ||
| return tc.DeliveryServiceNullable{}, false | ||
| } | ||
| return dses[0], true | ||
| } | ||
|
|
||
| // updateDSSafe updates the given delivery service in the database. Returns whether the DS existed, and any error. | ||
| func updateDSSafe(tx *sql.Tx, dsID int, ds tc.DeliveryServiceSafeUpdate) (bool, error) { | ||
| q := ` | ||
| UPDATE deliveryservice SET | ||
| display_name=$1, | ||
| info_url=$2, | ||
| long_desc=$3, | ||
| long_desc_1=$4 | ||
| WHERE id = $5 | ||
| RETURNING id | ||
| ` | ||
| res, err := tx.Exec(q, ds.DisplayName, ds.InfoURL, ds.LongDesc, ds.LongDesc1, dsID) | ||
|
ocket8888 marked this conversation as resolved.
|
||
| if err != nil { | ||
| return false, errors.New("updating delivery service safe: " + err.Error()) | ||
| } | ||
| rowsAffected, err := res.RowsAffected() | ||
| if err != nil { | ||
| return false, errors.New("updating delivery service safe, checking rows affected: " + err.Error()) | ||
| } | ||
| if rowsAffected < 1 { | ||
| return false, nil | ||
| } | ||
| if rowsAffected > 1 { | ||
| return false, fmt.Errorf("updating delivery service safe, too many rows affected: %v", rowsAffected) | ||
| } | ||
| return true, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.