// restAddPort parses payload, adds the port to the service, then updates the service
328397
func restAddPort(w *rest.ResponseWriter, r *rest.Request, ctx *requestContext) {
	glog.V(1).Infof("Add PORT with %s %#v", r.URL.Path, r)

	serviceid, application, port, err := getPortContext(r)
	if err != nil {
		err := fmt.Errorf("Error removing port from service (%s): %v", serviceid, err)
		glog.Error(err)
		restServerError(w, err)
		return
	}

	var request endpointRequest
	err = r.DecodeJsonPayload(&request)
	if err != nil {
		restBadRequest(w, err)
		return
	}

	glog.V(0).Infof("Add PORT with %s %#v", r.URL.Path, r)

	facade := ctx.getFacade()
	dataCtx := ctx.getDatastoreContext()

	_, err = facade.AddPublicEndpointPort(dataCtx, serviceid, application,
		port, request.UseTLS, request.Protocol, true, true)
	if err != nil {
		glog.Errorf("Error adding port to service (%s): %v", request.ServiceName, err)
		restServerError(w, err)
		return
	}

	glog.V(2).Infof("Port (%s) added to service (%s), UseTLS=%s, protocol=%s",
		port, request.ServiceName, request.UseTLS, request.Protocol)

	restSuccess(w)
}
386557
func (s *Service) EnablePort(application string, portAddr string, enable bool) error {
	appFound := false
	portFound := false
	for _, ep := range s.GetServicePorts() {
		if ep.Application == application {
			appFound = true
			for i, port := range ep.PortList {
				if port.PortAddr == portAddr {
					portFound = true
					ep.PortList[i].Enabled = enable
					plog.WithFields(log.Fields{
						"portaddr":    portAddr,
						"serviceid":   s.ID,
						"application": application,
						"enable":      enable,
					}).Debug("Enable port")
				}
			}
		}
	}
	if !appFound {
		return fmt.Errorf("port %s not found; application %s not found in service %s:%s", portAddr, application, s.ID, s.Name)
	}
	if !portFound {
		return fmt.Errorf("port %s not found in service %s:%s", portAddr, s.ID, s.Name)
	}

	return nil
}
437676
func (s *ProxyLBSetting) DeleteBindPort(mode string, port int) {
	var res []*ProxyLBBindPorts
	for i := range s.BindPorts {
		if s.BindPorts[i].ProxyMode != mode || s.BindPorts[i].Port != port {
			res = append(res, s.BindPorts[i])
		}
	}
	s.BindPorts = res
}
165155
func buildPortsToEndpointsMap(endpoints *v1.Endpoints) map[string][]hostPortPair {
	portsToEndpoints := map[string][]hostPortPair{}
	for i := range endpoints.Subsets {
		ss := &endpoints.Subsets[i]
		for i := range ss.Ports {
			port := &ss.Ports[i]
			for i := range ss.Addresses {
				addr := &ss.Addresses[i]
				portsToEndpoints[port.Name] = append(portsToEndpoints[port.Name], hostPortPair{addr.IP, int(port.Port)})
				// Ignore the protocol field - we'll get that from the Service objects.
			}
		}
	}
	return portsToEndpoints
}
698693
func restAddPool(w *rest.ResponseWriter, r *rest.Request, ctx *requestContext) {
	var payload pool.ResourcePool
	err := r.DecodeJsonPayload(&payload)
	if err != nil {
		glog.V(1).Info("Could not decode pool payload: ", err)
		restBadRequest(w, err)
		return
	}

	facade := ctx.getFacade()
	err = facade.AddResourcePool(ctx.getDatastoreContext(), &payload)
	if err != nil {
		glog.Error("Unable to add pool: ", err)
		restServerError(w, err)
		return
	}

	glog.V(0).Info("Added pool ", payload.ID)
	w.WriteJson(&simpleResponse{"Added resource pool", poolLinks(payload.ID)})
}
914013
func (c *Client) EnablePublicEndpointPort(serviceid, endpointName, portAddr string, isEnabled bool) error {
	request := &PublicEndpointRequest{
		Serviceid:    serviceid,
		EndpointName: endpointName,
		Name:         portAddr,
		IsEnabled:    isEnabled,
	}
	return c.call("EnablePublicEndpointPort", request, nil)
}
919895
func (a *api) RemovePublicEndpointPort(serviceid, endpointName, portAddr string) error {
	client, err := a.connectMaster()
	if err != nil {
		return err
	}

	return client.RemovePublicEndpointPort(serviceid, endpointName, portAddr)
}
1142018
func portForServicePort(svc *service.Service, portStr string, pType string) *service.Port {
	// Look up the ServicePort and translate to Docker port
	svcPort, err := strconv.ParseInt(portStr, 10, 64)
	if err != nil {
		log.Warnf(
			"SidecarListener label found on %s, can't decode port '%s'",
			svc.ID, portStr,
		)
		return nil
	}

	for _, port := range svc.Ports {
		if port.ServicePort == svcPort && port.Type == pType {
			return &port
		}
	}

	return nil
}
821157
func (r *Request) AddSort(keyName string) *Request {
	if r.Sort == nil {
		r.Sort = []string{}
	}
	r.Sort = append(r.Sort, keyName)
	return r
}
1017865
func (c *Client) RemovePublicEndpointPort(serviceid, endpointName, portAddr string) error {
	request := &PublicEndpointRequest{
		Serviceid:    serviceid,
		EndpointName: endpointName,
		Name:         portAddr,
	}
	return c.call("RemovePublicEndpointPort", request, nil)
}
58157
func (a *api) EnablePublicEndpointPort(serviceid, endpointName, portAddr string, isEnabled bool) error {
	client, err := a.connectMaster()
	if err != nil {
		return err
	}

	return client.EnablePublicEndpointPort(serviceid, endpointName, portAddr, isEnabled)
}
429211
func SrcPort(port uint16) TransportChecker {
	return func(t *testing.T, h header.Transport) {
		t.Helper()

		if p := h.SourcePort(); p != port {
			t.Errorf("Bad source port, got %v, want %v", p, port)
		}
	}
}
989279
func (res *Resolver) RestPorts(req *restful.Request, resp *restful.Response) {
	err := resp.WriteErrorString(http.StatusNotImplemented, "To be implemented...")
	if err != nil {
		logging.Error.Println(err)
	}
}
1004762
func restAddPoolVirtualIP(w *rest.ResponseWriter, r *rest.Request, ctx *requestContext) {
	//TODO replace virtualiprequest with model object
	var request pool.VirtualIP
	err := r.DecodeJsonPayload(&request)
	if err != nil {
		restBadRequest(w, err)
		return
	}

	glog.V(0).Infof("Add virtual ip: %+v", request)

	client, err := ctx.getMasterClient()
	if err != nil {
		restServerError(w, err)
		return
	}

	if err := client.AddVirtualIP(request); err != nil {
		glog.Errorf("Failed to add virtual IP(%+v): %v", request, err)
		restServerError(w, err)
		return
	}

	restSuccess(w)
}
959132
func (l VirtualDeviceList) ConnectSerialPort(device *types.VirtualSerialPort, uri string, client bool, proxyuri string) *types.VirtualSerialPort {
	if strings.HasPrefix(uri, "[") {
		device.Backing = &types.VirtualSerialPortFileBackingInfo{
			VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
				FileName: uri,
			},
		}

		return device
	}

	direction := types.VirtualDeviceURIBackingOptionDirectionServer
	if client {
		direction = types.VirtualDeviceURIBackingOptionDirectionClient
	}

	device.Backing = &types.VirtualSerialPortURIBackingInfo{
		VirtualDeviceURIBackingInfo: types.VirtualDeviceURIBackingInfo{
			Direction:  string(direction),
			ServiceURI: uri,
			ProxyURI:   proxyuri,
		},
	}

	return device
}
104441
func restAddAddressable(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()
	var a models.Addressable
	err := json.NewDecoder(r.Body).Decode(&a)
	if err != nil {
		LoggingClient.Error(err.Error())
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	id, err := addAddressable(a)
	if err != nil {
		switch err.(type) {
		case *types.ErrDuplicateAddressableName:
			http.Error(w, err.Error(), http.StatusConflict)
		case *types.ErrEmptyAddressableName:
			http.Error(w, err.Error(), http.StatusBadRequest)
		default:
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
		return
	}
	w.WriteHeader(http.StatusOK)
	_, err = w.Write([]byte(id))
	if err != nil {
		LoggingClient.Error(err.Error())
		return
	}
}
252982
func (l VirtualDeviceList) DisconnectSerialPort(device *types.VirtualSerialPort) *types.VirtualSerialPort {
	l.setDefaultSerialPortBacking(device)
	return device
}