Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaji-dgraph committed Apr 18, 2024
1 parent cafcea6 commit 0ddedc3
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 56 deletions.
7 changes: 4 additions & 3 deletions check_upgrade/check_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func queryACLNodes(ctx context.Context, dg *dgo.Dgraph) ([]map[string][]string,
Nodes []ACLNode `json:"nodes"`
}
var result Nodes
if err := json.Unmarshal([]byte(resp.Json), &result); err != nil {
if err := json.Unmarshal(resp.Json, &result); err != nil {
return nil, errors.Wrapf(err, "while unmarshalling response: %v", string(resp.Json))

}
Expand Down Expand Up @@ -161,7 +161,7 @@ func checkUpgrade() error {
return errors.Wrapf(err, "while setting up clients")
}

hc.LoginIntoNamespace(dgraphtest.DefaultUser, dgraphtest.DefaultPassword, x.GalaxyNamespace)
err = hc.LoginIntoNamespace(dgraphtest.DefaultUser, dgraphtest.DefaultPassword, x.GalaxyNamespace)
if err != nil {
return errors.Wrapf(err, "while logging into namespace: %v", x.GalaxyNamespace)
}
Expand Down Expand Up @@ -206,5 +206,6 @@ func checkUpgrade() error {
return nil
}
func parseInput() *commandInput {
return &commandInput{alphaGrpc: CheckUpgrade.Conf.GetString(alphaGrpc), alphaHttp: CheckUpgrade.Conf.GetString(alphaHttp)}
return &commandInput{alphaGrpc: CheckUpgrade.Conf.GetString(alphaGrpc),
alphaHttp: CheckUpgrade.Conf.GetString(alphaHttp)}
}
175 changes: 175 additions & 0 deletions check_upgrade/dgraph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* Copyright 2024 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package checkupgrade

import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"

"github.com/dgraph-io/dgraph/graphql/schema"
"github.com/dgraph-io/dgraph/x"
"github.com/pkg/errors"
)

type GraphQLParams struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
Extensions *schema.RequestExtensions `json:"extensions,omitempty"`
}

type GraphQLResponse struct {
Data json.RawMessage `json:"data,omitempty"`
Errors x.GqlErrorList `json:"errors,omitempty"`
Code string `json:"code"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}

var client *http.Client = &http.Client{
Timeout: 120 * time.Second,
}

type HTTPClient struct {
url string
accessToken string
}

func doReq(req *http.Request) ([]byte, error) {
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "error performing HTTP request")
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Printf("[WARNING] error closing response body: %v", err)
}
}()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrapf(err, "error reading response body: url: [%v], err: [%v]", req.URL, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("got non 200 resp: %v", string(respBody))
}
return respBody, nil
}

func doPost(body []byte, url string, contentType string, accessJwt string) ([]byte, error) {
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(body))
if err != nil {
return nil, errors.Wrapf(err, "error building req for endpoint [%v]", url)
}
req.Header.Add("Content-Type", contentType)

if accessJwt != "" {
req.Header.Add("X-Dgraph-AccessToken", accessJwt)
}

return doReq(req)
}

func (hc *HTTPClient) RunGraphqlQuery(params GraphQLParams) ([]byte, error) {
reqBody, err := json.Marshal(params)
if err != nil {
return nil, errors.Wrap(err, "error while marshalling params")
}

respBody, err := doPost(reqBody, hc.url, "application/json", hc.accessToken)
if err != nil {
return nil, errors.Wrap(err, "error while running graphql query")
}

var gqlResp GraphQLResponse
if err := json.Unmarshal(respBody, &gqlResp); err != nil {
return nil, errors.Wrap(err, "error unmarshalling GQL response")
}
if len(gqlResp.Errors) > 0 {
return nil, errors.Wrapf(gqlResp.Errors, "error while running graphql query, resp: %v", string(gqlResp.Data))
}
return gqlResp.Data, nil
}

func (hc *HTTPClient) LoginIntoNamespace(user, password string, ns uint64) error {

q := `mutation login($userId: String, $password: String, $namespace: Int) {
login(userId: $userId, password: $password, namespace: $namespace) {
response {
accessJWT
refreshJWT
}
}
}`
params := GraphQLParams{
Query: q,
Variables: map[string]interface{}{
"userId": user,
"password": password,
"namespace": ns,
},
}

resp, err := hc.RunGraphqlQuery(params)
if err != nil {
return err
}
var r struct {
Login struct {
Response struct {
AccessJWT string
RefreshJwt string
}
}
}
if err := json.Unmarshal(resp, &r); err != nil {
return errors.Wrap(err, "error unmarshalling response into object")
}
if r.Login.Response.AccessJWT == "" {
return errors.Errorf("no access JWT found in the response")
}

hc.accessToken = r.Login.Response.AccessJWT
return nil
}

func (hc *HTTPClient) ListNamespaces(url string, token string) ([]uint64, error) {
const listNss = `{ state {
namespaces
}
}`

params := GraphQLParams{Query: listNss}
resp, err := hc.RunGraphqlQuery(params)
if err != nil {
return nil, err
}

var result struct {
State struct {
Namespaces []uint64 `json:"namespaces"`
} `json:"state"`
}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, errors.Wrap(err, "error unmarshalling response")
}

return result.State.Namespaces, nil
}
52 changes: 0 additions & 52 deletions systest/integration2/acl_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion testutil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func init() {
SockAddrZero4 = ContainerAddr("zero2", 5080)
SockAddrZero4Http = ContainerAddr("zero2", 6080)

fmt.Printf("testutil: %q %s %s\n", DockerPrefix, SockAddr, SockAddrZero)
// fmt.Printf("testutil: %q %s %s\n", DockerPrefix, SockAddr, SockAddrZero)
}

// DgraphClientDropAll creates a Dgraph client and drops all existing data.
Expand Down

0 comments on commit 0ddedc3

Please sign in to comment.