You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by oc...@apache.org on 2020/04/01 17:42:53 UTC

[trafficcontrol] branch master updated: PUT /snapshot?cdn=not-found returns 404 (#4556)

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

ocket8888 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 9a7db19  PUT /snapshot?cdn=not-found returns 404 (#4556)
9a7db19 is described below

commit 9a7db19f5e169252c34cd945a6eaa0b36e0a564c
Author: Jeremy Mitchell <mi...@users.noreply.github.com>
AuthorDate: Wed Apr 1 11:42:43 2020 -0600

    PUT /snapshot?cdn=not-found returns 404 (#4556)
    
    * adds 404 if cdn name not found as well as a couple of snapshot tests
    
    * uses dbhelpers.GetCDNIDFromName to get more granular error info
---
 traffic_ops/client/crconfig.go                     | 24 ++++++++++-
 traffic_ops/testing/api/v2/crconfig_test.go        | 46 ++++++++++++++++++++++
 traffic_ops/traffic_ops_golang/crconfig/handler.go | 11 ++++++
 3 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/traffic_ops/client/crconfig.go b/traffic_ops/client/crconfig.go
index 2bbe99b..27f15de 100644
--- a/traffic_ops/client/crconfig.go
+++ b/traffic_ops/client/crconfig.go
@@ -17,10 +17,16 @@ package client
 
 import (
 	"encoding/json"
+	"fmt"
+	"github.com/apache/trafficcontrol/lib/go-tc"
 	"net/http"
 	"net/url"
 )
 
+const (
+	API_SNAPSHOT = apiBase + "/snapshot"
+)
+
 type OuterResponse struct {
 	Response json.RawMessage `json:"response"`
 }
@@ -40,9 +46,25 @@ func (to *Session) GetCRConfig(cdn string) ([]byte, ReqInf, error) {
 	return []byte(resp.Response), reqInf, nil
 }
 
+// SnapshotCRConfig snapshots a CDN by name.
 func (to *Session) SnapshotCRConfig(cdn string) (ReqInf, error) {
-	uri := apiBase + `/snapshot?cdn=` + url.QueryEscape(cdn)
+	uri := fmt.Sprintf("%s?cdn=%s", API_SNAPSHOT, url.QueryEscape(cdn))
 	_, remoteAddr, err := to.request(http.MethodPut, uri, nil)
 	reqInf := ReqInf{RemoteAddr: remoteAddr, CacheHitStatus: CacheHitStatusMiss}
 	return reqInf, err
 }
+
+// SnapshotCDNByID snapshots a CDN by ID.
+func (to *Session) SnapshotCRConfigByID(id int) (tc.Alerts, ReqInf, error) {
+	url := fmt.Sprintf("%s?cdnID=%d", API_SNAPSHOT, id)
+	resp, remoteAddr, err := to.request(http.MethodPut, url, nil)
+	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+	if err != nil {
+		return tc.Alerts{}, reqInf, err
+	}
+	defer resp.Body.Close()
+	var alerts tc.Alerts
+	err = json.NewDecoder(resp.Body).Decode(&alerts)
+	return alerts, reqInf, nil
+}
+
diff --git a/traffic_ops/testing/api/v2/crconfig_test.go b/traffic_ops/testing/api/v2/crconfig_test.go
index 47a38e5..cce4404 100644
--- a/traffic_ops/testing/api/v2/crconfig_test.go
+++ b/traffic_ops/testing/api/v2/crconfig_test.go
@@ -26,6 +26,10 @@ import (
 func TestCRConfig(t *testing.T) {
 	WithObjs(t, []TCObj{CDNs, Types, Tenants, Parameters, Profiles, Statuses, Divisions, Regions, PhysLocations, CacheGroups, Servers, DeliveryServices}, func() {
 		UpdateTestCRConfigSnapshot(t)
+		SnapshotTestCDNbyName(t)
+		SnapshotTestCDNbyInvalidName(t)
+		SnapshotTestCDNbyID(t)
+		SnapshotTestCDNbyInvalidID(t)
 	})
 }
 
@@ -134,3 +138,45 @@ func UpdateTestCRConfigSnapshot(t *testing.T) {
 		t.Fatalf("cannot DELETE Parameter by name: %v - %v", err, delResp)
 	}
 }
+
+func SnapshotTestCDNbyName(t *testing.T) {
+
+	firstCDN := testData.CDNs[0]
+	_, err := TOSession.SnapshotCRConfig(firstCDN.Name)
+	if err != nil {
+		t.Errorf("failed to snapshot CDN by name: %v", err)
+	}
+}
+
+func SnapshotTestCDNbyInvalidName(t *testing.T) {
+
+	invalidCDNName := "cdn-invalid"
+	_, err := TOSession.SnapshotCRConfig(invalidCDNName)
+	if err == nil {
+		t.Errorf("snapshot occurred on invalid cdn name: %v - %v", invalidCDNName, err)
+	}
+}
+
+func SnapshotTestCDNbyID(t *testing.T) {
+
+	firstCDN := testData.CDNs[0]
+	// Retrieve the CDN by name so we can get the id for the snapshot
+	resp, _, err := TOSession.GetCDNByName(firstCDN.Name)
+	if err != nil {
+		t.Errorf("cannot GET CDN by name: '%s', %v", firstCDN.Name, err)
+	}
+	remoteCDN := resp[0]
+	alert, _, err := TOSession.SnapshotCRConfigByID(remoteCDN.ID)
+	if err != nil {
+		t.Errorf("failed to snapshot CDN by id: %v - %v", err, alert)
+	}
+}
+
+func SnapshotTestCDNbyInvalidID(t *testing.T) {
+
+	invalidCDNID := 999999
+	alert, _, err := TOSession.SnapshotCRConfigByID(invalidCDNID)
+	if err == nil {
+		t.Errorf("snapshot occurred on invalid cdn id: %v - %v - %v", invalidCDNID, err, alert)
+	}
+}
diff --git a/traffic_ops/traffic_ops_golang/crconfig/handler.go b/traffic_ops/traffic_ops_golang/crconfig/handler.go
index eba20dc..0798e4c 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/handler.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/handler.go
@@ -31,6 +31,7 @@ import (
 	"github.com/apache/trafficcontrol/lib/go-rfc"
 	"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/dbhelpers"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/deliveryservice"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/monitoring"
 )
@@ -174,6 +175,16 @@ func snapshotHandler(w http.ResponseWriter, r *http.Request, deprecated bool) {
 			return
 		}
 		cdn = name
+	} else {
+		_, ok, err := dbhelpers.GetCDNIDFromName(inf.Tx.Tx, tc.CDNName(cdn))
+		if err != nil {
+			api.HandleErrOptionalDeprecation(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("Error getting CDN ID from name: "+err.Error()), deprecated, &alt)
+			return
+		}
+		if !ok {
+			api.HandleErrOptionalDeprecation(w, r, inf.Tx.Tx, http.StatusNotFound, errors.New("No CDN ID found with that name"), nil, deprecated, &alt)
+			return
+		}
 	}
 
 	crConfig, err := Make(inf.Tx.Tx, cdn, inf.User.UserName, r.Host, r.URL.Path, inf.Config.Version, inf.Config.CRConfigUseRequestHost, inf.Config.CRConfigEmulateOldPath)