-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cafcea6
commit 0ddedc3
Showing
4 changed files
with
180 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters