Beispiel #1
0
// SetStatus sets the status of each given entity.
func (s *StatusSetter) SetStatus(args params.SetStatus) (params.ErrorResults, error) {
	// This is only to ensure compatibility with v1.12.
	// DEPRECATE(v1.14)
	if len(args.Entities) == 0 {
		args.Entities = args.Machines
	}
	result := params.ErrorResults{
		Results: make([]params.ErrorResult, len(args.Entities)),
	}
	if len(args.Entities) == 0 {
		return result, nil
	}
	canModify, err := s.getCanModify()
	if err != nil {
		return params.ErrorResults{}, err
	}
	for i, arg := range args.Entities {
		err := ErrPerm
		if canModify(arg.Tag) {
			err = s.setEntityStatus(arg.Tag, arg.Status, arg.Info)
		}
		result.Results[i].Error = ServerError(err)
	}
	return result, nil
}
Beispiel #2
0
func (*statusSetterSuite) TestSetStatus(c *gc.C) {
	st := &fakeState{
		entities: map[string]entityWithError{
			"x0": &fakeStatusSetter{status: params.StatusPending, info: "blah", err: fmt.Errorf("x0 fails")},
			"x1": &fakeStatusSetter{status: params.StatusStarted, info: "foo"},
			"x2": &fakeStatusSetter{status: params.StatusError, info: "some info"},
			"x3": &fakeStatusSetter{fetchError: "x3 error"},
			"x4": &fakeStatusSetter{status: params.StatusStopped, info: ""},
		},
	}
	getCanModify := func() (common.AuthFunc, error) {
		return func(tag string) bool {
			switch tag {
			case "x0", "x1", "x2", "x3":
				return true
			}
			return false
		}, nil
	}
	s := common.NewStatusSetter(st, getCanModify)
	args := params.SetStatus{
		Entities: []params.SetEntityStatus{
			{"x0", params.StatusStarted, "bar"},
			{"x1", params.StatusStopped, ""},
			{"x2", params.StatusPending, "not really"},
			{"x3", params.StatusStopped, ""},
			{"x4", params.StatusError, "blarg"},
			{"x5", params.StatusStarted, "42"},
		},
	}
	result, err := s.SetStatus(args)
	c.Assert(err, gc.IsNil)
	c.Assert(result, gc.DeepEquals, params.ErrorResults{
		Results: []params.ErrorResult{
			{&params.Error{Message: "x0 fails"}},
			{nil},
			{nil},
			{&params.Error{Message: "x3 error"}},
			{apiservertesting.ErrUnauthorized},
			{apiservertesting.ErrUnauthorized},
		},
	})
	get := func(tag string) *fakeStatusSetter {
		return st.entities[tag].(*fakeStatusSetter)
	}
	c.Assert(get("x1").status, gc.Equals, params.StatusStopped)
	c.Assert(get("x1").info, gc.Equals, "")
	c.Assert(get("x2").status, gc.Equals, params.StatusPending)
	c.Assert(get("x2").info, gc.Equals, "not really")

	// Test compatibility with v1.12.
	// Remove the rest of this test once it's deprecated.
	// DEPRECATE(v1.14)
	args.Machines = args.Entities
	args.Entities = nil
	result, err = s.SetStatus(args)
	c.Assert(err, gc.IsNil)
	c.Assert(result, gc.DeepEquals, params.ErrorResults{
		Results: []params.ErrorResult{
			{&params.Error{Message: "x0 fails"}},
			{nil},
			{nil},
			{&params.Error{Message: "x3 error"}},
			{apiservertesting.ErrUnauthorized},
			{apiservertesting.ErrUnauthorized},
		},
	})
}