func (t *toolsURLGetter) ToolsURL(v version.Binary) (string, error) { apiHostPorts, err := t.apiHostPortsGetter.APIHostPorts() if err != nil { return "", err } if len(apiHostPorts) == 0 { return "", errors.New("no API host ports") } // TODO(axw) return all known URLs, so clients can try each one. // // The clients currently accept a single URL; we should change // the clients to disregard the URL, and have them download // straight from the API server. // // For now we choose a API server at random, and then select its // cloud-local address. The only user that will care about the URL // is the upgrader, and that is cloud-local. hostPorts := apiHostPorts[rand.Int()%len(apiHostPorts)] apiAddress := network.SelectInternalHostPort(hostPorts, false) if apiAddress == "" { return "", errors.Errorf("no suitable API server address to pick from %v", hostPorts) } serverRoot := fmt.Sprintf("https://%s/model/%s", apiAddress, t.modelUUID) return ToolsURL(serverRoot, v), nil }
func (*HostPortSuite) TestSelectInternalMachineHostPort(c *gc.C) { for i, t0 := range selectInternalMachineTests { t := t0.hostPortTest() c.Logf("test %d: %s", i, t.about) c.Check(network.SelectInternalHostPort(t.hostPorts, true), gc.DeepEquals, t.expected()) } }
func (s *PortSuite) TestSelectInternalHostPort(c *gc.C) { for i, t0 := range selectInternalTests { t := t0.hostPortTest() c.Logf("test %d: %s", i, t.about) c.Assert(network.SelectInternalHostPort(t.hostPorts, false), jc.DeepEquals, t.expected()) } }
func (s *PortSuite) TestSelectInternalHostPort(c *gc.C) { for i, t0 := range selectInternalTests { t := t0.hostPortTest() c.Logf("test %d: %s", i, t.about) network.PreferIPv6 = t.preferIPv6 c.Check(network.SelectInternalHostPort(t.hostPorts, false), jc.DeepEquals, t.expected()) } network.PreferIPv6 = false }
func (s *PortSuite) TestSelectInternalMachineHostPort(c *gc.C) { oldValue := network.GetPreferIPv6() defer func() { network.SetPreferIPv6(oldValue) }() for i, t0 := range selectInternalMachineTests { t := t0.hostPortTest() c.Logf("test %d: %s", i, t.about) network.SetPreferIPv6(t.preferIPv6) c.Check(network.SelectInternalHostPort(t.hostPorts, true), gc.DeepEquals, t.expected()) } }
func (c *configInternal) SetAPIHostPorts(servers [][]network.HostPort) { if c.apiDetails == nil { return } var addrs []string for _, serverHostPorts := range servers { addr := network.SelectInternalHostPort(serverHostPorts, false) if addr != "" { addrs = append(addrs, addr) } } c.apiDetails.addresses = addrs }
// APIAddresses returns the list of addresses used to connect to the API. func (api *APIAddresser) APIAddresses() (params.StringsResult, error) { apiHostPorts, err := api.getter.APIHostPorts() if err != nil { return params.StringsResult{}, err } var addrs = make([]string, 0, len(apiHostPorts)) for _, hostPorts := range apiHostPorts { addr := network.SelectInternalHostPort(hostPorts, false) if addr != "" { addrs = append(addrs, addr) } } return params.StringsResult{ Result: addrs, }, nil }
// CharmArchiveURLs returns the URLS for the charm archive // (bundle) data for each charm url in the given parameters. func (u *UniterAPIV3) CharmArchiveURLs(args params.CharmURLs) (params.StringsResults, error) { apiHostPorts, err := u.st.APIHostPorts() if err != nil { return params.StringsResults{}, err } modelUUID := u.st.ModelUUID() result := params.StringsResults{ Results: make([]params.StringsResult, len(args.URLs)), } for i, curl := range args.URLs { if _, err := charm.ParseURL(curl.URL); err != nil { result.Results[i].Error = common.ServerError(common.ErrPerm) continue } urlPath := "/" if modelUUID != "" { urlPath = path.Join(urlPath, "model", modelUUID) } urlPath = path.Join(urlPath, "charms") archiveURLs := make([]string, len(apiHostPorts)) for j, server := range apiHostPorts { archiveURL := &url.URL{ Scheme: "https", Host: network.SelectInternalHostPort(server, false), Path: urlPath, } q := archiveURL.Query() q.Set("url", curl.URL) q.Set("file", "*") archiveURL.RawQuery = q.Encode() archiveURLs[j] = archiveURL.String() } result.Results[i].Result = archiveURLs } return result, nil }
// SelectPeerHostPort returns the HostPort to use as the // mongo replica set peer by selecting it from the given hostPorts. func SelectPeerHostPort(hostPorts []network.HostPort) string { return network.SelectInternalHostPort(hostPorts, false) }