本文整理匯總了Golang中github.com/juju/names.NewEnvironTag函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewEnvironTag函數的具體用法?Golang NewEnvironTag怎麽用?Golang NewEnvironTag使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewEnvironTag函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestOpenHonorsEnvironTag
func (s *apiclientSuite) TestOpenHonorsEnvironTag(c *gc.C) {
info := s.APIInfo(c)
// TODO(jam): 2014-06-05 http://pad.lv/1326802
// we want to test this eventually, but for now s.APIInfo uses
// conn.StateInfo() which doesn't know about EnvironTag.
// c.Check(info.EnvironTag, gc.Equals, env.Tag())
// c.Assert(info.EnvironTag, gc.Not(gc.Equals), "")
// We start by ensuring we have an invalid tag, and Open should fail.
info.EnvironTag = names.NewEnvironTag("bad-tag")
_, err := api.Open(info, api.DialOpts{})
c.Check(err, gc.ErrorMatches, `unknown environment: "bad-tag"`)
c.Check(params.ErrCode(err), gc.Equals, params.CodeNotFound)
// Now set it to the right tag, and we should succeed.
info.EnvironTag = s.State.EnvironTag()
st, err := api.Open(info, api.DialOpts{})
c.Assert(err, jc.ErrorIsNil)
st.Close()
// Backwards compatibility, we should succeed if we do not set an
// environ tag
info.EnvironTag = names.NewEnvironTag("")
st, err = api.Open(info, api.DialOpts{})
c.Assert(err, jc.ErrorIsNil)
st.Close()
}
示例2: setup
func (s *PresenceSuite) setup(c *gc.C, key string) (*presence.Watcher, *presence.Pinger, <-chan presence.Change) {
uuid, err := utils.NewUUID()
c.Assert(err, jc.ErrorIsNil)
envUUID := uuid.String()
w := presence.NewWatcher(s.presence, names.NewEnvironTag(envUUID))
p := presence.NewPinger(s.presence, names.NewEnvironTag(envUUID), key)
ch := make(chan presence.Change)
w.Watch(key, ch)
assertChange(c, ch, presence.Change{key, false})
return w, p, ch
}
示例3: NewAPIAuthenticator
// NewAPIAuthenticator gets the state and api info once from the
// provisioner API.
func NewAPIAuthenticator(st *apiprovisioner.State) (AuthenticationProvider, error) {
stateAddresses, err := st.StateAddresses()
if err != nil {
return nil, errors.Trace(err)
}
apiAddresses, err := st.APIAddresses()
if err != nil {
return nil, errors.Trace(err)
}
caCert, err := st.CACert()
if err != nil {
return nil, errors.Trace(err)
}
envUUID, err := st.EnvironUUID()
if err != nil {
return nil, errors.Trace(err)
}
stateInfo := &mongo.MongoInfo{
Info: mongo.Info{
Addrs: stateAddresses,
CACert: caCert,
},
}
apiInfo := &api.Info{
Addrs: apiAddresses,
CACert: caCert,
EnvironTag: names.NewEnvironTag(envUUID),
}
return &simpleAuth{stateInfo, apiInfo}, nil
}
示例4: EnvironmentsForUser
// EnvironmentsForUser returns a list of enviroments that the user
// is able to access.
func (st *State) EnvironmentsForUser(user names.UserTag) ([]*UserEnvironment, error) {
// Since there are no groups at this stage, the simplest way to get all
// the environments that a particular user can see is to look through the
// environment user collection. A raw collection is required to support
// queries across multiple environments.
envUsers, userCloser := st.getRawCollection(envUsersC)
defer userCloser()
// TODO: consider adding an index to the envUsers collection on the username.
var userSlice []envUserDoc
err := envUsers.Find(bson.D{{"user", user.Username()}}).All(&userSlice)
if err != nil {
return nil, err
}
var result []*UserEnvironment
for _, doc := range userSlice {
envTag := names.NewEnvironTag(doc.EnvUUID)
env, err := st.GetEnvironment(envTag)
if err != nil {
return nil, errors.Trace(err)
}
result = append(result, &UserEnvironment{Environment: env, LastConnection: doc.LastConnection})
}
return result, nil
}
示例5: SetUpSuite
func (s *PresenceSuite) SetUpSuite(c *gc.C) {
s.BaseSuite.SetUpSuite(c)
s.MgoSuite.SetUpSuite(c)
uuid, err := utils.NewUUID()
c.Assert(err, jc.ErrorIsNil)
s.envTag = names.NewEnvironTag(uuid.String())
}
示例6: AddLeadershipSettingsDocs
// AddLeadsershipSettingsDocs creates service leadership documents in
// the settings collection for all services in all environments.
func AddLeadershipSettingsDocs(st *State) error {
environments, closer := st.getCollection(environmentsC)
defer closer()
var envDocs []bson.M
err := environments.Find(nil).Select(bson.M{"_id": 1}).All(&envDocs)
if err != nil {
return errors.Annotate(err, "failed to read environments")
}
for _, envDoc := range envDocs {
envUUID := envDoc["_id"].(string)
envSt, err := st.ForEnviron(names.NewEnvironTag(envUUID))
if err != nil {
return errors.Annotatef(err, "failed to open environment %q", envUUID)
}
defer envSt.Close()
services, err := envSt.AllServices()
if err != nil {
return errors.Annotatef(err, "failed to retrieve services for environment %q", envUUID)
}
for _, service := range services {
// The error from this is intentionally ignored as the
// transaction will fail if the service already has a
// leadership settings doc.
envSt.runTransaction([]txn.Op{
addLeadershipSettingsOp(service.Name()),
})
}
}
return nil
}
示例7: apiInfoConnect
// apiInfoConnect looks for endpoint on the given environment and
// tries to connect to it, sending the result on the returned channel.
func apiInfoConnect(info configstore.EnvironInfo, apiOpen api.OpenFunc, stop <-chan struct{}, bClient *httpbakery.Client) (api.Connection, error) {
endpoint := info.APIEndpoint()
if info == nil || len(endpoint.Addresses) == 0 {
return nil, &infoConnectError{fmt.Errorf("no cached addresses")}
}
logger.Infof("connecting to API addresses: %v", endpoint.Addresses)
var environTag names.EnvironTag
if names.IsValidEnvironment(endpoint.EnvironUUID) {
environTag = names.NewEnvironTag(endpoint.EnvironUUID)
}
apiInfo := &api.Info{
Addrs: endpoint.Addresses,
CACert: endpoint.CACert,
Tag: environInfoUserTag(info),
Password: info.APICredentials().Password,
EnvironTag: environTag,
}
if apiInfo.Tag == nil {
apiInfo.UseMacaroons = true
}
dialOpts := api.DefaultDialOpts()
dialOpts.BakeryClient = bClient
st, err := apiOpen(apiInfo, dialOpts)
if err != nil {
return nil, &infoConnectError{err}
}
return st, nil
}
示例8: APIInfo
// APIInfo returns an api.Info for the environment. The result is populated
// with addresses and CA certificate, but no tag or password.
func APIInfo(env Environ) (*api.Info, error) {
instanceIds, err := env.StateServerInstances()
if err != nil {
return nil, err
}
logger.Debugf("StateServerInstances returned: %v", instanceIds)
addrs, err := waitAnyInstanceAddresses(env, instanceIds)
if err != nil {
return nil, err
}
config := env.Config()
cert, hasCert := config.CACert()
if !hasCert {
return nil, errors.New("config has no CACert")
}
apiPort := config.APIPort()
apiAddrs := network.HostPortsToStrings(
network.AddressesWithPort(addrs, apiPort),
)
uuid, uuidSet := config.UUID()
if !uuidSet {
return nil, errors.New("config has no UUID")
}
envTag := names.NewEnvironTag(uuid)
apiInfo := &api.Info{Addrs: apiAddrs, CACert: cert, EnvironTag: envTag}
return apiInfo, nil
}
示例9: AddDefaultBlockDevicesDocs
// AddDefaultBlockDevicesDocs creates block devices documents
// for all existing machines in all environments.
func AddDefaultBlockDevicesDocs(st *State) error {
environments, closer := st.getCollection(environmentsC)
defer closer()
var envDocs []bson.M
err := environments.Find(nil).Select(bson.M{"_id": 1}).All(&envDocs)
if err != nil {
return errors.Annotate(err, "failed to read environments")
}
for _, envDoc := range envDocs {
envUUID := envDoc["_id"].(string)
envSt, err := st.ForEnviron(names.NewEnvironTag(envUUID))
if err != nil {
return errors.Annotatef(err, "failed to open environment %q", envUUID)
}
defer envSt.Close()
machines, err := envSt.AllMachines()
if err != nil {
return errors.Annotatef(err, "failed to retrieve machines for environment %q", envUUID)
}
for _, machine := range machines {
// If a txn fails because the doc already exists, that's ok.
if err := envSt.runTransaction([]txn.Op{
createMachineBlockDevicesOp(machine.Id()),
}); err != nil && err != txn.ErrAborted {
return err
}
}
}
return nil
}
示例10: apiInfoConnect
// apiInfoConnect looks for endpoint on the given environment and
// tries to connect to it, sending the result on the returned channel.
func apiInfoConnect(store configstore.Storage, info configstore.EnvironInfo, apiOpen apiOpenFunc, stop <-chan struct{}) (apiState, error) {
endpoint := info.APIEndpoint()
if info == nil || len(endpoint.Addresses) == 0 {
return nil, &infoConnectError{fmt.Errorf("no cached addresses")}
}
logger.Infof("connecting to API addresses: %v", endpoint.Addresses)
var environTag names.Tag
if endpoint.EnvironUUID != "" {
// Note: we should be validating that EnvironUUID contains a
// valid UUID.
environTag = names.NewEnvironTag(endpoint.EnvironUUID)
}
username := info.APICredentials().User
if username == "" {
username = "admin"
}
apiInfo := &api.Info{
Addrs: endpoint.Addresses,
CACert: endpoint.CACert,
Tag: names.NewUserTag(username),
Password: info.APICredentials().Password,
EnvironTag: environTag,
}
st, err := apiOpen(apiInfo, api.DefaultDialOpts())
if err != nil {
return nil, &infoConnectError{err}
}
return st, nil
}
示例11: apiInfoConnect
// apiInfoConnect looks for endpoint on the given environment and
// tries to connect to it, sending the result on the returned channel.
func apiInfoConnect(store configstore.Storage, info configstore.EnvironInfo, apiOpen apiOpenFunc, stop <-chan struct{}) (apiState, error) {
endpoint := info.APIEndpoint()
if info == nil || len(endpoint.Addresses) == 0 {
return nil, &infoConnectError{fmt.Errorf("no cached addresses")}
}
logger.Infof("connecting to API addresses: %v", endpoint.Addresses)
var environTag names.EnvironTag
if names.IsValidEnvironment(endpoint.EnvironUUID) {
environTag = names.NewEnvironTag(endpoint.EnvironUUID)
} else {
// For backwards-compatibility, we have to allow connections
// with an empty UUID. Login will work for the same reasons.
logger.Warningf("ignoring invalid API endpoint environment UUID %v", endpoint.EnvironUUID)
}
apiInfo := &api.Info{
Addrs: endpoint.Addresses,
CACert: endpoint.CACert,
Tag: environInfoUserTag(info),
Password: info.APICredentials().Password,
EnvironTag: environTag,
}
st, err := apiOpen(apiInfo, api.DefaultDialOpts())
if err != nil {
return nil, &infoConnectError{err}
}
return st, nil
}
示例12: validateEnvironUUID
// validateEnvironUUID is the common validator for the various
// apiserver components that need to check for a valid environment
// UUID. An empty envUUID means that the connection has come in at
// the root of the URL space and refers to the state server
// environment.
//
// It returns the validated environment UUID.
func validateEnvironUUID(args validateArgs) (string, error) {
ssState := args.statePool.SystemState()
if args.envUUID == "" {
// We allow the environUUID to be empty for 2 cases
// 1) Compatibility with older clients
// 2) TODO: server a limited API at the root (empty envUUID)
// with just the user manager and environment manager
// if the connection comes over a sufficiently up to date
// login command.
if args.strict {
return "", errors.Trace(common.UnknownEnvironmentError(args.envUUID))
}
logger.Debugf("validate env uuid: empty envUUID")
return ssState.EnvironUUID(), nil
}
if args.envUUID == ssState.EnvironUUID() {
logger.Debugf("validate env uuid: state server environment - %s", args.envUUID)
return args.envUUID, nil
}
if args.stateServerEnvOnly {
return "", errors.Unauthorizedf("requested environment %q is not the state server environment", args.envUUID)
}
if !names.IsValidEnvironment(args.envUUID) {
return "", errors.Trace(common.UnknownEnvironmentError(args.envUUID))
}
envTag := names.NewEnvironTag(args.envUUID)
if _, err := ssState.GetEnvironment(envTag); err != nil {
return "", errors.Wrap(err, common.UnknownEnvironmentError(args.envUUID))
}
logger.Debugf("validate env uuid: %s", args.envUUID)
return args.envUUID, nil
}
示例13: TestAllEnvironments
func (s *systemManagerSuite) TestAllEnvironments(c *gc.C) {
admin := s.Factory.MakeUser(c, &factory.UserParams{Name: "foobar"})
s.Factory.MakeEnvironment(c, &factory.EnvParams{
Name: "owned", Owner: admin.UserTag()}).Close()
remoteUserTag := names.NewUserTag("[email protected]")
st := s.Factory.MakeEnvironment(c, &factory.EnvParams{
Name: "user", Owner: remoteUserTag})
defer st.Close()
st.AddEnvironmentUser(admin.UserTag(), remoteUserTag, "Foo Bar")
s.Factory.MakeEnvironment(c, &factory.EnvParams{
Name: "no-access", Owner: remoteUserTag}).Close()
response, err := s.systemManager.AllEnvironments()
c.Assert(err, jc.ErrorIsNil)
// The results are sorted.
expected := []string{"dummyenv", "no-access", "owned", "user"}
var obtained []string
for _, env := range response.UserEnvironments {
obtained = append(obtained, env.Name)
stateEnv, err := s.State.GetEnvironment(names.NewEnvironTag(env.UUID))
c.Assert(err, jc.ErrorIsNil)
s.checkEnvironmentMatches(c, env.Environment, stateEnv)
}
c.Assert(obtained, jc.DeepEquals, expected)
}
示例14: TestConnectWebsocketToRoot
func (s *apiclientSuite) TestConnectWebsocketToRoot(c *gc.C) {
info := s.APIInfo(c)
info.EnvironTag = names.NewEnvironTag("")
conn, _, err := api.ConnectWebsocket(info, api.DialOpts{})
c.Assert(err, jc.ErrorIsNil)
defer conn.Close()
assertConnAddrForRoot(c, conn, info.Addrs[0])
}
示例15: TestResourceTagsUUID
func (*tagsSuite) TestResourceTagsUUID(c *gc.C) {
testResourceTags(c, testing.EnvironmentTag, nil, map[string]string{
"juju-env-uuid": testing.EnvironmentTag.Id(),
})
testResourceTags(c, names.NewEnvironTag(""), nil, map[string]string{
"juju-env-uuid": "",
})
}