github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  )
    12  
    13  // DestroyController will attempt to destroy the controller. If the args
    14  // specify the removal of blocks or the destruction of the models, this
    15  // method will attempt to do so.
    16  //
    17  // If the controller has any non-Dead hosted models, then an error with
    18  // the code params.CodeHasHostedModels will be transmitted, regardless of
    19  // the value of the DestroyModels parameter. This is to inform the client
    20  // that it should wait for hosted models to be completely cleaned up
    21  // before proceeding.
    22  func (s *ControllerAPI) DestroyController(args params.DestroyControllerArgs) error {
    23  	controllerEnv, err := s.state.ControllerModel()
    24  	if err != nil {
    25  		return errors.Trace(err)
    26  	}
    27  	systemTag := controllerEnv.ModelTag()
    28  
    29  	if err = s.ensureNotBlocked(args); err != nil {
    30  		return errors.Trace(err)
    31  	}
    32  
    33  	// If we are destroying models, we need to tolerate living
    34  	// models but set the controller to dying to prevent new
    35  	// models sneaking in. If we are not destroying hosted models,
    36  	// this will fail if any hosted models are found.
    37  	if args.DestroyModels {
    38  		return errors.Trace(common.DestroyModelIncludingHosted(s.state, systemTag))
    39  	}
    40  	if err := common.DestroyModel(s.state, systemTag); err != nil {
    41  		return errors.Trace(err)
    42  	}
    43  	return nil
    44  }
    45  
    46  func (s *ControllerAPI) ensureNotBlocked(args params.DestroyControllerArgs) error {
    47  	// If there are blocks let the user know.
    48  	blocks, err := s.state.AllBlocksForController()
    49  	if err != nil {
    50  		logger.Debugf("Unable to get blocks for controller: %s", err)
    51  		return errors.Trace(err)
    52  	}
    53  
    54  	if len(blocks) > 0 {
    55  		return common.OperationBlockedError("found blocks in controller models")
    56  	}
    57  	return nil
    58  }