func (s *serviceSuite) TestUnitService(c *gc.C) { for i, test := range unitNameTests { c.Logf("test %d: %q", i, test.pattern) if !test.valid { expect := fmt.Sprintf("%q is not a valid unit name", test.pattern) testFunc := func() { names.UnitService(test.pattern) } c.Assert(testFunc, gc.PanicMatches, expect) } else { c.Assert(names.UnitService(test.pattern), gc.Equals, test.service) } } }
func (c *DebugHooksCommand) validateHooks() error { if len(c.hooks) == 0 { return nil } service := names.UnitService(c.Target) relations, err := c.apiClient.ServiceCharmRelations(service) if err != nil { return err } validHooks := make(map[string]bool) for _, hook := range hooks.UnitHooks() { validHooks[string(hook)] = true } for _, relation := range relations { for _, hook := range hooks.RelationHooks() { hook := fmt.Sprintf("%s-%s", relation, hook) validHooks[hook] = true } } for _, hook := range c.hooks { if !validHooks[hook] { names := make([]string, 0, len(validHooks)) for hookName, _ := range validHooks { names = append(names, hookName) } sort.Strings(names) logger.Infof("unknown hook %s, valid hook names: %v", hook, names) return fmt.Errorf("unit %q does not contain hook %q", c.Target, hook) } } return nil }
// Service returns the service. func (u *Unit) Service() (*Service, error) { serviceTag := names.ServiceTag(names.UnitService(u.Name())) service := &Service{ st: u.st, tag: serviceTag, } // Call Refresh() immediately to get the up-to-date // life and other needed locally cached fields. err := service.Refresh() if err != nil { return nil, err } return service, nil }
func (u *UniterAPI) checkRemoteUnit(relUnit *state.RelationUnit, remoteUnitTag string) (string, error) { // Make sure the unit is indeed remote. if remoteUnitTag == u.auth.GetAuthTag() { return "", common.ErrPerm } // Check remoteUnit is indeed related. Note that we don't want to actually get // the *Unit, because it might have been removed; but its relation settings will // persist until the relation itself has been removed (and must remain accessible // because the local unit's view of reality may be time-shifted). _, remoteUnitName, err := names.ParseTag(remoteUnitTag, names.UnitTagKind) if err != nil { return "", err } remoteServiceName := names.UnitService(remoteUnitName) rel := relUnit.Relation() _, err = rel.RelatedEndpoints(remoteServiceName) if err != nil { return "", common.ErrPerm } return remoteUnitName, nil }
// ServiceName returns the service name. func (u *Unit) ServiceName() string { return names.UnitService(u.Name()) }