本文整理匯總了Golang中github.com/juju/names.IsValidUnit函數的典型用法代碼示例。如果您正苦於以下問題:Golang IsValidUnit函數的具體用法?Golang IsValidUnit怎麽用?Golang IsValidUnit使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了IsValidUnit函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: PrivateAddress
// PrivateAddress implements the server side of Client.PrivateAddress.
func (c *Client) PrivateAddress(p params.PrivateAddress) (results params.PrivateAddressResults, err error) {
switch {
case names.IsValidMachine(p.Target):
machine, err := c.api.stateAccessor.Machine(p.Target)
if err != nil {
return results, err
}
addr, err := machine.PrivateAddress()
if err != nil {
return results, errors.Annotatef(err, "error fetching address for machine %q", machine)
}
return params.PrivateAddressResults{PrivateAddress: addr.Value}, nil
case names.IsValidUnit(p.Target):
unit, err := c.api.stateAccessor.Unit(p.Target)
if err != nil {
return results, err
}
addr, err := unit.PrivateAddress()
if err != nil {
return results, errors.Annotatef(err, "error fetching address for unit %q", unit)
}
return params.PrivateAddressResults{PrivateAddress: addr.Value}, nil
}
return results, fmt.Errorf("unknown unit or machine %q", p.Target)
}
示例2: Init
func (c *RunCommand) Init(args []string) error {
// make sure we aren't in an existing hook context
if contextId, err := getenv("JUJU_CONTEXT_ID"); err == nil && contextId != "" {
return fmt.Errorf("juju-run cannot be called from within a hook, have context %q", contextId)
}
if !c.noContext {
if len(args) < 1 {
return fmt.Errorf("missing unit-name")
}
var unitName string
unitName, args = args[0], args[1:]
// If the command line param is a unit id (like service/2) we need to
// change it to the unit tag as that is the format of the agent directory
// on disk (unit-service-2).
if names.IsValidUnit(unitName) {
c.unit = names.NewUnitTag(unitName)
} else {
var err error
c.unit, err = names.ParseUnitTag(unitName)
if err != nil {
return errors.Trace(err)
}
}
}
if len(args) < 1 {
return fmt.Errorf("missing commands")
}
c.commands, args = args[0], args[1:]
return cmd.CheckEmpty(args)
}
示例3: PrivateAddress
// PrivateAddress implements the server side of Client.PrivateAddress.
func (c *Client) PrivateAddress(p params.PrivateAddress) (results params.PrivateAddressResults, err error) {
switch {
case names.IsValidMachine(p.Target):
machine, err := c.api.state.Machine(p.Target)
if err != nil {
return results, err
}
addr := network.SelectInternalAddress(machine.Addresses(), false)
if addr == "" {
return results, fmt.Errorf("machine %q has no internal address", machine)
}
return params.PrivateAddressResults{PrivateAddress: addr}, nil
case names.IsValidUnit(p.Target):
unit, err := c.api.state.Unit(p.Target)
if err != nil {
return results, err
}
addr, ok := unit.PrivateAddress()
if !ok {
return results, fmt.Errorf("unit %q has no internal address", unit)
}
return params.PrivateAddressResults{PrivateAddress: addr}, nil
}
return results, fmt.Errorf("unknown unit or machine %q", p.Target)
}
示例4: Init
// Init initializes the command for running.
func (a *UnitAgent) Init(args []string) error {
if a.UnitName == "" {
return cmdutil.RequiredError("unit-name")
}
if !names.IsValidUnit(a.UnitName) {
return fmt.Errorf(`--unit-name option expects "<service>/<n>" argument`)
}
if err := a.AgentConf.CheckArgs(args); err != nil {
return err
}
a.runner = worker.NewRunner(cmdutil.IsFatal, cmdutil.MoreImportant, worker.RestartDelay)
if !a.logToStdErr {
if err := a.ReadConfig(a.Tag().String()); err != nil {
return err
}
agentConfig := a.CurrentConfig()
// the writer in ctx.stderr gets set as the loggo writer in github.com/juju/cmd/logging.go
a.ctx.Stderr = &lumberjack.Logger{
Filename: agent.LogFilename(agentConfig),
MaxSize: 300, // megabytes
MaxBackups: 2,
}
}
return nil
}
示例5: ReadSettings
// ReadSettings returns a map holding the settings of the unit with the
// supplied name within this relation. An error will be returned if the
// relation no longer exists, or if the unit's service is not part of the
// relation, or the settings are invalid; but mere non-existence of the
// unit is not grounds for an error, because the unit settings are
// guaranteed to persist for the lifetime of the relation, regardless
// of the lifetime of the unit.
func (ru *RelationUnit) ReadSettings(uname string) (params.RelationSettings, error) {
if !names.IsValidUnit(uname) {
return nil, errors.Errorf("%q is not a valid unit", uname)
}
tag := names.NewUnitTag(uname)
var results params.RelationSettingsResults
args := params.RelationUnitPairs{
RelationUnitPairs: []params.RelationUnitPair{{
Relation: ru.relation.tag.String(),
LocalUnit: ru.unit.tag.String(),
RemoteUnit: tag.String(),
}},
}
err := ru.st.facade.FacadeCall("ReadRemoteSettings", 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 result.Error != nil {
return nil, result.Error
}
return result.Settings, nil
}
示例6: hostFromTarget
func (c *SSHCommon) hostFromTarget(target string) (string, error) {
// If the target is neither a machine nor a unit,
// assume it's a hostname and try it directly.
if !names.IsValidMachine(target) && !names.IsValidUnit(target) {
return target, nil
}
// A target may not initially have an address (e.g. the
// address updater hasn't yet run), so we must do this in
// a loop.
if _, err := c.ensureAPIClient(); err != nil {
return "", err
}
var err error
for a := sshHostFromTargetAttemptStrategy.Start(); a.Next(); {
var addr string
if c.proxy {
addr, err = c.apiClient.PrivateAddress(target)
} else {
addr, err = c.apiClient.PublicAddress(target)
}
if err == nil {
return addr, nil
}
}
return "", err
}
示例7: TestServiceNameFormats
func (s *serviceSuite) TestServiceNameFormats(c *gc.C) {
assertService := func(s string, expect bool) {
c.Assert(names.IsValidService(s), gc.Equals, expect)
// Check that anything that is considered a valid service name
// is also (in)valid if a(n) (in)valid unit designator is added
// to it.
c.Assert(names.IsValidUnit(s+"/0"), gc.Equals, expect)
c.Assert(names.IsValidUnit(s+"/99"), gc.Equals, expect)
c.Assert(names.IsValidUnit(s+"/-1"), gc.Equals, false)
c.Assert(names.IsValidUnit(s+"/blah"), gc.Equals, false)
c.Assert(names.IsValidUnit(s+"/"), gc.Equals, false)
}
for i, test := range serviceNameTests {
c.Logf("test %d: %q", i, test.pattern)
assertService(test.pattern, test.valid)
}
}
示例8: IsValid
// IsValid checks if the port range is valid.
func (p PortRange) IsValid() bool {
proto := strings.ToLower(p.Protocol)
if proto != "tcp" && proto != "udp" {
return false
}
if !names.IsValidUnit(p.UnitName) {
return false
}
return p.FromPort <= p.ToPort
}
示例9: Unit
// Unit returns the service's unit with name.
func (s *Service) Unit(name string) (*Unit, error) {
if !names.IsValidUnit(name) {
return nil, fmt.Errorf("%q is not a valid unit name", name)
}
udoc := &unitDoc{}
sel := bson.D{{"_id", name}, {"service", s.doc.Name}}
if err := s.st.units.Find(sel).One(udoc); err != nil {
return nil, fmt.Errorf("cannot get unit %q from service %q: %v", name, s.doc.Name, err)
}
return newUnit(s.st, udoc), nil
}
示例10: resolveMachine
// resolveMachine returns the machine id resolving the given unit or machine
// placeholder.
func (h *bundleHandler) resolveMachine(placeholder string) (string, error) {
machineOrUnit := resolve(placeholder, h.results)
if !names.IsValidUnit(machineOrUnit) {
return machineOrUnit, nil
}
for h.unitStatus[machineOrUnit] == "" {
if err := h.updateUnitStatus(); err != nil {
return "", errors.Annotate(err, "cannot resolve machine")
}
}
return h.unitStatus[machineOrUnit], nil
}
示例11: Init
func (c *RemoveUnitCommand) Init(args []string) error {
c.UnitNames = args
if len(c.UnitNames) == 0 {
return fmt.Errorf("no units specified")
}
for _, name := range c.UnitNames {
if !names.IsValidUnit(name) {
return fmt.Errorf("invalid unit name %q", name)
}
}
return nil
}
示例12: Init
func (c *resolvedCommand) Init(args []string) error {
if len(args) > 0 {
c.UnitName = args[0]
if !names.IsValidUnit(c.UnitName) {
return fmt.Errorf("invalid unit name %q", c.UnitName)
}
args = args[1:]
} else {
return fmt.Errorf("no unit specified")
}
return cmd.CheckEmpty(args)
}
示例13: Handle
func (u unitAssigner) Handle(ids []string) error {
logger.Tracef("Handling unit assignments: %q", ids)
if len(ids) == 0 {
return nil
}
units := make([]names.UnitTag, len(ids))
for i, id := range ids {
if !names.IsValidUnit(id) {
return errors.Errorf("%q is not a valid unit id", id)
}
units[i] = names.NewUnitTag(id)
}
results, err := u.api.AssignUnits(units)
if err != nil {
return err
}
failures := map[string]error{}
logger.Tracef("Unit assignment results: %q", results)
// errors are returned in the same order as the ids given. Any errors from
// the assign units call must be reported as error statuses on the
// respective units (though the assignments will be retried). Not found
// errors indicate that the unit was removed before the assignment was
// requested, which can be safely ignored.
for i, err := range results {
if err != nil && !errors.IsNotFound(err) {
failures[units[i].String()] = err
}
}
if len(failures) > 0 {
args := params.SetStatus{
Entities: make([]params.EntityStatusArgs, len(failures)),
}
x := 0
for unit, err := range failures {
args.Entities[x] = params.EntityStatusArgs{
Tag: unit,
Status: params.StatusError,
Info: err.Error(),
}
x++
}
return u.api.SetAgentStatus(args)
}
return nil
}
示例14: Validate
// IsValid checks if the port range is valid.
func (p PortRange) Validate() error {
proto := strings.ToLower(p.Protocol)
if proto != "tcp" && proto != "udp" {
return errors.Errorf("invalid protocol %q", proto)
}
if !names.IsValidUnit(p.UnitName) {
return errors.Errorf("invalid unit %q", p.UnitName)
}
if p.FromPort > p.ToPort {
return errors.Errorf("invalid port range %d-%d", p.FromPort, p.ToPort)
}
return nil
}
示例15: Init
// Init initializes the command for running.
func (a *UnitAgent) Init(args []string) error {
if a.UnitName == "" {
return requiredError("unit-name")
}
if !names.IsValidUnit(a.UnitName) {
return fmt.Errorf(`--unit-name option expects "<service>/<n>" argument`)
}
if err := a.AgentConf.CheckArgs(args); err != nil {
return err
}
a.runner = worker.NewRunner(isFatal, moreImportant)
return nil
}