Beispiel #1
0
// WatchActionNotifications returns a StringsWatcher for observing
// incoming action calls to a unit. See also state/watcher.go
// Unit.WatchActionNotifications(). This method is called from
// api/uniter/uniter.go WatchActionNotifications().
func (u *UniterAPIV3) WatchActionNotifications(args params.Entities) (params.StringsWatchResults, error) {
	tagToActionReceiver := common.TagToActionReceiverFn(u.st.FindEntity)
	watchOne := common.WatchOneActionReceiverNotifications(tagToActionReceiver, u.resources.Register)
	canAccess, err := u.accessUnit()
	if err != nil {
		return params.StringsWatchResults{}, err
	}
	return common.WatchActionNotifications(args, canAccess, watchOne), nil
}
Beispiel #2
0
// internalList takes a list of Entities representing ActionReceivers
// and returns all of the Actions the extractorFn can get out of the
// ActionReceiver.
func (a *ActionAPI) internalList(arg params.Entities, fn extractorFn) (params.ActionsByReceivers, error) {
	tagToActionReceiver := common.TagToActionReceiverFn(a.state.FindEntity)
	response := params.ActionsByReceivers{Actions: make([]params.ActionsByReceiver, len(arg.Entities))}
	for i, entity := range arg.Entities {
		currentResult := &response.Actions[i]
		receiver, err := tagToActionReceiver(entity.Tag)
		if err != nil {
			currentResult.Error = common.ServerError(common.ErrBadId)
			continue
		}
		currentResult.Receiver = receiver.Tag().String()

		results, err := fn(receiver)
		if err != nil {
			currentResult.Error = common.ServerError(err)
			continue
		}
		currentResult.Actions = results
	}
	return response, nil
}
Beispiel #3
0
func (s *actionsSuite) TestTagToActionReceiverFn(c *gc.C) {
	stubActionReceiver := fakeActionReceiver{}
	stubEntity := fakeEntity{}
	tagToEntity := map[string]state.Entity{
		"unit-valid-0":   stubActionReceiver,
		"unit-invalid-0": stubEntity,
	}
	tagFn := common.TagToActionReceiverFn(makeFindEntity(tagToEntity))

	for i, test := range []struct {
		tag    string
		err    error
		result state.ActionReceiver
	}{{
		tag:    "unit-valid-0",
		result: stubActionReceiver,
	}, {
		tag: "unit-invalid-0",
		err: common.ErrBadId,
	}, {
		tag: "unit-flustered-0",
		err: common.ErrBadId,
	}, {
		tag: "notatag",
		err: common.ErrBadId,
	}} {
		c.Logf("test %d", i)
		receiver, err := tagFn(test.tag)
		if test.err != nil {
			c.Check(err, gc.Equals, test.err)
			c.Check(receiver, gc.IsNil)
		} else {
			c.Assert(err, jc.ErrorIsNil)
			c.Assert(receiver, gc.Equals, test.result)
		}
	}
}
Beispiel #4
0
// Enqueue takes a list of Actions and queues them up to be executed by
// the designated ActionReceiver, returning the params.Action for each
// enqueued Action, or an error if there was a problem enqueueing the
// Action.
func (a *ActionAPI) Enqueue(arg params.Actions) (params.ActionResults, error) {
	if err := a.check.ChangeAllowed(); err != nil {
		return params.ActionResults{}, errors.Trace(err)
	}

	tagToActionReceiver := common.TagToActionReceiverFn(a.state.FindEntity)
	response := params.ActionResults{Results: make([]params.ActionResult, len(arg.Actions))}
	for i, action := range arg.Actions {
		currentResult := &response.Results[i]
		receiver, err := tagToActionReceiver(action.Receiver)
		if err != nil {
			currentResult.Error = common.ServerError(err)
			continue
		}
		enqueued, err := receiver.AddAction(action.Name, action.Parameters)
		if err != nil {
			currentResult.Error = common.ServerError(err)
			continue
		}

		response.Results[i] = common.MakeActionResult(receiver.Tag(), enqueued)
	}
	return response, nil
}
Beispiel #5
0
func (s *actionsSuite) TestWatchOneActionReceiverNotifications(c *gc.C) {
	expectErr := errors.New("zwoosh")
	registerFunc := func(common.Resource) string { return "bambalam" }
	tagToActionReceiver := common.TagToActionReceiverFn(makeFindEntity(map[string]state.Entity{
		"machine-1": &fakeActionReceiver{watcher: &fakeWatcher{}},
		"machine-2": &fakeActionReceiver{watcher: &fakeWatcher{err: expectErr}},
	}))

	watchOneFn := common.WatchOneActionReceiverNotifications(tagToActionReceiver, registerFunc)

	for i, test := range []struct {
		tag       names.Tag
		err       string
		watcherId string
	}{{
		tag: names.NewMachineTag("0"),
		err: "id not found",
	}, {
		tag:       names.NewMachineTag("1"),
		watcherId: "bambalam",
	}, {
		tag: names.NewMachineTag("2"),
		err: "zwoosh",
	}} {
		c.Logf("test %d", i)
		c.Logf(test.tag.String())
		result, err := watchOneFn(test.tag)
		if test.err != "" {
			c.Check(err, gc.ErrorMatches, test.err)
			c.Check(result, jc.DeepEquals, params.StringsWatchResult{})
		} else {
			c.Check(err, jc.ErrorIsNil)
			c.Check(result.StringsWatcherId, gc.Equals, test.watcherId)
		}
	}
}
Beispiel #6
0
func (shim backendShim) TagToActionReceiverFn(findEntity func(names.Tag) (state.Entity, error)) func(string) (state.ActionReceiver, error) {
	return common.TagToActionReceiverFn(findEntity)
}