2024-03-05 14:52:55 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type V2WriteRequest struct {
|
2024-09-23 20:14:39 +00:00
|
|
|
Data map[string]any `json:"data"`
|
2024-03-05 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type V2WriteResponse struct {
|
2024-09-23 20:14:39 +00:00
|
|
|
ID struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
} `json:"id"`
|
|
|
|
Data map[string]any `json:"data"`
|
2024-03-05 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We are testing a v2 endpoint here in the v1 api module as a temporary measure to
|
|
|
|
// support v2 CRUD operations, until we have a final design for v2 api clients.
|
|
|
|
func TestAPI_RawV2ExportedServices(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) {
|
|
|
|
conf.EnableDebug = true
|
|
|
|
})
|
|
|
|
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
endpoint := strings.ToLower(fmt.Sprintf("/api/multicluster/v2/exportedservices/e1"))
|
|
|
|
wResp := &V2WriteResponse{}
|
|
|
|
|
|
|
|
wReq := &V2WriteRequest{
|
2024-09-23 20:14:39 +00:00
|
|
|
Data: map[string]any{
|
|
|
|
"consumers": []map[string]any{
|
|
|
|
{"peer": "p1"},
|
|
|
|
},
|
|
|
|
"services": []string{"s1"},
|
|
|
|
},
|
2024-03-05 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := c.Raw().Write(endpoint, wReq, wResp, &WriteOptions{Datacenter: "dc1"})
|
2024-09-23 20:14:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, wResp.ID.Name)
|
2024-03-05 14:52:55 +00:00
|
|
|
|
|
|
|
qOpts := &QueryOptions{Datacenter: "dc1"}
|
2024-09-23 20:14:39 +00:00
|
|
|
|
2024-03-05 14:52:55 +00:00
|
|
|
var out map[string]interface{}
|
|
|
|
_, err = c.Raw().Query(endpoint, &out, qOpts)
|
2024-09-23 20:14:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, map[string]any{
|
|
|
|
"consumers": []any{
|
|
|
|
map[string]any{"peer": "p1"},
|
|
|
|
},
|
|
|
|
"services": []any{"s1"},
|
|
|
|
}, out["data"])
|
2024-03-05 14:52:55 +00:00
|
|
|
|
|
|
|
_, err = c.Raw().Delete(endpoint, qOpts)
|
2024-09-23 20:14:39 +00:00
|
|
|
require.NoError(t, err)
|
2024-03-05 14:52:55 +00:00
|
|
|
|
|
|
|
out = make(map[string]interface{})
|
|
|
|
_, err = c.Raw().Query(endpoint, &out, qOpts)
|
|
|
|
require.ErrorContains(t, err, "404")
|
|
|
|
}
|