// DestroyUnits removes a given set of application units. func (api *API) DestroyUnits(args params.DestroyApplicationUnits) error { if err := api.checkCanWrite(); err != nil { return err } if err := api.check.RemoveAllowed(); err != nil { return errors.Trace(err) } var errs []string for _, name := range args.UnitNames { unit, err := api.backend.Unit(name) switch { case errors.IsNotFound(err): err = errors.Errorf("unit %q does not exist", name) case err != nil: case unit.Life() != state.Alive: continue case unit.IsPrincipal(): err = unit.Destroy() default: err = errors.Errorf("unit %q is a subordinate", name) } if err != nil { errs = append(errs, err.Error()) } } return common.DestroyErr("units", args.UnitNames, errs) }
func (s *errorsSuite) TestDestroyErr(c *gc.C) { errs := []string{ "error one", "error two", "error three", } ids := []string{ "id1", "id2", "id3", } c.Assert(common.DestroyErr("entities", ids, nil), jc.ErrorIsNil) err := common.DestroyErr("entities", ids, errs) c.Assert(err, gc.ErrorMatches, "no entities were destroyed: error one; error two; error three") err = common.DestroyErr("entities", ids, errs[1:]) c.Assert(err, gc.ErrorMatches, "some entities were not destroyed: error two; error three") }
// DestroyServiceUnits removes a given set of service units. func (c *Client) DestroyServiceUnits(args params.DestroyServiceUnits) error { if err := c.check.RemoveAllowed(); err != nil { return errors.Trace(err) } var errs []string for _, name := range args.UnitNames { unit, err := c.api.stateAccessor.Unit(name) switch { case errors.IsNotFound(err): err = fmt.Errorf("unit %q does not exist", name) case err != nil: case unit.Life() != state.Alive: continue case unit.IsPrincipal(): err = unit.Destroy() default: err = fmt.Errorf("unit %q is a subordinate", name) } if err != nil { errs = append(errs, err.Error()) } } return common.DestroyErr("units", args.UnitNames, errs) }