-
Notifications
You must be signed in to change notification settings - Fork 56
/
create.go
137 lines (119 loc) · 4.7 KB
/
create.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
package scalyr
import (
"io"
"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v6/fastly"
)
// CreateCommand calls the Fastly API to create a Scalyr logging endpoint.
type CreateCommand struct {
cmd.Base
Manifest manifest.Data
// required
EndpointName string // Can't shadow cmd.Base method Name().
Token string
ServiceName cmd.OptionalServiceNameID
ServiceVersion cmd.OptionalServiceVersion
// optional
AutoClone cmd.OptionalAutoClone
Region cmd.OptionalString
Format cmd.OptionalString
FormatVersion cmd.OptionalUint
ResponseCondition cmd.OptionalString
Placement cmd.OptionalString
}
// NewCreateCommand returns a usable command registered under the parent.
func NewCreateCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *CreateCommand {
var c CreateCommand
c.Globals = globals
c.Manifest = data
c.CmdClause = parent.Command("create", "Create a Scalyr logging endpoint on a Fastly service version").Alias("add")
c.CmdClause.Flag("name", "The name of the Scalyr logging object. Used as a primary key for API access").Short('n').Required().StringVar(&c.EndpointName)
c.RegisterFlag(cmd.StringFlagOpts{
Name: cmd.FlagVersionName,
Description: cmd.FlagVersionDesc,
Dst: &c.ServiceVersion.Value,
Required: true,
})
c.RegisterAutoCloneFlag(cmd.AutoCloneFlagOpts{
Action: c.AutoClone.Set,
Dst: &c.AutoClone.Value,
})
c.CmdClause.Flag("auth-token", "The token to use for authentication (https://www.scalyr.com/keys)").Required().StringVar(&c.Token)
c.RegisterFlag(cmd.StringFlagOpts{
Name: cmd.FlagServiceIDName,
Description: cmd.FlagServiceIDDesc,
Dst: &c.Manifest.Flag.ServiceID,
Short: 's',
})
c.RegisterFlag(cmd.StringFlagOpts{
Action: c.ServiceName.Set,
Name: cmd.FlagServiceName,
Description: cmd.FlagServiceDesc,
Dst: &c.ServiceName.Value,
})
c.CmdClause.Flag("region", "The region that log data will be sent to. One of US or EU. Defaults to US if undefined").Action(c.Region.Set).StringVar(&c.Region.Value)
c.CmdClause.Flag("format", "Apache style log formatting").Action(c.Format.Set).StringVar(&c.Format.Value)
c.CmdClause.Flag("format-version", "The version of the custom logging format used for the configured endpoint. Can be either 2 (default) or 1").Action(c.FormatVersion.Set).UintVar(&c.FormatVersion.Value)
c.CmdClause.Flag("response-condition", "The name of an existing condition in the configured endpoint, or leave blank to always execute").Action(c.ResponseCondition.Set).StringVar(&c.ResponseCondition.Value)
c.CmdClause.Flag("placement", "Where in the generated VCL the logging call should be placed, overriding any format_version default. Can be none or waf_debug").Action(c.Placement.Set).StringVar(&c.Placement.Value)
return &c
}
// ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library.
func (c *CreateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.CreateScalyrInput, error) {
var input fastly.CreateScalyrInput
input.ServiceID = serviceID
input.ServiceVersion = serviceVersion
input.Name = c.EndpointName
input.Token = c.Token
if c.Region.WasSet {
input.Region = c.Region.Value
}
if c.Format.WasSet {
input.Format = c.Format.Value
}
if c.FormatVersion.WasSet {
input.FormatVersion = c.FormatVersion.Value
}
if c.ResponseCondition.WasSet {
input.ResponseCondition = c.ResponseCondition.Value
}
if c.Placement.WasSet {
input.Placement = c.Placement.Value
}
return &input, nil
}
// Exec invokes the application logic for the command.
func (c *CreateCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, serviceVersion, err := cmd.ServiceDetails(cmd.ServiceDetailsOpts{
AutoCloneFlag: c.AutoClone,
APIClient: c.Globals.APIClient,
Manifest: c.Manifest,
Out: out,
ServiceNameFlag: c.ServiceName,
ServiceVersionFlag: c.ServiceVersion,
VerboseMode: c.Globals.Flag.Verbose,
})
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]interface{}{
"Service ID": serviceID,
"Service Version": errors.ServiceVersion(serviceVersion),
})
return err
}
input, err := c.ConstructInput(serviceID, serviceVersion.Number)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}
d, err := c.Globals.APIClient.CreateScalyr(input)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}
text.Success(out, "Created Scalyr logging endpoint %s (service %s version %d)", d.Name, d.ServiceID, d.ServiceVersion)
return nil
}