-
Notifications
You must be signed in to change notification settings - Fork 56
/
create.go
186 lines (160 loc) · 7.46 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package elasticsearch
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 an Elasticsearch logging endpoint.
type CreateCommand struct {
cmd.Base
Manifest manifest.Data
// required
EndpointName string // Can't shadow cmd.Base method Name().
Index string
URL string
ServiceName cmd.OptionalServiceNameID
ServiceVersion cmd.OptionalServiceVersion
// optional
AutoClone cmd.OptionalAutoClone
Pipeline cmd.OptionalString
RequestMaxEntries cmd.OptionalUint
RequestMaxBytes cmd.OptionalUint
User cmd.OptionalString
Password cmd.OptionalString
TLSCACert cmd.OptionalString
TLSClientCert cmd.OptionalString
TLSClientKey cmd.OptionalString
TLSHostname cmd.OptionalString
Format cmd.OptionalString
FormatVersion cmd.OptionalUint
Placement cmd.OptionalString
ResponseCondition 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 an Elasticsearch logging endpoint on a Fastly service version").Alias("add")
c.CmdClause.Flag("name", "The name of the Elasticsearch 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("index", `The name of the Elasticsearch index to send documents (logs) to. The index must follow the Elasticsearch index format rules (https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html). We support strftime (http://man7.org/linux/man-pages/man3/strftime.3.html) interpolated variables inside braces prefixed with a pound symbol. For example, #{%F} will interpolate as YYYY-MM-DD with today's date`).Required().StringVar(&c.Index)
c.CmdClause.Flag("url", "The URL to stream logs to. Must use HTTPS.").Required().StringVar(&c.URL)
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("pipeline", "The ID of the Elasticsearch ingest pipeline to apply pre-process transformations to before indexing. For example my_pipeline_id. Learn more about creating a pipeline in the Elasticsearch docs (https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest.html)").Action(c.Password.Set).StringVar(&c.Pipeline.Value)
c.CmdClause.Flag("tls-ca-cert", "A secure certificate to authenticate the server with. Must be in PEM format").Action(c.TLSCACert.Set).StringVar(&c.TLSCACert.Value)
c.CmdClause.Flag("tls-client-cert", "The client certificate used to make authenticated requests. Must be in PEM format").Action(c.TLSClientCert.Set).StringVar(&c.TLSClientCert.Value)
c.CmdClause.Flag("tls-client-key", "The client private key used to make authenticated requests. Must be in PEM format").Action(c.TLSClientKey.Set).StringVar(&c.TLSClientKey.Value)
c.CmdClause.Flag("tls-hostname", "The hostname used to verify the server's certificate. It can either be the Common Name or a Subject Alternative Name (SAN)").Action(c.TLSHostname.Set).StringVar(&c.TLSHostname.Value)
c.CmdClause.Flag("format", "Apache style log formatting. Your log must produce valid JSON that Elasticsearch can ingest").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("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)
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("request-max-entries", "Maximum number of logs to append to a batch, if non-zero. Defaults to 0 for unbounded").Action(c.RequestMaxEntries.Set).UintVar(&c.RequestMaxEntries.Value)
c.CmdClause.Flag("request-max-bytes", "Maximum size of log batch, if non-zero. Defaults to 0 for unbounded").Action(c.RequestMaxBytes.Set).UintVar(&c.RequestMaxBytes.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.CreateElasticsearchInput, error) {
var input fastly.CreateElasticsearchInput
input.ServiceID = serviceID
input.ServiceVersion = serviceVersion
input.Name = c.EndpointName
input.Index = c.Index
input.URL = c.URL
if c.Pipeline.WasSet {
input.Pipeline = c.Pipeline.Value
}
if c.RequestMaxEntries.WasSet {
input.RequestMaxEntries = c.RequestMaxEntries.Value
}
if c.RequestMaxBytes.WasSet {
input.RequestMaxBytes = c.RequestMaxBytes.Value
}
if c.User.WasSet {
input.User = c.User.Value
}
if c.Password.WasSet {
input.Password = c.Password.Value
}
if c.TLSCACert.WasSet {
input.TLSCACert = c.TLSCACert.Value
}
if c.TLSClientCert.WasSet {
input.TLSClientCert = c.TLSClientCert.Value
}
if c.TLSClientKey.WasSet {
input.TLSClientKey = c.TLSClientKey.Value
}
if c.TLSHostname.WasSet {
input.TLSHostname = c.TLSHostname.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.CreateElasticsearch(input)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}
text.Success(out, "Created Elasticsearch logging endpoint %s (service %s version %d)", d.Name, d.ServiceID, d.ServiceVersion)
return nil
}