github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/daemon/cluster/helpers.go (about)

     1  package cluster // import "github.com/docker/docker/daemon/cluster"
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/docker/docker/errdefs"
     8  	swarmapi "github.com/docker/swarmkit/api"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  func getSwarm(ctx context.Context, c swarmapi.ControlClient) (*swarmapi.Cluster, error) {
    13  	rl, err := c.ListClusters(ctx, &swarmapi.ListClustersRequest{})
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  
    18  	if len(rl.Clusters) == 0 {
    19  		return nil, errors.WithStack(errNoSwarm)
    20  	}
    21  
    22  	// TODO: assume one cluster only
    23  	return rl.Clusters[0], nil
    24  }
    25  
    26  func getNode(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Node, error) {
    27  	// GetNode to match via full ID.
    28  	if rg, err := c.GetNode(ctx, &swarmapi.GetNodeRequest{NodeID: input}); err == nil {
    29  		return rg.Node, nil
    30  	}
    31  
    32  	// If any error (including NotFound), ListNodes to match via full name.
    33  	rl, err := c.ListNodes(ctx, &swarmapi.ListNodesRequest{
    34  		Filters: &swarmapi.ListNodesRequest_Filters{
    35  			Names: []string{input},
    36  		},
    37  	})
    38  	if err != nil || len(rl.Nodes) == 0 {
    39  		// If any error or 0 result, ListNodes to match via ID prefix.
    40  		rl, err = c.ListNodes(ctx, &swarmapi.ListNodesRequest{
    41  			Filters: &swarmapi.ListNodesRequest_Filters{
    42  				IDPrefixes: []string{input},
    43  			},
    44  		})
    45  	}
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	if len(rl.Nodes) == 0 {
    51  		err := fmt.Errorf("node %s not found", input)
    52  		return nil, errdefs.NotFound(err)
    53  	}
    54  
    55  	if l := len(rl.Nodes); l > 1 {
    56  		return nil, errdefs.InvalidParameter(fmt.Errorf("node %s is ambiguous (%d matches found)", input, l))
    57  	}
    58  
    59  	return rl.Nodes[0], nil
    60  }
    61  
    62  func getService(ctx context.Context, c swarmapi.ControlClient, input string, insertDefaults bool) (*swarmapi.Service, error) {
    63  	// GetService to match via full ID.
    64  	if rg, err := c.GetService(ctx, &swarmapi.GetServiceRequest{ServiceID: input, InsertDefaults: insertDefaults}); err == nil {
    65  		return rg.Service, nil
    66  	}
    67  
    68  	// If any error (including NotFound), ListServices to match via full name.
    69  	rl, err := c.ListServices(ctx, &swarmapi.ListServicesRequest{
    70  		Filters: &swarmapi.ListServicesRequest_Filters{
    71  			Names: []string{input},
    72  		},
    73  	})
    74  	if err != nil || len(rl.Services) == 0 {
    75  		// If any error or 0 result, ListServices to match via ID prefix.
    76  		rl, err = c.ListServices(ctx, &swarmapi.ListServicesRequest{
    77  			Filters: &swarmapi.ListServicesRequest_Filters{
    78  				IDPrefixes: []string{input},
    79  			},
    80  		})
    81  	}
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	if len(rl.Services) == 0 {
    87  		err := fmt.Errorf("service %s not found", input)
    88  		return nil, errdefs.NotFound(err)
    89  	}
    90  
    91  	if l := len(rl.Services); l > 1 {
    92  		return nil, errdefs.InvalidParameter(fmt.Errorf("service %s is ambiguous (%d matches found)", input, l))
    93  	}
    94  
    95  	if !insertDefaults {
    96  		return rl.Services[0], nil
    97  	}
    98  
    99  	rg, err := c.GetService(ctx, &swarmapi.GetServiceRequest{ServiceID: rl.Services[0].ID, InsertDefaults: true})
   100  	if err == nil {
   101  		return rg.Service, nil
   102  	}
   103  	return nil, err
   104  }
   105  
   106  func getTask(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Task, error) {
   107  	// GetTask to match via full ID.
   108  	if rg, err := c.GetTask(ctx, &swarmapi.GetTaskRequest{TaskID: input}); err == nil {
   109  		return rg.Task, nil
   110  	}
   111  
   112  	// If any error (including NotFound), ListTasks to match via full name.
   113  	rl, err := c.ListTasks(ctx, &swarmapi.ListTasksRequest{
   114  		Filters: &swarmapi.ListTasksRequest_Filters{
   115  			Names: []string{input},
   116  		},
   117  	})
   118  	if err != nil || len(rl.Tasks) == 0 {
   119  		// If any error or 0 result, ListTasks to match via ID prefix.
   120  		rl, err = c.ListTasks(ctx, &swarmapi.ListTasksRequest{
   121  			Filters: &swarmapi.ListTasksRequest_Filters{
   122  				IDPrefixes: []string{input},
   123  			},
   124  		})
   125  	}
   126  	if err != nil {
   127  		return nil, err
   128  	}
   129  
   130  	if len(rl.Tasks) == 0 {
   131  		err := fmt.Errorf("task %s not found", input)
   132  		return nil, errdefs.NotFound(err)
   133  	}
   134  
   135  	if l := len(rl.Tasks); l > 1 {
   136  		return nil, errdefs.InvalidParameter(fmt.Errorf("task %s is ambiguous (%d matches found)", input, l))
   137  	}
   138  
   139  	return rl.Tasks[0], nil
   140  }
   141  
   142  func getSecret(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Secret, error) {
   143  	// attempt to lookup secret by full ID
   144  	if rg, err := c.GetSecret(ctx, &swarmapi.GetSecretRequest{SecretID: input}); err == nil {
   145  		return rg.Secret, nil
   146  	}
   147  
   148  	// If any error (including NotFound), ListSecrets to match via full name.
   149  	rl, err := c.ListSecrets(ctx, &swarmapi.ListSecretsRequest{
   150  		Filters: &swarmapi.ListSecretsRequest_Filters{
   151  			Names: []string{input},
   152  		},
   153  	})
   154  	if err != nil || len(rl.Secrets) == 0 {
   155  		// If any error or 0 result, ListSecrets to match via ID prefix.
   156  		rl, err = c.ListSecrets(ctx, &swarmapi.ListSecretsRequest{
   157  			Filters: &swarmapi.ListSecretsRequest_Filters{
   158  				IDPrefixes: []string{input},
   159  			},
   160  		})
   161  	}
   162  	if err != nil {
   163  		return nil, err
   164  	}
   165  
   166  	if len(rl.Secrets) == 0 {
   167  		err := fmt.Errorf("secret %s not found", input)
   168  		return nil, errdefs.NotFound(err)
   169  	}
   170  
   171  	if l := len(rl.Secrets); l > 1 {
   172  		return nil, errdefs.InvalidParameter(fmt.Errorf("secret %s is ambiguous (%d matches found)", input, l))
   173  	}
   174  
   175  	return rl.Secrets[0], nil
   176  }
   177  
   178  func getConfig(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Config, error) {
   179  	// attempt to lookup config by full ID
   180  	if rg, err := c.GetConfig(ctx, &swarmapi.GetConfigRequest{ConfigID: input}); err == nil {
   181  		return rg.Config, nil
   182  	}
   183  
   184  	// If any error (including NotFound), ListConfigs to match via full name.
   185  	rl, err := c.ListConfigs(ctx, &swarmapi.ListConfigsRequest{
   186  		Filters: &swarmapi.ListConfigsRequest_Filters{
   187  			Names: []string{input},
   188  		},
   189  	})
   190  	if err != nil || len(rl.Configs) == 0 {
   191  		// If any error or 0 result, ListConfigs to match via ID prefix.
   192  		rl, err = c.ListConfigs(ctx, &swarmapi.ListConfigsRequest{
   193  			Filters: &swarmapi.ListConfigsRequest_Filters{
   194  				IDPrefixes: []string{input},
   195  			},
   196  		})
   197  	}
   198  	if err != nil {
   199  		return nil, err
   200  	}
   201  
   202  	if len(rl.Configs) == 0 {
   203  		err := fmt.Errorf("config %s not found", input)
   204  		return nil, errdefs.NotFound(err)
   205  	}
   206  
   207  	if l := len(rl.Configs); l > 1 {
   208  		return nil, errdefs.InvalidParameter(fmt.Errorf("config %s is ambiguous (%d matches found)", input, l))
   209  	}
   210  
   211  	return rl.Configs[0], nil
   212  }
   213  
   214  func getNetwork(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Network, error) {
   215  	// GetNetwork to match via full ID.
   216  	if rg, err := c.GetNetwork(ctx, &swarmapi.GetNetworkRequest{NetworkID: input}); err == nil {
   217  		return rg.Network, nil
   218  	}
   219  
   220  	// If any error (including NotFound), ListNetworks to match via ID prefix and full name.
   221  	rl, err := c.ListNetworks(ctx, &swarmapi.ListNetworksRequest{
   222  		Filters: &swarmapi.ListNetworksRequest_Filters{
   223  			Names: []string{input},
   224  		},
   225  	})
   226  	if err != nil || len(rl.Networks) == 0 {
   227  		rl, err = c.ListNetworks(ctx, &swarmapi.ListNetworksRequest{
   228  			Filters: &swarmapi.ListNetworksRequest_Filters{
   229  				IDPrefixes: []string{input},
   230  			},
   231  		})
   232  	}
   233  	if err != nil {
   234  		return nil, err
   235  	}
   236  
   237  	if len(rl.Networks) == 0 {
   238  		return nil, fmt.Errorf("network %s not found", input)
   239  	}
   240  
   241  	if l := len(rl.Networks); l > 1 {
   242  		return nil, errdefs.InvalidParameter(fmt.Errorf("network %s is ambiguous (%d matches found)", input, l))
   243  	}
   244  
   245  	return rl.Networks[0], nil
   246  }