Skip to content
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/test/fixture/output
/my-debug
*.log
/.vscode
/.vscode
/tmp
5 changes: 3 additions & 2 deletions cmd/backup/backup_alertsconditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/IBM/newrelic-cli/cmd/get"
"github.com/IBM/newrelic-cli/newrelic"
"github.com/IBM/newrelic-cli/tracker"
"github.com/IBM/newrelic-cli/utils"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -159,7 +160,7 @@ var alertsconditionsCmd = &cobra.Command{
backupPolicyMeta.FileName = backupFolder + "/all-in-one-bundle.alert-conditions.bak"
} else {
backupPolicyMeta.Policy = strconv.FormatInt(ID, 10)
backupPolicyMeta.FileName = backupFolder + "/" + fileNamePrefix + ".alert-conditions.bak"
backupPolicyMeta.FileName = backupFolder + "/" + utils.FileNameEscape(fileNamePrefix) + ".alert-conditions.bak"
}
backupPolicyMeta.OperationStatus = "fail"
// backupPolicyMeta.PolicyName = policyName
Expand Down Expand Up @@ -242,7 +243,7 @@ var alertsconditionsCmd = &cobra.Command{
if err != nil {
fmt.Println(err)
}
var fileName = backupFolder + "/" + fileNamePrefix + ".alert-conditions.bak"
var fileName = backupFolder + "/" + utils.FileNameEscape(fileNamePrefix) + ".alert-conditions.bak"
err = ioutil.WriteFile(fileName, fileContent, 0666)
if err != nil {
fmt.Println(err)
Expand Down
3 changes: 2 additions & 1 deletion cmd/backup/backup_dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/IBM/newrelic-cli/cmd/get"
"github.com/IBM/newrelic-cli/tracker"
"github.com/IBM/newrelic-cli/utils"
"github.com/spf13/cobra"
"github.com/tidwall/gjson"
"github.com/tidwall/pretty"
Expand Down Expand Up @@ -131,7 +132,7 @@ var dashboardsCmd = &cobra.Command{
id := gjson.Parse(dashboard.String()).Get("id")
title := gjson.Parse(dashboard.String()).Get("title")
name := title.String()
var fileName = backupFolder + "/" + name + "-" + id.String() + ".dashboard.bak"
var fileName = backupFolder + "/" + utils.FileNameEscape(name) + "-" + id.String() + ".dashboard.bak"
if bSingle == true {
fileName = backupFolder + "/all-in-one-bundle.dashboard.bak"
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/backup/backup_monitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/IBM/newrelic-cli/cmd/get"
"github.com/IBM/newrelic-cli/tracker"
"github.com/IBM/newrelic-cli/utils"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -108,7 +109,7 @@ var monitorsCmd = &cobra.Command{
if err != nil {
fmt.Println(err)
}
var fileName = backupFolder + "/" + name + ".monitor.bak"
var fileName = backupFolder + "/" + utils.FileNameEscape(name) + ".monitor.bak"
err = ioutil.WriteFile(fileName, fileContent, 0666)
if err != nil {
fmt.Println(err)
Expand Down
5 changes: 3 additions & 2 deletions cmd/get/get_alertsconditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,11 @@ func GetAllConditionsByAlertPolicyID(id int64) (*newrelic.AlertsConditionList, e
var defaultConditionsLen = len(list.AlertsDefaultConditions)
var externalServiceConditionsLen = len(list.AlertsExternalServiceConditions)
var nrqlConditionsLen = len(list.AlertsNRQLConditions)
var pluginsConditionsLen = len(list.AlertsPluginsConditions)
//var pluginsConditionsLen = len(list.AlertsPluginsConditions)
var syntheticsConditionsLen = len(list.AlertsSyntheticsConditions)

if defaultConditionsLen == 0 && externalServiceConditionsLen == 0 && nrqlConditionsLen == 0 && pluginsConditionsLen == 0 && syntheticsConditionsLen == 0 {
//if defaultConditionsLen == 0 && externalServiceConditionsLen == 0 && nrqlConditionsLen == 0 && pluginsConditionsLen == 0 && syntheticsConditionsLen == 0 {
if defaultConditionsLen == 0 && externalServiceConditionsLen == 0 && nrqlConditionsLen == 0 && syntheticsConditionsLen == 0 {
break
} else {
//merge conditions list
Expand Down
6 changes: 5 additions & 1 deletion cmd/get/get_monitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ func GetMonitors() ([]*newrelic.Monitor, error, tracker.ReturnValue) {
fmt.Printf("Fetching script for Monitor: %s\n", name)
scriptText, resp, err := client.SyntheticsScript.GetByID(context.Background(), id)
<-chTaskCtrl
tracker.AppendRESTCallResult(client.SyntheticsScript, tracker.OPERATION_NAME_GET_MONITOR_SCRIPT, resp.StatusCode, "monitor id: "+id+", monitor name: "+name)
statusCode := -1
if resp != nil {
statusCode = resp.StatusCode
}
tracker.AppendRESTCallResult(client.SyntheticsScript, tracker.OPERATION_NAME_GET_MONITOR_SCRIPT, statusCode, "monitor id: "+id+", monitor name: "+name)
if err != nil {
fmt.Println(err)
r <- nil
Expand Down
3 changes: 2 additions & 1 deletion newrelic/alerts_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func (s *AlertsConditionsService) ListAll(ctx context.Context, opt *AlertsCondit
}

list := new(AlertsConditionList)
cats := []ConditionCategory{ConditionDefault, ConditionPlugins, ConditionExternalService, ConditionSynthetics, ConditionNRQL, ConditionInfrastructure}
//cats := []ConditionCategory{ConditionDefault, ConditionPlugins, ConditionExternalService, ConditionSynthetics, ConditionNRQL, ConditionInfrastructure}
cats := []ConditionCategory{ConditionDefault, ConditionExternalService, ConditionSynthetics, ConditionNRQL, ConditionInfrastructure}
for _, cat := range cats {
// TODO: paralleize and use ctx.Done() to cancel the parent context
listFunc := s.listByCategory(cat)
Expand Down
2 changes: 1 addition & 1 deletion newrelic/alerts_plugins_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type AlertsPluginsCondition struct {
ID *int64 `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Entities []*int64 `json:"entities,omitempty"`
Entities []*string `json:"entities,omitempty"`
MetricDescription *string `json:"metric_description,omitempty"`
Metric *string `json:"metric,omitempty"`
ValueFunction *string `json:"value_function,omitempty"`
Expand Down
12 changes: 7 additions & 5 deletions newrelic/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"reflect"
Expand Down Expand Up @@ -238,19 +238,21 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res

var resp *http.Response
var err error
sleepBase := 30
for retries > 0 {
resp, err = c.client.Do(req)
if err != nil || resp.StatusCode > 299 {
if retries == 1 {
if err == nil {
if resp != nil {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
bodyString := truncateString(string(bodyBytes), 100)
err = errors.New(fmt.Sprintf("%s %s returns status:%d body: %s", req.Method, req.URL.String(), resp.StatusCode, bodyString))
fmt.Printf("%s %s returns status:%d body: %s", req.Method, req.URL.String(), resp.StatusCode, bodyString)
}
log.Println(err)
fmt.Println(err)
break
}
time.Sleep(time.Duration(3) * time.Second)
time.Sleep(time.Duration(sleepBase+rand.Intn(30)) * time.Second)
sleepBase = sleepBase * 2
retries--
} else {
break
Expand Down
2 changes: 1 addition & 1 deletion tracker/result_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func GenerateBackupMonitorMeta(monitorList []*newrelic.Monitor, backupFolder str
if singleFile == true {
m.FileName = backupFolder + "/all-in-one-bundle.monitor.bak"
} else {
m.FileName = backupFolder + "/" + m.Name + ".monitor.bak"
m.FileName = backupFolder + "/" + utils.FileNameEscape(m.Name) + ".monitor.bak"
}
backupMonitorMetaList = append(backupMonitorMetaList, m)
}
Expand Down
7 changes: 7 additions & 0 deletions utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/url"
"os"
"strconv"
"strings"

"github.com/IBM/newrelic-cli/newrelic"
)
Expand Down Expand Up @@ -290,3 +291,9 @@ func MergeAlertChannelList(s1 []*newrelic.AlertsChannel, s2 []*newrelic.AlertsCh

return slice
}

func FileNameEscape(origName string) string {
name := strings.Replace(origName, ":", "%3A", -1)
name = strings.Replace(name, "/", "%2F", -1)
return name
}