github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/controller/destroy.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package controller
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/apiserver/common"
    10  	"github.com/juju/juju/apiserver/params"
    11  	"github.com/juju/juju/permission"
    12  )
    13  
    14  // DestroyController will attempt to destroy the controller. If the args
    15  // specify the removal of blocks or the destruction of the models, this
    16  // method will attempt to do so.
    17  //
    18  // If the controller has any non-Dead hosted models, then an error with
    19  // the code params.CodeHasHostedModels will be transmitted, regardless of
    20  // the value of the DestroyModels parameter. This is to inform the client
    21  // that it should wait for hosted models to be completely cleaned up
    22  // before proceeding.
    23  func (s *ControllerAPI) DestroyController(args params.DestroyControllerArgs) error {
    24  	hasPermission, err := s.authorizer.HasPermission(permission.SuperuserAccess, s.state.ControllerTag())
    25  	if err != nil {
    26  		return errors.Trace(err)
    27  	}
    28  	if !hasPermission {
    29  		return errors.Trace(common.ErrPerm)
    30  	}
    31  
    32  	st := common.NewModelManagerBackend(s.state)
    33  	controllerModel, err := st.ControllerModel()
    34  	if err != nil {
    35  		return errors.Trace(err)
    36  	}
    37  	systemTag := controllerModel.ModelTag()
    38  
    39  	if err = s.ensureNotBlocked(args); err != nil {
    40  		return errors.Trace(err)
    41  	}
    42  
    43  	// If we are destroying models, we need to tolerate living
    44  	// models but set the controller to dying to prevent new
    45  	// models sneaking in. If we are not destroying hosted models,
    46  	// this will fail if any hosted models are found.
    47  	if args.DestroyModels {
    48  		return errors.Trace(common.DestroyModelIncludingHosted(st, systemTag))
    49  	}
    50  	if err := common.DestroyModel(st, systemTag); err != nil {
    51  		return errors.Trace(err)
    52  	}
    53  	return nil
    54  }
    55  
    56  func (s *ControllerAPI) ensureNotBlocked(args params.DestroyControllerArgs) error {
    57  	// If there are blocks let the user know.
    58  	blocks, err := s.state.AllBlocksForController()
    59  	if err != nil {
    60  		logger.Debugf("Unable to get blocks for controller: %s", err)
    61  		return errors.Trace(err)
    62  	}
    63  
    64  	if len(blocks) > 0 {
    65  		return common.OperationBlockedError("found blocks in controller models")
    66  	}
    67  	return nil
    68  }