launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/state/statecmd/destroymachines.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package statecmd
     5  
     6  import (
     7  	"strings"
     8  
     9  	errgo "launchpad.net/errgo/errors"
    10  	"launchpad.net/juju-core/errors"
    11  	"launchpad.net/juju-core/state"
    12  )
    13  
    14  // DestroyMachines1dot16 destroys the machines with the specified ids.
    15  // This is copied from the 1.16.3 code to enable compatibility. It should be
    16  // removed when we release a version that goes via the API only (whatever is
    17  // after 1.18)
    18  func DestroyMachines1dot16(st *state.State, ids ...string) (err error) {
    19  	var errs []string
    20  	for _, id := range ids {
    21  		machine, err := st.Machine(id)
    22  		switch {
    23  		case errors.IsNotFoundError(err):
    24  			err = errgo.Newf("machine %s does not exist", id)
    25  		case err != nil:
    26  		case machine.Life() != state.Alive:
    27  			continue
    28  		default:
    29  			err = machine.Destroy()
    30  		}
    31  		if err != nil {
    32  			errs = append(errs, err.Error())
    33  		}
    34  	}
    35  	if len(errs) == 0 {
    36  		return nil
    37  	}
    38  	msg := "some machines were not destroyed"
    39  	if len(errs) == len(ids) {
    40  		msg = "no machines were destroyed"
    41  	}
    42  	return errgo.Newf("%s: %s", msg, strings.Join(errs, "; "))
    43  }