Skip to content

Commit

Permalink
Merge pull request #38 from linkernetworks/hwchiu/goreports
Browse files Browse the repository at this point in the history
Fix the goreports.
  • Loading branch information
Hung-Wei Chiu authored Jun 24, 2018
2 parents cd9638e + d760a9b commit bbe2aaa
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 27 deletions.
9 changes: 9 additions & 0 deletions src/kubernetes/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ import (
"k8s.io/client-go/kubernetes"
)

/*
KubeCtl object is used to interact with the kubernetes cluster.
Use the export function New to Get a KubeCtl object.
*/
type KubeCtl struct {
Clientset kubernetes.Interface
Namespace string
}

/*
The API to New a kubectl object and you need to pass two parameters
1. The kubernetes clientset object from the client-go library. You can also use the fake-client for testing
2. The namespace of the kubernetes you want to manipulate
*/
func New(clientset kubernetes.Interface, namespace string) *KubeCtl {
return &KubeCtl{
Clientset: clientset,
Expand Down
3 changes: 3 additions & 0 deletions src/kubernetes/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

//Get the Node object by the node name
func (kc *KubeCtl) GetNode(name string) (*corev1.Node, error) {
return kc.Clientset.CoreV1().Nodes().Get(name, metav1.GetOptions{})
}

//Get all nodes from the k8s cluster
func (kc *KubeCtl) GetNodes() ([]*corev1.Node, error) {
nodes := []*corev1.Node{}
nodesList, err := kc.Clientset.CoreV1().Nodes().List(metav1.ListOptions{})
Expand All @@ -21,6 +23,7 @@ func (kc *KubeCtl) GetNodes() ([]*corev1.Node, error) {
return nodes, nil
}

//Get the external IP address of node
func (kc *KubeCtl) GetNodeExternalIP(name string) (string, error) {
node, err := kc.GetNode(name)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions src/net/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/linkernetworks/vortex/src/web"
)

// The interface for native http handler(http.Request and http.ResponseWriter)
type NativeContextHandler func(*web.NativeContext)

// CompositeServiceProvider apply mongo client to HandlerFunc
Expand All @@ -20,8 +21,10 @@ func CompositeServiceHandler(sp *serviceprovider.Container, handler NativeContex
}
}

// The interface for restfuul handler(restful.Request,restful.Response)
type RESTfulContextHandler func(*web.Context)

// The wrapper to combine the RESTfulContextHandler with our serviceprovider object
func RESTfulServiceHandler(sp *serviceprovider.Container, handler RESTfulContextHandler) restful.RouteFunction {
return func(req *restful.Request, resp *restful.Response) {
ctx := web.Context{sp, req, resp}
Expand Down
2 changes: 1 addition & 1 deletion src/net/http/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type QueryUrl struct {
Url url.Values `json:"url"`
Url url.Values `json:"url"`
}

func New(url url.Values) *QueryUrl {
Expand Down
19 changes: 15 additions & 4 deletions src/net/http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import (
"net/http"
)

// The Structure to contain the Error Message from the HTTP response
// Contains the Error Messagess (At most two errors)
type ErrorPayload struct {
XMLName xml.Name `json:"-" xml:"response"`
Error bool `json:"error" xml:"error"`
Message string `json:"message" xml:"message"`
PreviousMessage string `json:"previousMessage,omitempty" xml:"previousMessage,omitempty"`
}

// Return the ErrorPayload message according the parameters (errors)
// The ErrorPayload contains at most error messages
func NewErrorPayload(errs ...error) ErrorPayload {
payload := ErrorPayload{Error: true}

Expand All @@ -26,6 +30,8 @@ func NewErrorPayload(errs ...error) ErrorPayload {
return payload
}

// The function to get the error from the request and ErrorPayload
// It should contains the errorType and error content
func EncodeErrorPayload(req *http.Request, payload ErrorPayload) (out []byte, contentType string, encErr error) {
contentType = req.Header.Get("content-type")

Expand All @@ -42,6 +48,7 @@ func EncodeErrorPayload(req *http.Request, payload ErrorPayload) (out []byte, co
return out, contentType, encErr
}

// Write the error status and error code to the http request and return the written byte count of the http request
func WriteStatusAndError(req *http.Request, resp http.ResponseWriter, status int, errs ...error) (int, error) {
payload := NewErrorPayload(errs...)
out, contentType, encErr := EncodeErrorPayload(req, payload)
Expand All @@ -55,38 +62,42 @@ func WriteStatusAndError(req *http.Request, resp http.ResponseWriter, status int
return resp.Write(out)
}

// Set the status code http.StatusForbidden to the HTTP response message
func Forbidden(req *http.Request, resp http.ResponseWriter, errs ...error) (int, error) {
return WriteStatusAndError(req, resp, http.StatusForbidden, errs...)
}

// Set the status code http.StatusBadRequest to the HTTP response message
func BadRequest(req *http.Request, resp http.ResponseWriter, errs ...error) (int, error) {
return WriteStatusAndError(req, resp, http.StatusBadRequest, errs...)
}

// Set the status code http.StatusOK to the HTTP response message
func OK(req *http.Request, resp http.ResponseWriter, errs ...error) (int, error) {
return WriteStatusAndError(req, resp, http.StatusOK, errs...)
}

// Response http.StatusNotFound
// Set the status code http.StatusNotFound to the HTTP response message
func NotFound(req *http.Request, resp http.ResponseWriter, errs ...error) (int, error) {
return WriteStatusAndError(req, resp, http.StatusNotFound, errs...)
}

// Set the status code http.StatusUnauthorized to the HTTP response message
func Unauthorized(req *http.Request, resp http.ResponseWriter, errs ...error) (int, error) {
return WriteStatusAndError(req, resp, http.StatusUnauthorized, errs...)
}

// Response http.StatusInternalServerError
// Set the status code http.StatusInternalServerError to the HTTP response message
func InternalServerError(req *http.Request, resp http.ResponseWriter, errs ...error) (int, error) {
return WriteStatusAndError(req, resp, http.StatusInternalServerError, errs...)
}

// Response http.StatusConflict
// Set the status code http.StatusConflict to the HTTP response message
func Conflict(req *http.Request, resp http.ResponseWriter, errs ...error) (int, error) {
return WriteStatusAndError(req, resp, http.StatusConflict, errs...)
}

// Response http.StatusUnprocessableEntity
// Set the status code http.StatusUnprocessableEntity to the HTTP response message
func UnprocessableEntity(req *http.Request, resp http.ResponseWriter, errs ...error) (int, error) {
return WriteStatusAndError(req, resp, http.StatusUnprocessableEntity, errs...)
}
20 changes: 0 additions & 20 deletions src/net/http/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,6 @@ func ExampleWriteStatusAndError() {
WriteStatusAndError(request, recorder, http.StatusBadRequest, errors.New("bad request"))
}

func ExampleDefaultEncoding() {
recorder := httptest.NewRecorder()
request, err := http.NewRequest("POST", "http://here.com/v1/signin", nil)
if err != nil {
panic(err)
}
InternalServerError(request, recorder, errors.New("Failed to do something"))
}

func ExampleJsonEncoding() {
recorder := httptest.NewRecorder()
request, err := http.NewRequest("POST", "http://here.com/v1/signin", nil)
if err != nil {
panic(err)
}

request.Header.Set("content-type", "application/json")
InternalServerError(request, recorder, errors.New("Failed to do something"))
}

func TestEncodeErrorPayload(t *testing.T) {
testCases := []struct {
cases string
Expand Down
2 changes: 1 addition & 1 deletion src/server/handler_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/linkernetworks/vortex/src/serviceprovider"
)

func VersionHandler(sp *serviceprovider.Container) restful.RouteFunction {
func versionHandler(sp *serviceprovider.Container) restful.RouteFunction {
return func(req *restful.Request, resp *restful.Response) {
}
}
2 changes: 1 addition & 1 deletion src/server/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (a *App) AppRoute() *mux.Router {
func newVersionService(sp *serviceprovider.Container) *restful.WebService {
webService := new(restful.WebService)
webService.Path("/v1/versions").Consumes(restful.MIME_JSON, restful.MIME_JSON).Produces(restful.MIME_JSON, restful.MIME_JSON)
webService.Route(webService.GET("/").To(VersionHandler(sp)))
webService.Route(webService.GET("/").To(versionHandler(sp)))
return webService
}

Expand Down
2 changes: 2 additions & 0 deletions src/web/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"github.com/linkernetworks/vortex/src/serviceprovider"
)

// The struct to combine the restful message with our own serviceProvider
type Context struct {
ServiceProvider *serviceprovider.Container
Request *restful.Request
Response *restful.Response
}

// The struct to combine the native http message with our own serviceProvider
type NativeContext struct {
ServiceProvider *serviceprovider.Container
Request *http.Request
Expand Down

0 comments on commit bbe2aaa

Please sign in to comment.