github.com/yogeshlonkar/moby@v1.13.2-0.20201203103638-c0b64beaea94/daemon/cluster/helpers.go (about)

     1  package cluster
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/docker/docker/api/errors"
     7  	swarmapi "github.com/docker/swarmkit/api"
     8  	"golang.org/x/net/context"
     9  )
    10  
    11  func getSwarm(ctx context.Context, c swarmapi.ControlClient) (*swarmapi.Cluster, error) {
    12  	rl, err := c.ListClusters(ctx, &swarmapi.ListClustersRequest{})
    13  	if err != nil {
    14  		return nil, err
    15  	}
    16  
    17  	if len(rl.Clusters) == 0 {
    18  		return nil, errors.NewRequestNotFoundError(ErrNoSwarm)
    19  	}
    20  
    21  	// TODO: assume one cluster only
    22  	return rl.Clusters[0], nil
    23  }
    24  
    25  func getNode(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Node, error) {
    26  	// GetNode to match via full ID.
    27  	rg, err := c.GetNode(ctx, &swarmapi.GetNodeRequest{NodeID: input})
    28  	if err != nil {
    29  		// If any error (including NotFound), ListNodes to match via full name.
    30  		rl, err := c.ListNodes(ctx, &swarmapi.ListNodesRequest{Filters: &swarmapi.ListNodesRequest_Filters{Names: []string{input}}})
    31  
    32  		if err != nil || len(rl.Nodes) == 0 {
    33  			// If any error or 0 result, ListNodes to match via ID prefix.
    34  			rl, err = c.ListNodes(ctx, &swarmapi.ListNodesRequest{Filters: &swarmapi.ListNodesRequest_Filters{IDPrefixes: []string{input}}})
    35  		}
    36  
    37  		if err != nil {
    38  			return nil, err
    39  		}
    40  
    41  		if len(rl.Nodes) == 0 {
    42  			err := fmt.Errorf("node %s not found", input)
    43  			return nil, errors.NewRequestNotFoundError(err)
    44  		}
    45  
    46  		if l := len(rl.Nodes); l > 1 {
    47  			return nil, fmt.Errorf("node %s is ambiguous (%d matches found)", input, l)
    48  		}
    49  
    50  		return rl.Nodes[0], nil
    51  	}
    52  	return rg.Node, nil
    53  }
    54  
    55  func getService(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Service, error) {
    56  	// GetService to match via full ID.
    57  	rg, err := c.GetService(ctx, &swarmapi.GetServiceRequest{ServiceID: input})
    58  	if err != nil {
    59  		// If any error (including NotFound), ListServices to match via full name.
    60  		rl, err := c.ListServices(ctx, &swarmapi.ListServicesRequest{Filters: &swarmapi.ListServicesRequest_Filters{Names: []string{input}}})
    61  		if err != nil || len(rl.Services) == 0 {
    62  			// If any error or 0 result, ListServices to match via ID prefix.
    63  			rl, err = c.ListServices(ctx, &swarmapi.ListServicesRequest{Filters: &swarmapi.ListServicesRequest_Filters{IDPrefixes: []string{input}}})
    64  		}
    65  
    66  		if err != nil {
    67  			return nil, err
    68  		}
    69  
    70  		if len(rl.Services) == 0 {
    71  			err := fmt.Errorf("service %s not found", input)
    72  			return nil, errors.NewRequestNotFoundError(err)
    73  		}
    74  
    75  		if l := len(rl.Services); l > 1 {
    76  			return nil, fmt.Errorf("service %s is ambiguous (%d matches found)", input, l)
    77  		}
    78  
    79  		return rl.Services[0], nil
    80  	}
    81  	return rg.Service, nil
    82  }
    83  
    84  func getTask(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Task, error) {
    85  	// GetTask to match via full ID.
    86  	rg, err := c.GetTask(ctx, &swarmapi.GetTaskRequest{TaskID: input})
    87  	if err != nil {
    88  		// If any error (including NotFound), ListTasks to match via full name.
    89  		rl, err := c.ListTasks(ctx, &swarmapi.ListTasksRequest{Filters: &swarmapi.ListTasksRequest_Filters{Names: []string{input}}})
    90  
    91  		if err != nil || len(rl.Tasks) == 0 {
    92  			// If any error or 0 result, ListTasks to match via ID prefix.
    93  			rl, err = c.ListTasks(ctx, &swarmapi.ListTasksRequest{Filters: &swarmapi.ListTasksRequest_Filters{IDPrefixes: []string{input}}})
    94  		}
    95  
    96  		if err != nil {
    97  			return nil, err
    98  		}
    99  
   100  		if len(rl.Tasks) == 0 {
   101  			err := fmt.Errorf("task %s not found", input)
   102  			return nil, errors.NewRequestNotFoundError(err)
   103  		}
   104  
   105  		if l := len(rl.Tasks); l > 1 {
   106  			return nil, fmt.Errorf("task %s is ambiguous (%d matches found)", input, l)
   107  		}
   108  
   109  		return rl.Tasks[0], nil
   110  	}
   111  	return rg.Task, nil
   112  }