Skip to content

Commit

Permalink
fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vpranckaitis committed Nov 6, 2020
1 parent 3247dcc commit f867b1c
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/cmd/tools/dtest/docker/harness/resources/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package resources

import (
"bytes"
"encoding/json"
"context"
"fmt"
"io/ioutil"
"net"
Expand All @@ -32,6 +32,7 @@ import (
"github.com/m3db/m3/src/query/generated/proto/admin"

"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/ory/dockertest"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -240,13 +241,7 @@ func (c *coordinator) CreateDatabase(
zapMethod("createDatabase"), zap.String("url", url),
zap.String("request", addRequest.String()))

b, err := json.Marshal(addRequest)
if err != nil {
logger.Error("failed to marshal", zap.Error(err))
return admin.DatabaseCreateResponse{}, err
}

resp, err := http.Post(url, "application/json", bytes.NewReader(b))
resp, err := makePostRequest(logger, url, &addRequest)
if err != nil {
logger.Error("failed post", zap.Error(err))
return admin.DatabaseCreateResponse{}, err
Expand Down Expand Up @@ -274,15 +269,7 @@ func (c *coordinator) AddNamespace(
zapMethod("addNamespace"), zap.String("url", url),
zap.String("request", addRequest.String()))

marshaller := jsonpb.Marshaler{EmitDefaults: true}
data := bytes.NewBuffer(nil)
err := marshaller.Marshal(data, &addRequest)
if err != nil {
logger.Error("failed to marshal", zap.Error(err))
return admin.NamespaceGetResponse{}, err
}

resp, err := http.Post(url, "application/json", data)
resp, err := makePostRequest(logger, url, &addRequest)
if err != nil {
logger.Error("failed post", zap.Error(err))
return admin.NamespaceGetResponse{}, err
Expand Down Expand Up @@ -333,6 +320,26 @@ func (c *coordinator) WriteCarbon(
// return nil
}

func makePostRequest(logger *zap.Logger, url string, body proto.Message) (*http.Response, error) {
data := bytes.NewBuffer(nil)
if err := (&jsonpb.Marshaler{}).Marshal(data, body); err != nil {
logger.Error("failed to marshal", zap.Error(err))

return &http.Response{}, fmt.Errorf("failed to marshal: %w", err)
}

req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, url, data)
if err != nil {
logger.Error("failed to construct request", zap.Error(err))

return &http.Response{}, fmt.Errorf("failed to construct request: %w", err)
}

req.Header.Add("Content-Type", "application/json")

return http.DefaultClient.Do(req)
}

func (c *coordinator) query(
verifier GoalStateVerifier, query string,
) error {
Expand Down

0 comments on commit f867b1c

Please sign in to comment.