-
Notifications
You must be signed in to change notification settings - Fork 70
/
command_deployment.go
166 lines (138 loc) · 6.14 KB
/
command_deployment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package entities
import (
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/newrelic/newrelic-client-go/v2/pkg/nrtime"
"github.com/newrelic/newrelic-cli/internal/output"
"github.com/newrelic/newrelic-cli/internal/utils"
"github.com/newrelic/newrelic-client-go/v2/pkg/changetracking"
"github.com/newrelic/newrelic-client-go/v2/pkg/common"
"github.com/newrelic/newrelic-cli/internal/client"
)
var (
changelog string
commit string
customAttribute []string
customAttributes string
deepLink string
deploymentType string
description string
groupID string
timestamp int64
user string
version string
)
var cmdEntityDeployment = &cobra.Command{
Use: "deployment",
Short: "Manage deployment markers for a New Relic entity",
Long: `Manage deployment markers for a New Relic entity
The deployment command manages deployments for a New Relic entity. Use --help for more information.
`,
Example: "newrelic entity deployment create --guid <GUID> --version <0.0.1>",
}
var cmdEntityDeploymentCreateExample = fmt.Sprintf(`newrelic entity deployment create --guid <GUID> --version <0.0.1> --changelog 'what changed' --commit '12345e' --deepLink <link back to deployer> --deploymentType 'BASIC' --description 'about' --timestamp %v --user 'jenkins-bot'`, time.Now().Unix())
var cmdEntityDeploymentCreate = &cobra.Command{
Use: "create",
Short: "Create a New Relic entity deployment marker",
Long: `Create a New Relic entity deployment marker
The deployment command marks a change for a New Relic entity
`,
Example: cmdEntityDeploymentCreateExample,
PreRun: client.RequireClient,
Run: func(cmd *cobra.Command, args []string) {
params := changetracking.ChangeTrackingDeploymentInput{}
if timestamp == 0 {
params.Timestamp = nrtime.EpochMilliseconds(time.Now())
} else {
params.Timestamp = nrtime.EpochMilliseconds(time.Unix(timestamp, 0))
}
if version == "" {
log.Fatal("--version cannot be empty")
}
var (
attrs map[string]interface{}
err error
)
if len(customAttribute) > 0 || customAttributes != "" {
attrs, err = getCustomAttributes(customAttribute, customAttributes)
if err != nil {
log.Fatal(err)
}
params.CustomAttributes = attrs
}
params.Changelog = changelog
params.Commit = commit
params.DeepLink = deepLink
params.DeploymentType = changetracking.ChangeTrackingDeploymentType(deploymentType)
params.Description = description
params.EntityGUID = common.EntityGUID(entityGUID)
params.GroupId = groupID
params.User = user
params.Version = version
result, err := client.NRClient.ChangeTracking.ChangeTrackingCreateDeploymentWithContext(
utils.SignalCtx,
changetracking.ChangeTrackingDataHandlingRules{ValidationFlags: []changetracking.ChangeTrackingValidationFlag{changetracking.ChangeTrackingValidationFlagTypes.FAIL_ON_FIELD_LENGTH}},
params,
)
utils.LogIfFatal(err)
utils.LogIfFatal(output.Print(result))
},
}
func init() {
Command.AddCommand(cmdEntityDeployment)
cmdEntityDeployment.AddCommand(cmdEntityDeploymentCreate)
cmdEntityDeploymentCreate.Flags().StringVarP(&entityGUID, "guid", "g", "", "the GUID of the entity associated with this deployment. guid is required.")
utils.LogIfError(cmdEntityDeploymentCreate.MarkFlagRequired("guid"))
cmdEntityDeploymentCreate.Flags().StringVarP(&version, "version", "v", "", "the version of the deployed software, for example, something like v1.1. version is required.")
utils.LogIfError(cmdEntityDeploymentCreate.MarkFlagRequired("version"))
cmdEntityDeploymentCreate.Flags().StringVar(&changelog, "changelog", "", "a URL for the changelog or list of changes if not linkable")
cmdEntityDeploymentCreate.Flags().StringVar(&commit, "commit", "", "the commit identifier, for example, a Git commit SHA")
cmdEntityDeploymentCreate.Flags().StringSliceVar(&customAttribute, "customAttribute", []string{}, "(EARLY ACCESS) a comma separated list of key:value custom attributes to apply to the deployment")
_ = cmdEntityDeploymentCreate.Flags().MarkDeprecated("customAttribute", "please use 'customAttributes'")
cmdEntityDeploymentCreate.Flags().StringVar(&customAttributes, "customAttributes", "", "(EARLY ACCESS) key-value pairs of custom attributes in JSON format to apply to the deployment")
_ = cmdEntityDeploymentCreate.Flags().MarkHidden("customAttributes")
cmdEntityDeploymentCreate.Flags().StringVar(&deepLink, "deepLink", "", "a link back to the system generating the deployment")
cmdEntityDeploymentCreate.Flags().StringVar(&deploymentType, "deploymentType", "", "type of deployment, one of BASIC, BLUE_GREEN, CANARY, OTHER, ROLLING or SHADOW")
cmdEntityDeploymentCreate.Flags().StringVar(&description, "description", "", "a description of the deployment")
cmdEntityDeploymentCreate.Flags().StringVar(&groupID, "groupId", "", "string that can be used to correlate two or more events")
cmdEntityDeploymentCreate.Flags().Int64VarP(×tamp, "timestamp", "t", 0, "the start time of the deployment, the number of milliseconds since the Unix epoch, defaults to now")
cmdEntityDeploymentCreate.Flags().StringVarP(&user, "user", "u", "", "username of the deployer or bot")
}
func parseCustomAttributes(a *[]string) (*map[string]interface{}, error) {
if len(*a) < 1 {
return nil, nil
}
customAttributeMap := make(map[string]interface{})
for _, v := range *a {
pair := strings.Split(v, ":")
if len(pair) != 2 {
return nil, errors.New("invalid format, please use comma separated key-value pairs (--customAttribute key1:value1,key2:value2)")
}
customAttributeMap[pair[0]] = pair[1]
}
return &customAttributeMap, nil
}
func getCustomAttributes(a []string, b string) (map[string]interface{}, error) {
var result *map[string]interface{}
if len(a) > 0 && b == "" {
attrsMap, err := parseCustomAttributes(&a)
if err != nil {
return nil, errors.New("unable to parse custom attributes")
}
result = attrsMap
}
if b != "" {
var attrsJSON *map[string]interface{}
err := json.Unmarshal([]byte(b), &attrsJSON)
if err != nil {
return nil, errors.New("unable to unmarshal custom attributes")
}
result = attrsJSON
}
return *result, nil
}