本文整理汇总了Golang中github.com/juju/names.NewRelationTag函数的典型用法代码示例。如果您正苦于以下问题:Golang NewRelationTag函数的具体用法?Golang NewRelationTag怎么用?Golang NewRelationTag使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewRelationTag函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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)
}
}
}
}
示例2: TestRelationsChanged
func (s *WatcherSuite) TestRelationsChanged(c *gc.C) {
signalAll(&s.st, &s.leadership)
assertNotifyEvent(c, s.watcher.RemoteStateChanged(), "waiting for remote state change")
relationTag := names.NewRelationTag("mysql:peer")
s.st.relations[relationTag] = &mockRelation{
id: 123, life: params.Alive,
}
s.st.relationUnitsWatchers[relationTag] = &mockRelationUnitsWatcher{
changes: make(chan multiwatcher.RelationUnitsChange, 1),
}
s.st.unit.service.relationsWatcher.changes <- []string{relationTag.Id()}
// There should not be any signal until the relation units watcher has
// returned its initial event also.
assertNoNotifyEvent(c, s.watcher.RemoteStateChanged(), "remote state change")
s.st.relationUnitsWatchers[relationTag].changes <- multiwatcher.RelationUnitsChange{
Changed: map[string]multiwatcher.UnitSettings{"mysql/1": {1}, "mysql/2": {2}},
}
assertNotifyEvent(c, s.watcher.RemoteStateChanged(), "waiting for remote state change")
c.Assert(
s.watcher.Snapshot().Relations,
jc.DeepEquals,
map[int]remotestate.RelationSnapshot{
123: remotestate.RelationSnapshot{
Life: params.Alive,
Members: map[string]int64{"mysql/1": 1, "mysql/2": 2},
},
},
)
// If a relation is known, then updating it does not require any input
// from the relation units watcher.
s.st.relations[relationTag].life = params.Dying
s.st.unit.service.relationsWatcher.changes <- []string{relationTag.Id()}
assertNotifyEvent(c, s.watcher.RemoteStateChanged(), "waiting for remote state change")
c.Assert(s.watcher.Snapshot().Relations[123].Life, gc.Equals, params.Dying)
// If a relation is not found, then it should be removed from the
// snapshot and its relation units watcher stopped.
delete(s.st.relations, relationTag)
s.st.unit.service.relationsWatcher.changes <- []string{relationTag.Id()}
assertNotifyEvent(c, s.watcher.RemoteStateChanged(), "waiting for remote state change")
c.Assert(s.watcher.Snapshot().Relations, gc.HasLen, 0)
c.Assert(s.st.relationUnitsWatchers[relationTag].stopped, jc.IsTrue)
}
示例3: TestNewRelationsWithExistingRelations
func (s *relationsSuite) TestNewRelationsWithExistingRelations(c *gc.C) {
unitTag := names.NewUnitTag("wordpress/0")
abort := make(chan struct{})
var numCalls int32
unitEntity := params.Entities{Entities: []params.Entity{params.Entity{Tag: "unit-wordpress-0"}}}
relationUnits := params.RelationUnits{RelationUnits: []params.RelationUnit{
{Relation: "relation-wordpress.db#mysql.db", Unit: "unit-wordpress-0"},
}}
relationResults := params.RelationResults{
Results: []params.RelationResult{
{
Id: 1,
Key: "wordpress:db mysql:db",
Life: params.Alive,
Endpoint: multiwatcher.Endpoint{
ServiceName: "wordpress",
Relation: charm.Relation{Name: "mysql", Role: charm.RoleProvider, Interface: "db"},
}},
},
}
apiCaller := mockAPICaller(c, &numCalls,
uniterApiCall("Life", unitEntity, params.LifeResults{Results: []params.LifeResult{{Life: params.Alive}}}, nil),
uniterApiCall("JoinedRelations", unitEntity, params.StringsResults{Results: []params.StringsResult{{Result: []string{"relation-wordpress:db mysql:db"}}}}, nil),
uniterApiCall("Relation", relationUnits, relationResults, nil),
uniterApiCall("Relation", relationUnits, relationResults, nil),
uniterApiCall("Watch", unitEntity, params.NotifyWatchResults{Results: []params.NotifyWatchResult{{NotifyWatcherId: "1"}}}, nil),
uniterApiCall("EnterScope", relationUnits, params.ErrorResults{Results: []params.ErrorResult{{}}}, nil),
)
st := uniter.NewState(apiCaller, unitTag)
r, err := relation.NewRelations(st, unitTag, s.stateDir, s.relationsDir, abort)
c.Assert(err, jc.ErrorIsNil)
assertNumCalls(c, &numCalls, 6)
info := r.GetInfo()
c.Assert(info, gc.HasLen, 1)
oneInfo := info[1]
c.Assert(oneInfo.RelationUnit.Relation().Tag(), gc.Equals, names.NewRelationTag("wordpress:db mysql:db"))
c.Assert(oneInfo.RelationUnit.Endpoint(), jc.DeepEquals, uniter.Endpoint{
Relation: charm.Relation{Name: "mysql", Role: "provider", Interface: "db", Optional: false, Limit: 0, Scope: ""},
})
c.Assert(oneInfo.MemberNames, gc.HasLen, 0)
}
示例4: relationsChanged
// relationsChanged responds to service relation changes.
func (w *RemoteStateWatcher) relationsChanged(keys []string) error {
w.mu.Lock()
defer w.mu.Unlock()
for _, key := range keys {
relationTag := names.NewRelationTag(key)
rel, err := w.st.Relation(relationTag)
if params.IsCodeNotFoundOrCodeUnauthorized(err) {
// If it's actually gone, this unit cannot have entered
// scope, and therefore never needs to know about it.
if ruw, ok := w.relations[relationTag]; ok {
worker.Stop(ruw)
delete(w.relations, relationTag)
delete(w.current.Relations, ruw.relationId)
}
} else if err != nil {
return errors.Trace(err)
} else {
if _, ok := w.relations[relationTag]; ok {
relationSnapshot := w.current.Relations[rel.Id()]
relationSnapshot.Life = rel.Life()
w.current.Relations[rel.Id()] = relationSnapshot
continue
}
ruw, err := w.st.WatchRelationUnits(relationTag, w.unit.Tag())
if err != nil {
return errors.Trace(err)
}
// Because of the delay before handing off responsibility to
// newRelationUnitsWatcher below, add to our own catacomb to
// ensure errors get picked up if they happen.
if err := w.catacomb.Add(ruw); err != nil {
return errors.Trace(err)
}
if err := w.watchRelationUnits(rel, relationTag, ruw); err != nil {
return errors.Trace(err)
}
}
}
return nil
}
示例5: TestRelationUnitsChanged
func (s *WatcherSuite) TestRelationUnitsChanged(c *gc.C) {
signalAll(&s.st, &s.leadership)
assertNotifyEvent(c, s.watcher.RemoteStateChanged(), "waiting for remote state change")
relationTag := names.NewRelationTag("mysql:peer")
s.st.relations[relationTag] = &mockRelation{
id: 123, life: params.Alive,
}
s.st.relationUnitsWatchers[relationTag] = &mockRelationUnitsWatcher{
changes: make(chan multiwatcher.RelationUnitsChange, 1),
}
s.st.unit.service.relationsWatcher.changes <- []string{relationTag.Id()}
s.st.relationUnitsWatchers[relationTag].changes <- multiwatcher.RelationUnitsChange{
Changed: map[string]multiwatcher.UnitSettings{"mysql/1": {1}},
}
assertNotifyEvent(c, s.watcher.RemoteStateChanged(), "waiting for remote state change")
s.st.relationUnitsWatchers[relationTag].changes <- multiwatcher.RelationUnitsChange{
Changed: map[string]multiwatcher.UnitSettings{"mysql/1": {2}, "mysql/2": {1}},
}
assertNotifyEvent(c, s.watcher.RemoteStateChanged(), "waiting for remote state change")
c.Assert(
s.watcher.Snapshot().Relations[123].Members,
jc.DeepEquals,
map[string]int64{"mysql/1": 2, "mysql/2": 1},
)
s.st.relationUnitsWatchers[relationTag].changes <- multiwatcher.RelationUnitsChange{
Departed: []string{"mysql/1", "mysql/42"},
}
assertNotifyEvent(c, s.watcher.RemoteStateChanged(), "waiting for remote state change")
c.Assert(
s.watcher.Snapshot().Relations[123].Members,
jc.DeepEquals,
map[string]int64{"mysql/2": 1},
)
}
示例6: relationsChanged
// relationsChanged responds to service relation changes.
func (w *RemoteStateWatcher) relationsChanged(keys []string) error {
w.mu.Lock()
defer w.mu.Unlock()
for _, key := range keys {
relationTag := names.NewRelationTag(key)
rel, err := w.st.Relation(relationTag)
if params.IsCodeNotFoundOrCodeUnauthorized(err) {
// If it's actually gone, this unit cannot have entered
// scope, and therefore never needs to know about it.
if ruw, ok := w.relations[relationTag]; ok {
if err := ruw.Stop(); err != nil {
return errors.Trace(err)
}
delete(w.relations, relationTag)
delete(w.current.Relations, ruw.relationId)
}
} else if err != nil {
return err
} else {
if _, ok := w.relations[relationTag]; ok {
relationSnapshot := w.current.Relations[rel.Id()]
relationSnapshot.Life = rel.Life()
w.current.Relations[rel.Id()] = relationSnapshot
continue
}
in, err := w.st.WatchRelationUnits(relationTag, w.unit.Tag())
if err != nil {
return errors.Trace(err)
}
if err := w.watchRelationUnits(rel, relationTag, in); err != nil {
watcher.Stop(in, &w.tomb)
return errors.Trace(err)
}
}
}
return nil
}
示例7: RelationById
// RelationById returns the existing relation with the given id.
func (st *State) RelationById(id int) (*Relation, error) {
var results params.RelationResults
args := params.RelationIds{
RelationIds: []int{id},
}
err := st.facade.FacadeCall("RelationById", args, &results)
if err != nil {
return nil, err
}
if len(results.Results) != 1 {
return nil, fmt.Errorf("expected 1 result, got %d", len(results.Results))
}
result := results.Results[0]
if err := result.Error; err != nil {
return nil, err
}
relationTag := names.NewRelationTag(result.Key)
return &Relation{
id: result.Id,
tag: relationTag,
life: result.Life,
st: st,
}, nil
}
示例8: Tag
// Tag returns a name identifying the relation.
func (r *Relation) Tag() names.Tag {
return names.NewRelationTag(r.doc.Key)
}
示例9: TestParseRelationTag
c.Assert(names.IsValidRelation(key), gc.Equals, isValid)
c.Assert(names.IsValidRelation(peerKey), gc.Equals, isValid)
}
}
}
var parseRelationTagTests = []struct {
tag string
expected names.Tag
err error
}{{
tag: "",
err: names.InvalidTagError("", ""),
}, {
tag: "relation-wordpress:db mysql:db",
expected: names.NewRelationTag("wordpress:db mysql:db"),
}, {
tag: "relation-wordpress:mysql",
expected: names.NewRelationTag("wordpress:mysql"),
}, {
tag: "dave",
err: names.InvalidTagError("dave", ""),
}, {
tag: "service-dave",
err: names.InvalidTagError("service-dave", names.RelationTagKind),
}}
func (s *relationSuite) TestParseRelationTag(c *gc.C) {
for i, t := range parseRelationTagTests {
c.Logf("test %d: %s", i, t.tag)
got, err := names.ParseRelationTag(t.tag)
示例10: loop
//.........这里部分代码省略.........
// We start reacting to address changes after the
// first config-changed is processed, ignoring the
// initial address changed event.
addressChanges = addressesw.Changes()
if _, ok := <-addressChanges; !ok {
return watcher.MustErr(addressesw)
}
}
filterLogger.Debugf("preparing new config event")
f.outConfig = f.outConfigOn
discardConfig = f.discardConfig
case _, ok = <-addressChanges:
filterLogger.Debugf("got address change")
if !ok {
return watcher.MustErr(addressesw)
}
// address change causes config-changed event
filterLogger.Debugf("preparing new config event")
f.outConfig = f.outConfigOn
case ids, ok := <-actionsw.Changes():
filterLogger.Debugf("got %d actions", len(ids))
if !ok {
return watcher.MustErr(actionsw)
}
f.actionsPending = append(f.actionsPending, ids...)
f.nextAction = f.getNextAction()
case keys, ok := <-relationsw.Changes():
filterLogger.Debugf("got relations change")
if !ok {
return watcher.MustErr(relationsw)
}
var ids []int
for _, key := range keys {
relationTag := names.NewRelationTag(key).String()
rel, err := f.st.Relation(relationTag)
if params.IsCodeNotFoundOrCodeUnauthorized(err) {
// If it's actually gone, this unit cannot have entered
// scope, and therefore never needs to know about it.
} else if err != nil {
return err
} else {
ids = append(ids, rel.Id())
}
}
f.relationsChanged(ids)
// Send events on active out chans.
case f.outUpgrade <- f.upgrade:
filterLogger.Debugf("sent upgrade event")
f.outUpgrade = nil
case f.outResolved <- f.resolved:
filterLogger.Debugf("sent resolved event")
f.outResolved = nil
case f.outConfig <- nothing:
filterLogger.Debugf("sent config event")
f.outConfig = nil
case f.outAction <- f.nextAction:
f.nextAction = f.getNextAction()
filterLogger.Debugf("sent action event")
case f.outRelations <- f.relations:
filterLogger.Debugf("sent relations event")
f.outRelations = nil
f.relations = nil
// Handle explicit requests.
case curl := <-f.setCharm:
示例11: Tag
// Tag returns a name identifying the relation that is safe to use
// as a file name.
func (r *Relation) Tag() string {
return names.NewRelationTag(r.doc.Key).String()
}
示例12: TestParseTag
resultId: "wordpress" + names.ActionMarker + "333",
}, {
tag: "action-wordpress/0" + names.ActionMarker + "333",
expectKind: names.ActionTagKind,
expectType: names.ActionTag{},
resultId: "wordpress/0" + names.ActionMarker + "333",
}, {
tag: "foo",
resultErr: `"foo" is not a valid tag`,
}}
var makeTag = map[string]func(string) names.Tag{
names.MachineTagKind: func(tag string) names.Tag { return names.NewMachineTag(tag) },
names.UnitTagKind: func(tag string) names.Tag { return names.NewUnitTag(tag) },
names.ServiceTagKind: func(tag string) names.Tag { return names.NewServiceTag(tag) },
names.RelationTagKind: func(tag string) names.Tag { return names.NewRelationTag(tag) },
names.EnvironTagKind: func(tag string) names.Tag { return names.NewEnvironTag(tag) },
names.UserTagKind: func(tag string) names.Tag { return names.NewUserTag(tag) },
names.NetworkTagKind: func(tag string) names.Tag { return names.NewNetworkTag(tag) },
names.ActionTagKind: func(tag string) names.Tag { return names.NewActionTag(tag) },
}
func (*tagSuite) TestParseTag(c *gc.C) {
for i, test := range parseTagTests {
c.Logf("test %d: %q expectKind %q", i, test.tag, test.expectKind)
tag, err := names.ParseTag(test.tag)
if test.resultErr != "" {
c.Assert(err, gc.ErrorMatches, test.resultErr)
c.Assert(tag, gc.IsNil)
// If the tag has a valid kind which matches the
示例13: loop
//.........这里部分代码省略.........
return watcher.EnsureErr(configw)
}
seenConfigChange = true
maybePrepareConfigEvent()
case _, ok = <-addressesw.Changes():
filterLogger.Debugf("got address change")
if !ok {
return watcher.EnsureErr(addressesw)
}
seenAddressChange = true
maybePrepareConfigEvent()
case _, ok = <-meterStatusw.Changes():
filterLogger.Debugf("got meter status change")
if !ok {
return watcher.EnsureErr(meterStatusw)
}
if err = f.meterStatusChanged(); err != nil {
return errors.Trace(err)
}
case ids, ok := <-actionsw.Changes():
filterLogger.Debugf("got %d actions", len(ids))
if !ok {
return watcher.EnsureErr(actionsw)
}
f.actionsPending = append(f.actionsPending, ids...)
f.nextAction = f.getNextAction()
case keys, ok := <-relationsw.Changes():
filterLogger.Debugf("got relations change")
if !ok {
return watcher.EnsureErr(relationsw)
}
var ids []int
for _, key := range keys {
relationTag := names.NewRelationTag(key)
rel, err := f.st.Relation(relationTag)
if params.IsCodeNotFoundOrCodeUnauthorized(err) {
// If it's actually gone, this unit cannot have entered
// scope, and therefore never needs to know about it.
} else if err != nil {
return err
} else {
ids = append(ids, rel.Id())
}
}
f.relationsChanged(ids)
case ids, ok := <-storagew.Changes():
filterLogger.Debugf("got storage change")
if !ok {
return watcher.EnsureErr(storagew)
}
tags := make([]names.StorageTag, len(ids))
for i, id := range ids {
tag := names.NewStorageTag(id)
tags[i] = tag
}
f.storageChanged(tags)
case _, ok = <-leaderSettingsw.Changes():
filterLogger.Debugf("got leader settings change: ok=%t", ok)
if !ok {
return watcher.EnsureErr(leaderSettingsw)
}
if sendLeaderSettings {
// only send the leader settings changed event
// if it hasn't been explicitly disabled
f.outLeaderSettings = f.outLeaderSettingsOn
} else {