You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by ne...@apache.org on 2016/11/09 21:23:19 UTC

[3/4] incubator-trafficcontrol git commit: create traffic_ops client `UpdateDeliveryService` method

create traffic_ops client `UpdateDeliveryService` method


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/d4f6e028
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/d4f6e028
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/d4f6e028

Branch: refs/heads/master
Commit: d4f6e0280f28005213a923087ecfd07068ec0c62
Parents: a046d7d
Author: Mike Ball <mi...@gmail.com>
Authored: Thu Nov 3 21:24:21 2016 -0400
Committer: Dave Neuman <ne...@apache.org>
Committed: Wed Nov 9 14:22:40 2016 -0700

----------------------------------------------------------------------
 traffic_ops/client/cachegroup.go                |  2 +-
 traffic_ops/client/cdn.go                       |  4 +-
 traffic_ops/client/delivery_service.go          | 52 +++++++++++++++-----
 traffic_ops/client/fixtures/delivery_service.go | 13 +++++
 traffic_ops/client/hardware.go                  |  2 +-
 traffic_ops/client/parameter.go                 |  2 +-
 traffic_ops/client/profile.go                   |  2 +-
 traffic_ops/client/server.go                    |  4 +-
 traffic_ops/client/stats_summary.go             |  6 +--
 .../client/tests/delivery_service_test.go       | 48 ++++++++++++++++++
 traffic_ops/client/traffic_monitor_config.go    |  2 +-
 traffic_ops/client/traffic_ops.go               | 10 ++--
 traffic_ops/client/type.go                      |  2 +-
 traffic_ops/client/user.go                      |  2 +-
 14 files changed, 120 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d4f6e028/traffic_ops/client/cachegroup.go
----------------------------------------------------------------------
diff --git a/traffic_ops/client/cachegroup.go b/traffic_ops/client/cachegroup.go
index 2505abb..44aadd9 100644
--- a/traffic_ops/client/cachegroup.go
+++ b/traffic_ops/client/cachegroup.go
@@ -38,7 +38,7 @@ type CacheGroup struct {
 // (note CacheGroup used to be called location)
 func (to *Session) CacheGroups() ([]CacheGroup, error) {
 	url := "/api/1.2/cachegroups.json"
-	resp, err := to.request(url, nil)
+	resp, err := to.request("GET", url, nil)
 	if err != nil {
 		return nil, err
 	}

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d4f6e028/traffic_ops/client/cdn.go
----------------------------------------------------------------------
diff --git a/traffic_ops/client/cdn.go b/traffic_ops/client/cdn.go
index a32b5f1..665382e 100644
--- a/traffic_ops/client/cdn.go
+++ b/traffic_ops/client/cdn.go
@@ -35,7 +35,7 @@ type CDN struct {
 // CDNs gets an array of CDNs
 func (to *Session) CDNs() ([]CDN, error) {
 	url := "/api/1.2/cdns.json"
-	resp, err := to.request(url, nil)
+	resp, err := to.request("GET", url, nil)
 	if err != nil {
 		return nil, err
 	}
@@ -51,7 +51,7 @@ func (to *Session) CDNs() ([]CDN, error) {
 // CDNName gets an array of CDNs
 func (to *Session) CDNName(name string) ([]CDN, error) {
 	url := fmt.Sprintf("/api/1.2/cdns/name/%s.json", name)
-	resp, err := to.request(url, nil)
+	resp, err := to.request("GET", url, nil)
 	if err != nil {
 		return nil, err
 	}

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d4f6e028/traffic_ops/client/delivery_service.go
----------------------------------------------------------------------
diff --git a/traffic_ops/client/delivery_service.go b/traffic_ops/client/delivery_service.go
index 03817b5..06595bc 100644
--- a/traffic_ops/client/delivery_service.go
+++ b/traffic_ops/client/delivery_service.go
@@ -20,7 +20,7 @@ import "encoding/json"
 // DeliveryServices gets an array of DeliveryServices
 func (to *Session) DeliveryServices() ([]DeliveryService, error) {
 	var data DeliveryServiceResponse
-	err := makeReq(to, deliveryServicesEp(), nil, &data)
+	err := get(to, deliveryServicesEp(), &data)
 	if err != nil {
 		return nil, err
 	}
@@ -31,7 +31,7 @@ func (to *Session) DeliveryServices() ([]DeliveryService, error) {
 // DeliveryService gets the DeliveryService for the ID it's passed
 func (to *Session) DeliveryService(id string) (*DeliveryService, error) {
 	var data DeliveryServiceResponse
-	err := makeReq(to, deliveryServiceEp(id), nil, &data)
+	err := get(to, deliveryServiceEp(id), &data)
 	if err != nil {
 		return nil, err
 	}
@@ -46,7 +46,23 @@ func (to *Session) CreateDeliveryService(ds *DeliveryService) (*CreateDeliverySe
 	if err != nil {
 		return nil, err
 	}
-	err = makeReq(to, deliveryServicesEp(), jsonReq, &data)
+	err = post(to, deliveryServicesEp(), jsonReq, &data)
+	if err != nil {
+		return nil, err
+	}
+
+	return &data, nil
+}
+
+// UpdateDeliveryService updates the DeliveryService matching the ID it's passed with
+// the DeliveryService it is passed
+func (to *Session) UpdateDeliveryService(id string, ds *DeliveryService) (*CreateDeliveryServiceResponse, error) {
+	var data CreateDeliveryServiceResponse
+	jsonReq, err := json.Marshal(ds)
+	if err != nil {
+		return nil, err
+	}
+	err = put(to, deliveryServiceEp(id), jsonReq, &data)
 	if err != nil {
 		return nil, err
 	}
@@ -57,7 +73,7 @@ func (to *Session) CreateDeliveryService(ds *DeliveryService) (*CreateDeliverySe
 // DeliveryServiceState gets the DeliveryServiceState for the ID it's passed
 func (to *Session) DeliveryServiceState(id string) (*DeliveryServiceState, error) {
 	var data DeliveryServiceStateResponse
-	err := makeReq(to, deliveryServiceStateEp(id), nil, &data)
+	err := get(to, deliveryServiceStateEp(id), &data)
 	if err != nil {
 		return nil, err
 	}
@@ -68,7 +84,7 @@ func (to *Session) DeliveryServiceState(id string) (*DeliveryServiceState, error
 // DeliveryServiceHealth gets the DeliveryServiceHealth for the ID it's passed
 func (to *Session) DeliveryServiceHealth(id string) (*DeliveryServiceHealth, error) {
 	var data DeliveryServiceHealthResponse
-	err := makeReq(to, deliveryServiceHealthEp(id), nil, &data)
+	err := get(to, deliveryServiceHealthEp(id), &data)
 	if err != nil {
 		return nil, err
 	}
@@ -79,7 +95,7 @@ func (to *Session) DeliveryServiceHealth(id string) (*DeliveryServiceHealth, err
 // DeliveryServiceCapacity gets the DeliveryServiceCapacity for the ID it's passed
 func (to *Session) DeliveryServiceCapacity(id string) (*DeliveryServiceCapacity, error) {
 	var data DeliveryServiceCapacityResponse
-	err := makeReq(to, deliveryServiceCapacityEp(id), nil, &data)
+	err := get(to, deliveryServiceCapacityEp(id), &data)
 	if err != nil {
 		return nil, err
 	}
@@ -90,7 +106,7 @@ func (to *Session) DeliveryServiceCapacity(id string) (*DeliveryServiceCapacity,
 // DeliveryServiceRouting gets the DeliveryServiceRouting for the ID it's passed
 func (to *Session) DeliveryServiceRouting(id string) (*DeliveryServiceRouting, error) {
 	var data DeliveryServiceRoutingResponse
-	err := makeReq(to, deliveryServiceRoutingEp(id), nil, &data)
+	err := get(to, deliveryServiceRoutingEp(id), &data)
 	if err != nil {
 		return nil, err
 	}
@@ -101,7 +117,7 @@ func (to *Session) DeliveryServiceRouting(id string) (*DeliveryServiceRouting, e
 // DeliveryServiceServer gets the DeliveryServiceServer
 func (to *Session) DeliveryServiceServer(page, limit string) ([]DeliveryServiceServer, error) {
 	var data DeliveryServiceServerResponse
-	err := makeReq(to, deliveryServiceServerEp(page, limit), nil, &data)
+	err := get(to, deliveryServiceServerEp(page, limit), &data)
 	if err != nil {
 		return nil, err
 	}
@@ -112,7 +128,7 @@ func (to *Session) DeliveryServiceServer(page, limit string) ([]DeliveryServiceS
 // DeliveryServiceSSLKeysByID gets the DeliveryServiceSSLKeys by ID
 func (to *Session) DeliveryServiceSSLKeysByID(id string) (*DeliveryServiceSSLKeys, error) {
 	var data DeliveryServiceSSLKeysResponse
-	err := makeReq(to, deliveryServiceSSLKeysByIDEp(id), nil, &data)
+	err := get(to, deliveryServiceSSLKeysByIDEp(id), &data)
 	if err != nil {
 		return nil, err
 	}
@@ -123,7 +139,7 @@ func (to *Session) DeliveryServiceSSLKeysByID(id string) (*DeliveryServiceSSLKey
 // DeliveryServiceSSLKeysByHostname gets the DeliveryServiceSSLKeys by Hostname
 func (to *Session) DeliveryServiceSSLKeysByHostname(hostname string) (*DeliveryServiceSSLKeys, error) {
 	var data DeliveryServiceSSLKeysResponse
-	err := makeReq(to, deliveryServiceSSLKeysByHostnameEp(hostname), nil, &data)
+	err := get(to, deliveryServiceSSLKeysByHostnameEp(hostname), &data)
 	if err != nil {
 		return nil, err
 	}
@@ -131,8 +147,20 @@ func (to *Session) DeliveryServiceSSLKeysByHostname(hostname string) (*DeliveryS
 	return &data.Response, nil
 }
 
-func makeReq(to *Session, endpoint string, body []byte, respStruct interface{}) error {
-	resp, err := to.request(endpoint, body)
+func get(to *Session, endpoint string, respStruct interface{}) error {
+	return makeReq(to, "GET", endpoint, nil, respStruct)
+}
+
+func post(to *Session, endpoint string, body []byte, respStruct interface{}) error {
+	return makeReq(to, "POST", endpoint, body, respStruct)
+}
+
+func put(to *Session, endpoint string, body []byte, respStruct interface{}) error {
+	return makeReq(to, "PUT", endpoint, body, respStruct)
+}
+
+func makeReq(to *Session, method, endpoint string, body []byte, respStruct interface{}) error {
+	resp, err := to.request(method, endpoint, body)
 	if err != nil {
 		return err
 	}

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d4f6e028/traffic_ops/client/fixtures/delivery_service.go
----------------------------------------------------------------------
diff --git a/traffic_ops/client/fixtures/delivery_service.go b/traffic_ops/client/fixtures/delivery_service.go
index c7be07c..5b274a0 100644
--- a/traffic_ops/client/fixtures/delivery_service.go
+++ b/traffic_ops/client/fixtures/delivery_service.go
@@ -64,6 +64,19 @@ func CreateDeliveryService() *client.CreateDeliveryServiceResponse {
 	}
 }
 
+// UpdateDeliveryService returns a default CreateDeliveryServiceResponse to be used for testing.
+func UpdateDeliveryService() *client.CreateDeliveryServiceResponse {
+	return &client.CreateDeliveryServiceResponse{
+		Response: DeliveryServices().Response[0],
+		Alerts: []client.DeliveryServiceAlert{
+			client.DeliveryServiceAlert{
+				Level: "level",
+				Text:  "text",
+			},
+		},
+	}
+}
+
 // DeliveryServiceState returns a default DeliveryServiceStateResponse to be used for testing.
 func DeliveryServiceState() *client.DeliveryServiceStateResponse {
 	dest := client.DeliveryServiceDestination{

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d4f6e028/traffic_ops/client/hardware.go
----------------------------------------------------------------------
diff --git a/traffic_ops/client/hardware.go b/traffic_ops/client/hardware.go
index 4bdf124..939ce12 100644
--- a/traffic_ops/client/hardware.go
+++ b/traffic_ops/client/hardware.go
@@ -35,7 +35,7 @@ type Hardware struct {
 // Hardware gets an array of Hardware
 func (to *Session) Hardware() ([]Hardware, error) {
 	url := "/api/1.2/hwinfo.json"
-	resp, err := to.request(url, nil)
+	resp, err := to.request("GET", url, nil)
 	if err != nil {
 		return nil, err
 	}

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d4f6e028/traffic_ops/client/parameter.go
----------------------------------------------------------------------
diff --git a/traffic_ops/client/parameter.go b/traffic_ops/client/parameter.go
index 72b2f16..937e450 100644
--- a/traffic_ops/client/parameter.go
+++ b/traffic_ops/client/parameter.go
@@ -37,7 +37,7 @@ type Parameter struct {
 // Parameters gets an array of parameter structs for the profile given
 func (to *Session) Parameters(profileName string) ([]Parameter, error) {
 	url := fmt.Sprintf("/api/1.2/parameters/profile/%s.json", profileName)
-	resp, err := to.request(url, nil)
+	resp, err := to.request("GET", url, nil)
 	if err != nil {
 		return nil, err
 	}

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d4f6e028/traffic_ops/client/profile.go
----------------------------------------------------------------------
diff --git a/traffic_ops/client/profile.go b/traffic_ops/client/profile.go
index 4ef640c..b417886 100644
--- a/traffic_ops/client/profile.go
+++ b/traffic_ops/client/profile.go
@@ -33,7 +33,7 @@ type Profile struct {
 // Profiles gets an array of Profiles
 func (to *Session) Profiles() ([]Profile, error) {
 	url := "/api/1.2/profiles.json"
-	resp, err := to.request(url, nil)
+	resp, err := to.request("GET", url, nil)
 	if err != nil {
 		return nil, err
 	}

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d4f6e028/traffic_ops/client/server.go
----------------------------------------------------------------------
diff --git a/traffic_ops/client/server.go b/traffic_ops/client/server.go
index 227f9d2..93ea9e5 100644
--- a/traffic_ops/client/server.go
+++ b/traffic_ops/client/server.go
@@ -68,7 +68,7 @@ type Server struct {
 // Servers gets an array of servers
 func (to *Session) Servers() ([]Server, error) {
 	url := "/api/1.2/servers.json"
-	resp, err := to.request(url, nil)
+	resp, err := to.request("GET", url, nil)
 	if err != nil {
 		return nil, err
 	}
@@ -85,7 +85,7 @@ func (to *Session) Servers() ([]Server, error) {
 // ServersByType gets an array of serves of a specified type.
 func (to *Session) ServersByType(qparams url.Values) ([]Server, error) {
 	url := fmt.Sprintf("/api/1.2/servers.json?%s", qparams.Encode())
-	resp, err := to.request(url, nil)
+	resp, err := to.request("GET", url, nil)
 	if err != nil {
 		return nil, err
 	}

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d4f6e028/traffic_ops/client/stats_summary.go
----------------------------------------------------------------------
diff --git a/traffic_ops/client/stats_summary.go b/traffic_ops/client/stats_summary.go
index 1c5df48..2045c23 100644
--- a/traffic_ops/client/stats_summary.go
+++ b/traffic_ops/client/stats_summary.go
@@ -67,7 +67,7 @@ func (to *Session) SummaryStats(cdn string, deliveryService string, statName str
 		queryURL += queryParamString
 	}
 
-	resp, err := to.request(queryURL, nil)
+	resp, err := to.request("GET", queryURL, nil)
 	if err != nil {
 		return nil, err
 	}
@@ -88,7 +88,7 @@ func (to *Session) SummaryStatsLastUpdated(statName string) (string, error) {
 		queryURL += fmt.Sprintf("?statName=%s", statName)
 	}
 
-	resp, err := to.request(queryURL, nil)
+	resp, err := to.request("GET", queryURL, nil)
 	if err != nil {
 		return "", err
 	}
@@ -114,7 +114,7 @@ func (to *Session) AddSummaryStats(statsSummary StatsSummary) error {
 	}
 
 	url := "/api/1.2/stats_summary/create"
-	resp, err := to.request(url, reqBody)
+	resp, err := to.request("POST", url, reqBody)
 	if err != nil {
 		return err
 	}

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d4f6e028/traffic_ops/client/tests/delivery_service_test.go
----------------------------------------------------------------------
diff --git a/traffic_ops/client/tests/delivery_service_test.go b/traffic_ops/client/tests/delivery_service_test.go
index 45336b1..62794f5 100644
--- a/traffic_ops/client/tests/delivery_service_test.go
+++ b/traffic_ops/client/tests/delivery_service_test.go
@@ -186,6 +186,54 @@ func TestCreateDeliveryServiceUnauthorized(t *testing.T) {
 	}
 }
 
+func TestUpdateDeliveryService(t *testing.T) {
+	resp := fixtures.UpdateDeliveryService()
+	server := testHelper.ValidHTTPServer(resp)
+	defer server.Close()
+
+	var httpClient http.Client
+	to := client.Session{
+		URL:       server.URL,
+		UserAgent: &httpClient,
+	}
+
+	testHelper.Context(t, "Given the need to test a successful Traffic Ops request to update a DeliveryService")
+
+	ds, err := to.UpdateDeliveryService("123", &client.DeliveryService{})
+	if err != nil {
+		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
+	} else {
+		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
+	}
+
+	actual := ds.Response.ID
+	if actual != "001" {
+		testHelper.Error(t, "Should get back \"001\" for \"Response.ID\", got: %s", actual)
+	} else {
+		testHelper.Success(t, "Should get back \"0001\" for \"Response.ID\"")
+	}
+}
+
+func TestUpdateDeliveryServiceUnauthorized(t *testing.T) {
+	server := testHelper.InvalidHTTPServer(http.StatusUnauthorized)
+	defer server.Close()
+
+	var httpClient http.Client
+	to := client.Session{
+		URL:       server.URL,
+		UserAgent: &httpClient,
+	}
+
+	testHelper.Context(t, "Given the need to test a failed Traffic Ops request to update a DeliveryService")
+
+	_, err := to.UpdateDeliveryService("123", &client.DeliveryService{})
+	if err == nil {
+		testHelper.Error(t, "Should not be able to make a request to Traffic Ops")
+	} else {
+		testHelper.Success(t, "Should not be able to make a request to Traffic Ops")
+	}
+}
+
 func TestDeliveryServiceState(t *testing.T) {
 	resp := fixtures.DeliveryServiceState()
 	server := testHelper.ValidHTTPServer(resp)

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d4f6e028/traffic_ops/client/traffic_monitor_config.go
----------------------------------------------------------------------
diff --git a/traffic_ops/client/traffic_monitor_config.go b/traffic_ops/client/traffic_monitor_config.go
index 04e7a8a..50ec7cd 100644
--- a/traffic_ops/client/traffic_monitor_config.go
+++ b/traffic_ops/client/traffic_monitor_config.go
@@ -102,7 +102,7 @@ func (to *Session) TrafficMonitorConfigMap(cdn string) (*TrafficMonitorConfigMap
 // TrafficMonitorConfig ...
 func (to *Session) TrafficMonitorConfig(cdn string) (*TrafficMonitorConfig, error) {
 	url := fmt.Sprintf("/api/1.2/cdns/%s/configs/monitoring.json", cdn)
-	resp, err := to.request(url, nil)
+	resp, err := to.request("GET", url, nil)
 	if err != nil {
 		return nil, err
 	}

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d4f6e028/traffic_ops/client/traffic_ops.go
----------------------------------------------------------------------
diff --git a/traffic_ops/client/traffic_ops.go b/traffic_ops/client/traffic_ops.go
index 9095481..c5abfa9 100644
--- a/traffic_ops/client/traffic_ops.go
+++ b/traffic_ops/client/traffic_ops.go
@@ -125,7 +125,7 @@ func Login(toURL string, toUser string, toPasswd string, insecure bool) (*Sessio
 	}
 
 	path := "/api/1.2/user/login"
-	resp, err := to.request(path, credentials)
+	resp, err := to.request("POST", path, credentials)
 	if err != nil {
 		return nil, err
 	}
@@ -153,14 +153,14 @@ func Login(toURL string, toUser string, toPasswd string, insecure bool) (*Sessio
 }
 
 // request performs the actual HTTP request to Traffic Ops
-func (to *Session) request(path string, body []byte) (*http.Response, error) {
+func (to *Session) request(method, path string, body []byte) (*http.Response, error) {
 	url := fmt.Sprintf("%s%s", to.URL, path)
 
 	var req *http.Request
 	var err error
 
-	if body != nil {
-		req, err = http.NewRequest("POST", url, bytes.NewBuffer(body))
+	if body != nil && method != "GET" {
+		req, err = http.NewRequest(method, url, bytes.NewBuffer(body))
 		if err != nil {
 			return nil, err
 		}
@@ -255,7 +255,7 @@ func (to *Session) getBytesWithTTL(path string, ttl int64) ([]byte, CacheHitStat
 // GetBytes - get []bytes array for a certain path on the to session.
 // returns the raw body
 func (to *Session) getBytes(path string) ([]byte, error) {
-	resp, err := to.request(path, nil)
+	resp, err := to.request("GET", path, nil)
 	if err != nil {
 		return nil, err
 	}

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d4f6e028/traffic_ops/client/type.go
----------------------------------------------------------------------
diff --git a/traffic_ops/client/type.go b/traffic_ops/client/type.go
index 288952c..9bfe19f 100644
--- a/traffic_ops/client/type.go
+++ b/traffic_ops/client/type.go
@@ -42,7 +42,7 @@ func (to *Session) Types(useInTable ...string) ([]Type, error) {
 	}
 
 	url := "/api/1.2/types.json"
-	resp, err := to.request(url, nil)
+	resp, err := to.request("GET", url, nil)
 	if err != nil {
 		return nil, err
 	}

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/d4f6e028/traffic_ops/client/user.go
----------------------------------------------------------------------
diff --git a/traffic_ops/client/user.go b/traffic_ops/client/user.go
index 2d508c3..b2f1bb6 100644
--- a/traffic_ops/client/user.go
+++ b/traffic_ops/client/user.go
@@ -41,7 +41,7 @@ type User struct {
 // Users gets an array of Users.
 func (to *Session) Users() ([]User, error) {
 	url := "/api/1.2/users.json"
-	resp, err := to.request(url, nil)
+	resp, err := to.request("GET", url, nil)
 	if err != nil {
 		return nil, err
 	}