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