From f351123149ff80373a532beeafa799ff06319000 Mon Sep 17 00:00:00 2001 From: heartlock <21521209@zju.edu.cn> Date: Thu, 22 Sep 2016 11:23:34 +0800 Subject: [PATCH 1/2] fix some compile error Signed-off-by: heartlock <21521209@zju.edu.cn> --- pkg/common/client.go | 77 +++++----------- pkg/common/opendaylight.go | 150 ++++++++++++------------------- pkg/harmonystack/harmonystack.go | 75 ---------------- pkg/types/types.proto | 1 - 4 files changed, 79 insertions(+), 224 deletions(-) diff --git a/pkg/common/client.go b/pkg/common/client.go index 80f6a66..f7caece 100644 --- a/pkg/common/client.go +++ b/pkg/common/client.go @@ -22,7 +22,6 @@ import ( "io" "io/ioutil" "net/http" - "strings" "github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers" @@ -30,7 +29,6 @@ import ( "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" "github.com/gophercloud/gophercloud/openstack/networking/v2/ports" "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets" - "github.com/gophercloud/gophercloud/pagination" ) type OpenDaylightClient struct { @@ -40,8 +38,8 @@ type OpenDaylightClient struct { HTTPClient http.Client } -func NewOpenDaylightClient(config *Config) (OpenDaylightClient, error) { - return OpenDaylightClient{ +func NewOpenDaylightClient(config *Config) (*OpenDaylightClient, error) { + return &OpenDaylightClient{ BaseUrl: config.Global.Url, HTTPClient: *http.DefaultClient, }, nil @@ -155,38 +153,6 @@ func (client *OpenDaylightClient) Request(method, url string, options *RequestOp if error400er, ok := errType.(Err400er); ok { err = error400er.Error400(respErr) } - case http.StatusUnauthorized: - if client.ReauthFunc != nil { - err = client.ReauthFunc() - if err != nil { - e := &ErrUnableToReauthenticate{} - e.ErrOriginal = respErr - return nil, e - } - if options.RawBody != nil { - if seeker, ok := options.RawBody.(io.Seeker); ok { - seeker.Seek(0, 0) - } - } - resp, err = client.Request(method, url, options) - if err != nil { - switch err.(type) { - case *ErrUnexpectedResponseCode: - e := &ErrErrorAfterReauthentication{} - e.ErrOriginal = err.(*ErrUnexpectedResponseCode) - return nil, e - default: - e := &ErrErrorAfterReauthentication{} - e.ErrOriginal = err - return nil, e - } - } - return resp, nil - } - err = ErrDefault401{respErr} - if error401er, ok := errType.(Err401er); ok { - err = error401er.Error401(respErr) - } case http.StatusNotFound: err = ErrDefault404{respErr} if error404er, ok := errType.(Err404er); ok { @@ -366,22 +332,21 @@ func (client *OpenDaylightClient) CreateNetwork(opts networks.CreateOpts) (r net func (client *OpenDaylightClient) DeleteNetwork(networkId string) (r networks.DeleteResult) { url := client.BaseUrl + "/v2.0/network/" + networkId - _, r.Err = client.Delete(deleteURL(c, networkID), nil) + _, r.Err = client.Delete(url, nil) return } -func (client *OpenDaylightClient) ListNetwork(opts *networks.ListOpts) pagination.Pager { +func (client *OpenDaylightClient) ListNetwork(opts *networks.ListOpts) ([]networks.Network, error) { url := client.BaseUrl + "/v2.0/networks/" if opts != nil { query, err := opts.ToNetworkListQuery() if err != nil { - return pagination.Pager{Err: err} + return nil, err } url += query } - return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { - return NetworkPage{pagination.LinkedPageBase{PageResult: r}} - }) + // TODO: get list of networks + return nil, nil } //TODO: update network @@ -432,18 +397,17 @@ func (client *OpenDaylightClient) DeletePort(portId string) (r ports.DeleteResul return } -func (client *OpenDaylightClient) ListPort(opts ports.ListOpts) pagination.Pager { +func (client *OpenDaylightClient) ListPort(opts ports.ListOptsBuilder) ([]ports.Port, error) { url := client.BaseUrl + "/v2.0/ports/" if opts != nil { query, err := opts.ToPortListQuery() if err != nil { - return pagination.Pager{Err: err} + return nil, err } url += query + //TODO: get list of ports } - return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Pager { - return PortPage{pagination.LinkedPageBase{PageResult: r}} - }) + return nil, nil } func (client *OpenDaylightClient) UpdatePort(portId string, opts portsbinding.UpdateOpts) (r portsbinding.UpdateResult) { @@ -453,7 +417,7 @@ func (client *OpenDaylightClient) UpdatePort(portId string, opts portsbinding.Up r.Err = err return r } - _, r.Err = client.Put(url, b, &r.Body, &gophercloud.RequestOpts{ + _, r.Err = client.Put(url, b, &r.Body, &RequestOpts{ OkCodes: []int{200, 201}, }) return @@ -467,7 +431,7 @@ func (client *OpenDaylightClient) CreateRouter(opts routers.CreateOpts) (r route r.Err = err return } - _, r.Err = client.Post(rootURL(c), b, &r.Body, nil) + _, r.Err = client.Post(url, b, &r.Body, nil) return } @@ -477,16 +441,15 @@ func (client *OpenDaylightClient) DeleteRouter(routerId string) (r routers.Delet return } -func (client *OpenDaylightClient) ListRouter(opts routers.ListOpts) pagination.Pager { +func (client *OpenDaylightClient) ListRouter(opts routers.ListOpts) ([]routers.Router, error) { url := client.BaseUrl + "/v2.0/routers/" q, err := gophercloud.BuildQueryString(&opts) if err != nil { - return pagination.Pager{Err: err} + return nil, err } - u := url + q.String() - return pagination.NewPager(client, u, func(r pagination.PageResult) pagination.Page { - return RouterPage{pagination.LinkedPageBase{PageResult: r}} - }) + url += q.String() + //TODO: get list of routers + return nil, nil } func (client *OpenDaylightClient) UpdateRouter() { @@ -501,7 +464,7 @@ func (client *OpenDaylightClient) AddInterface(routerId string, opts routers.Add r.Err = err return } - _, r.Err = client.Put(url, b, &r.Body, &gophercloud.RequestOpts{ + _, r.Err = client.Put(url, b, &r.Body, &RequestOpts{ OkCodes: []int{200}, }) return @@ -514,7 +477,7 @@ func (client *OpenDaylightClient) RemoveInterface(routerId string, opts routers. r.Err = err return } - _, r.Err = client.Put(url, b, &r.Body, &gophercloud.RequestOpts{ + _, r.Err = client.Put(url, b, &r.Body, &RequestOpts{ OkCodes: []int{200}, }) return diff --git a/pkg/common/opendaylight.go b/pkg/common/opendaylight.go index cfac0f7..18e0e33 100644 --- a/pkg/common/opendaylight.go +++ b/pkg/common/opendaylight.go @@ -28,6 +28,7 @@ import ( "code.google.com/p/gcfg" "github.com/docker/distribution/uuid" "github.com/golang/glog" + "github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers" "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/portsbinding" "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" @@ -98,6 +99,7 @@ type Config struct { Global struct { Url string `gcfg:"auth-url"` //TODO info for authorization + ExtNetID string } LoadBalancer LoadBalancerOpts Plugin PluginOpts @@ -121,7 +123,6 @@ func NewOpenDaylight(config io.Reader) (*OpenDaylight, error) { os := OpenDaylight{ odlClient: odl, - lbOpts: cfg.LoadBalancer, pluginOpts: cfg.Plugin, ExtNetID: cfg.Global.ExtNetID, } @@ -166,26 +167,24 @@ func (os *OpenDaylight) getOpenDaylightNetworkByName(name string) (*networks.Net // Get opendaylight network func (os *OpenDaylight) getOpenDaylightNetwork(opts *networks.ListOpts) (*networks.Network, error) { - var osNetwork *Network - pager := os.odlClient.ListNetwork(*opts) - err := pager.EachPage(func(page pagination.Page) (bool, error) { - networkList, e := networks.ExtractNetworks(page) - if len(networkList) > 1 { - return false, ErrMultipleResults - } - - if len(networkList) == 1 { - osNetwork = &networkList[0] - } + var osNetwork *networks.Network + networkList, err := os.odlClient.ListNetwork(opts) + if err != nil { + return nil, err + } + if len(networkList) > 1 { + return nil, ErrMultipleResults + } - return true, e - }) + if len(networkList) == 1 { + osNetwork = &networkList[0] + } - if err == nil && osNetwork == nil { + if networkList == nil { return nil, ErrNotFound } - return osNetwork, err + return osNetwork, nil } // Get provider subnet by id @@ -324,7 +323,7 @@ func (os *OpenDaylight) CreateNetwork(network *provider.Network) error { GatewayIP: &sub.Gateway, DNSNameservers: sub.Dnsservers, } - s, err := os.odlclient.CreateSubnet(subnetOpts).Extract() + s, err := os.odlClient.CreateSubnet(subnetOpts).Extract() if err != nil { glog.Errorf("Create opendaylight subnet %s failed: %v", sub.Name, err) delErr := os.DeleteNetwork(network.Name) @@ -362,21 +361,17 @@ func (os *OpenDaylight) getRouterByName(name string) (*routers.Router, error) { var result *routers.Router opts := routers.ListOpts{Name: name} - pager := os.odlClient.ListRouter(opts) - err := pager.EachPage(func(page pagination.Page) (bool, error) { - routerList, e := routers.ExtractRouters(page) - if len(routerList) > 1 { - return false, ErrMultipleResults - } else if len(routerList) == 1 { - result = &routerList[0] - } - - return true, e - }) + routerList, err := os.odlClient.ListRouter(opts) if err != nil { return nil, err } - + if len(routerList) > 1 { + return nil, ErrMultipleResults + } else if len(routerList) == 1 { + result = &routerList[0] + } else if routerList == nil { + return nil, ErrNotFound + } return result, nil } @@ -391,27 +386,22 @@ func (os *OpenDaylight) DeleteNetwork(networkName string) error { if osNetwork != nil { // Delete ports opts := ports.ListOpts{NetworkID: osNetwork.ID} - pager := os.odlClient.ListPort(opts) - err := pager.EachPage(func(page pagination.Page) (bool, error) { - portList, err := ports.ExtractPorts(page) - if err != nil { - glog.Errorf("Get opendaylight ports error: %v", err) - return false, err - } + portList, err := os.odlClient.ListPort(opts) + if err != nil { + return err + } - for _, port := range portList { - if port.DeviceOwner == "network:router_interface" { - continue - } + for _, port := range portList { + if port.DeviceOwner == "network:router_interface" { + continue + } - err = os.odlClient.DeletePort(port.ID).ExtractErr() - if err != nil { - glog.Warningf("Delete port %v failed: %v", port.ID, err) - } + err = os.odlClient.DeletePort(port.ID).ExtractErr() + if err != nil { + glog.Warningf("Delete port %v failed: %v", port.ID, err) } + } - return true, nil - }) if err != nil { glog.Errorf("Delete ports error: %v", err) } @@ -467,25 +457,16 @@ func (os *OpenDaylight) ListPorts(networkID, deviceOwner string) ([]ports.Port, NetworkID: networkID, DeviceOwner: deviceOwner, } - pager := os.odlClient.ListPort(os.network, opts) - err := pager.EachPage(func(page pagination.Page) (bool, error) { - portList, err := ports.ExtractPorts(page) - if err != nil { - glog.Errorf("Get opendaylight ports error: %v", err) - return false, err - } - - for _, port := range portList { - results = append(results, port) - } - - return true, err - }) + portList, err := os.odlClient.ListPort(opts) if err != nil { + glog.Errorf("Get opendaylight ports error: %v", err) return nil, err } + for _, port := range portList { + results = append(results, port) + } return results, nil } @@ -493,23 +474,16 @@ func (os *OpenDaylight) ListPorts(networkID, deviceOwner string) ([]ports.Port, // Create an port func (os *OpenDaylight) CreatePort(networkID, tenantID, portName, podHostname string) (*portsbinding.Port, error) { - securitygroup, err := os.ensureSecurityGroup(tenantID) - if err != nil { - glog.Errorf("EnsureSecurityGroup failed: %v", err) - return nil, err - } - opts := portsbinding.CreateOpts{ HostID: getHostName(), DNSName: podHostname, CreateOptsBuilder: ports.CreateOpts{ - NetworkID: networkID, - Name: portName, - AdminStateUp: &adminStateUp, - TenantID: tenantID, - DeviceID: uuid.Generate().String(), - DeviceOwner: fmt.Sprintf("compute:%s", getHostName()), - SecurityGroups: []string{securitygroup}, + NetworkID: networkID, + Name: portName, + AdminStateUp: &adminStateUp, + TenantID: tenantID, + DeviceID: uuid.Generate().String(), + DeviceOwner: fmt.Sprintf("compute:%s", getHostName()), }, } @@ -541,28 +515,22 @@ func (os *OpenDaylight) CreatePort(networkID, tenantID, portName, podHostname st func (os *OpenDaylight) GetPort(name string) (*ports.Port, error) { opts := ports.ListOpts{Name: name} - pager := os.odlClient.List(opts) - + portList, err := os.odlClient.ListPort(opts) + if err != nil { + glog.Errorf("Get opendaylight ports error: %v", err) + return nil, err + } var port *ports.Port - err := pager.EachPage(func(page pagination.Page) (bool, error) { - portList, err := ports.ExtractPorts(page) - if err != nil { - glog.Errorf("Get opendaylight ports error: %v", err) - return false, err - } - if len(portList) > 1 { - return false, ErrMultipleResults - } - - if len(portList) == 0 { - return false, ErrNotFound - } + if len(portList) > 1 { + return nil, ErrMultipleResults + } - port = &portList[0] + if len(portList) == 0 { + return nil, ErrNotFound + } - return true, err - }) + port = &portList[0] return port, err } diff --git a/pkg/harmonystack/harmonystack.go b/pkg/harmonystack/harmonystack.go index b740f5d..ef700b4 100644 --- a/pkg/harmonystack/harmonystack.go +++ b/pkg/harmonystack/harmonystack.go @@ -53,7 +53,6 @@ func (h *HarmonyHandler) Serve(addr string) error { } func (h *HarmonyHandler) registerServer() { - provider.RegisterLoadBalancersServer(h.server, h) provider.RegisterNetworksServer(h.server, h) provider.RegisterPodsServer(h.server, h) } @@ -68,21 +67,6 @@ func (h *HarmonyHandler) Active(c context.Context, req *provider.ActiveRequest) return &resp, nil } -func (h *HarmonyHandler) CheckTenantID(c context.Context, req *provider.CheckTenantIDRequest) (*provider.CheckTenantIDResponse, error) { - glog.V(4).Infof("CheckTenantID with request %v", req.TenantID) - - resp := provider.CheckTenantIDResponse{} - checkResult, err := h.driver.CheckTenantID(req.TenantID) - if err != nil { - resp.Error = err.Error() - } else { - resp.Result = checkResult - } - - glog.V(4).Infof("CheckTenantID result %v", resp) - return &resp, nil -} - func (h *HarmonyHandler) GetNetwork(c context.Context, req *provider.GetNetworkRequest) (*provider.GetNetworkResponse, error) { glog.V(4).Infof("GetNetwork with request %v", req.String()) @@ -109,7 +93,6 @@ func (h *HarmonyHandler) CreateNetwork(c context.Context, req *provider.CreateNe glog.V(4).Infof("CreateNetwork with request %v", req) resp := provider.CommonResponse{} - req.Network.TenantID = h.driver.ToTenantID(req.Network.TenantID) err := h.driver.CreateNetwork(req.Network) if err != nil { resp.Error = err.Error() @@ -123,7 +106,6 @@ func (h *HarmonyHandler) UpdateNetwork(c context.Context, req *provider.UpdateNe glog.V(4).Infof("UpdateNetwork with request %v", req.String()) resp := provider.CommonResponse{} - req.Network.TenantID = h.driver.ToTenantID(req.Network.TenantID) err := h.driver.UpdateNetwork(req.Network) if err != nil { resp.Error = err.Error() @@ -146,63 +128,6 @@ func (h *HarmonyHandler) DeleteNetwork(c context.Context, req *provider.DeleteNe return &resp, nil } -func (h *HarmonyHandler) GetLoadBalancer(c context.Context, req *provider.GetLoadBalancerRequest) (*provider.GetLoadBalancerResponse, error) { - resp := provider.GetLoadBalancerResponse{} - lb, err := h.driver.GetLoadBalancer(req.Name) - if err != nil { - resp.Error = err.Error() - } else { - resp.LoadBalancer = lb - } - - return &resp, nil -} - -func (h *HarmonyHandler) CreateLoadBalancer(c context.Context, req *provider.CreateLoadBalancerRequest) (*provider.CreateLoadBalancerResponse, error) { - glog.V(4).Infof("CreateLoadBalancer with request %v", req.String()) - - resp := provider.CreateLoadBalancerResponse{} - req.LoadBalancer.TenantID = h.driver.ToTenantID(req.LoadBalancer.TenantID) - vip, err := h.driver.CreateLoadBalancer(req.LoadBalancer, string(req.Affinity)) - if err != nil { - resp.Error = err.Error() - } else { - resp.Vip = vip - } - - glog.V(4).Infof("CreateLoadBalancer result %v", resp) - return &resp, nil -} - -func (h *HarmonyHandler) UpdateLoadBalancer(c context.Context, req *provider.UpdateLoadBalancerRequest) (*provider.UpdateLoadBalancerResponse, error) { - glog.V(4).Infof("UpdateLoadBalancer with request %v", req.String()) - - resp := provider.UpdateLoadBalancerResponse{} - vip, err := h.driver.UpdateLoadBalancer(req.Name, req.Hosts, req.ExternalIPs) - if err != nil { - resp.Error = err.Error() - } else { - resp.Vip = vip - } - - glog.V(4).Infof("UpdateLoadBalancer result %v", resp) - - return &resp, nil -} - -func (h *HarmonyHandler) DeleteLoadBalancer(c context.Context, req *provider.DeleteLoadBalancerRequest) (*provider.CommonResponse, error) { - glog.V(4).Infof("DeleteLoadBalancer with request %v", req.String()) - - resp := provider.CommonResponse{} - err := h.driver.DeleteLoadBalancer(req.Name) - if err != nil { - resp.Error = err.Error() - } - - glog.V(4).Infof("DeleteLoadBalancer result %v", resp) - return &resp, nil -} - func (h *HarmonyHandler) SetupPod(c context.Context, req *provider.SetupPodRequest) (*provider.CommonResponse, error) { glog.V(4).Infof("SetupPod with request %v", req.String()) diff --git a/pkg/types/types.proto b/pkg/types/types.proto index c89b881..6a4b04b 100644 --- a/pkg/types/types.proto +++ b/pkg/types/types.proto @@ -6,7 +6,6 @@ package types; service Networks { rpc Active(ActiveRequest) returns (ActivateResponse) {} - rpc CheckTenantID(CheckTenantIDRequest) returns (CheckTenantIDResponse) {} rpc GetNetwork(GetNetworkRequest) returns (GetNetworkResponse) {} rpc CreateNetwork(CreateNetworkRequest) returns (CommonResponse) {} rpc UpdateNetwork(UpdateNetworkRequest) returns (CommonResponse) {} From a9a2007cf58dadd5985a0b01d5e9bea9c81922f3 Mon Sep 17 00:00:00 2001 From: heartlock <21521209@zju.edu.cn> Date: Wed, 21 Sep 2016 22:11:24 -0700 Subject: [PATCH 2/2] compile passed Signed-off-by: heartlock <21521209@zju.edu.cn> --- pkg/types/types.pb.go | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/pkg/types/types.pb.go b/pkg/types/types.pb.go index c9bf12a..fd3961c 100644 --- a/pkg/types/types.pb.go +++ b/pkg/types/types.pb.go @@ -442,7 +442,6 @@ var _ grpc.ClientConn type NetworksClient interface { Active(ctx context.Context, in *ActiveRequest, opts ...grpc.CallOption) (*ActivateResponse, error) - CheckTenantID(ctx context.Context, in *CheckTenantIDRequest, opts ...grpc.CallOption) (*CheckTenantIDResponse, error) GetNetwork(ctx context.Context, in *GetNetworkRequest, opts ...grpc.CallOption) (*GetNetworkResponse, error) CreateNetwork(ctx context.Context, in *CreateNetworkRequest, opts ...grpc.CallOption) (*CommonResponse, error) UpdateNetwork(ctx context.Context, in *UpdateNetworkRequest, opts ...grpc.CallOption) (*CommonResponse, error) @@ -466,15 +465,6 @@ func (c *networksClient) Active(ctx context.Context, in *ActiveRequest, opts ... return out, nil } -func (c *networksClient) CheckTenantID(ctx context.Context, in *CheckTenantIDRequest, opts ...grpc.CallOption) (*CheckTenantIDResponse, error) { - out := new(CheckTenantIDResponse) - err := grpc.Invoke(ctx, "/types.Networks/CheckTenantID", in, out, c.cc, opts...) - if err != nil { - return nil, err - } - return out, nil -} - func (c *networksClient) GetNetwork(ctx context.Context, in *GetNetworkRequest, opts ...grpc.CallOption) (*GetNetworkResponse, error) { out := new(GetNetworkResponse) err := grpc.Invoke(ctx, "/types.Networks/GetNetwork", in, out, c.cc, opts...) @@ -515,7 +505,6 @@ func (c *networksClient) DeleteNetwork(ctx context.Context, in *DeleteNetworkReq type NetworksServer interface { Active(context.Context, *ActiveRequest) (*ActivateResponse, error) - CheckTenantID(context.Context, *CheckTenantIDRequest) (*CheckTenantIDResponse, error) GetNetwork(context.Context, *GetNetworkRequest) (*GetNetworkResponse, error) CreateNetwork(context.Context, *CreateNetworkRequest) (*CommonResponse, error) UpdateNetwork(context.Context, *UpdateNetworkRequest) (*CommonResponse, error) @@ -538,18 +527,6 @@ func _Networks_Active_Handler(srv interface{}, ctx context.Context, dec func(int return out, nil } -func _Networks_CheckTenantID_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error) (interface{}, error) { - in := new(CheckTenantIDRequest) - if err := dec(in); err != nil { - return nil, err - } - out, err := srv.(NetworksServer).CheckTenantID(ctx, in) - if err != nil { - return nil, err - } - return out, nil -} - func _Networks_GetNetwork_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error) (interface{}, error) { in := new(GetNetworkRequest) if err := dec(in); err != nil { @@ -606,10 +583,6 @@ var _Networks_serviceDesc = grpc.ServiceDesc{ MethodName: "Active", Handler: _Networks_Active_Handler, }, - { - MethodName: "CheckTenantID", - Handler: _Networks_CheckTenantID_Handler, - }, { MethodName: "GetNetwork", Handler: _Networks_GetNetwork_Handler,