Skip to content

Commit

Permalink
feat(#17): adds transaction_detection_rule resource
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryEMartland committed Aug 11, 2020
1 parent 2050d9e commit 6331fa5
Show file tree
Hide file tree
Showing 13 changed files with 1,082 additions and 5 deletions.
2 changes: 1 addition & 1 deletion appdynamics/client/health_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *AppDClient) GetHealthRule(healthRuleId int, applicationId int) (*Health

if resp.Response().StatusCode != 200 {
respString, _ := resp.ToString()
return nil, errors.New(fmt.Sprintf("Error getting Health Rule: %d, %s, %s", resp.Response().StatusCode, c.createHealthRuleUrl(healthRuleId, applicationId), respString))
return nil, errors.New(fmt.Sprintf("Error getting Health TransactionRule: %d, %s, %s", resp.Response().StatusCode, c.createHealthRuleUrl(healthRuleId, applicationId), respString))
}

updated := HealthRule{}
Expand Down
188 changes: 188 additions & 0 deletions appdynamics/client/transaction_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package client

import (
"errors"
"fmt"
"github.com/imroc/req"
)

func (c *AppDClient) GetTransactionRules(applicationId int) (*TransactionRules, error) {

resp, err := req.Get(c.createGetTransactionRuleUrl(applicationId), c.createAuthHeader())
if err != nil {
return nil, err
}

if resp.Response().StatusCode != 200 {
respString, _ := resp.ToString()
return nil, errors.New(fmt.Sprintf("Error getting Transaction configs: %d, %s", resp.Response().StatusCode, respString))
}

var updated TransactionRules
err = resp.ToJSON(&updated)
if err != nil {
return nil, err
}

return &updated, nil
}

func (c *AppDClient) GetTransactionRule(applicationId int, transactionRuleId string) (*RuleScope, bool, error) {

rules, err := c.GetTransactionRules(applicationId)

if err != nil {
return nil, false, err
}

for _, n := range rules.RuleScopeSummaryMappings {
if transactionRuleId == n.Rule.Summary.Id {
return n, true, nil
}
}

return nil, false, nil
}

func (c *AppDClient) CreateTransactionRule(applicationId int, rule *TransactionRule) (*UpdateResult, error) {

resp, err := req.Post(c.createCreateTransactionRuleUrl(), c.createAuthHeader(), req.QueryParam{"scopeId": "", "applicationId": applicationId}, req.BodyJSON(rule))
if err != nil {
return nil, err
}

if resp.Response().StatusCode != 200 {
respString, _ := resp.ToString()
return nil, errors.New(fmt.Sprintf("Error creating Transaction rule: %d, %s", resp.Response().StatusCode, respString))
}

var updated UpdateResult
err = resp.ToJSON(&updated)
if err != nil {
return nil, err
}

return &updated, nil
}

func (c *AppDClient) DeleteTransactionRules(transactionRuleIds []string) (*UpdateResult, error) {

resp, err := req.Post(c.createDeleteTransactionRuleUrl(), c.createAuthHeader(), req.BodyJSON(transactionRuleIds))
if err != nil {
return nil, err
}

if resp.Response().StatusCode != 200 {
respString, _ := resp.ToString()
return nil, errors.New(fmt.Sprintf("Error deleting Transaction rule: %d, %s", resp.Response().StatusCode, respString))
}

var updated UpdateResult
err = resp.ToJSON(&updated)
if err != nil {
return nil, err
}

return &updated, nil
}

func (c *AppDClient) UpdateTransactionRule(applicationId int, transactionConfig *TransactionRule) (*UpdateResult, error) {

resp, err := req.Post(c.createUpdateTransactionRuleUrl(), c.createAuthHeader(), req.QueryParam{"scopeId": "", "applicationId": applicationId}, req.BodyJSON(transactionConfig))
if err != nil {
return nil, err
}

if resp.Response().StatusCode != 200 {
respString, _ := resp.ToString()
return nil, errors.New(fmt.Sprintf("Error deleting Transaction rule: %d, %s", resp.Response().StatusCode, respString))
}

var updated UpdateResult
err = resp.ToJSON(&updated)
if err != nil {
return nil, err
}

return &updated, nil
}

func (c *AppDClient) createGetTransactionRuleUrl(applicationId int) string {
return fmt.Sprintf("%s/controller/restui/transactionConfigProto/getRules/%d", c.BaseUrl, applicationId)
}

func (c *AppDClient) createCreateTransactionRuleUrl() string {
return fmt.Sprintf("%s/controller/restui/transactionConfigProto/createRule", c.BaseUrl)
}

func (c *AppDClient) createDeleteTransactionRuleUrl() string {
return fmt.Sprintf("%s/controller/restui/transactionConfigProto/deleteRules", c.BaseUrl)
}

func (c *AppDClient) createUpdateTransactionRuleUrl() string {
return fmt.Sprintf("%s/controller/restui/transactionConfigProto/updateRule", c.BaseUrl)
}

type UpdateResult struct {
ResultType string `json:"resultType"`
Successes []*Success `json:"successes"`
}

type Success struct {
Summary *Summary `json:"summary"`
}

type TransactionRules struct {
RuleScopeSummaryMappings []*RuleScope `json:"ruleScopeSummaryMappings"`
}

type RuleScope struct {
Rule *TransactionRule `json:"rule"`
}

type TransactionRule struct {
Type string `json:"type"`
Summary *Summary `json:"summary"`
Enabled bool `json:"enabled"`
Priority int `json:"priority"`
Version *int `json:"version,omitempty"`
AgentType string `json:"agentType"`
TxMatchRule *TxMatchRule `json:"txMatchRule"`
}

type Summary struct {
Id string `json:"id"`
Type string `json:"type"`
AccountId string `json:"accountId"`
Name string `json:"name"`
Description string `json:"description"`
CreatedOn *int `json:"createdOn,omitempty"`
UpdatedOn *int `json:"updatedOn,omitempty"`
}

type TxMatchRule struct {
AgentType string `json:"agentType"`
Type string `json:"type"`
TxCustomRule *TxCustomRule `json:"txCustomRule"`
}

type TxCustomRule struct {
Type string `json:"type"`
TxEntryPointType string `json:"txEntryPointType"`
MatchConditions []*MatchCondition `json:"matchConditions"`
}

type MatchCondition struct {
Type string `json:"type"`
HttpMatch *HttpMatch `json:"httpMatch"`
}

type HttpMatch struct {
Uri *Uri `json:"uri"`
HttpMethod string `json:"httpMethod"`
}

type Uri struct {
Type string `json:"type"`
MatchStrings []interface{} `json:"matchStrings"`
}
35 changes: 35 additions & 0 deletions appdynamics/client/transaction_rule_expected_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"type": "TX_MATCH_RULE",
"enabled": true,
"priority": 1,
"agentType": "NODE_JS_SERVER",
"summary": {
"type": "com.appdynamics.platform.services.configuration.impl.persistenceapi.model.ConfigRuleEntity",
"accountId": "ad14e72e-da84-11ea-87d0-0242ac130003",
"id": "",
"name": "test",
"description": "test description"
},
"txMatchRule": {
"agentType": "NODE_JS_SERVER",
"type": "CUSTOM",
"txCustomRule": {
"type": "INCLUDE",
"txEntryPointType": "NODEJS_WEB",
"matchConditions": [
{
"type": "HTTP",
"httpMatch": {
"uri": {
"type": "EQUALS",
"matchStrings": [
"/bob"
]
},
"httpMethod": "POST"
}
}
]
}
}
}
12 changes: 12 additions & 0 deletions appdynamics/client/transaction_rule_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"resultType": "SUCCESS",
"successes": [
{
"summary": {
"id": "878bdfc0-da7b-11ea-87d0-0242ac130003",
"type": "com.appdynamics.platform.services.configuration.impl.persistenceapi.model.ConfigRuleEntity",
"accountId": "8c469528-da7b-11ea-87d0-0242ac130003"
}
}
]
}
Loading

0 comments on commit 6331fa5

Please sign in to comment.