-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into e2eImprovements
- Loading branch information
Showing
11 changed files
with
241 additions
and
66 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,54 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/pkg/errors" | ||
|
||
api_types "github.com/kumahq/kuma/api/openapi/types" | ||
util_http "github.com/kumahq/kuma/pkg/util/http" | ||
) | ||
|
||
type ResourcesListClient interface { | ||
List(ctx context.Context) (api_types.ResourceTypeDescriptionList, error) | ||
} | ||
|
||
var _ ResourcesListClient = &HTTPResourcesListClient{} | ||
|
||
type HTTPResourcesListClient struct { | ||
client util_http.Client | ||
} | ||
|
||
func NewHTTPResourcesListClient(client util_http.Client) ResourcesListClient { | ||
return &HTTPResourcesListClient{ | ||
client: client, | ||
} | ||
} | ||
|
||
func (h HTTPResourcesListClient) List(ctx context.Context) (api_types.ResourceTypeDescriptionList, error) { | ||
req, err := http.NewRequest("GET", "/_resources", nil) | ||
if err != nil { | ||
return api_types.ResourceTypeDescriptionList{}, err | ||
} | ||
req.Header.Set("Accept", "application/json") | ||
resp, err := h.client.Do(req.WithContext(ctx)) | ||
if err != nil { | ||
return api_types.ResourceTypeDescriptionList{}, err | ||
} | ||
defer resp.Body.Close() | ||
b, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return api_types.ResourceTypeDescriptionList{}, err | ||
} | ||
if resp.StatusCode != 200 { | ||
return api_types.ResourceTypeDescriptionList{}, errors.Errorf("unexpected status code: %d %s", resp.StatusCode, b) | ||
} | ||
list := api_types.ResourceTypeDescriptionList{} | ||
if err := json.Unmarshal(b, &list); err != nil { | ||
return api_types.ResourceTypeDescriptionList{}, err | ||
} | ||
return list, nil | ||
} |
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,62 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
util_http "github.com/kumahq/kuma/pkg/util/http" | ||
) | ||
|
||
var _ = Describe("Resources Client", func() { | ||
It("should return resources list", func() { | ||
// given | ||
mux := http.NewServeMux() | ||
server := httptest.NewServer(mux) | ||
defer server.Close() | ||
mux.HandleFunc("/_resources", func(writer http.ResponseWriter, req *http.Request) { | ||
defer GinkgoRecover() | ||
Expect(req.Header.Get("accept")).To(Equal("application/json")) | ||
|
||
resp := `{ | ||
"resources": [ | ||
{ | ||
"includeInDump": true, | ||
"name": "CircuitBreaker", | ||
"path": "circuit-breakers", | ||
"pluralDisplayName": "Circuit Breakers", | ||
"policy": { | ||
"hasFromTargetRef": false, | ||
"hasToTargetRef": false, | ||
"isTargetRef": false | ||
}, | ||
"readOnly": false, | ||
"scope": "Mesh", | ||
"singularDisplayName": "Circuit Breaker" | ||
} | ||
] | ||
}` | ||
|
||
_, err := writer.Write([]byte(resp)) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
serverURL, err := url.Parse(server.URL) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
resListClient := NewHTTPResourcesListClient( | ||
util_http.ClientWithBaseURL(http.DefaultClient, serverURL, nil), | ||
) | ||
|
||
// when | ||
obj, err := resListClient.List(context.Background()) | ||
|
||
// then | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(obj.Resources).To(HaveLen(1)) | ||
Expect(obj.Resources[0].Name).To(Equal("CircuitBreaker")) | ||
}) | ||
}) |
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
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
Oops, something went wrong.