Skip to content
This repository was archived by the owner on Nov 24, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions traffic_ops/client/delivery_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ func (to *Session) UpdateDeliveryService(id string, ds *DeliveryService) (*Deliv
return &data, nil
}

// DeleteDeliveryService deletes the DeliveryService matching the ID it's passed
func (to *Session) DeleteDeliveryService(id string) (*DeleteDeliveryServiceResponse, error) {
var data DeleteDeliveryServiceResponse
err := del(to, deliveryServiceEp(id), &data)
if err != nil {
return nil, err
}

return &data, nil
}

// DeliveryServiceState gets the DeliveryServiceState for the ID it's passed
func (to *Session) DeliveryServiceState(id string) (*DeliveryServiceState, error) {
var data DeliveryServiceStateResponse
Expand Down Expand Up @@ -159,6 +170,10 @@ func put(to *Session, endpoint string, body []byte, respStruct interface{}) erro
return makeReq(to, "PUT", endpoint, body, respStruct)
}

func del(to *Session, endpoint string, respStruct interface{}) error {
return makeReq(to, "DELETE", endpoint, nil, respStruct)
}

func makeReq(to *Session, method, endpoint string, body []byte, respStruct interface{}) error {
resp, err := to.request(method, endpoint, body)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions traffic_ops/client/delivery_service_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type DeliveryServiceResponse struct {
Alerts []DeliveryServiceAlert `json:"alerts"`
}

// DeleteDeliveryServiceResponse ...
type DeleteDeliveryServiceResponse struct {
Alerts []DeliveryServiceAlert `json:"alerts"`
}

// DeliveryService ...
type DeliveryService struct {
ID string `json:"id"`
Expand Down
23 changes: 17 additions & 6 deletions traffic_ops/client/fixtures/delivery_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,27 @@ func DeliveryServices() *client.GetDeliveryServiceResponse {
}
}

func alerts() []client.DeliveryServiceAlert {
return []client.DeliveryServiceAlert{
client.DeliveryServiceAlert{
Level: "level",
Text: "text",
},
}
}

// DeliveryService returns a default DeliveryServiceResponse to be used for testing.
func DeliveryService() *client.DeliveryServiceResponse {
return &client.DeliveryServiceResponse{
Response: DeliveryServices().Response[0],
Alerts: []client.DeliveryServiceAlert{
client.DeliveryServiceAlert{
Level: "level",
Text: "text",
},
},
Alerts: alerts(),
}
}

// DeleteDeliveryService returns a default DeleteDeliveryServiceResponse to be used for testing.
func DeleteDeliveryService() *client.DeleteDeliveryServiceResponse {
return &client.DeleteDeliveryServiceResponse{
Alerts: alerts(),
}
}

Expand Down
48 changes: 48 additions & 0 deletions traffic_ops/client/tests/delivery_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,54 @@ func TestUpdateDeliveryServiceUnauthorized(t *testing.T) {
}
}

func TestDeleteDeliveryService(t *testing.T) {
resp := fixtures.DeleteDeliveryService()
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 delete a DeliveryService")

ds, err := to.DeleteDeliveryService("123")
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.Alerts[0].Text
if actual != "text" {
testHelper.Error(t, "Should get back \"text\" for \"Alerts[0].Text\", got: %s", actual)
} else {
testHelper.Success(t, "Should get back \"text\" for \"Alerts[0].Text\"")
}
}

func TestDeleteDeliveryServiceUnauthorized(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 delete a DeliveryService")

_, err := to.DeleteDeliveryService("123")
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)
Expand Down