open-consul/agent/federation_state_endpoint.go
Ronald dd0e8eec14
copyright headers for agent folder (#16704)
* copyright headers for agent folder

* Ignore test data files

* fix proto files and remove headers in agent/uiserver folder

* ignore deep-copy files
2023-03-28 14:39:22 -04:00

95 lines
2.6 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package agent
import (
"net/http"
"strings"
"github.com/hashicorp/consul/agent/structs"
)
// GET /v1/internal/federation-state/<datacenter>
func (s *HTTPHandlers) FederationStateGet(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
datacenterName := strings.TrimPrefix(req.URL.Path, "/v1/internal/federation-state/")
if datacenterName == "" {
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing datacenter name"}
}
args := structs.FederationStateQuery{
Datacenter: datacenterName,
}
if done := s.parse(resp, req, &args.TargetDatacenter, &args.QueryOptions); done {
return nil, nil
}
var out structs.FederationStateResponse
defer setMeta(resp, &out.QueryMeta)
if err := s.agent.RPC(req.Context(), "FederationState.Get", &args, &out); err != nil {
return nil, err
}
if out.State == nil {
resp.WriteHeader(http.StatusNotFound)
return nil, nil
}
return out, nil
}
// GET /v1/internal/federation-states
func (s *HTTPHandlers) FederationStateList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
var args structs.DCSpecificRequest
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
return nil, nil
}
if args.Datacenter == "" {
args.Datacenter = s.agent.config.Datacenter
}
var out structs.IndexedFederationStates
defer setMeta(resp, &out.QueryMeta)
if err := s.agent.RPC(req.Context(), "FederationState.List", &args, &out); err != nil {
return nil, err
}
// make sure we return an array and not nil
if out.States == nil {
out.States = make(structs.FederationStates, 0)
}
return out.States, nil
}
// GET /v1/internal/federation-states/mesh-gateways
func (s *HTTPHandlers) FederationStateListMeshGateways(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
var args structs.DCSpecificRequest
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
return nil, nil
}
if args.Datacenter == "" {
args.Datacenter = s.agent.config.Datacenter
}
var out structs.DatacenterIndexedCheckServiceNodes
defer setMeta(resp, &out.QueryMeta)
if err := s.agent.RPC(req.Context(), "FederationState.ListMeshGateways", &args, &out); err != nil {
return nil, err
}
// make sure we return a arrays and not nils
if out.DatacenterNodes == nil {
out.DatacenterNodes = make(map[string]structs.CheckServiceNodes)
}
for dc, nodes := range out.DatacenterNodes {
if nodes == nil {
out.DatacenterNodes[dc] = make(structs.CheckServiceNodes, 0)
}
}
return out.DatacenterNodes, nil
}