Skip to content

Commit

Permalink
changing url, response object
Browse files Browse the repository at this point in the history
  • Loading branch information
tauhid621 committed Jan 5, 2024
1 parent 0baf5ae commit f642f2b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .changelog/20015.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```release-note:improvement
api: add a new api(/v1/config/resolved-exported-services) to list exported service and their consumers.
api: add a new api(/v1/exported-services) to list all the exported service and their consumers.
```
13 changes: 10 additions & 3 deletions agent/config_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/consul/acl"
external "github.com/hashicorp/consul/agent/grpc-external"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/proto/private/pbconfigentry"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -181,9 +182,9 @@ func (s *HTTPHandlers) parseEntMetaForConfigEntryKind(kind string, req *http.Req
return s.parseEntMetaNoWildcard(req, entMeta)
}

// ResolvedExportedServices returns all the exported services by resolving wildcards and sameness groups
// ExportedServices returns all the exported services by resolving wildcards and sameness groups
// in the exported services configuration entry
func (s *HTTPHandlers) ResolvedExportedServices(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
func (s *HTTPHandlers) ExportedServices(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
var entMeta acl.EnterpriseMeta
if err := s.parseEntMetaPartition(req, &entMeta); err != nil {
return nil, err
Expand Down Expand Up @@ -214,5 +215,11 @@ func (s *HTTPHandlers) ResolvedExportedServices(resp http.ResponseWriter, req *h
return nil, err
}

return result.Services, nil
svcs := make([]api.ResolvedExportedService, len(result.Services))

for idx, svc := range result.Services {
svcs[idx] = *svc.ToAPI()
}

return svcs, nil
}
16 changes: 8 additions & 8 deletions agent/config_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/proto/private/pbconfigentry"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/testrpc"
)

Expand Down Expand Up @@ -736,7 +736,7 @@ func TestConfig_Apply_ProxyDefaultsExpose(t *testing.T) {
}
}

func TestConfig_Resolved_Exported_Services(t *testing.T) {
func TestConfig_Exported_Services(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}
Expand Down Expand Up @@ -782,27 +782,27 @@ func TestConfig_Resolved_Exported_Services(t *testing.T) {
}

t.Run("exported services", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/v1/config/resolved-exported-services", nil)
req, _ := http.NewRequest("GET", "/v1/exported-services", nil)
resp := httptest.NewRecorder()
raw, err := a.srv.ResolvedExportedServices(resp, req)
raw, err := a.srv.ExportedServices(resp, req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.Code)

services, ok := raw.([]*pbconfigentry.ResolvedExportedService)
services, ok := raw.([]api.ResolvedExportedService)
require.True(t, ok)
require.Len(t, services, 2)
assertIndex(t, resp)

expected := []*pbconfigentry.ResolvedExportedService{
expected := []api.ResolvedExportedService{
{
Service: "api",
Consumers: &pbconfigentry.Consumers{
Consumers: api.ResolvedConsumers{
Peers: []string{"east", "west"},
},
},
{
Service: "db",
Consumers: &pbconfigentry.Consumers{
Consumers: api.ResolvedConsumers{
Peers: []string{"east"},
},
},
Expand Down
2 changes: 1 addition & 1 deletion agent/http_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func init() {
registerEndpoint("/v1/catalog/node/", []string{"GET"}, (*HTTPHandlers).CatalogNodeServices)
registerEndpoint("/v1/catalog/node-services/", []string{"GET"}, (*HTTPHandlers).CatalogNodeServiceList)
registerEndpoint("/v1/catalog/gateway-services/", []string{"GET"}, (*HTTPHandlers).CatalogGatewayServices)
registerEndpoint("/v1/config/resolved-exported-services", []string{"GET"}, (*HTTPHandlers).ResolvedExportedServices)
registerEndpoint("/v1/config/", []string{"GET", "DELETE"}, (*HTTPHandlers).Config)
registerEndpoint("/v1/config", []string{"PUT"}, (*HTTPHandlers).ConfigApply)
registerEndpoint("/v1/connect/ca/configuration", []string{"GET", "PUT"}, (*HTTPHandlers).ConnectCAConfiguration)
Expand All @@ -87,6 +86,7 @@ func init() {
registerEndpoint("/v1/internal/federation-states/mesh-gateways", []string{"GET"}, (*HTTPHandlers).FederationStateListMeshGateways)
registerEndpoint("/v1/internal/federation-state/", []string{"GET"}, (*HTTPHandlers).FederationStateGet)
registerEndpoint("/v1/discovery-chain/", []string{"GET", "POST"}, (*HTTPHandlers).DiscoveryChainRead)
registerEndpoint("/v1/exported-services", []string{"GET"}, (*HTTPHandlers).ExportedServices)
registerEndpoint("/v1/event/fire/", []string{"PUT"}, (*HTTPHandlers).EventFire)
registerEndpoint("/v1/event/list", []string{"GET"}, (*HTTPHandlers).EventList)
registerEndpoint("/v1/health/node/", []string{"GET"}, (*HTTPHandlers).HealthNodeChecks)
Expand Down
21 changes: 20 additions & 1 deletion api/config_entry_exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

package api

import "encoding/json"
import (
"encoding/json"

"github.com/hashicorp/consul/acl"
)

// ExportedServicesConfigEntry manages the exported services for a single admin partition.
// Admin Partitions are a Consul Enterprise feature.
Expand Down Expand Up @@ -80,3 +84,18 @@ func (e *ExportedServicesConfigEntry) MarshalJSON() ([]byte, error) {
}
return json.Marshal(source)
}

type ResolvedExportedService struct {
// Service is the name of the service which is exported.
Service string

acl.EnterpriseMeta `hcl:",squash" mapstructure:",squash"`

// Consumers is a list of downstream consumers of the service.
Consumers ResolvedConsumers
}

type ResolvedConsumers struct {
Peers []string `json:",omitempty"`
Partitions []string `json:",omitempty"`
}
12 changes: 12 additions & 0 deletions proto/private/pbconfigentry/config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/proto/private/pbcommon"
"github.com/hashicorp/consul/types"
)
Expand Down Expand Up @@ -603,3 +604,14 @@ func serviceRefFromStructs(a structs.ServiceRouteReferences) map[string]*ListOfR
}
return m
}

func (r *ResolvedExportedService) ToAPI() *api.ResolvedExportedService {
var t api.ResolvedExportedService

t.Service = r.Service
pbcommon.EnterpriseMetaToStructs(r.EnterpriseMeta, &t.EnterpriseMeta)
t.Consumers.Peers = r.Consumers.Peers
t.Consumers.Partitions = r.Consumers.Partitions

return &t
}

0 comments on commit f642f2b

Please sign in to comment.