github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/cluster/configs.go (about)

     1  package cluster // import "github.com/demonoid81/moby/daemon/cluster"
     2  
     3  import (
     4  	"context"
     5  
     6  	apitypes "github.com/demonoid81/moby/api/types"
     7  	types "github.com/demonoid81/moby/api/types/swarm"
     8  	"github.com/demonoid81/moby/daemon/cluster/convert"
     9  	swarmapi "github.com/docker/swarmkit/api"
    10  	"google.golang.org/grpc"
    11  )
    12  
    13  // GetConfig returns a config from a managed swarm cluster
    14  func (c *Cluster) GetConfig(input string) (types.Config, error) {
    15  	var config *swarmapi.Config
    16  
    17  	if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
    18  		s, err := getConfig(ctx, state.controlClient, input)
    19  		if err != nil {
    20  			return err
    21  		}
    22  		config = s
    23  		return nil
    24  	}); err != nil {
    25  		return types.Config{}, err
    26  	}
    27  	return convert.ConfigFromGRPC(config), nil
    28  }
    29  
    30  // GetConfigs returns all configs of a managed swarm cluster.
    31  func (c *Cluster) GetConfigs(options apitypes.ConfigListOptions) ([]types.Config, error) {
    32  	c.mu.RLock()
    33  	defer c.mu.RUnlock()
    34  
    35  	state := c.currentNodeState()
    36  	if !state.IsActiveManager() {
    37  		return nil, c.errNoManager(state)
    38  	}
    39  
    40  	filters, err := newListConfigsFilters(options.Filters)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	ctx, cancel := c.getRequestContext()
    45  	defer cancel()
    46  
    47  	r, err := state.controlClient.ListConfigs(ctx,
    48  		&swarmapi.ListConfigsRequest{Filters: filters},
    49  		grpc.MaxCallRecvMsgSize(defaultRecvSizeForListResponse))
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	configs := []types.Config{}
    55  
    56  	for _, config := range r.Configs {
    57  		configs = append(configs, convert.ConfigFromGRPC(config))
    58  	}
    59  
    60  	return configs, nil
    61  }
    62  
    63  // CreateConfig creates a new config in a managed swarm cluster.
    64  func (c *Cluster) CreateConfig(s types.ConfigSpec) (string, error) {
    65  	var resp *swarmapi.CreateConfigResponse
    66  	if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
    67  		configSpec := convert.ConfigSpecToGRPC(s)
    68  
    69  		r, err := state.controlClient.CreateConfig(ctx,
    70  			&swarmapi.CreateConfigRequest{Spec: &configSpec})
    71  		if err != nil {
    72  			return err
    73  		}
    74  		resp = r
    75  		return nil
    76  	}); err != nil {
    77  		return "", err
    78  	}
    79  	return resp.Config.ID, nil
    80  }
    81  
    82  // RemoveConfig removes a config from a managed swarm cluster.
    83  func (c *Cluster) RemoveConfig(input string) error {
    84  	return c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
    85  		config, err := getConfig(ctx, state.controlClient, input)
    86  		if err != nil {
    87  			return err
    88  		}
    89  
    90  		req := &swarmapi.RemoveConfigRequest{
    91  			ConfigID: config.ID,
    92  		}
    93  
    94  		_, err = state.controlClient.RemoveConfig(ctx, req)
    95  		return err
    96  	})
    97  }
    98  
    99  // UpdateConfig updates a config in a managed swarm cluster.
   100  // Note: this is not exposed to the CLI but is available from the API only
   101  func (c *Cluster) UpdateConfig(input string, version uint64, spec types.ConfigSpec) error {
   102  	return c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
   103  		config, err := getConfig(ctx, state.controlClient, input)
   104  		if err != nil {
   105  			return err
   106  		}
   107  
   108  		configSpec := convert.ConfigSpecToGRPC(spec)
   109  
   110  		_, err = state.controlClient.UpdateConfig(ctx,
   111  			&swarmapi.UpdateConfigRequest{
   112  				ConfigID: config.ID,
   113  				ConfigVersion: &swarmapi.Version{
   114  					Index: version,
   115  				},
   116  				Spec: &configSpec,
   117  			})
   118  		return err
   119  	})
   120  }