github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/model/constraints.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package model
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/gnuflag"
    12  
    13  	"github.com/juju/juju/cmd/juju/block"
    14  	"github.com/juju/juju/cmd/modelcmd"
    15  	"github.com/juju/juju/constraints"
    16  )
    17  
    18  // getConstraintsDoc is multi-line since we need to use ` to denote
    19  // commands for ease in markdown.
    20  const getConstraintsDoc = "" +
    21  	"Shows machine constraints that have been set on the model with\n" +
    22  	"`juju set-model-constraints.`\n" +
    23  	"By default, the model is the current model.\n" +
    24  	"Model constraints are combined with constraints set on an application\n" +
    25  	"with `juju set-constraints` for commands (such as 'deploy') that provision\n" +
    26  	"machines for applications. Where model and application constraints overlap, the\n" +
    27  	"application constraints take precedence.\n" +
    28  	"Constraints for a specific application can be viewed with `juju get-constraints`.\n" + getConstraintsDocExamples
    29  
    30  const getConstraintsDocExamples = `
    31  Examples:
    32  
    33      juju get-model-constraints
    34      juju get-model-constraints -m mymodel
    35  
    36  See also:
    37      models
    38      get-constraints
    39      set-constraints
    40      set-model-constraints
    41  `
    42  
    43  // setConstraintsDoc is multi-line since we need to use ` to denote
    44  // commands for ease in markdown.
    45  const setConstraintsDoc = "" +
    46  	"Sets machine constraints on the model that can be viewed with\n" +
    47  	"`juju get-model-constraints`.  By default, the model is the current model.\n" +
    48  	"Model constraints are combined with constraints set for an application with\n" +
    49  	"`juju set-constraints` for commands (such as 'deploy') that provision\n" +
    50  	"machines for applications. Where model and application constraints overlap, the\n" +
    51  	"application constraints take precedence.\n" +
    52  	"Constraints for a specific application can be viewed with `juju get-constraints`.\n" + setConstraintsDocExamples
    53  
    54  const setConstraintsDocExamples = `
    55  Examples:
    56  
    57      juju set-model-constraints cores=8 mem=16G
    58      juju set-model-constraints -m mymodel root-disk=64G
    59  
    60  See also:
    61      models
    62      get-model-constraints
    63      get-constraints
    64      set-constraints
    65  `
    66  
    67  // ConstraintsAPI defines methods on the client API that
    68  // the get-constraints and set-constraints commands call
    69  type ConstraintsAPI interface {
    70  	Close() error
    71  	GetModelConstraints() (constraints.Value, error)
    72  	SetModelConstraints(constraints.Value) error
    73  }
    74  
    75  // NewModelGetConstraintsCommand returns a command to get model constraints.
    76  func NewModelGetConstraintsCommand() cmd.Command {
    77  	return modelcmd.Wrap(&modelGetConstraintsCommand{})
    78  }
    79  
    80  // modelGetConstraintsCommand shows the constraints for a model.
    81  type modelGetConstraintsCommand struct {
    82  	modelcmd.ModelCommandBase
    83  	out cmd.Output
    84  	api ConstraintsAPI
    85  }
    86  
    87  func (c *modelGetConstraintsCommand) Info() *cmd.Info {
    88  	return &cmd.Info{
    89  		Name:    "get-model-constraints",
    90  		Purpose: "Displays machine constraints for a model.",
    91  		Doc:     getConstraintsDoc,
    92  	}
    93  }
    94  
    95  func (c *modelGetConstraintsCommand) Init(args []string) error {
    96  	return cmd.CheckEmpty(args)
    97  }
    98  
    99  func (c *modelGetConstraintsCommand) getAPI() (ConstraintsAPI, error) {
   100  	if c.api != nil {
   101  		return c.api, nil
   102  	}
   103  	return c.NewAPIClient()
   104  }
   105  
   106  func formatConstraints(writer io.Writer, value interface{}) error {
   107  	fmt.Fprint(writer, value.(constraints.Value).String())
   108  	return nil
   109  }
   110  
   111  func (c *modelGetConstraintsCommand) SetFlags(f *gnuflag.FlagSet) {
   112  	c.ModelCommandBase.SetFlags(f)
   113  	c.out.AddFlags(f, "constraints", map[string]cmd.Formatter{
   114  		"constraints": formatConstraints,
   115  		"yaml":        cmd.FormatYaml,
   116  		"json":        cmd.FormatJson,
   117  	})
   118  }
   119  
   120  func (c *modelGetConstraintsCommand) Run(ctx *cmd.Context) error {
   121  	apiclient, err := c.getAPI()
   122  	if err != nil {
   123  		return err
   124  	}
   125  	defer apiclient.Close()
   126  
   127  	cons, err := apiclient.GetModelConstraints()
   128  	if err != nil {
   129  		return err
   130  	}
   131  	return c.out.Write(ctx, cons)
   132  }
   133  
   134  // NewModelSetConstraintsCommand returns a command to set model constraints.
   135  func NewModelSetConstraintsCommand() cmd.Command {
   136  	return modelcmd.Wrap(&modelSetConstraintsCommand{})
   137  }
   138  
   139  // modelSetConstraintsCommand sets the constraints for a model.
   140  type modelSetConstraintsCommand struct {
   141  	modelcmd.ModelCommandBase
   142  	api         ConstraintsAPI
   143  	Constraints constraints.Value
   144  }
   145  
   146  func (c *modelSetConstraintsCommand) Info() *cmd.Info {
   147  	return &cmd.Info{
   148  		Name:    "set-model-constraints",
   149  		Args:    "<constraint>=<value> ...",
   150  		Purpose: "Sets machine constraints on a model.",
   151  		Doc:     setConstraintsDoc,
   152  	}
   153  }
   154  
   155  func (c *modelSetConstraintsCommand) Init(args []string) (err error) {
   156  	c.Constraints, err = constraints.Parse(args...)
   157  	return err
   158  }
   159  
   160  func (c *modelSetConstraintsCommand) getAPI() (ConstraintsAPI, error) {
   161  	if c.api != nil {
   162  		return c.api, nil
   163  	}
   164  	return c.NewAPIClient()
   165  }
   166  
   167  func (c *modelSetConstraintsCommand) Run(_ *cmd.Context) (err error) {
   168  	apiclient, err := c.getAPI()
   169  	if err != nil {
   170  		return err
   171  	}
   172  	defer apiclient.Close()
   173  
   174  	err = apiclient.SetModelConstraints(c.Constraints)
   175  	return block.ProcessBlockedError(err, block.BlockChange)
   176  }