本文整理匯總了Golang中github.com/wallyworld/core/names.ParseTag函數的典型用法代碼示例。如果您正苦於以下問題:Golang ParseTag函數的具體用法?Golang ParseTag怎麽用?Golang ParseTag使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ParseTag函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: networkParamsToStateParams
func networkParamsToStateParams(networks []params.Network, ifaces []params.NetworkInterface) (
[]state.NetworkInfo, []state.NetworkInterfaceInfo, error,
) {
stateNetworks := make([]state.NetworkInfo, len(networks))
for i, network := range networks {
_, networkName, err := names.ParseTag(network.Tag, names.NetworkTagKind)
if err != nil {
return nil, nil, err
}
stateNetworks[i] = state.NetworkInfo{
Name: networkName,
ProviderId: network.ProviderId,
CIDR: network.CIDR,
VLANTag: network.VLANTag,
}
}
stateInterfaces := make([]state.NetworkInterfaceInfo, len(ifaces))
for i, iface := range ifaces {
_, networkName, err := names.ParseTag(iface.NetworkTag, names.NetworkTagKind)
if err != nil {
return nil, nil, err
}
stateInterfaces[i] = state.NetworkInterfaceInfo{
MACAddress: iface.MACAddress,
NetworkName: networkName,
InterfaceName: iface.InterfaceName,
IsVirtual: iface.IsVirtual,
}
}
return stateNetworks, stateInterfaces, nil
}
示例2: authenticate
// authenticate parses HTTP basic authentication and authorizes the
// request by looking up the provided tag and password against state.
func (h *httpHandler) authenticate(r *http.Request) error {
parts := strings.Fields(r.Header.Get("Authorization"))
if len(parts) != 2 || parts[0] != "Basic" {
// Invalid header format or no header provided.
return fmt.Errorf("invalid request format")
}
// Challenge is a base64-encoded "tag:pass" string.
// See RFC 2617, Section 2.
challenge, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return fmt.Errorf("invalid request format")
}
tagPass := strings.SplitN(string(challenge), ":", 2)
if len(tagPass) != 2 {
return fmt.Errorf("invalid request format")
}
// Only allow users, not agents.
_, _, err = names.ParseTag(tagPass[0], names.UserTagKind)
if err != nil {
return common.ErrBadCreds
}
// Ensure the credentials are correct.
_, err = checkCreds(h.state, params.Creds{
AuthTag: tagPass[0],
Password: tagPass[1],
})
return err
}
示例3: Name
// Name returns the service name.
func (s *Service) Name() string {
_, serviceName, err := names.ParseTag(s.tag, names.ServiceTagKind)
if err != nil {
panic(fmt.Sprintf("%q is not a valid service tag", s.tag))
}
return serviceName
}
示例4: getService
func (u *UniterAPI) getService(tag string) (*state.Service, error) {
_, name, err := names.ParseTag(tag, names.ServiceTagKind)
if err != nil {
return nil, err
}
return u.st.Service(name)
}
示例5: Name
// Name returns the name of the unit.
func (u *Unit) Name() string {
_, unitName, err := names.ParseTag(u.tag, names.UnitTagKind)
if err != nil {
panic(fmt.Sprintf("%q is not a valid unit tag", u.tag))
}
return unitName
}
示例6: Id
// Id returns the machine id.
func (m *Machine) Id() string {
_, machineId, err := names.ParseTag(m.tag, names.MachineTagKind)
if err != nil {
panic(fmt.Sprintf("%q is not a valid machine tag", m.tag))
}
return machineId
}
示例7: watchOneMachineContainers
func (p *ProvisionerAPI) watchOneMachineContainers(arg params.WatchContainer) (params.StringsWatchResult, error) {
nothing := params.StringsWatchResult{}
canAccess, err := p.getAuthFunc()
if err != nil {
return nothing, err
}
if !canAccess(arg.MachineTag) {
return nothing, common.ErrPerm
}
_, id, err := names.ParseTag(arg.MachineTag, names.MachineTagKind)
if err != nil {
return nothing, err
}
machine, err := p.st.Machine(id)
if err != nil {
return nothing, err
}
var watch state.StringsWatcher
if arg.ContainerType != "" {
watch = machine.WatchContainers(instance.ContainerType(arg.ContainerType))
} else {
watch = machine.WatchAllContainers()
}
// Consume the initial event and forward it to the result.
if changes, ok := <-watch.Changes(); ok {
return params.StringsWatchResult{
StringsWatcherId: p.resources.Register(watch),
Changes: changes,
}, nil
}
return nothing, watcher.MustErr(watch)
}
示例8: String
// String returns the relation as a string.
func (r *Relation) String() string {
_, relId, err := names.ParseTag(r.tag, names.RelationTagKind)
if err != nil {
panic(fmt.Sprintf("%q is not a valid relation tag", r.tag))
}
return relId
}
示例9: parseTag
// parseTag, given an entity tag, returns the collection name and id
// of the entity document.
func (st *State) parseTag(tag string) (coll string, id string, err error) {
kind, id, err := names.ParseTag(tag, "")
if err != nil {
return "", "", err
}
switch kind {
case names.MachineTagKind:
coll = st.machines.Name
case names.ServiceTagKind:
coll = st.services.Name
case names.UnitTagKind:
coll = st.units.Name
case names.UserTagKind:
coll = st.users.Name
case names.RelationTagKind:
coll = st.relations.Name
case names.EnvironTagKind:
coll = st.environments.Name
case names.NetworkTagKind:
coll = st.networks.Name
default:
return "", "", fmt.Errorf("%q is not a valid collection tag", tag)
}
return coll, id, nil
}
示例10: getUnit
func (u *UniterAPI) getUnit(tag string) (*state.Unit, error) {
_, name, err := names.ParseTag(tag, names.UnitTagKind)
if err != nil {
return nil, err
}
return u.st.Unit(name)
}
示例11: NewProvisionerAPI
// NewProvisionerAPI creates a new server-side ProvisionerAPI facade.
func NewProvisionerAPI(
st *state.State,
resources *common.Resources,
authorizer common.Authorizer,
) (*ProvisionerAPI, error) {
if !authorizer.AuthMachineAgent() && !authorizer.AuthEnvironManager() {
return nil, common.ErrPerm
}
getAuthFunc := func() (common.AuthFunc, error) {
isEnvironManager := authorizer.AuthEnvironManager()
isMachineAgent := authorizer.AuthMachineAgent()
authEntityTag := authorizer.GetAuthTag()
return func(tag string) bool {
if isMachineAgent && tag == authEntityTag {
// A machine agent can always access its own machine.
return true
}
_, id, err := names.ParseTag(tag, names.MachineTagKind)
if err != nil {
return false
}
parentId := state.ParentId(id)
if parentId == "" {
// All top-level machines are accessible by the
// environment manager.
return isEnvironManager
}
// All containers with the authenticated machine as a
// parent are accessible by it.
return isMachineAgent && names.MachineTag(parentId) == authEntityTag
}, nil
}
// Both provisioner types can watch the environment.
getCanWatch := common.AuthAlways(true)
// Only the environment provisioner can read secrets.
getCanReadSecrets := common.AuthAlways(authorizer.AuthEnvironManager())
return &ProvisionerAPI{
Remover: common.NewRemover(st, false, getAuthFunc),
StatusSetter: common.NewStatusSetter(st, getAuthFunc),
DeadEnsurer: common.NewDeadEnsurer(st, getAuthFunc),
PasswordChanger: common.NewPasswordChanger(st, getAuthFunc),
LifeGetter: common.NewLifeGetter(st, getAuthFunc),
StateAddresser: common.NewStateAddresser(st),
APIAddresser: common.NewAPIAddresser(st, resources),
ToolsGetter: common.NewToolsGetter(st, getAuthFunc),
EnvironWatcher: common.NewEnvironWatcher(st, resources, getCanWatch, getCanReadSecrets),
EnvironMachinesWatcher: common.NewEnvironMachinesWatcher(st, resources, getCanReadSecrets),
InstanceIdGetter: common.NewInstanceIdGetter(st, getAuthFunc),
st: st,
resources: resources,
authorizer: authorizer,
getAuthFunc: getAuthFunc,
getCanWatchMachines: getCanReadSecrets,
}, nil
}
示例12: networkTagsToNames
func networkTagsToNames(tags []string) ([]string, error) {
netNames := make([]string, len(tags))
for i, tag := range tags {
_, name, err := names.ParseTag(tag, names.NetworkTagKind)
if err != nil {
return nil, err
}
netNames[i] = name
}
return netNames, nil
}
示例13: TestParseTag
func (*tagSuite) TestParseTag(c *gc.C) {
for i, test := range parseTagTests {
c.Logf("test %d: %q expectKind %q", i, test.tag, test.expectKind)
kind, id, err := names.ParseTag(test.tag, test.expectKind)
if test.resultErr != "" {
c.Assert(err, gc.ErrorMatches, test.resultErr)
c.Assert(kind, gc.Equals, "")
c.Assert(id, gc.Equals, "")
// If the tag has a valid kind which matches the
// expected kind, test that using an empty
// expectKind does not change the error message.
if tagKind, err := names.TagKind(test.tag); err == nil && tagKind == test.expectKind {
kind, id, err := names.ParseTag(test.tag, "")
c.Assert(err, gc.ErrorMatches, test.resultErr)
c.Assert(kind, gc.Equals, "")
c.Assert(id, gc.Equals, "")
}
} else {
c.Assert(err, gc.IsNil)
c.Assert(id, gc.Equals, test.resultId)
if test.expectKind != "" {
c.Assert(kind, gc.Equals, test.expectKind)
} else {
expectKind, err := names.TagKind(test.tag)
c.Assert(err, gc.IsNil)
c.Assert(kind, gc.Equals, expectKind)
}
// Check that it's reversible.
if f := makeTag[kind]; f != nil {
reversed := f(id)
c.Assert(reversed, gc.Equals, test.tag)
}
// Check that it parses ok without an expectKind.
kind1, id1, err1 := names.ParseTag(test.tag, "")
c.Assert(err1, gc.IsNil)
c.Assert(kind1, gc.Equals, test.expectKind)
c.Assert(id1, gc.Equals, id)
}
}
}
示例14: reconcileInstances
// reconcileInstances compares the initially started watcher for machines,
// units and services with the opened and closed ports of the instances and
// opens and closes the appropriate ports for each instance.
func (fw *Firewaller) reconcileInstances() error {
for _, machined := range fw.machineds {
m, err := machined.machine()
if params.IsCodeNotFound(err) {
if err := fw.forgetMachine(machined); err != nil {
return err
}
continue
} else if err != nil {
return err
}
instanceId, err := m.InstanceId()
if err != nil {
return err
}
instances, err := fw.environ.Instances([]instance.Id{instanceId})
if err == environs.ErrNoInstances {
return nil
} else if err != nil {
return err
}
_, machineId, err := names.ParseTag(machined.tag, names.MachineTagKind)
if err != nil {
return err
}
initialPorts, err := instances[0].Ports(machineId)
if err != nil {
return err
}
// Check which ports to open or to close.
toOpen := Diff(machined.ports, initialPorts)
toClose := Diff(initialPorts, machined.ports)
if len(toOpen) > 0 {
logger.Infof("opening instance ports %v for %q",
toOpen, machined.tag)
if err := instances[0].OpenPorts(machineId, toOpen); err != nil {
// TODO(mue) Add local retry logic.
return err
}
instance.SortPorts(toOpen)
}
if len(toClose) > 0 {
logger.Infof("closing instance ports %v for %q",
toClose, machined.tag)
if err := instances[0].ClosePorts(machineId, toClose); err != nil {
// TODO(mue) Add local retry logic.
return err
}
instance.SortPorts(toClose)
}
}
return nil
}
示例15: cacheAPIInfo
// cacheAPIInfo updates the local environment settings (.jenv file)
// with the provided apiInfo, assuming we've just successfully
// connected to the API server.
func cacheAPIInfo(info configstore.EnvironInfo, apiInfo *api.Info) error {
info.SetAPIEndpoint(configstore.APIEndpoint{
Addresses: apiInfo.Addrs,
CACert: string(apiInfo.CACert),
})
_, username, err := names.ParseTag(apiInfo.Tag, names.UserTagKind)
if err != nil {
return fmt.Errorf("invalid API user tag: %v", err)
}
info.SetAPICredentials(configstore.APICredentials{
User: username,
Password: apiInfo.Password,
})
return info.Write()
}