Skip to content

Commit

Permalink
Merge pull request #14 from heartlock/add-errortype
Browse files Browse the repository at this point in the history
add error type and fix some compile error
  • Loading branch information
heartlock authored Sep 21, 2016
2 parents b41e796 + ddc3e60 commit cdb2285
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 7 deletions.
13 changes: 6 additions & 7 deletions pkg/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"strings"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"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"
Expand All @@ -44,7 +43,7 @@ type OpenDaylightClient struct {
func NewOpenDaylightClient(config *Config) (OpenDaylightClient, error) {
return OpenDaylightClient{
BaseUrl: config.Global.Url,
HTTPClient: http.DefaultClient,
HTTPClient: *http.DefaultClient,
}, nil

}
Expand Down Expand Up @@ -447,7 +446,7 @@ func (client *OpenDaylightClient) ListPort(opts ports.ListOpts) pagination.Pager
})
}

func (client *OpenDaylightClient) UpdatePort(portId string, opts portsbinding.UpdateOpts) (r UpdateResult) {
func (client *OpenDaylightClient) UpdatePort(portId string, opts portsbinding.UpdateOpts) (r portsbinding.UpdateResult) {
url := client.BaseUrl + "/v2.0/port/" + portId
b, err := opts.ToPortUpdateMap()
if err != nil {
Expand All @@ -461,7 +460,7 @@ func (client *OpenDaylightClient) UpdatePort(portId string, opts portsbinding.Up
}

//router
func (client *OpenDaylightClient) CreateRouter(opts routers.CreateOpts) {
func (client *OpenDaylightClient) CreateRouter(opts routers.CreateOpts) (r routers.CreateResult) {
url := client.BaseUrl + "/v2.0/routers"
b, err := opts.ToRouterCreateMap()
if err != nil {
Expand All @@ -472,7 +471,7 @@ func (client *OpenDaylightClient) CreateRouter(opts routers.CreateOpts) {
return
}

func (client *OpenDaylightClient) DeleteRouter(routerId string) {
func (client *OpenDaylightClient) DeleteRouter(routerId string) (r routers.DeleteResult) {
url := client.BaseUrl + "/v2.0/routers/" + routerId
_, r.Err = client.Delete(url, nil)
return
Expand All @@ -495,7 +494,7 @@ func (client *OpenDaylightClient) UpdateRouter() {
}

// interface
func (client *OpenDaylightClient) AddInterface(routerId string, opts routers.AddInterfaceOpts) (r InterfaceResult) {
func (client *OpenDaylightClient) AddInterface(routerId string, opts routers.AddInterfaceOpts) (r routers.InterfaceResult) {
url := client.BaseUrl + "/v2.0/routers/" + routerId
b, err := opts.ToRouterAddInterfaceMap()
if err != nil {
Expand All @@ -508,7 +507,7 @@ func (client *OpenDaylightClient) AddInterface(routerId string, opts routers.Add
return
}

func (client *OpenDaylightClient) RemoveInterface(routerId string, opts routers.RemoveInterfaceOpts) (r InterfaceResult) {
func (client *OpenDaylightClient) RemoveInterface(routerId string, opts routers.RemoveInterfaceOpts) (r routers.InterfaceResult) {
url := client.BaseUrl + "/v2.0/routers/" + routerId
b, err := opts.ToRouterRemoveInterfaceMap()
if err != nil {
Expand Down
276 changes: 276 additions & 0 deletions pkg/common/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
package common

import "fmt"

// BaseError is an error type that all other error types embed.
type BaseError struct {
DefaultErrString string
Info string
}

func (e BaseError) Error() string {
e.DefaultErrString = "An error occurred while executing a Gophercloud request."
return e.choseErrString()
}

func (e BaseError) choseErrString() string {
if e.Info != "" {
return e.Info
}
return e.DefaultErrString
}

// ErrMissingInput is the error when input is required in a particular
// situation but not provided by the user
type ErrMissingInput struct {
BaseError
Argument string
}

func (e ErrMissingInput) Error() string {
e.DefaultErrString = fmt.Sprintf("Missing input for argument [%s]", e.Argument)
return e.choseErrString()
}

// ErrInvalidInput is an error type used for most non-HTTP Gophercloud errors.
type ErrInvalidInput struct {
ErrMissingInput
Value interface{}
}

func (e ErrInvalidInput) Error() string {
e.DefaultErrString = fmt.Sprintf("Invalid input provided for argument [%s]: [%+v]", e.Argument, e.Value)
return e.choseErrString()
}

// ErrUnexpectedResponseCode is returned by the Request method when a response code other than
// those listed in OkCodes is encountered.
type ErrUnexpectedResponseCode struct {
BaseError
URL string
Method string
Expected []int
Actual int
Body []byte
}

func (e ErrUnexpectedResponseCode) Error() string {
e.DefaultErrString = fmt.Sprintf(
"Expected HTTP response code %v when accessing [%s %s], but got %d instead\n%s",
e.Expected, e.Method, e.URL, e.Actual, e.Body,
)
return e.choseErrString()
}

// ErrDefault400 is the default error type returned on a 400 HTTP response code.
type ErrDefault400 struct {
ErrUnexpectedResponseCode
}

// ErrDefault401 is the default error type returned on a 401 HTTP response code.
type ErrDefault401 struct {
ErrUnexpectedResponseCode
}

// ErrDefault404 is the default error type returned on a 404 HTTP response code.
type ErrDefault404 struct {
ErrUnexpectedResponseCode
}

// ErrDefault405 is the default error type returned on a 405 HTTP response code.
type ErrDefault405 struct {
ErrUnexpectedResponseCode
}

// ErrDefault408 is the default error type returned on a 408 HTTP response code.
type ErrDefault408 struct {
ErrUnexpectedResponseCode
}

// ErrDefault429 is the default error type returned on a 429 HTTP response code.
type ErrDefault429 struct {
ErrUnexpectedResponseCode
}

// ErrDefault500 is the default error type returned on a 500 HTTP response code.
type ErrDefault500 struct {
ErrUnexpectedResponseCode
}

// ErrDefault503 is the default error type returned on a 503 HTTP response code.
type ErrDefault503 struct {
ErrUnexpectedResponseCode
}

func (e ErrDefault400) Error() string {
return "Invalid request due to incorrect syntax or missing required parameters."
}
func (e ErrDefault401) Error() string {
return "Authentication failed"
}
func (e ErrDefault404) Error() string {
return "Resource not found"
}
func (e ErrDefault405) Error() string {
return "Method not allowed"
}
func (e ErrDefault408) Error() string {
return "The server timed out waiting for the request"
}
func (e ErrDefault429) Error() string {
return "Too many requests have been sent in a given amount of time. Pause" +
" requests, wait up to one minute, and try again."
}
func (e ErrDefault500) Error() string {
return "Internal Server Error"
}
func (e ErrDefault503) Error() string {
return "The service is currently unable to handle the request due to a temporary" +
" overloading or maintenance. This is a temporary condition. Try again later."
}

// Err400er is the interface resource error types implement to override the error message
// from a 400 error.
type Err400er interface {
Error400(ErrUnexpectedResponseCode) error
}

// Err401er is the interface resource error types implement to override the error message
// from a 401 error.
type Err401er interface {
Error401(ErrUnexpectedResponseCode) error
}

// Err404er is the interface resource error types implement to override the error message
// from a 404 error.
type Err404er interface {
Error404(ErrUnexpectedResponseCode) error
}

// Err405er is the interface resource error types implement to override the error message
// from a 405 error.
type Err405er interface {
Error405(ErrUnexpectedResponseCode) error
}

// Err408er is the interface resource error types implement to override the error message
// from a 408 error.
type Err408er interface {
Error408(ErrUnexpectedResponseCode) error
}

// Err429er is the interface resource error types implement to override the error message
// from a 429 error.
type Err429er interface {
Error429(ErrUnexpectedResponseCode) error
}

// Err500er is the interface resource error types implement to override the error message
// from a 500 error.
type Err500er interface {
Error500(ErrUnexpectedResponseCode) error
}

// Err503er is the interface resource error types implement to override the error message
// from a 503 error.
type Err503er interface {
Error503(ErrUnexpectedResponseCode) error
}

// ErrTimeOut is the error type returned when an operations times out.
type ErrTimeOut struct {
BaseError
}

func (e ErrTimeOut) Error() string {
e.DefaultErrString = "A time out occurred"
return e.choseErrString()
}

// ErrUnableToReauthenticate is the error type returned when reauthentication fails.
type ErrUnableToReauthenticate struct {
BaseError
ErrOriginal error
}

func (e ErrUnableToReauthenticate) Error() string {
e.DefaultErrString = fmt.Sprintf("Unable to re-authenticate: %s", e.ErrOriginal)
return e.choseErrString()
}

// ErrErrorAfterReauthentication is the error type returned when reauthentication
// succeeds, but an error occurs afterword (usually an HTTP error).
type ErrErrorAfterReauthentication struct {
BaseError
ErrOriginal error
}

func (e ErrErrorAfterReauthentication) Error() string {
e.DefaultErrString = fmt.Sprintf("Successfully re-authenticated, but got error executing request: %s", e.ErrOriginal)
return e.choseErrString()
}

// ErrServiceNotFound is returned when no service in a service catalog matches
// the provided EndpointOpts. This is generally returned by provider service
// factory methods like "NewComputeV2()" and can mean that a service is not
// enabled for your account.
type ErrServiceNotFound struct {
BaseError
}

func (e ErrServiceNotFound) Error() string {
e.DefaultErrString = "No suitable service could be found in the service catalog."
return e.choseErrString()
}

// ErrEndpointNotFound is returned when no available endpoints match the
// provided EndpointOpts. This is also generally returned by provider service
// factory methods, and usually indicates that a region was specified
// incorrectly.
type ErrEndpointNotFound struct {
BaseError
}

func (e ErrEndpointNotFound) Error() string {
e.DefaultErrString = "No suitable endpoint could be found in the service catalog."
return e.choseErrString()
}

// ErrResourceNotFound is the error when trying to retrieve a resource's
// ID by name and the resource doesn't exist.
type ErrResourceNotFound struct {
BaseError
Name string
ResourceType string
}

func (e ErrResourceNotFound) Error() string {
e.DefaultErrString = fmt.Sprintf("Unable to find %s with name %s", e.ResourceType, e.Name)
return e.choseErrString()
}

// ErrMultipleResourcesFound is the error when trying to retrieve a resource's
// ID by name and multiple resources have the user-provided name.
type ErrMultipleResourcesFound struct {
BaseError
Name string
Count int
ResourceType string
}

func (e ErrMultipleResourcesFound) Error() string {
e.DefaultErrString = fmt.Sprintf("Found %d %ss matching %s", e.Count, e.ResourceType, e.Name)
return e.choseErrString()
}

// ErrUnexpectedType is the error when an unexpected type is encountered
type ErrUnexpectedType struct {
BaseError
Expected string
Actual string
}

func (e ErrUnexpectedType) Error() string {
e.DefaultErrString = fmt.Sprintf("Expected %s but got %s", e.Expected, e.Actual)
return e.choseErrString()
}
5 changes: 5 additions & 0 deletions pkg/common/opendaylight.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import (
"code.google.com/p/gcfg"
"github.com/docker/distribution/uuid"
"github.com/golang/glog"
"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"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
"github.com/heartlock/harmonystack/pkg/plugins"
provider "github.com/heartlock/harmonystack/pkg/types"

Expand Down

0 comments on commit cdb2285

Please sign in to comment.