Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Ayush Rangwala <[email protected]>
  • Loading branch information
aayushrangwala committed Jan 5, 2024
1 parent 25968c5 commit e46e6e5
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 98 deletions.
3 changes: 1 addition & 2 deletions metal/bgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
metal "github.com/equinix/equinix-sdk-go/services/metalv1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
)

type bgp struct {
Expand Down Expand Up @@ -62,7 +61,7 @@ func (b *bgp) enableBGP() error {
Asn: int32(b.localASN),
Md5: &b.bgpPass,
DeploymentType: "local",
UseCase: pointer.String("kubernetes-load-balancer"),
UseCase: metal.PtrString("kubernetes-load-balancer"),
}
_, err = b.client.
RequestBgpConfig(context.Background(), b.project).
Expand Down
2 changes: 1 addition & 1 deletion metal/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
metal "github.com/equinix/equinix-sdk-go/services/metalv1"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/component-base/version"
"k8s.io/klog"
"k8s.io/klog/v2"
)

const (
Expand Down
104 changes: 9 additions & 95 deletions metal/cloud_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package metal

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"

metaltest "github.com/equinix/cloud-provider-equinix-metal/metal/testing"

metal "github.com/equinix/equinix-sdk-go/services/metalv1"
"github.com/google/uuid"
"github.com/gorilla/mux"
clientset "k8s.io/client-go/kubernetes"
k8sfake "k8s.io/client-go/kubernetes/fake"
restclient "k8s.io/client-go/rest"
Expand All @@ -26,16 +25,6 @@ const (
validZoneCode = "ewr1"
)

type MockMetalServer struct {
DeviceStore map[string]*metal.Device
ProjectStore map[string]struct {
Devices []*metal.Device
BgpEnabled bool
}

T *testing.T
}

// mockControllerClientBuilder mock implementation of https://pkg.go.dev/k8s.io/cloud-provider#ControllerClientBuilder
// so we can pass it to cloud.Initialize()
type mockControllerClientBuilder struct{}
Expand All @@ -57,14 +46,9 @@ func (m mockControllerClientBuilder) ClientOrDie(name string) clientset.Interfac
}

// create a valid cloud with a client
func testGetValidCloud(t *testing.T, LoadBalancerSetting string) (*cloud, *MockMetalServer) {
mockServer := &MockMetalServer{
DeviceStore: map[string]*metal.Device{},
ProjectStore: map[string]struct {
Devices []*metal.Device
BgpEnabled bool
}{},
}
func testGetValidCloud(t *testing.T, LoadBalancerSetting string) (*cloud, *metaltest.MockMetalServer) {
mockServer := metaltest.NewMockMetalServer(t)

// mock endpoint so our client can handle it
ts := httptest.NewServer(mockServer.CreateHandler())

Expand Down Expand Up @@ -210,88 +194,18 @@ func TestHasClusterID(t *testing.T) {

// builds an Equinix Metal client
func constructClient(authToken string, baseUrl *string) *metal.APIClient {
configuration := &metal.Configuration{
DefaultHeader: make(map[string]string),
UserAgent: "metal-go/0.29.0",
Debug: false,
Servers: metal.ServerConfigurations{},
OperationServers: map[string]metal.ServerConfigurations{},
}
configuration := metal.NewConfiguration()
configuration.AddDefaultHeader("X-Auth-Token", authToken)
configuration.UserAgent = fmt.Sprintf("cloud-provider-equinix-metal/%s %s", version.Get(), configuration.UserAgent)

servers := metal.ServerConfigurations{
{
URL: "https://api.equinix.com/metal/v1",
Description: "No description provided",
},
}
if baseUrl != nil {
servers = metal.ServerConfigurations{
configuration.Servers = metal.ServerConfigurations{
{
URL: *baseUrl,
Description: "No description provided",
},
}
}

configuration.Servers = servers
configuration.AddDefaultHeader("X-Auth-Token", authToken)
configuration.UserAgent = fmt.Sprintf("cloud-provider-equinix-metal/%s %s", version.Get(), configuration.UserAgent)
return metal.NewAPIClient(configuration)
}

func (s *MockMetalServer) CreateHandler() http.Handler {
r := mux.NewRouter()
// create a BGP config for a project
r.HandleFunc("/projects/{projectID}/bgp-configs", s.createBGPHandler).Methods("POST")
// get all devices for a project
r.HandleFunc("/projects/{projectID}/devices", s.listDevicesHandler).Methods("GET")
// get a single device
r.HandleFunc("/devices/{deviceID}", s.getDeviceHandler).Methods("GET")
// handle metadata requests
return r
}

func (s *MockMetalServer) listDevicesHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
projectID := vars["projectID"]

data := s.ProjectStore[projectID]
devices := data.Devices
var resp = struct {
Devices []*metal.Device `json:"devices"`
}{
Devices: devices,
}

w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(&resp); err != nil {
s.T.Fatal(err.Error())
}
}

// get information about a specific device
func (s *MockMetalServer) getDeviceHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
volID := vars["deviceID"]
dev := s.DeviceStore[volID]
w.Header().Set("Content-Type", "application/json")
if dev != nil {
err := json.NewEncoder(w).Encode(dev)
if err != nil {
s.T.Fatal(err)
}
return
}
w.WriteHeader(http.StatusNotFound)
}

// createBGPHandler enable BGP for a project
func (s *MockMetalServer) createBGPHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
projectID := vars["projectID"]
projectData := s.ProjectStore[projectID]
projectData.BgpEnabled = true
s.ProjectStore[projectID] = projectData
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
}
88 changes: 88 additions & 0 deletions metal/testing/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package testing

import (
"encoding/json"
"net/http"
"testing"

metal "github.com/equinix/equinix-sdk-go/services/metalv1"
"github.com/gorilla/mux"

Check failure on line 9 in metal/testing/server.go

View workflow job for this annotation

GitHub Actions / Test

no required module provides package github.com/gorilla/mux; to add it:
)

type MockMetalServer struct {
DeviceStore map[string]*metal.Device
ProjectStore map[string]struct {
Devices []*metal.Device
BgpEnabled bool
}

T *testing.T
}

func NewMockMetalServer(t *testing.T) *MockMetalServer {
return &MockMetalServer{
DeviceStore: map[string]*metal.Device{},
ProjectStore: map[string]struct {
Devices []*metal.Device
BgpEnabled bool
}{},
T: t,
}
}

func (s *MockMetalServer) CreateHandler() http.Handler {
r := mux.NewRouter()
// create a BGP config for a project
r.HandleFunc("/projects/{projectID}/bgp-configs", s.createBGPHandler).Methods("POST")
// get all devices for a project
r.HandleFunc("/projects/{projectID}/devices", s.listDevicesHandler).Methods("GET")
// get a single device
r.HandleFunc("/devices/{deviceID}", s.getDeviceHandler).Methods("GET")
// handle metadata requests
return r
}

func (s *MockMetalServer) listDevicesHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
projectID := vars["projectID"]

data := s.ProjectStore[projectID]
devices := data.Devices
var resp = struct {
Devices []*metal.Device `json:"devices"`
}{
Devices: devices,
}

w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(&resp); err != nil {
s.T.Fatal(err.Error())
}
}

// get information about a specific device
func (s *MockMetalServer) getDeviceHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
volID := vars["deviceID"]
dev := s.DeviceStore[volID]
w.Header().Set("Content-Type", "application/json")
if dev != nil {
err := json.NewEncoder(w).Encode(dev)
if err != nil {
s.T.Fatal(err)
}
return
}
w.WriteHeader(http.StatusNotFound)
}

// createBGPHandler enable BGP for a project
func (s *MockMetalServer) createBGPHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
projectID := vars["projectID"]
projectData := s.ProjectStore[projectID]
projectData.BgpEnabled = true
s.ProjectStore[projectID] = projectData
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
}

0 comments on commit e46e6e5

Please sign in to comment.