本文整理匯總了Golang中github.com/juju/names.IsValidMachine函數的典型用法代碼示例。如果您正苦於以下問題:Golang IsValidMachine函數的具體用法?Golang IsValidMachine怎麽用?Golang IsValidMachine使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了IsValidMachine函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Init
func (c *MigrateCommand) Init(args []string) error {
if len(args) == 0 {
return errors.New("missing operation")
}
c.operation, args = args[0], args[1:]
switch c.operation {
case "export":
if len(args) == 0 {
return errors.New("missing model uuid")
}
c.modelUUID, args = args[0], args[1:]
case "import":
if len(args) == 0 {
return errors.New("missing yaml filename")
}
c.filename, args = args[0], args[1:]
default:
return errors.Errorf("unknown operation %q", c.operation)
}
if !names.IsValidMachine(c.machineId) {
return errors.Errorf("%q is not a valid machine id", c.machineId)
}
c.machineTag = names.NewMachineTag(c.machineId)
return cmd.CheckEmpty(args)
}
示例2: TestMachineIdFormats
func (s *machineSuite) TestMachineIdFormats(c *gc.C) {
for i, test := range machineIdTests {
c.Logf("test %d: %q", i, test.pattern)
c.Assert(names.IsValidMachine(test.pattern), gc.Equals, test.valid)
c.Assert(names.IsContainerMachine(test.pattern), gc.Equals, test.container)
}
}
示例3: AddUnits
// AddUnits starts n units of the given service and allocates machines
// to them as necessary.
func AddUnits(st *state.State, svc *state.Service, n int, machineIdSpec string) ([]*state.Unit, error) {
if machineIdSpec != "" && n != 1 {
return nil, errors.Errorf("cannot add multiple units of service %q to a single machine", svc.Name())
}
var placement []*instance.Placement
if machineIdSpec != "" {
mid := machineIdSpec
scope := instance.MachineScope
var containerType instance.ContainerType
specParts := strings.SplitN(machineIdSpec, ":", 2)
if len(specParts) > 1 {
firstPart := specParts[0]
var err error
if containerType, err = instance.ParseContainerType(firstPart); err == nil {
mid = specParts[1]
scope = string(containerType)
}
}
if !names.IsValidMachine(mid) {
return nil, fmt.Errorf("invalid force machine id %q", mid)
}
placement = []*instance.Placement{
{
Scope: scope,
Directive: mid,
},
}
}
return AddUnitsWithPlacement(st, svc, n, placement)
}
示例4: 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)
}
示例5: 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
}
示例6: 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)
}
示例7: addServiceUnits
// addServiceUnits adds a given number of units to a service.
func addServiceUnits(state *state.State, args params.AddServiceUnits) ([]*state.Unit, error) {
service, err := state.Service(args.ServiceName)
if err != nil {
return nil, err
}
if args.NumUnits < 1 {
return nil, fmt.Errorf("must add at least one unit")
}
// New API uses placement directives.
if len(args.Placement) > 0 {
return jjj.AddUnitsWithPlacement(state, service, args.NumUnits, args.Placement)
}
// Otherwise we use the older machine spec.
if args.NumUnits > 1 && args.ToMachineSpec != "" {
return nil, fmt.Errorf("cannot use NumUnits with ToMachineSpec")
}
if args.ToMachineSpec != "" && names.IsValidMachine(args.ToMachineSpec) {
_, err = state.Machine(args.ToMachineSpec)
if err != nil {
return nil, errors.Annotatef(err, `cannot add units for service "%v" to machine %v`, args.ServiceName, args.ToMachineSpec)
}
}
return jjj.AddUnits(state, service, args.NumUnits, args.ToMachineSpec)
}
示例8: makePlacement
// makePlacement makes a placement directive for the given machineIdSpec.
func makePlacement(machineIdSpec string) ([]*instance.Placement, error) {
if machineIdSpec == "" {
return nil, nil
}
mid := machineIdSpec
scope := instance.MachineScope
var containerType instance.ContainerType
specParts := strings.SplitN(machineIdSpec, ":", 2)
if len(specParts) > 1 {
firstPart := specParts[0]
var err error
if containerType, err = instance.ParseContainerType(firstPart); err == nil {
mid = specParts[1]
scope = string(containerType)
}
}
if !names.IsValidMachine(mid) {
return nil, errors.Errorf("invalid force machine id %q", mid)
}
return []*instance.Placement{
{
Scope: scope,
Directive: mid,
},
}, nil
}
示例9: ParseFilesystemAttachmentId
// ParseFilesystemAttachmentId parses a string as a filesystem attachment ID,
// returning the machine and filesystem components.
func ParseFilesystemAttachmentId(id string) (names.MachineTag, names.FilesystemTag, error) {
fields := strings.SplitN(id, ":", 2)
if len(fields) != 2 || !names.IsValidMachine(fields[0]) || !names.IsValidFilesystem(fields[1]) {
return names.MachineTag{}, names.FilesystemTag{}, errors.Errorf("invalid filesystem attachment ID %q", id)
}
machineTag := names.NewMachineTag(fields[0])
filesystemTag := names.NewFilesystemTag(fields[1])
return machineTag, filesystemTag, nil
}
示例10: ParseVolumeAttachmentId
// ParseVolumeAttachmentId parses a string as a volume attachment ID,
// returning the machine and volume components.
func ParseVolumeAttachmentId(id string) (names.MachineTag, names.VolumeTag, error) {
fields := strings.SplitN(id, ":", 2)
if len(fields) != 2 || !names.IsValidMachine(fields[0]) || !names.IsValidVolume(fields[1]) {
return names.MachineTag{}, names.VolumeTag{}, errors.Errorf("invalid volume attachment ID %q", id)
}
machineTag := names.NewMachineTag(fields[0])
volumeTag := names.NewVolumeTag(fields[1])
return machineTag, volumeTag, nil
}
示例11: Init
func (c *removeCommand) Init(args []string) error {
if len(args) == 0 {
return fmt.Errorf("no machines specified")
}
for _, id := range args {
if !names.IsValidMachine(id) {
return fmt.Errorf("invalid machine id %q", id)
}
}
c.MachineIds = args
return nil
}
示例12: Init
// Init initializes the command for running.
func (a *MachineAgent) Init(args []string) error {
if !names.IsValidMachine(a.MachineId) {
return fmt.Errorf("--machine-id option must be set, and expects a non-negative integer")
}
if err := a.AgentConf.CheckArgs(args); err != nil {
return err
}
a.runner = newRunner(isFatal, moreImportant)
a.workersStarted = make(chan struct{})
a.upgradeWorkerContext = NewUpgradeWorkerContext()
return nil
}
示例13: Init
func (c *FixitCommand) Init(args []string) error {
if len(args) == 0 {
return errors.New("missing machine-id")
}
var machineId string
machineId, args = args[0], args[1:]
if !names.IsValidMachine(machineId) {
return errors.Errorf("%q is not a valid machine id", machineId)
}
c.machineTag = names.NewMachineTag(machineId)
return cmd.CheckEmpty(args)
}
示例14: Init
func (c *RetryProvisioningCommand) Init(args []string) error {
if len(args) == 0 {
return fmt.Errorf("no machine specified")
}
c.Machines = make([]names.MachineTag, len(args))
for i, arg := range args {
if !names.IsValidMachine(arg) {
return fmt.Errorf("invalid machine %q", arg)
}
c.Machines[i] = names.NewMachineTag(arg)
}
return nil
}
示例15: ParsePlacement
// ParsePlacement attempts to parse the specified string and create a
// corresponding Placement structure.
//
// If the placement directive is non-empty and missing a scope,
// ErrPlacementScopeMissing will be returned as well as a Placement
// with an empty Scope field.
func ParsePlacement(directive string) (*Placement, error) {
if directive == "" {
return nil, nil
}
if colon := strings.IndexRune(directive, ':'); colon != -1 {
scope, directive := directive[:colon], directive[colon+1:]
if scope == "" {
return nil, ErrPlacementScopeMissing
}
// Sanity check: machine/container scopes require a machine ID as the value.
if (scope == MachineScope || isContainerType(scope)) && !names.IsValidMachine(directive) {
return nil, fmt.Errorf("invalid value %q for %q scope: expected machine-id", directive, scope)
}
return &Placement{Scope: scope, Directive: directive}, nil
}
if names.IsValidMachine(directive) {
return &Placement{Scope: MachineScope, Directive: directive}, nil
}
if isContainerType(directive) {
return &Placement{Scope: directive}, nil
}
return nil, ErrPlacementScopeMissing
}