This is an automated email from the ASF dual-hosted git repository.

srijeet0406 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 2aa7a60d30 Update only if ASN is unique (#7756)
2aa7a60d30 is described below

commit 2aa7a60d30a0f59fe2bc6fbd12551003b6158b04
Author: Rima Shah <[email protected]>
AuthorDate: Tue Aug 29 15:38:40 2023 -0600

    Update only if ASN is unique (#7756)
    
    * checking if an asn already exists before updating it.
    
    * added test
    
    * edited test
    
    * updated tc-fixtures
---
 traffic_ops/testing/api/v5/asns_test.go     | 10 ++++++++++
 traffic_ops/testing/api/v5/tc-fixtures.json |  4 ++++
 traffic_ops/traffic_ops_golang/asn/asns.go  | 14 +++++++++++++-
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/traffic_ops/testing/api/v5/asns_test.go 
b/traffic_ops/testing/api/v5/asns_test.go
index 91e88598ca..cbd69bac2b 100644
--- a/traffic_ops/testing/api/v5/asns_test.go
+++ b/traffic_ops/testing/api/v5/asns_test.go
@@ -77,6 +77,16 @@ func TestASN(t *testing.T) {
                                        },
                                        Expectations: 
utils.CkRequest(utils.HasError(), 
utils.HasStatus(http.StatusPreconditionFailed)),
                                },
+                               "BAD REQUEST when ASN is not unique": {
+                                       ClientSession: TOSession,
+                                       EndpointID:    GetASNId(t, "5555"),
+                                       RequestBody: map[string]interface{}{
+                                               "asn":            9999,
+                                               "cachegroupName": 
"originCachegroup",
+                                               "cachegroupId":   -1,
+                                       },
+                                       Expectations: 
utils.CkRequest(utils.HasError(), utils.HasStatus(http.StatusBadRequest)),
+                               },
                        },
                        "GET AFTER CHANGES": {
                                "OK when CHANGES made": {
diff --git a/traffic_ops/testing/api/v5/tc-fixtures.json 
b/traffic_ops/testing/api/v5/tc-fixtures.json
index 5e97ee55ca..e79a1c3aed 100644
--- a/traffic_ops/testing/api/v5/tc-fixtures.json
+++ b/traffic_ops/testing/api/v5/tc-fixtures.json
@@ -7,6 +7,10 @@
         {
             "asn": 9999,
             "cachegroupName": "multiOriginCachegroup"
+        },
+        {
+            "asn": 5555,
+            "cachegroupName": "originCachegroup"
         }
     ],
     "cachegroups": [
diff --git a/traffic_ops/traffic_ops_golang/asn/asns.go 
b/traffic_ops/traffic_ops_golang/asn/asns.go
index 3e1e694b1c..5954594f87 100644
--- a/traffic_ops/traffic_ops_golang/asn/asns.go
+++ b/traffic_ops/traffic_ops_golang/asn/asns.go
@@ -340,6 +340,18 @@ func Update(w http.ResponseWriter, r *http.Request) {
                return
        }
 
+       // check if asn already exists
+       var count int
+       err := tx.QueryRow("SELECT count(*) from asn where asn=$1", 
asn.ASN).Scan(&count)
+       if err != nil {
+               api.HandleErr(w, r, tx, http.StatusInternalServerError, nil, 
fmt.Errorf("error: %w, when checking if asn '%d' exists", err, asn.ASN))
+               return
+       }
+       if count == 1 {
+               api.HandleErr(w, r, tx, http.StatusBadRequest, 
fmt.Errorf("asn:'%d' already exists", asn.ASN), nil)
+               return
+       }
+
        //update asn and cachegroup of an asn
        query := `UPDATE asn SET
                asn = $1,
@@ -347,7 +359,7 @@ func Update(w http.ResponseWriter, r *http.Request) {
        WHERE id = $3
        RETURNING id, last_updated, (select name FROM cachegroup where id = $2)`
 
-       err := tx.QueryRow(query, asn.ASN, asn.CachegroupID, 
requestedAsnId).Scan(&asn.ID, &asn.LastUpdated, &asn.Cachegroup)
+       err = tx.QueryRow(query, asn.ASN, asn.CachegroupID, 
requestedAsnId).Scan(&asn.ID, &asn.LastUpdated, &asn.Cachegroup)
        if err != nil {
                if errors.Is(err, sql.ErrNoRows) {
                        api.HandleErr(w, r, tx, http.StatusBadRequest, 
fmt.Errorf("asn: %d not found", asn.ASN), nil)

Reply via email to