github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/errors/machine.go (about)

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package errors
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/juju/errors"
    11  	"github.com/juju/names/v5"
    12  )
    13  
    14  const (
    15  	HasAssignedUnitsError = errors.ConstError("has assigned units")
    16  
    17  	// HasAttachmentsError indicates that an attempt to destroy
    18  	// a machine failed due to it having storage attachments.
    19  	HasAttachmentsError = errors.ConstError("machine has attachments")
    20  
    21  	// HasContainersError indicates that the machine had attempted to be
    22  	// destroyed with containers still running.
    23  	HasContainersError = errors.ConstError("machine is hosting containers")
    24  
    25  	// IsControllerMemberError indicates the machine had attempted to be
    26  	// destroyed whilst still considered a controller.
    27  	IsControllerMemberError = errors.ConstError("machine is still a controller member")
    28  )
    29  
    30  // NewHasAssignedUnitsError creates a new error that satisfies HasAssignedUnitsError.
    31  func NewHasAssignedUnitsError(machineId string, unitNames []string) error {
    32  	return errors.WithType(
    33  		fmt.Errorf("machine %s has unit %q assigned",
    34  			machineId,
    35  			unitNames[0]),
    36  		HasAssignedUnitsError,
    37  	)
    38  }
    39  
    40  // NewHasContainersError creates a new error that satisfies HasContainersError.
    41  func NewHasContainersError(machineId string, containerIds []string) error {
    42  	return errors.WithType(
    43  		fmt.Errorf("machine %s is hosting containers %q",
    44  			machineId,
    45  			strings.Join(containerIds, ",")),
    46  		HasContainersError,
    47  	)
    48  }
    49  
    50  // NewHasAttachmentsError creates a new error that satisfies HasAttachmentsError.
    51  func NewHasAttachmentsError(machineId string, attachments []names.Tag) error {
    52  	return errors.WithType(
    53  		fmt.Errorf(
    54  			"machine %s has attachments %s",
    55  			machineId,
    56  			attachments),
    57  		HasAttachmentsError,
    58  	)
    59  }
    60  
    61  const (
    62  	voting    = "voting"
    63  	nonvoting = "non-voting"
    64  )
    65  
    66  // NewIsControllerMemberError creates a new error that satisfies IsControllerMemberError.
    67  func NewIsControllerMemberError(machineId string, isVoting bool) error {
    68  	status := nonvoting
    69  	if isVoting {
    70  		status = voting
    71  	}
    72  	return errors.WithType(
    73  		fmt.Errorf(
    74  			"machine %s is still a %s controller member",
    75  			machineId, status),
    76  		IsControllerMemberError,
    77  	)
    78  }