2015-11-12 01:27:51 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2018-09-06 10:34:28 +00:00
|
|
|
cachetype "github.com/hashicorp/consul/agent/cache-types"
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2015-11-12 01:27:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// preparedQueryCreateResponse is used to wrap the query ID.
|
|
|
|
type preparedQueryCreateResponse struct {
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
|
2015-11-17 17:16:04 +00:00
|
|
|
// preparedQueryCreate makes a new prepared query.
|
|
|
|
func (s *HTTPServer) preparedQueryCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
args := structs.PreparedQueryRequest{
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
}
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
|
|
|
s.parseToken(req, &args.Token)
|
2018-01-10 17:33:49 +00:00
|
|
|
if err := decodeBody(req, &args.Query, nil); err != nil {
|
|
|
|
resp.WriteHeader(http.StatusBadRequest)
|
|
|
|
fmt.Fprintf(resp, "Request decode failed: %v", err)
|
|
|
|
return nil, nil
|
2015-11-17 17:16:04 +00:00
|
|
|
}
|
2015-11-12 01:27:51 +00:00
|
|
|
|
2015-11-17 17:16:04 +00:00
|
|
|
var reply string
|
2017-06-16 07:54:09 +00:00
|
|
|
if err := s.agent.RPC("PreparedQuery.Apply", &args, &reply); err != nil {
|
2015-11-17 17:16:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return preparedQueryCreateResponse{reply}, nil
|
|
|
|
}
|
2015-11-12 01:27:51 +00:00
|
|
|
|
2015-11-17 17:16:04 +00:00
|
|
|
// preparedQueryList returns all the prepared queries.
|
|
|
|
func (s *HTTPServer) preparedQueryList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
var args structs.DCSpecificRequest
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-11-12 01:27:51 +00:00
|
|
|
|
2015-11-17 17:16:04 +00:00
|
|
|
var reply structs.IndexedPreparedQueries
|
2018-03-30 15:14:44 +00:00
|
|
|
defer setMeta(resp, &reply.QueryMeta)
|
|
|
|
RETRY_ONCE:
|
2017-06-16 07:54:09 +00:00
|
|
|
if err := s.agent.RPC("PreparedQuery.List", &args, &reply); err != nil {
|
2015-11-17 17:16:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-30 15:14:44 +00:00
|
|
|
if args.QueryOptions.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < reply.LastContact {
|
|
|
|
args.AllowStale = false
|
|
|
|
args.MaxStaleDuration = 0
|
|
|
|
goto RETRY_ONCE
|
|
|
|
}
|
|
|
|
reply.ConsistencyLevel = args.QueryOptions.ConsistencyLevel()
|
2015-11-15 05:59:23 +00:00
|
|
|
|
2015-11-17 17:16:04 +00:00
|
|
|
// Use empty list instead of nil.
|
|
|
|
if reply.Queries == nil {
|
|
|
|
reply.Queries = make(structs.PreparedQueries, 0)
|
|
|
|
}
|
|
|
|
return reply.Queries, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PreparedQueryGeneral handles all the general prepared query requests.
|
|
|
|
func (s *HTTPServer) PreparedQueryGeneral(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
switch req.Method {
|
|
|
|
case "POST":
|
|
|
|
return s.preparedQueryCreate(resp, req)
|
|
|
|
|
|
|
|
case "GET":
|
|
|
|
return s.preparedQueryList(resp, req)
|
2015-11-12 01:27:51 +00:00
|
|
|
|
|
|
|
default:
|
2017-09-26 06:11:19 +00:00
|
|
|
return nil, MethodNotAllowedError{req.Method, []string{"GET", "POST"}}
|
2015-11-12 01:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseLimit parses the optional limit parameter for a prepared query execution.
|
|
|
|
func parseLimit(req *http.Request, limit *int) error {
|
|
|
|
*limit = 0
|
|
|
|
if arg := req.URL.Query().Get("limit"); arg != "" {
|
2017-04-21 01:59:42 +00:00
|
|
|
i, err := strconv.Atoi(arg)
|
|
|
|
if err != nil {
|
2015-11-12 01:27:51 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
*limit = i
|
2015-11-12 01:27:51 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-17 17:16:04 +00:00
|
|
|
// preparedQueryExecute executes a prepared query.
|
|
|
|
func (s *HTTPServer) preparedQueryExecute(id string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
args := structs.PreparedQueryExecuteRequest{
|
|
|
|
QueryIDOrName: id,
|
2016-06-30 23:51:18 +00:00
|
|
|
Agent: structs.QuerySource{
|
|
|
|
Node: s.agent.config.NodeName,
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
2017-09-25 18:40:42 +00:00
|
|
|
Segment: s.agent.config.SegmentName,
|
2016-06-30 23:51:18 +00:00
|
|
|
},
|
2015-11-17 17:16:04 +00:00
|
|
|
}
|
|
|
|
s.parseSource(req, &args.Source)
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if err := parseLimit(req, &args.Limit); err != nil {
|
|
|
|
return nil, fmt.Errorf("Bad limit: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-06-05 23:04:50 +00:00
|
|
|
params := req.URL.Query()
|
|
|
|
if raw := params.Get("connect"); raw != "" {
|
|
|
|
val, err := strconv.ParseBool(raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error parsing 'connect' value: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args.Connect = val
|
|
|
|
}
|
|
|
|
|
2015-11-17 17:16:04 +00:00
|
|
|
var reply structs.PreparedQueryExecuteResponse
|
2018-03-30 15:14:44 +00:00
|
|
|
defer setMeta(resp, &reply.QueryMeta)
|
2018-09-06 10:34:28 +00:00
|
|
|
|
|
|
|
if args.QueryOptions.UseCache {
|
|
|
|
raw, m, err := s.agent.cache.Get(cachetype.PreparedQueryName, &args)
|
|
|
|
if err != nil {
|
|
|
|
// Don't return error if StaleIfError is set and we are within it and had
|
|
|
|
// a cached value.
|
|
|
|
if raw != nil && m.Hit && args.QueryOptions.StaleIfError > m.Age {
|
|
|
|
// Fall through to the happy path below
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer setCacheMeta(resp, &m)
|
|
|
|
r, ok := raw.(*structs.PreparedQueryExecuteResponse)
|
|
|
|
if !ok {
|
|
|
|
// This should never happen, but we want to protect against panics
|
|
|
|
return nil, fmt.Errorf("internal error: response type not correct")
|
|
|
|
}
|
|
|
|
reply = *r
|
|
|
|
} else {
|
|
|
|
RETRY_ONCE:
|
|
|
|
if err := s.agent.RPC("PreparedQuery.Execute", &args, &reply); err != nil {
|
|
|
|
// We have to check the string since the RPC sheds
|
|
|
|
// the specific error type.
|
|
|
|
if err.Error() == consul.ErrQueryNotFound.Error() {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
fmt.Fprint(resp, err.Error())
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if args.QueryOptions.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < reply.LastContact {
|
|
|
|
args.AllowStale = false
|
|
|
|
args.MaxStaleDuration = 0
|
|
|
|
goto RETRY_ONCE
|
2015-11-17 17:16:04 +00:00
|
|
|
}
|
2018-03-30 15:14:44 +00:00
|
|
|
}
|
|
|
|
reply.ConsistencyLevel = args.QueryOptions.ConsistencyLevel()
|
2015-11-17 17:16:04 +00:00
|
|
|
|
2016-08-15 22:05:02 +00:00
|
|
|
// Note that we translate using the DC that the results came from, since
|
|
|
|
// a query can fail over to a different DC than where the execute request
|
|
|
|
// was sent to. That's why we use the reply's DC and not the one from
|
|
|
|
// the args.
|
2017-06-30 19:42:53 +00:00
|
|
|
s.agent.TranslateAddresses(reply.Datacenter, reply.Nodes)
|
2016-08-15 22:05:02 +00:00
|
|
|
|
2015-11-17 17:16:04 +00:00
|
|
|
// Use empty list instead of nil.
|
|
|
|
if reply.Nodes == nil {
|
|
|
|
reply.Nodes = make(structs.CheckServiceNodes, 0)
|
|
|
|
}
|
|
|
|
return reply, nil
|
|
|
|
}
|
|
|
|
|
2016-03-04 01:30:36 +00:00
|
|
|
// preparedQueryExplain shows which query a name resolves to, the fully
|
|
|
|
// interpolated template (if it's a template), as well as additional info
|
|
|
|
// about the execution of a query.
|
|
|
|
func (s *HTTPServer) preparedQueryExplain(id string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-03-03 09:04:12 +00:00
|
|
|
args := structs.PreparedQueryExecuteRequest{
|
|
|
|
QueryIDOrName: id,
|
2016-06-30 23:51:18 +00:00
|
|
|
Agent: structs.QuerySource{
|
|
|
|
Node: s.agent.config.NodeName,
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
2017-09-25 18:40:42 +00:00
|
|
|
Segment: s.agent.config.SegmentName,
|
2016-06-30 23:51:18 +00:00
|
|
|
},
|
2016-03-03 09:04:12 +00:00
|
|
|
}
|
2016-03-04 01:30:36 +00:00
|
|
|
s.parseSource(req, &args.Source)
|
2016-03-03 09:04:12 +00:00
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-03-04 01:30:36 +00:00
|
|
|
if err := parseLimit(req, &args.Limit); err != nil {
|
|
|
|
return nil, fmt.Errorf("Bad limit: %s", err)
|
|
|
|
}
|
2016-03-03 09:04:12 +00:00
|
|
|
|
2016-03-04 01:30:36 +00:00
|
|
|
var reply structs.PreparedQueryExplainResponse
|
2018-03-30 15:14:44 +00:00
|
|
|
defer setMeta(resp, &reply.QueryMeta)
|
|
|
|
RETRY_ONCE:
|
2017-06-16 07:54:09 +00:00
|
|
|
if err := s.agent.RPC("PreparedQuery.Explain", &args, &reply); err != nil {
|
2016-03-03 09:04:12 +00:00
|
|
|
// We have to check the string since the RPC sheds
|
|
|
|
// the specific error type.
|
|
|
|
if err.Error() == consul.ErrQueryNotFound.Error() {
|
2017-08-23 19:19:11 +00:00
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
fmt.Fprint(resp, err.Error())
|
2016-03-03 09:04:12 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-30 15:14:44 +00:00
|
|
|
if args.QueryOptions.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < reply.LastContact {
|
|
|
|
args.AllowStale = false
|
|
|
|
args.MaxStaleDuration = 0
|
|
|
|
goto RETRY_ONCE
|
|
|
|
}
|
|
|
|
reply.ConsistencyLevel = args.QueryOptions.ConsistencyLevel()
|
2016-03-03 09:04:12 +00:00
|
|
|
return reply, nil
|
|
|
|
}
|
|
|
|
|
2015-11-17 17:16:04 +00:00
|
|
|
// preparedQueryGet returns a single prepared query.
|
|
|
|
func (s *HTTPServer) preparedQueryGet(id string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
args := structs.PreparedQuerySpecificRequest{
|
|
|
|
QueryID: id,
|
|
|
|
}
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var reply structs.IndexedPreparedQueries
|
2018-03-30 15:14:44 +00:00
|
|
|
defer setMeta(resp, &reply.QueryMeta)
|
|
|
|
RETRY_ONCE:
|
2017-06-16 07:54:09 +00:00
|
|
|
if err := s.agent.RPC("PreparedQuery.Get", &args, &reply); err != nil {
|
2015-11-17 17:16:04 +00:00
|
|
|
// We have to check the string since the RPC sheds
|
|
|
|
// the specific error type.
|
|
|
|
if err.Error() == consul.ErrQueryNotFound.Error() {
|
2017-08-23 19:19:11 +00:00
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
fmt.Fprint(resp, err.Error())
|
2015-11-17 17:16:04 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-30 15:14:44 +00:00
|
|
|
if args.QueryOptions.AllowStale && args.MaxStaleDuration > 0 && args.MaxStaleDuration < reply.LastContact {
|
|
|
|
args.AllowStale = false
|
|
|
|
args.MaxStaleDuration = 0
|
|
|
|
goto RETRY_ONCE
|
|
|
|
}
|
|
|
|
reply.ConsistencyLevel = args.QueryOptions.ConsistencyLevel()
|
2015-11-17 17:16:04 +00:00
|
|
|
return reply.Queries, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// preparedQueryUpdate updates a prepared query.
|
|
|
|
func (s *HTTPServer) preparedQueryUpdate(id string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
args := structs.PreparedQueryRequest{
|
|
|
|
Op: structs.PreparedQueryUpdate,
|
|
|
|
}
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
|
|
|
s.parseToken(req, &args.Token)
|
|
|
|
if req.ContentLength > 0 {
|
|
|
|
if err := decodeBody(req, &args.Query, nil); err != nil {
|
2017-08-23 19:19:11 +00:00
|
|
|
resp.WriteHeader(http.StatusBadRequest)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
fmt.Fprintf(resp, "Request decode failed: %v", err)
|
2015-11-17 17:16:04 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 06:11:19 +00:00
|
|
|
if args.Query == nil {
|
|
|
|
args.Query = &structs.PreparedQuery{}
|
|
|
|
}
|
|
|
|
|
2015-11-17 17:16:04 +00:00
|
|
|
// Take the ID from the URL, not the embedded one.
|
|
|
|
args.Query.ID = id
|
|
|
|
|
|
|
|
var reply string
|
2017-06-16 07:54:09 +00:00
|
|
|
if err := s.agent.RPC("PreparedQuery.Apply", &args, &reply); err != nil {
|
2015-11-17 17:16:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// preparedQueryDelete deletes prepared query.
|
|
|
|
func (s *HTTPServer) preparedQueryDelete(id string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
args := structs.PreparedQueryRequest{
|
|
|
|
Op: structs.PreparedQueryDelete,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
ID: id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
|
|
|
s.parseToken(req, &args.Token)
|
|
|
|
|
|
|
|
var reply string
|
2017-06-16 07:54:09 +00:00
|
|
|
if err := s.agent.RPC("PreparedQuery.Apply", &args, &reply); err != nil {
|
2015-11-17 17:16:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-02-18 01:33:21 +00:00
|
|
|
// PreparedQuerySpecificOptions handles OPTIONS requests to prepared query endpoints.
|
|
|
|
func (s *HTTPServer) preparedQuerySpecificOptions(resp http.ResponseWriter, req *http.Request) interface{} {
|
|
|
|
path := req.URL.Path
|
|
|
|
switch {
|
|
|
|
case strings.HasSuffix(path, "/execute"):
|
|
|
|
resp.Header().Add("Allow", strings.Join([]string{"OPTIONS", "GET"}, ","))
|
|
|
|
return resp
|
|
|
|
|
|
|
|
case strings.HasSuffix(path, "/explain"):
|
|
|
|
resp.Header().Add("Allow", strings.Join([]string{"OPTIONS", "GET"}, ","))
|
|
|
|
return resp
|
|
|
|
|
|
|
|
default:
|
|
|
|
resp.Header().Add("Allow", strings.Join([]string{"OPTIONS", "GET", "PUT", "DELETE"}, ","))
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 17:16:04 +00:00
|
|
|
// PreparedQuerySpecific handles all the prepared query requests specific to a
|
2015-11-12 01:27:51 +00:00
|
|
|
// particular query.
|
|
|
|
func (s *HTTPServer) PreparedQuerySpecific(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2018-02-18 01:33:21 +00:00
|
|
|
if req.Method == "OPTIONS" {
|
|
|
|
return s.preparedQuerySpecificOptions(resp, req), nil
|
|
|
|
}
|
|
|
|
|
2017-09-26 06:11:19 +00:00
|
|
|
path := req.URL.Path
|
|
|
|
id := strings.TrimPrefix(path, "/v1/query/")
|
2015-11-12 01:27:51 +00:00
|
|
|
|
2017-09-26 06:11:19 +00:00
|
|
|
switch {
|
|
|
|
case strings.HasSuffix(path, "/execute"):
|
|
|
|
if req.Method != "GET" {
|
|
|
|
return nil, MethodNotAllowedError{req.Method, []string{"GET"}}
|
2015-11-12 01:27:51 +00:00
|
|
|
}
|
2017-09-26 06:11:19 +00:00
|
|
|
id = strings.TrimSuffix(id, "/execute")
|
|
|
|
return s.preparedQueryExecute(id, resp, req)
|
2015-11-12 01:27:51 +00:00
|
|
|
|
2017-09-26 06:11:19 +00:00
|
|
|
case strings.HasSuffix(path, "/explain"):
|
|
|
|
if req.Method != "GET" {
|
|
|
|
return nil, MethodNotAllowedError{req.Method, []string{"GET"}}
|
|
|
|
}
|
|
|
|
id = strings.TrimSuffix(id, "/explain")
|
|
|
|
return s.preparedQueryExplain(id, resp, req)
|
2015-11-12 01:27:51 +00:00
|
|
|
|
|
|
|
default:
|
2017-09-26 06:11:19 +00:00
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
|
|
|
return s.preparedQueryGet(id, resp, req)
|
|
|
|
|
|
|
|
case "PUT":
|
|
|
|
return s.preparedQueryUpdate(id, resp, req)
|
|
|
|
|
|
|
|
case "DELETE":
|
|
|
|
return s.preparedQueryDelete(id, resp, req)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, MethodNotAllowedError{req.Method, []string{"GET", "PUT", "DELETE"}}
|
|
|
|
}
|
2015-11-12 01:27:51 +00:00
|
|
|
}
|
|
|
|
}
|