github.com/olljanat/moby@v1.13.1/daemon/cluster/helpers.go (about)

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