本文整理匯總了Golang中github.com/juju/names.NewActionTag函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewActionTag函數的具體用法?Golang NewActionTag怎麽用?Golang NewActionTag使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewActionTag函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestAuthAndActionFromTagFn
func (s *actionsSuite) TestAuthAndActionFromTagFn(c *gc.C) {
notFoundActionTag := names.NewActionTag(utils.MustNewUUID().String())
authorizedActionTag := names.NewActionTag(utils.MustNewUUID().String())
authorizedMachineTag := names.NewMachineTag("1")
authorizedAction := fakeAction{name: "action1", receiver: authorizedMachineTag.Id()}
unauthorizedActionTag := names.NewActionTag(utils.MustNewUUID().String())
unauthorizedMachineTag := names.NewMachineTag("10")
unauthorizedAction := fakeAction{name: "action2", receiver: unauthorizedMachineTag.Id()}
invalidReceiverActionTag := names.NewActionTag(utils.MustNewUUID().String())
invalidReceiverAction := fakeAction{name: "action2", receiver: "masterexploder"}
canAccess := makeCanAccess(map[names.Tag]bool{
authorizedMachineTag: true,
})
getActionByTag := makeGetActionByTag(map[names.ActionTag]state.Action{
authorizedActionTag: authorizedAction,
unauthorizedActionTag: unauthorizedAction,
invalidReceiverActionTag: invalidReceiverAction,
})
tagFn := common.AuthAndActionFromTagFn(canAccess, getActionByTag)
for i, test := range []struct {
tag string
errString string
err error
expectedAction state.Action
}{{
tag: "invalid-action-tag",
errString: `"invalid-action-tag" is not a valid tag`,
}, {
tag: notFoundActionTag.String(),
errString: "action not found",
}, {
tag: invalidReceiverActionTag.String(),
errString: `invalid actionreceiver name "masterexploder"`,
}, {
tag: unauthorizedActionTag.String(),
err: common.ErrPerm,
}, {
tag: authorizedActionTag.String(),
expectedAction: authorizedAction,
}} {
c.Logf("test %d", i)
action, err := tagFn(test.tag)
if test.errString != "" {
c.Check(err, gc.ErrorMatches, test.errString)
c.Check(action, gc.IsNil)
} else if test.err != nil {
c.Check(err, gc.Equals, test.err)
c.Check(action, gc.IsNil)
} else {
c.Check(err, jc.ErrorIsNil)
c.Check(action, gc.Equals, action)
}
}
}
示例2: TestParseActionTag
func (s *actionSuite) TestParseActionTag(c *gc.C) {
parseActionTagTests := []struct {
tag string
expected names.Tag
err error
}{
{
tag: "",
expected: nil,
err: names.InvalidTagError("", ""),
}, {
tag: "action-good" + names.ActionMarker + "123",
expected: names.NewActionTag("good" + names.ActionMarker + "123"),
err: nil,
}, {
tag: "action-good/0" + names.ActionMarker + "123",
expected: names.NewActionTag("good/0" + names.ActionMarker + "123"),
err: nil,
}, {
tag: "action-bad/00" + names.ActionMarker + "123",
expected: nil,
err: names.InvalidTagError("action-bad/00"+names.ActionMarker+"123", names.ActionTagKind),
}, {
tag: "dave",
expected: nil,
err: names.InvalidTagError("dave", ""),
}, {
tag: "action-dave/0",
expected: nil,
err: names.InvalidTagError("action-dave/0", names.ActionTagKind),
}, {
tag: "action",
expected: nil,
err: names.InvalidTagError("action", ""),
}, {
tag: "user-dave",
expected: nil,
err: names.InvalidTagError("user-dave", names.ActionTagKind),
}}
for i, t := range parseActionTagTests {
c.Logf("test %d: %s", i, t.tag)
got, err := names.ParseActionTag(t.tag)
if err != nil || t.err != nil {
c.Check(err, gc.DeepEquals, t.err)
continue
}
c.Check(got, gc.FitsTypeOf, t.expected)
c.Check(got, gc.Equals, t.expected)
}
}
示例3: Handle
// Handle is part of the watcher.StringsHandler interface.
// It should give us any actions currently enqueued for this machine.
// We try to execute every action before returning
func (h *handler) Handle(_ <-chan struct{}, actionsSlice []string) error {
for _, actionId := range actionsSlice {
ok := names.IsValidAction(actionId)
if !ok {
return errors.Errorf("got invalid action id %s", actionId)
}
actionTag := names.NewActionTag(actionId)
action, err := h.config.Facade.Action(actionTag)
if err != nil {
return errors.Annotatef(err, "could not retrieve action %s", actionId)
}
err = h.config.Facade.ActionBegin(actionTag)
if err != nil {
return errors.Annotatef(err, "could not begin action %s", action.Name())
}
// We try to handle the action. The result returned from handling the action is
// sent through using ActionFinish. We only stop the loop if ActionFinish fails.
var finishErr error
results, err := h.config.HandleAction(action.Name(), action.Params())
if err != nil {
finishErr = h.config.Facade.ActionFinish(actionTag, params.ActionFailed, nil, err.Error())
} else {
finishErr = h.config.Facade.ActionFinish(actionTag, params.ActionCompleted, results, "")
}
if finishErr != nil {
return errors.Trace(finishErr)
}
}
return nil
}
示例4: TestActionFinishSuccess
func (s *ClientSuite) TestActionFinishSuccess(c *gc.C) {
tag := names.NewActionTag(utils.MustNewUUID().String())
status := "stubstatus"
actionResults := map[string]interface{}{"stub": "stub"}
message := "stubmsg"
expectedCalls := []jujutesting.StubCall{{
"MachineActions.FinishActions",
[]interface{}{"", params.ActionExecutionResults{
Results: []params.ActionExecutionResult{{
ActionTag: tag.String(),
Status: status,
Results: actionResults,
Message: message,
}},
}},
}}
var stub jujutesting.Stub
apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
stub.AddCall(objType+"."+request, id, arg)
c.Check(result, gc.FitsTypeOf, ¶ms.ErrorResults{})
*(result.(*params.ErrorResults)) = params.ErrorResults{
Results: []params.ErrorResult{{}},
}
return nil
})
client := machineactions.NewClient(apiCaller)
err := client.ActionFinish(tag, status, actionResults, message)
c.Assert(err, jc.ErrorIsNil)
stub.CheckCalls(c, expectedCalls)
}
示例5: TestActionFinishTooManyResults
func (s *ClientSuite) TestActionFinishTooManyResults(c *gc.C) {
tag := names.NewActionTag(utils.MustNewUUID().String())
expectedCalls := []jujutesting.StubCall{{
"MachineActions.FinishActions",
[]interface{}{"", params.ActionExecutionResults{
Results: []params.ActionExecutionResult{{
ActionTag: tag.String(),
Status: "",
Results: nil,
Message: "",
}},
}},
}}
var stub jujutesting.Stub
apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
stub.AddCall(objType+"."+request, id, arg)
c.Check(result, gc.FitsTypeOf, ¶ms.ErrorResults{})
res := result.(*params.ErrorResults)
res.Results = make([]params.ErrorResult, 2)
return nil
})
client := machineactions.NewClient(apiCaller)
err := client.ActionFinish(tag, "", nil, "")
c.Assert(err, gc.ErrorMatches, "expected 1 result, got 2")
stub.CheckCalls(c, expectedCalls)
}
示例6: TestGetActionSuccess
func (s *ClientSuite) TestGetActionSuccess(c *gc.C) {
tag := names.NewActionTag(utils.MustNewUUID().String())
expectedCalls := []jujutesting.StubCall{{
"MachineActions.Actions",
[]interface{}{"", params.Entities{
Entities: []params.Entity{{Tag: tag.String()}},
}},
}}
expectedName := "ack"
expectedParams := map[string]interface{}{"floob": "zgloob"}
var stub jujutesting.Stub
apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
stub.AddCall(objType+"."+request, id, arg)
c.Check(result, gc.FitsTypeOf, ¶ms.ActionResults{})
*(result.(*params.ActionResults)) = params.ActionResults{
Results: []params.ActionResult{{
Action: ¶ms.Action{
Name: expectedName,
Parameters: expectedParams,
},
}},
}
return nil
})
client := machineactions.NewClient(apiCaller)
action, err := client.Action(tag)
c.Assert(err, jc.ErrorIsNil)
c.Assert(action.Name(), gc.Equals, expectedName)
c.Assert(action.Params(), gc.DeepEquals, expectedParams)
stub.CheckCalls(c, expectedCalls)
}
示例7: TestGetActionResultError
func (s *ClientSuite) TestGetActionResultError(c *gc.C) {
tag := names.NewActionTag(utils.MustNewUUID().String())
expectedCalls := []jujutesting.StubCall{{
"MachineActions.Actions",
[]interface{}{"", params.Entities{
Entities: []params.Entity{{Tag: tag.String()}},
}},
}}
expectedErr := ¶ms.Error{
Message: "rigged",
Code: params.CodeNotAssigned,
}
var stub jujutesting.Stub
apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
stub.AddCall(objType+"."+request, id, arg)
c.Check(result, gc.FitsTypeOf, ¶ms.ActionResults{})
*(result.(*params.ActionResults)) = params.ActionResults{
Results: []params.ActionResult{{
Error: expectedErr,
}},
}
return nil
})
client := machineactions.NewClient(apiCaller)
action, err := client.Action(tag)
c.Assert(errors.Cause(err), gc.Equals, expectedErr)
c.Assert(action, gc.IsNil)
stub.CheckCalls(c, expectedCalls)
}
示例8: TestPrefixSuffix
func (s *actionSuite) TestPrefixSuffix(c *gc.C) {
var tests = []struct {
prefix string
suffix int
}{
{prefix: "asdf", suffix: 0},
{prefix: "qwer/0", suffix: 10},
{prefix: "zxcv/3", suffix: 11},
}
for _, test := range tests {
suf := fmt.Sprintf("%d", test.suffix)
action := names.NewActionTag(test.prefix + names.ActionMarker + suf)
c.Assert(action.Prefix(), gc.Equals, test.prefix)
c.Assert(action.Sequence(), gc.Equals, test.suffix)
result := names.NewActionResultTag(test.prefix + names.ActionResultMarker + suf)
c.Assert(result.Prefix(), gc.Equals, test.prefix)
c.Assert(result.Sequence(), gc.Equals, test.suffix)
c.Assert(action.PrefixTag(), gc.Not(gc.IsNil))
c.Assert(action.PrefixTag(), gc.DeepEquals, result.PrefixTag())
}
}
示例9: NewActionRunner
// NewActionRunner exists to satisfy the Factory interface.
func (f *factory) NewActionRunner(actionId string) (Runner, error) {
ch, err := getCharm(f.paths.GetCharmDir())
if err != nil {
return nil, errors.Trace(err)
}
ok := names.IsValidAction(actionId)
if !ok {
return nil, &badActionError{actionId, "not valid actionId"}
}
tag := names.NewActionTag(actionId)
action, err := f.state.Action(tag)
if params.IsCodeNotFoundOrCodeUnauthorized(err) {
return nil, ErrActionNotAvailable
} else if params.IsCodeActionNotAvailable(err) {
return nil, ErrActionNotAvailable
} else if err != nil {
return nil, errors.Trace(err)
}
name := action.Name()
spec, ok := ch.Actions().ActionSpecs[name]
if !ok {
return nil, &badActionError{name, "not defined"}
}
params := action.Params()
if err := spec.ValidateParams(params); err != nil {
return nil, &badActionError{name, err.Error()}
}
actionData := newActionData(name, &tag, params)
ctx, err := f.contextFactory.ActionContext(actionData)
runner := NewRunner(ctx, f.paths)
return runner, nil
}
示例10: TestActionBeginSuccess
func (s *ClientSuite) TestActionBeginSuccess(c *gc.C) {
tag := names.NewActionTag(utils.MustNewUUID().String())
expectedCalls := []jujutesting.StubCall{{
"MachineActions.BeginActions",
[]interface{}{"", params.Entities{
Entities: []params.Entity{{Tag: tag.String()}},
}},
}}
var stub jujutesting.Stub
apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
stub.AddCall(objType+"."+request, id, arg)
c.Check(result, gc.FitsTypeOf, ¶ms.ErrorResults{})
*(result.(*params.ErrorResults)) = params.ErrorResults{
Results: []params.ErrorResult{{}},
}
return nil
})
client := machineactions.NewClient(apiCaller)
err := client.ActionBegin(tag)
c.Assert(err, jc.ErrorIsNil)
stub.CheckCalls(c, expectedCalls)
}
示例11: TestActionFinishError
func (s *ClientSuite) TestActionFinishError(c *gc.C) {
tag := names.NewActionTag(utils.MustNewUUID().String())
expectedCalls := []jujutesting.StubCall{{
"MachineActions.FinishActions",
[]interface{}{"", params.ActionExecutionResults{
Results: []params.ActionExecutionResult{{
ActionTag: tag.String(),
Status: "",
Results: nil,
Message: "",
}},
}},
}}
expectedErr := errors.Errorf("blam")
var stub jujutesting.Stub
apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
stub.AddCall(objType+"."+request, id, arg)
c.Check(result, gc.FitsTypeOf, ¶ms.ErrorResults{})
return expectedErr
})
client := machineactions.NewClient(apiCaller)
err := client.ActionFinish(tag, "", nil, "")
c.Assert(errors.Cause(err), gc.Equals, expectedErr)
stub.CheckCalls(c, expectedCalls)
}
示例12: makeActionQuery
func makeActionQuery(actionID string, receiverType string, receiverTag names.Tag) actionQuery {
return actionQuery{
actionTag: names.NewActionTag(actionID),
receiver: actionReceiver{
receiverType: receiverType,
tag: receiverTag,
},
}
}
示例13: TestAuthFuncForTagKind
func (s *commonSuite) TestAuthFuncForTagKind(c *gc.C) {
// TODO(dimitern): This list of all supported tags and kinds needs
// to live in juju/names.
uuid, err := utils.NewUUID()
c.Assert(err, jc.ErrorIsNil)
allTags := []names.Tag{
nil, // invalid tag
names.NewActionTag(uuid.String()),
names.NewCharmTag("cs:precise/missing"),
names.NewModelTag(uuid.String()),
names.NewFilesystemTag("20/20"),
names.NewLocalUserTag("user"),
names.NewMachineTag("42"),
names.NewNetworkTag("public"),
names.NewRelationTag("wordpress:mysql mysql:db"),
names.NewServiceTag("wordpress"),
names.NewSpaceTag("apps"),
names.NewStorageTag("foo/42"),
names.NewUnitTag("wordpress/5"),
names.NewUserTag("joe"),
names.NewVolumeTag("80/20"),
}
for i, allowedTag := range allTags {
c.Logf("test #%d: allowedTag: %v", i, allowedTag)
var allowedKind string
if allowedTag != nil {
allowedKind = allowedTag.Kind()
}
getAuthFunc := common.AuthFuncForTagKind(allowedKind)
authFunc, err := getAuthFunc()
if allowedKind == "" {
c.Check(err, gc.ErrorMatches, "tag kind cannot be empty")
c.Check(authFunc, gc.IsNil)
continue
} else if !c.Check(err, jc.ErrorIsNil) {
continue
}
for j, givenTag := range allTags {
c.Logf("test #%d.%d: givenTag: %v", i, j, givenTag)
var givenKind string
if givenTag != nil {
givenKind = givenTag.Kind()
}
if allowedKind == givenKind {
c.Check(authFunc(givenTag), jc.IsTrue)
} else {
c.Check(authFunc(givenTag), jc.IsFalse)
}
}
}
}
示例14: setResponse
func (m *mockRunAPI) setResponse(id string, mock mockResponse) {
if m.runResponses == nil {
m.runResponses = make(map[string]params.ActionResult)
}
if m.receiverIdMap == nil {
m.receiverIdMap = make(map[string]string)
}
actionTag := names.NewActionTag(utils.MustNewUUID().String())
m.receiverIdMap[id] = actionTag.Id()
m.runResponses[id] = makeActionResult(mock, actionTag.String())
}
示例15: FailAction
// FailAction is part of the operation.Callbacks interface.
func (opc *operationCallbacks) FailAction(actionId, message string) error {
if !names.IsValidAction(actionId) {
return errors.Errorf("invalid action id %q", actionId)
}
tag := names.NewActionTag(actionId)
err := opc.u.st.ActionFinish(tag, params.ActionFailed, nil, message)
if params.IsCodeNotFoundOrCodeUnauthorized(err) {
err = nil
}
return err
}