Skip to content
This repository has been archived by the owner on Oct 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #66 from paultyng/delete-operations
Browse files Browse the repository at this point in the history
add delete operations for applications and deployments
  • Loading branch information
ctrombley authored Nov 1, 2019
2 parents 5cfeee9 + 0c6b56c commit 6ec43a8
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api/applications.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"fmt"
"net/url"
"strconv"
)
Expand Down Expand Up @@ -57,3 +58,10 @@ func (c *Client) queryApplications(filters applicationsFilters) ([]Application,
func (c *Client) ListApplications() ([]Application, error) {
return c.queryApplications(applicationsFilters{})
}

// DeleteApplication deletes a non-reporting application from your account.
func (c *Client) DeleteApplication(id int) error {
u := &url.URL{Path: fmt.Sprintf("/applications/%v.json", id)}
_, err := c.Do("DELETE", u.String(), nil, nil)
return err
}
37 changes: 37 additions & 0 deletions api/applications_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"fmt"
"net/http"
"testing"
)
Expand Down Expand Up @@ -35,3 +36,39 @@ func TestQueryApplications_Basic(t *testing.T) {
t.Fatal("No applications found")
}
}

func TestDeleteApplication(t *testing.T) {
c := newTestAPIClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`
{
"application": {
"id": 123,
"name": "test",
"language": "go",
"health_status": "gray",
"reporting": false,
"settings": {
"app_apdex_threshold": 0.5,
"end_user_apdex_threshold": 7,
"enable_real_user_monitoring": true,
"use_server_side_config": false
},
"links": {
"application_instances": [],
"servers": [],
"application_hosts": []
}
}
}
`))
}))

err := c.DeleteApplication(123)
if err != nil {
fmt.Printf("Error: %s", err)
t.Log(err)
t.Fatal("DeleteApplication error")
}
}
7 changes: 7 additions & 0 deletions api/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ func (c *Client) CreateDeployment(applicationID int, deployment Deployment) (*De

return &resp.Deployment, nil
}

// DeleteDeployment deletes an application deployment from an application.
func (c *Client) DeleteDeployment(applicationID, deploymentID int) error {
u := &url.URL{Path: fmt.Sprintf("/applications/%v/deployments/%v.json", applicationID, deploymentID)}
_, err := c.Do("DELETE", u.String(), nil, nil)
return err
}
34 changes: 34 additions & 0 deletions api/deployments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,37 @@ func TestCreateDeployment(t *testing.T) {
t.Fatal("Revision was not parsed correctly")
}
}

func TestDeleteDeployment(t *testing.T) {
c := newTestAPIClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`
{
"deployment": {
"id": 456,
"revision": "master",
"changelog": "Foo Bar",
"description": "12345678-1234-1234-1234-1234567890ab",
"user": "foo",
"timestamp": "2019-10-31T15:25:38-07:00",
"links": {
"application": 123
}
},
"links": {
"deployment.agent": "/v2/applications/{application_id}"
}
}
`))
}))

appID := 123
depID := 456

err := c.DeleteDeployment(appID, depID)
if err != nil {
t.Log(err)
t.Fatal("CreateDeployment error")
}
}
29 changes: 29 additions & 0 deletions cmd/applications.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"fmt"

"github.com/imdario/mergo"
"github.com/spf13/cobra"
)
Expand All @@ -24,6 +26,7 @@ var getApplicationsCmd = makeApplicationsCmd(cobra.Command{
if err != nil {
return err
}

resources, err := client.ListApplications()
if err != nil {
return err
Expand All @@ -33,6 +36,32 @@ var getApplicationsCmd = makeApplicationsCmd(cobra.Command{
},
})

var deleteApplicationCmd = makeApplicationsCmd(cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
client, err := newAPIClient(cmd)
if err != nil {
return err
}

id, err := cmd.Flags().GetInt("id")
if err != nil {
return err
}

err = client.DeleteApplication(id)
if err != nil {
return err
}

fmt.Printf("Application '%v' deleted.\n", id)

return nil
},
})

func init() {
getCmd.AddCommand(getApplicationsCmd)

deleteCmd.AddCommand(deleteApplicationCmd)
deleteApplicationCmd.Flags().Int("id", 0, "ID of the application to delete")
}
34 changes: 34 additions & 0 deletions cmd/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,34 @@ var createDeploymentCmd = makeDeploymentsCmd(cobra.Command{
},
})

var deleteDeploymentCmd = makeDeploymentsCmd(cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
client, err := newAPIClient(cmd)
if err != nil {
return err
}

appId, err := cmd.Flags().GetInt("application-id")
if err != nil {
return err
}

depId, err := cmd.Flags().GetInt("deployment-id")
if err != nil {
return err
}

err = client.DeleteDeployment(appId, depId)
if err != nil {
return err
}

fmt.Printf("Application deployment '%v' deleted.\n", depId)

return nil
},
})

func init() {
getCmd.AddCommand(getDeploymentsCmd)
getDeploymentsCmd.Flags().IntP("application-id", "a", 0, "application ID of the deployments to get")
Expand All @@ -102,4 +130,10 @@ func init() {
createDeploymentCmd.Flags().StringP("description", "d", "", "Description of the deployment")
createDeploymentCmd.MarkFlagRequired("application-id")
createDeploymentCmd.MarkFlagRequired("revision")

deleteCmd.AddCommand(deleteDeploymentCmd)
deleteDeploymentCmd.Flags().IntP("application-id", "a", 0, "application ID of the deployment to delete")
deleteDeploymentCmd.Flags().IntP("deployment-id", "d", 0, "deployment ID of the deployment to delete")
deleteDeploymentCmd.MarkFlagRequired("application-id")
deleteDeploymentCmd.MarkFlagRequired("deployment-id")
}

0 comments on commit 6ec43a8

Please sign in to comment.