...

Source file src/github.com/LINBIT/golinstor/client/connection.go

Documentation: github.com/LINBIT/golinstor/client

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/google/go-querystring/query"
     8  )
     9  
    10  type Connection struct {
    11  	NodeA string            `json:"node_a,omitempty"`
    12  	NodeB string            `json:"node_b,omitempty"`
    13  	Props map[string]string `json:"props,omitempty"`
    14  	Flags []string          `json:"flags,omitempty"`
    15  	Port  *int32            `json:"port,omitempty"`
    16  }
    17  
    18  // ConnectionProvider acts as an abstraction for a ConnectionService. It can be swapped out
    19  // for another ConnectionService implementation, for example for testing.
    20  type ConnectionProvider interface {
    21  	// GetNodeConnections lists all node connections, optionally limites to nodes A and B, if not empty.
    22  	GetNodeConnections(ctx context.Context, nodeA, nodeB string) ([]Connection, error)
    23  	// GetResourceConnections returns all connections of the given resource.
    24  	GetResourceConnections(ctx context.Context, resource string) ([]Connection, error)
    25  	// GetResourceConnection returns the connection between node A and B for the given resource.
    26  	GetResourceConnection(ctx context.Context, resource, nodeA, nodeB string) (*Connection, error)
    27  	// SetNodeConnection sets or updates the node connection between node A and B.
    28  	SetNodeConnection(ctx context.Context, nodeA, nodeB string, props GenericPropsModify) error
    29  	// SetResourceConnection sets or updates the connection between node A and B for a resource.
    30  	SetResourceConnection(ctx context.Context, resource, nodeA, nodeB string, props GenericPropsModify) error
    31  }
    32  
    33  // ConnectionService is the service that deals with connection related tasks.
    34  type ConnectionService struct {
    35  	client *Client
    36  }
    37  
    38  func (c *ConnectionService) GetNodeConnections(ctx context.Context, nodeA, nodeB string) ([]Connection, error) {
    39  	nodeA, nodeB = sortNodes(nodeA, nodeB)
    40  
    41  	vals, err := query.Values(struct {
    42  		NodeA string `url:"node_a,omitempty"`
    43  		NodeB string `url:"node_b,omitempty"`
    44  	}{NodeA: nodeA, NodeB: nodeB})
    45  	if err != nil {
    46  		return nil, fmt.Errorf("failed to encode node names: %w", err)
    47  	}
    48  
    49  	var conns []Connection
    50  	_, err = c.client.doGET(ctx, "/v1/node-connections?"+vals.Encode(), &conns)
    51  	return conns, err
    52  }
    53  
    54  func (c *ConnectionService) GetResourceConnections(ctx context.Context, resource string) ([]Connection, error) {
    55  	var conns []Connection
    56  	_, err := c.client.doGET(ctx, "/v1/resource-definitions/"+resource+"/resource-connections", &conns)
    57  	return conns, err
    58  }
    59  
    60  func (c *ConnectionService) GetResourceConnection(ctx context.Context, resource, nodeA, nodeB string) (*Connection, error) {
    61  	nodeA, nodeB = sortNodes(nodeA, nodeB)
    62  
    63  	var conn Connection
    64  	_, err := c.client.doGET(ctx, "/v1/resource-definitions/"+resource+"/resource-connections/"+nodeA+"/"+nodeB, &conn)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	return &conn, err
    69  }
    70  
    71  func (c *ConnectionService) SetNodeConnection(ctx context.Context, nodeA, nodeB string, props GenericPropsModify) error {
    72  	nodeA, nodeB = sortNodes(nodeA, nodeB)
    73  	_, err := c.client.doPUT(ctx, "/v1/node-connections/"+nodeA+"/"+nodeB, &props)
    74  	return err
    75  }
    76  
    77  func (c *ConnectionService) SetResourceConnection(ctx context.Context, resource, nodeA, nodeB string, props GenericPropsModify) error {
    78  	nodeA, nodeB = sortNodes(nodeA, nodeB)
    79  	_, err := c.client.doPUT(ctx, "/v1/resource-definitions/"+resource+"/resource-connections/"+nodeA+"/"+nodeB, &props)
    80  	return err
    81  }
    82  
    83  // Sort node parameters: LINSTOR is (sometimes) order-dependent with parameters.
    84  func sortNodes(a, b string) (string, string) {
    85  	if a < b {
    86  		return a, b
    87  	} else {
    88  		return b, a
    89  	}
    90  }
    91  

View as plain text