|
|
@ -4,34 +4,24 @@ |
|
|
|
package api |
|
|
|
package api |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
|
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
"testing" |
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
"google.golang.org/protobuf/encoding/protojson" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2" |
|
|
|
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil" |
|
|
|
"github.com/hashicorp/consul/sdk/testutil" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type V2WriteRequest struct { |
|
|
|
type V2WriteRequest struct { |
|
|
|
Metadata map[string]string `json:"metadata"` |
|
|
|
Data map[string]any `json:"data"` |
|
|
|
Data map[string]any `json:"data"` |
|
|
|
|
|
|
|
Owner *pbresource.ID `json:"owner"` |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type V2WriteResponse struct { |
|
|
|
type V2WriteResponse struct { |
|
|
|
Metadata map[string]string `json:"metadata"` |
|
|
|
ID struct { |
|
|
|
Data map[string]any `json:"data"` |
|
|
|
Name string `json:"name"` |
|
|
|
Owner *pbresource.ID `json:"owner,omitempty"` |
|
|
|
} `json:"id"` |
|
|
|
ID *pbresource.ID `json:"id"` |
|
|
|
Data map[string]any `json:"data"` |
|
|
|
Version string `json:"version"` |
|
|
|
|
|
|
|
Generation string `json:"generation"` |
|
|
|
|
|
|
|
Status map[string]any `json:"status"` |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// We are testing a v2 endpoint here in the v1 api module as a temporary measure to
|
|
|
|
// We are testing a v2 endpoint here in the v1 api module as a temporary measure to
|
|
|
@ -47,43 +37,34 @@ func TestAPI_RawV2ExportedServices(t *testing.T) { |
|
|
|
endpoint := strings.ToLower(fmt.Sprintf("/api/multicluster/v2/exportedservices/e1")) |
|
|
|
endpoint := strings.ToLower(fmt.Sprintf("/api/multicluster/v2/exportedservices/e1")) |
|
|
|
wResp := &V2WriteResponse{} |
|
|
|
wResp := &V2WriteResponse{} |
|
|
|
|
|
|
|
|
|
|
|
var consumers []map[string]any |
|
|
|
|
|
|
|
consumers = append(consumers, map[string]any{"peer": "p1"}) |
|
|
|
|
|
|
|
data := map[string]any{"consumers": consumers} |
|
|
|
|
|
|
|
data["services"] = []string{"s1"} |
|
|
|
|
|
|
|
wReq := &V2WriteRequest{ |
|
|
|
wReq := &V2WriteRequest{ |
|
|
|
Metadata: nil, |
|
|
|
Data: map[string]any{ |
|
|
|
Data: data, |
|
|
|
"consumers": []map[string]any{ |
|
|
|
Owner: nil, |
|
|
|
{"peer": "p1"}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
"services": []string{"s1"}, |
|
|
|
|
|
|
|
}, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_, err := c.Raw().Write(endpoint, wReq, wResp, &WriteOptions{Datacenter: "dc1"}) |
|
|
|
_, err := c.Raw().Write(endpoint, wReq, wResp, &WriteOptions{Datacenter: "dc1"}) |
|
|
|
if err != nil { |
|
|
|
require.NoError(t, err) |
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
require.NotEmpty(t, wResp.ID.Name) |
|
|
|
} |
|
|
|
|
|
|
|
if wResp.ID.Name == "" { |
|
|
|
|
|
|
|
t.Fatalf("no write response") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
qOpts := &QueryOptions{Datacenter: "dc1"} |
|
|
|
qOpts := &QueryOptions{Datacenter: "dc1"} |
|
|
|
|
|
|
|
|
|
|
|
var out map[string]interface{} |
|
|
|
var out map[string]interface{} |
|
|
|
_, err = c.Raw().Query(endpoint, &out, qOpts) |
|
|
|
_, err = c.Raw().Query(endpoint, &out, qOpts) |
|
|
|
if err != nil { |
|
|
|
require.NoError(t, err) |
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
require.Equal(t, map[string]any{ |
|
|
|
respData, _ := json.Marshal(out["data"]) |
|
|
|
"consumers": []any{ |
|
|
|
readData := &pbmulticluster.ExportedServices{} |
|
|
|
map[string]any{"peer": "p1"}, |
|
|
|
if err = protojson.Unmarshal(respData, readData); err != nil { |
|
|
|
}, |
|
|
|
t.Fatalf("invalid read response") |
|
|
|
"services": []any{"s1"}, |
|
|
|
} |
|
|
|
}, out["data"]) |
|
|
|
if len(readData.Services) != 1 { |
|
|
|
|
|
|
|
t.Fatalf("incorrect resource data") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_, err = c.Raw().Delete(endpoint, qOpts) |
|
|
|
_, err = c.Raw().Delete(endpoint, qOpts) |
|
|
|
if err != nil { |
|
|
|
require.NoError(t, err) |
|
|
|
t.Fatalf("err: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out = make(map[string]interface{}) |
|
|
|
out = make(map[string]interface{}) |
|
|
|
_, err = c.Raw().Query(endpoint, &out, qOpts) |
|
|
|
_, err = c.Raw().Query(endpoint, &out, qOpts) |
|
|
|