From 1b150f9cac7a6a75089c525fe4579ff6d2e6f82f Mon Sep 17 00:00:00 2001 From: Bridget Lane Date: Thu, 19 Oct 2017 15:04:01 -0400 Subject: [PATCH] add key transactions endpoints --- api/key_transactions.go | 56 +++++++++++++++++++++++++++++++++++ api/key_transactions_test.go | 48 ++++++++++++++++++++++++++++++ api/types.go | 13 ++++++++ cmd/key_transactions.go | 57 ++++++++++++++++++++++++++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 api/key_transactions.go create mode 100644 api/key_transactions_test.go create mode 100644 cmd/key_transactions.go diff --git a/api/key_transactions.go b/api/key_transactions.go new file mode 100644 index 00000000..2d368360 --- /dev/null +++ b/api/key_transactions.go @@ -0,0 +1,56 @@ +package api + +import ( + "fmt" + "net/url" +) + +func (c *Client) queryKeyTransactions() ([]KeyTransaction, error) { + transactions := []KeyTransaction{} + + reqURL, err := url.Parse("/key_transactions.json") + if err != nil { + return nil, err + } + + nextPath := reqURL.String() + + for nextPath != "" { + resp := struct { + Transactions []KeyTransaction `json:"key_transactions,omitempty"` + }{} + + nextPath, err = c.Do("GET", nextPath, nil, &resp) + if err != nil { + return nil, err + } + + transactions = append(transactions, resp.Transactions...) + } + + return transactions, nil +} + +// GetKeyTransaction returns a specific key transaction by ID. +func (c *Client) GetKeyTransaction(id int) (*KeyTransaction, error) { + reqURL, err := url.Parse(fmt.Sprintf("/key_transactions/%v.json", id)) + if err != nil { + return nil, err + } + + resp := struct { + Transaction KeyTransaction `json:"key_transaction,omitempty"` + }{} + + _, err = c.Do("GET", reqURL.String(), nil, &resp) + if err != nil { + return nil, err + } + + return &resp.Transaction, nil +} + +// ListKeyTransactions returns all key transactions for the account. +func (c *Client) ListKeyTransactions() ([]KeyTransaction, error) { + return c.queryKeyTransactions() +} diff --git a/api/key_transactions_test.go b/api/key_transactions_test.go new file mode 100644 index 00000000..e7e5db39 --- /dev/null +++ b/api/key_transactions_test.go @@ -0,0 +1,48 @@ +package api + +import ( + "net/http" + "testing" +) + +func TestKeyTransactions_Basic(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(` + { + "key_transactions": [{ + "id": 1, + "name": "foo", + "transaction_name": "/bar", + "health_status": "unknown", + "reporting": true, + "last_reported_at": "2017-10-19T18:16:08+00:00", + "application_summary": { + "response_time": 0.0, + "throughput": 0.0, + "error_rate": 0, + "apdex_target": 0.5, + "apdex_score": 0.0 + }, + "links": { + "application": 2 + } + }], + "links": { + "key_transaction.application": "/v2/applications/{application_id}" + } + } + `)) + })) + + apps, err := c.queryKeyTransactions() + if err != nil { + t.Log(err) + t.Fatal("queryKeyTransactions error") + } + + if len(apps) == 0 { + t.Fatal("No applications found") + } +} diff --git a/api/types.go b/api/types.go index 799a45e5..8b210e4c 100644 --- a/api/types.go +++ b/api/types.go @@ -233,3 +233,16 @@ type ComponentMetric struct { Name string `json:"name,omitempty"` Values []string `json:"values"` } + +// KeyTransaction represents information about a New Relic key transaction. +type KeyTransaction struct { + ID int `json:"id,omitempty"` + Name string `json:"name,omitempty"` + TransactionName string `json:"transaction_name,omitempty"` + HealthStatus string `json:"health_status,omitempty"` + Reporting bool `json:"reporting,omitempty"` + LastReportedAt string `json:"last_reported_at,omitempty"` + Summary ApplicationSummary `json:"application_summary,omitempty"` + EndUserSummary ApplicationEndUserSummary `json:"end_user_summary,omitempty"` + Links ApplicationLinks `json:"links,omitempty"` +} diff --git a/cmd/key_transactions.go b/cmd/key_transactions.go new file mode 100644 index 00000000..10ecc5fc --- /dev/null +++ b/cmd/key_transactions.go @@ -0,0 +1,57 @@ +package cmd + +import ( + "github.com/imdario/mergo" + "github.com/paultyng/go-newrelic/api" + "github.com/spf13/cobra" +) + +func makeKeyTransactionsCmd(dst cobra.Command) *cobra.Command { + src := cobra.Command{ + Use: "key-transactions", + Aliases: []string{"transactions", "kt"}, + } + + if err := mergo.Merge(&dst, src); err != nil { + panic(err) + } + + return &dst +} + +var getKeyTransactionsCmd = makeKeyTransactionsCmd(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 + } + + var resources []api.KeyTransaction + + if id != 0 { + resource, err := client.GetKeyTransaction(id) + if err != nil { + return err + } + + resources = []api.KeyTransaction{*resource} + } else { + resources, err = client.ListKeyTransactions() + if err != nil { + return err + } + } + + return outputList(cmd, resources) + }, +}) + +func init() { + getCmd.AddCommand(getKeyTransactionsCmd) + getKeyTransactionsCmd.Flags().Int("id", 0, "ID of the key transaction to get") +}