2023-02-28 20:20:07 +03:00
|
|
|
package wireserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-container-networking/cns"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
joinNetworkURLFmt = `http://%s/machine/plugins/?comp=nmagent&type=NetworkManagement/joinedVirtualNetworks/%s/api-version/1`
|
|
|
|
publishNCURLFmt = `http://%s/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/%s/networkContainers/%s/authenticationToken/%s/api-version/1`
|
|
|
|
unpublishNCURLFmt = `http://%s/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/%s/networkContainers/%s/authenticationToken/%s/api-version/1/method/DELETE`
|
|
|
|
)
|
|
|
|
|
|
|
|
type Proxy struct {
|
|
|
|
Host string
|
|
|
|
HTTPClient do
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Proxy) JoinNetwork(ctx context.Context, vnetID string) (*http.Response, error) {
|
|
|
|
reqURL := fmt.Sprintf(joinNetworkURLFmt, p.Host, vnetID)
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewBufferString(`""`))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "wireserver proxy: join network: could not build http request")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := p.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "wireserver proxy: join network: could not perform http request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Proxy) PublishNC(ctx context.Context, ncParams cns.NetworkContainerParameters, payload []byte) (*http.Response, error) {
|
|
|
|
reqURL := fmt.Sprintf(publishNCURLFmt, p.Host, ncParams.AssociatedInterfaceID, ncParams.NCID, ncParams.AuthToken)
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewBuffer(payload))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "wireserver proxy: publish nc: could not build http request")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := p.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "wireserver proxy: publish nc: could not perform http request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2023-03-02 00:44:32 +03:00
|
|
|
func (p *Proxy) UnpublishNC(ctx context.Context, ncParams cns.NetworkContainerParameters, payload []byte) (*http.Response, error) {
|
2023-02-28 20:20:07 +03:00
|
|
|
reqURL := fmt.Sprintf(unpublishNCURLFmt, p.Host, ncParams.AssociatedInterfaceID, ncParams.NCID, ncParams.AuthToken)
|
|
|
|
|
2023-03-02 00:44:32 +03:00
|
|
|
// a POST to wireserver must contain a body. For legacy purposes,
|
|
|
|
// an empty json string (two quote characters) should be sent by default.
|
|
|
|
body := []byte(`""`)
|
|
|
|
if len(payload) > 0 {
|
|
|
|
body = payload
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewBuffer(body))
|
2023-02-28 20:20:07 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "wireserver proxy: unpublish nc: could not build http request")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := p.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "wireserver proxy: unpublish nc: could not perform http request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|