func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentCurrentEnvironmentSet(c *gc.C) { err := envcmd.WriteCurrentEnvironment("fubar") c.Assert(err, gc.IsNil) env, err := envcmd.GetDefaultEnvironment() c.Assert(env, gc.Equals, "fubar") c.Assert(err, gc.IsNil) }
func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentBothSet(c *gc.C) { os.Setenv(osenv.JujuEnvEnvKey, "magic") err := envcmd.WriteCurrentEnvironment("fubar") c.Assert(err, gc.IsNil) env, err := envcmd.GetDefaultEnvironment() c.Assert(env, gc.Equals, "magic") c.Assert(err, gc.IsNil) }
func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentNothingSet(c *gc.C) { envPath := gitjujutesting.HomePath(".juju", "environments.yaml") err := os.Remove(envPath) c.Assert(err, gc.IsNil) env, err := envcmd.GetDefaultEnvironment() c.Assert(env, gc.Equals, "") c.Assert(err, jc.Satisfies, environs.IsNoEnv) }
// switchEnvironment changes the default environment to the given name and // return, if set, the current default environment name. func switchEnvironment(envName string) (string, error) { if defaultEnv := os.Getenv(osenv.JujuEnvEnvKey); defaultEnv != "" { return "", errors.Errorf("cannot switch when %s is overriding the environment (set to %q)", osenv.JujuEnvEnvKey, defaultEnv) } currentEnv, err := envcmd.GetDefaultEnvironment() if err != nil { return "", errors.Annotate(err, "cannot get the default environment") } if err := envcmd.WriteCurrentEnvironment(envName); err != nil { return "", errors.Trace(err) } return currentEnv, nil }
func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentJujuEnvSet(c *gc.C) { os.Setenv(osenv.JujuEnvEnvKey, "magic") env, err := envcmd.GetDefaultEnvironment() c.Assert(env, gc.Equals, "magic") c.Assert(err, jc.ErrorIsNil) }
func (s *EnvironmentCommandSuite) TestGetDefaultEnvironment(c *gc.C) { env, err := envcmd.GetDefaultEnvironment() c.Assert(env, gc.Equals, "erewhemos") c.Assert(err, jc.ErrorIsNil) }
// TODO(axw) define an error for "no answer" func (s *jujuNameServer) answer(q dns.Question) (dns.RR, error) { if q.Qtype != dns.TypeA { return nil, nil } var envName string entityName := strings.ToLower(strings.TrimSuffix(q.Name, "."+zone)) if i := strings.IndexRune(entityName, '.'); i >= 0 { envName = entityName[i+1:] entityName = entityName[:i] } else { var err error envName, err = envcmd.GetDefaultEnvironment() if err != nil { return nil, err } } // TODO(axw) cache API connection api, err := s.openAPI(envName) if err != nil { return nil, err } defer api.Close() client := api.Client() // If the entity name parses as a tag, extract the ID. This enables // us to address "unit-mysql-0", where we couldn't otherwise, since // slashes are not allowed in domain names. Similarly for container // machines (e.g. to address "0/lxc/0", pass "machine-0-lxc-0"). if tag, err := names.ParseTag(entityName); err == nil { entityName = tag.Id() } var addr string if names.IsValidService(entityName) { status, err := client.Status([]string{entityName}) if err != nil { return nil, err } service := status.Services[entityName] addresses := make([]string, 0, len(service.Units)) for _, unit := range service.Units { if unit.PublicAddress == "" { continue } if includeLost || unit.UnitAgent.Status != "lost" { addresses = append(addresses, unit.PublicAddress) } } // Might be nice to have additional info in TXT? if len(addresses) == 0 { return nil, nil } addr = addresses[rand.Intn(len(addresses))] } else { // Assume it's a machine or unit name. addr, err = client.PublicAddress(entityName) if err != nil { return nil, err } } ip := net.ParseIP(addr) if ip != nil { rr := new(dns.A) rr.Hdr = dns.RR_Header{ Name: q.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0, } rr.A = ip return rr, nil } else { rr := new(dns.CNAME) rr.Hdr = dns.RR_Header{ Name: q.Name, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: 0, } rr.Target = addr + "." return rr, nil } }