|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package conn |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "net/http" |
| 26 | + "path" |
| 27 | + |
| 28 | + "github.com/arangodb/go-driver" |
| 29 | + "github.com/arangodb/kube-arangodb/pkg/util/constants" |
| 30 | + "github.com/arangodb/kube-arangodb/pkg/util/errors" |
| 31 | +) |
| 32 | + |
| 33 | +func NewAsyncConnection(c driver.Connection) driver.Connection { |
| 34 | + return async{ |
| 35 | + connectionPass: connectionPass{ |
| 36 | + c: c, |
| 37 | + wrap: asyncConnectionWrap, |
| 38 | + }, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func asyncConnectionWrap(c driver.Connection) (driver.Connection, error) { |
| 43 | + return NewAsyncConnection(c), nil |
| 44 | +} |
| 45 | + |
| 46 | +type async struct { |
| 47 | + connectionPass |
| 48 | +} |
| 49 | + |
| 50 | +func (a async) isAsyncIDSet(ctx context.Context) (string, bool) { |
| 51 | + if ctx != nil { |
| 52 | + if q := ctx.Value(asyncOperatorContextKey); q != nil { |
| 53 | + if v, ok := q.(string); ok { |
| 54 | + return v, true |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return "", false |
| 60 | +} |
| 61 | + |
| 62 | +func (a async) Do(ctx context.Context, req driver.Request) (driver.Response, error) { |
| 63 | + if id, ok := a.isAsyncIDSet(ctx); ok { |
| 64 | + // We have ID Set, request should be done to fetch job id |
| 65 | + req, err := a.c.NewRequest(http.MethodPut, path.Join("/_api/job", id)) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + resp, err := a.c.Do(ctx, req) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + switch resp.StatusCode() { |
| 76 | + case http.StatusNotFound: |
| 77 | + return nil, newAsyncErrorNotFound(id) |
| 78 | + case http.StatusNoContent: |
| 79 | + return nil, newAsyncJobInProgress(id) |
| 80 | + default: |
| 81 | + return resp, nil |
| 82 | + } |
| 83 | + } else { |
| 84 | + req.SetHeader(constants.ArangoHeaderAsyncKey, constants.ArangoHeaderAsyncValue) |
| 85 | + |
| 86 | + resp, err := a.c.Do(ctx, req) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + switch resp.StatusCode() { |
| 92 | + case http.StatusAccepted: |
| 93 | + if v := resp.Header(constants.ArangoHeaderAsyncIDKey); len(v) == 0 { |
| 94 | + return nil, errors.Newf("Missing async key response") |
| 95 | + } else { |
| 96 | + return nil, newAsyncJobInProgress(v) |
| 97 | + } |
| 98 | + default: |
| 99 | + return nil, resp.CheckStatus(http.StatusAccepted) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments