2023-03-28 20:12:41 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-05-02 18:49:05 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-06-08 22:53:32 +00:00
|
|
|
"time"
|
2022-05-02 18:49:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PeeringState enumerates all the states a peering can be in
|
2022-05-25 18:43:35 +00:00
|
|
|
type PeeringState string
|
2022-05-02 18:49:05 +00:00
|
|
|
|
|
|
|
const (
|
2022-05-05 21:15:42 +00:00
|
|
|
// PeeringStateUndefined represents an unset value for PeeringState during
|
2022-05-02 18:49:05 +00:00
|
|
|
// writes.
|
2022-05-25 18:43:35 +00:00
|
|
|
PeeringStateUndefined PeeringState = "UNDEFINED"
|
|
|
|
|
2022-07-04 14:47:58 +00:00
|
|
|
// PeeringStatePending means the peering was created by generating a peering token.
|
|
|
|
// Peerings stay in a pending state until the peer uses the token to dial
|
|
|
|
// the local cluster.
|
|
|
|
PeeringStatePending PeeringState = "PENDING"
|
|
|
|
|
|
|
|
// PeeringStateEstablishing means the peering is being established from a peering token.
|
|
|
|
// This is the initial state for dialing peers.
|
|
|
|
PeeringStateEstablishing PeeringState = "ESTABLISHING"
|
2022-05-25 18:43:35 +00:00
|
|
|
|
|
|
|
// PeeringStateActive means that the peering connection is active and
|
|
|
|
// healthy.
|
|
|
|
PeeringStateActive PeeringState = "ACTIVE"
|
|
|
|
|
|
|
|
// PeeringStateFailing means the peering connection has been interrupted
|
|
|
|
// but has not yet been terminated.
|
|
|
|
PeeringStateFailing PeeringState = "FAILING"
|
|
|
|
|
2022-07-04 14:47:58 +00:00
|
|
|
// PeeringStateDeleting means a peering was marked for deletion and is in the process
|
|
|
|
// of being deleted.
|
|
|
|
PeeringStateDeleting PeeringState = "DELETING"
|
|
|
|
|
2022-05-25 18:43:35 +00:00
|
|
|
// PeeringStateTerminated means the peering relationship has been removed.
|
|
|
|
PeeringStateTerminated PeeringState = "TERMINATED"
|
2022-05-02 18:49:05 +00:00
|
|
|
)
|
|
|
|
|
2022-10-05 13:10:19 +00:00
|
|
|
type PeeringRemoteInfo struct {
|
|
|
|
// Partition is the remote peer's partition.
|
|
|
|
Partition string
|
|
|
|
// Datacenter is the remote peer's datacenter.
|
|
|
|
Datacenter string
|
2023-03-10 14:36:15 +00:00
|
|
|
Locality *Locality
|
2023-03-07 19:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Locality identifies where a given entity is running.
|
|
|
|
type Locality struct {
|
|
|
|
// Region is region the zone belongs to.
|
|
|
|
Region string
|
|
|
|
|
|
|
|
// Zone is the zone the entity is running in.
|
|
|
|
Zone string
|
2022-10-05 13:10:19 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 18:49:05 +00:00
|
|
|
type Peering struct {
|
|
|
|
// ID is a datacenter-scoped UUID for the peering.
|
|
|
|
ID string
|
|
|
|
// Name is the local alias for the peering relationship.
|
|
|
|
Name string
|
|
|
|
// Partition is the local partition connecting to the peer.
|
2022-05-25 18:43:35 +00:00
|
|
|
Partition string `json:",omitempty"`
|
2022-06-08 22:53:32 +00:00
|
|
|
// DeletedAt is the time when the Peering was marked for deletion
|
|
|
|
DeletedAt *time.Time `json:",omitempty" alias:"deleted_at"`
|
2022-05-09 20:47:37 +00:00
|
|
|
// Meta is a mapping of some string value to any other string value
|
|
|
|
Meta map[string]string `json:",omitempty"`
|
2022-05-02 18:49:05 +00:00
|
|
|
// State is one of the valid PeeringState values to represent the status of
|
|
|
|
// peering relationship.
|
|
|
|
State PeeringState
|
2022-05-25 18:43:35 +00:00
|
|
|
// PeerID is the ID that our peer assigned to this peering. This ID is to
|
|
|
|
// be used when dialing the peer, so that it can know who dialed it.
|
|
|
|
PeerID string `json:",omitempty"`
|
2022-05-02 18:49:05 +00:00
|
|
|
// PeerCAPems contains all the CA certificates for the remote peer.
|
2022-05-25 18:43:35 +00:00
|
|
|
PeerCAPems []string `json:",omitempty"`
|
2022-05-02 18:49:05 +00:00
|
|
|
// PeerServerName is the name of the remote server as it relates to TLS.
|
2022-05-25 18:43:35 +00:00
|
|
|
PeerServerName string `json:",omitempty"`
|
2022-05-02 18:49:05 +00:00
|
|
|
// PeerServerAddresses contains all the connection addresses for the remote peer.
|
2022-05-25 18:43:35 +00:00
|
|
|
PeerServerAddresses []string `json:",omitempty"`
|
2022-09-23 21:51:41 +00:00
|
|
|
// StreamStatus contains information computed on read based on the state of the stream.
|
|
|
|
StreamStatus PeeringStreamStatus
|
2022-05-02 18:49:05 +00:00
|
|
|
// CreateIndex is the Raft index at which the Peering was created.
|
|
|
|
CreateIndex uint64
|
2022-10-05 13:10:19 +00:00
|
|
|
// ModifyIndex is the latest Raft index at which the Peering was modified.
|
2022-05-02 18:49:05 +00:00
|
|
|
ModifyIndex uint64
|
2022-10-05 13:10:19 +00:00
|
|
|
// Remote contains metadata for the remote peer.
|
|
|
|
Remote PeeringRemoteInfo
|
2022-05-02 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 21:51:41 +00:00
|
|
|
type PeeringStreamStatus struct {
|
|
|
|
// ImportedServices is the list of services imported from this peering.
|
|
|
|
ImportedServices []string
|
|
|
|
// ExportedServices is the list of services exported to this peering.
|
|
|
|
ExportedServices []string
|
|
|
|
// LastHeartbeat represents when the last heartbeat message was received.
|
2022-11-03 18:51:22 +00:00
|
|
|
LastHeartbeat *time.Time
|
2022-09-23 21:51:41 +00:00
|
|
|
// LastReceive represents when any message was last received, regardless of success or error.
|
2022-11-03 18:51:22 +00:00
|
|
|
LastReceive *time.Time
|
2022-09-23 21:51:41 +00:00
|
|
|
// LastSend represents when any message was last sent, regardless of success or error.
|
2022-11-03 18:51:22 +00:00
|
|
|
LastSend *time.Time
|
2022-09-23 21:51:41 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 18:49:05 +00:00
|
|
|
type PeeringReadResponse struct {
|
|
|
|
Peering *Peering
|
|
|
|
}
|
|
|
|
|
|
|
|
type PeeringGenerateTokenRequest struct {
|
|
|
|
// PeerName is the name of the remote peer.
|
|
|
|
PeerName string
|
|
|
|
// Partition to be peered.
|
2022-07-12 23:18:05 +00:00
|
|
|
Partition string `json:",omitempty"`
|
|
|
|
// Meta is a mapping of some string value to any other string value
|
2022-05-09 20:47:37 +00:00
|
|
|
Meta map[string]string `json:",omitempty"`
|
2022-11-08 20:55:18 +00:00
|
|
|
// ServerExternalAddresses is a list of addresses to put into the generated token. This could be used to specify
|
|
|
|
// load balancer(s) or external IPs to reach the servers from the dialing side, and will override any server
|
|
|
|
// addresses obtained from the "consul" service.
|
|
|
|
ServerExternalAddresses []string `json:",omitempty"`
|
2022-05-02 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PeeringGenerateTokenResponse struct {
|
|
|
|
// PeeringToken is an opaque string provided to the remote peer for it to complete
|
|
|
|
// the peering initialization handshake.
|
|
|
|
PeeringToken string
|
|
|
|
}
|
|
|
|
|
2022-06-10 16:10:46 +00:00
|
|
|
type PeeringEstablishRequest struct {
|
2022-05-02 18:49:05 +00:00
|
|
|
// Name of the remote peer.
|
|
|
|
PeerName string
|
|
|
|
// The peering token returned from the peer's GenerateToken endpoint.
|
2022-05-25 18:43:35 +00:00
|
|
|
PeeringToken string `json:",omitempty"`
|
2022-07-15 14:07:07 +00:00
|
|
|
// Partition to be peered.
|
2022-07-12 23:18:05 +00:00
|
|
|
Partition string `json:",omitempty"`
|
2022-05-09 20:47:37 +00:00
|
|
|
// Meta is a mapping of some string value to any other string value
|
|
|
|
Meta map[string]string `json:",omitempty"`
|
2022-05-02 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 16:10:46 +00:00
|
|
|
type PeeringEstablishResponse struct {
|
2022-05-02 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 21:15:42 +00:00
|
|
|
type PeeringListRequest struct {
|
|
|
|
// future proofing in case we extend List functionality
|
|
|
|
}
|
|
|
|
|
2022-05-02 18:49:05 +00:00
|
|
|
type Peerings struct {
|
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Peerings returns a handle to the operator endpoints.
|
|
|
|
func (c *Client) Peerings() *Peerings {
|
|
|
|
return &Peerings{c: c}
|
|
|
|
}
|
|
|
|
|
2022-05-25 18:43:35 +00:00
|
|
|
func (p *Peerings) Read(ctx context.Context, name string, q *QueryOptions) (*Peering, *QueryMeta, error) {
|
|
|
|
if name == "" {
|
2022-05-02 18:49:05 +00:00
|
|
|
return nil, nil, fmt.Errorf("peering name cannot be empty")
|
|
|
|
}
|
|
|
|
|
2022-05-25 18:43:35 +00:00
|
|
|
req := p.c.newRequest("GET", fmt.Sprintf("/v1/peering/%s", name))
|
2022-05-02 18:49:05 +00:00
|
|
|
req.setQueryOptions(q)
|
|
|
|
req.ctx = ctx
|
|
|
|
|
|
|
|
rtt, resp, err := p.c.doRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer closeResponseBody(resp)
|
2022-05-25 18:43:35 +00:00
|
|
|
found, resp, err := requireNotFoundOrOK(resp)
|
|
|
|
if err != nil {
|
2022-05-02 18:49:05 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
qm := &QueryMeta{}
|
|
|
|
parseQueryMeta(resp, qm)
|
|
|
|
qm.RequestTime = rtt
|
|
|
|
|
2022-05-25 18:43:35 +00:00
|
|
|
if !found {
|
|
|
|
return nil, qm, nil
|
|
|
|
}
|
|
|
|
|
2022-05-02 18:49:05 +00:00
|
|
|
var out Peering
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, qm, nil
|
|
|
|
}
|
|
|
|
|
2022-05-25 18:43:35 +00:00
|
|
|
func (p *Peerings) Delete(ctx context.Context, name string, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
if name == "" {
|
|
|
|
return nil, fmt.Errorf("peering name cannot be empty")
|
2022-05-02 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 18:43:35 +00:00
|
|
|
req := p.c.newRequest("DELETE", fmt.Sprintf("/v1/peering/%s", name))
|
|
|
|
req.setWriteOptions(q)
|
2022-05-02 18:49:05 +00:00
|
|
|
req.ctx = ctx
|
|
|
|
|
|
|
|
rtt, resp, err := p.c.doRequest(req)
|
|
|
|
if err != nil {
|
2022-05-25 18:43:35 +00:00
|
|
|
return nil, err
|
2022-05-02 18:49:05 +00:00
|
|
|
}
|
|
|
|
defer closeResponseBody(resp)
|
|
|
|
if err := requireOK(resp); err != nil {
|
2022-05-25 18:43:35 +00:00
|
|
|
return nil, err
|
2022-05-02 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 18:43:35 +00:00
|
|
|
wm := &WriteMeta{RequestTime: rtt}
|
|
|
|
return wm, nil
|
2022-05-02 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 18:43:35 +00:00
|
|
|
// TODO(peering): verify this is the ultimate signature we want
|
2022-05-02 18:49:05 +00:00
|
|
|
func (p *Peerings) GenerateToken(ctx context.Context, g PeeringGenerateTokenRequest, wq *WriteOptions) (*PeeringGenerateTokenResponse, *WriteMeta, error) {
|
|
|
|
if g.PeerName == "" {
|
|
|
|
return nil, nil, fmt.Errorf("peer name cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
req := p.c.newRequest("POST", fmt.Sprint("/v1/peering/token"))
|
|
|
|
req.setWriteOptions(wq)
|
|
|
|
req.ctx = ctx
|
|
|
|
req.obj = g
|
|
|
|
|
|
|
|
rtt, resp, err := p.c.doRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer closeResponseBody(resp)
|
|
|
|
if err := requireOK(resp); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wm := &WriteMeta{RequestTime: rtt}
|
|
|
|
|
|
|
|
var out PeeringGenerateTokenResponse
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, wm, nil
|
|
|
|
}
|
|
|
|
|
2022-05-25 18:43:35 +00:00
|
|
|
// TODO(peering): verify this is the ultimate signature we want
|
2022-06-10 16:10:46 +00:00
|
|
|
func (p *Peerings) Establish(ctx context.Context, i PeeringEstablishRequest, wq *WriteOptions) (*PeeringEstablishResponse, *WriteMeta, error) {
|
|
|
|
req := p.c.newRequest("POST", fmt.Sprint("/v1/peering/establish"))
|
2022-05-02 18:49:05 +00:00
|
|
|
req.setWriteOptions(wq)
|
|
|
|
req.ctx = ctx
|
|
|
|
req.obj = i
|
|
|
|
|
|
|
|
rtt, resp, err := p.c.doRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer closeResponseBody(resp)
|
|
|
|
if err := requireOK(resp); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wm := &WriteMeta{RequestTime: rtt}
|
|
|
|
|
2022-06-10 16:10:46 +00:00
|
|
|
var out PeeringEstablishResponse
|
2022-05-02 18:49:05 +00:00
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, wm, nil
|
|
|
|
}
|
2022-05-05 21:15:42 +00:00
|
|
|
|
2022-05-25 18:43:35 +00:00
|
|
|
func (p *Peerings) List(ctx context.Context, q *QueryOptions) ([]*Peering, *QueryMeta, error) {
|
2022-05-05 21:15:42 +00:00
|
|
|
req := p.c.newRequest("GET", "/v1/peerings")
|
|
|
|
req.setQueryOptions(q)
|
|
|
|
req.ctx = ctx
|
|
|
|
|
|
|
|
rtt, resp, err := p.c.doRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer closeResponseBody(resp)
|
|
|
|
if err := requireOK(resp); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
qm := &QueryMeta{}
|
|
|
|
parseQueryMeta(resp, qm)
|
|
|
|
qm.RequestTime = rtt
|
|
|
|
|
|
|
|
var out []*Peering
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, qm, nil
|
|
|
|
}
|