terraform/vendor/github.com/joyent/triton-go/errors.go

127 lines
3.4 KiB
Go
Raw Normal View History

provider/triton: Move to joyent/triton-go (#13225) * provider/triton: Move to joyent/triton-go This commit moves the Triton provider to the new joyent/triton-go library from gosdc. This has a number of advantages - not least that requests can be signed using an SSH agent without having to keep unencrypted key material in memory. Schema has been maintained for all resources, and several tests have been added and acceptance tests repaired - in some cases by fixing bugs in the underlying resources. After applying this patch, all acceptance tests pass: ``` go generate $(go list ./... | grep -v /terraform/vendor/) 2017/03/30 13:48:33 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/triton -v -timeout 120m === RUN TestProvider --- PASS: TestProvider (0.00s) === RUN TestProvider_impl --- PASS: TestProvider_impl (0.00s) === RUN TestAccTritonFabric_basic --- PASS: TestAccTritonFabric_basic (15.11s) === RUN TestAccTritonFirewallRule_basic --- PASS: TestAccTritonFirewallRule_basic (1.48s) === RUN TestAccTritonFirewallRule_update --- PASS: TestAccTritonFirewallRule_update (1.55s) === RUN TestAccTritonFirewallRule_enable --- PASS: TestAccTritonFirewallRule_enable (1.52s) === RUN TestAccTritonKey_basic --- PASS: TestAccTritonKey_basic (11.76s) === RUN TestAccTritonKey_noKeyName --- PASS: TestAccTritonKey_noKeyName (11.20s) === RUN TestAccTritonMachine_basic --- PASS: TestAccTritonMachine_basic (82.19s) === RUN TestAccTritonMachine_dns --- PASS: TestAccTritonMachine_dns (173.36s) === RUN TestAccTritonMachine_nic --- PASS: TestAccTritonMachine_nic (167.82s) === RUN TestAccTritonMachine_addNIC --- PASS: TestAccTritonMachine_addNIC (192.11s) === RUN TestAccTritonMachine_firewall --- PASS: TestAccTritonMachine_firewall (188.53s) === RUN TestAccTritonMachine_metadata --- PASS: TestAccTritonMachine_metadata (614.57s) === RUN TestAccTritonVLAN_basic --- PASS: TestAccTritonVLAN_basic (0.93s) === RUN TestAccTritonVLAN_update --- PASS: TestAccTritonVLAN_update (1.50s) PASS ok github.com/hashicorp/terraform/builtin/providers/triton 1463.621s ``` * provider/triton: Update docs for provider config * deps: Vendor github.com/joyent/triton-go/... * deps: Remove github.com/joyent/gosdc
2017-03-31 00:25:27 +02:00
package triton
import (
"fmt"
"github.com/hashicorp/errwrap"
)
// TritonError represents an error code and message along with
// the status code of the HTTP request which resulted in the error
// message. Error codes used by the Triton API are listed at
// https://apidocs.joyent.com/cloudapi/#cloudapi-http-responses
type TritonError struct {
StatusCode int
Code string `json:"code"`
Message string `json:"message"`
}
// Error implements interface Error on the TritonError type.
func (e TritonError) Error() string {
return fmt.Sprintf("%s: %s", e.Code, e.Message)
}
// IsBadRequest tests whether err wraps a TritonError with
// code BadRequest
func IsBadRequest(err error) bool {
return isSpecificError(err, "BadRequest")
}
// IsInternalError tests whether err wraps a TritonError with
// code InternalError
func IsInternalError(err error) bool {
return isSpecificError(err, "InternalError")
}
// IsInUseError tests whether err wraps a TritonError with
// code InUseError
func IsInUseError(err error) bool {
return isSpecificError(err, "InUseError")
}
// IsInvalidArgument tests whether err wraps a TritonError with
// code InvalidArgument
func IsInvalidArgument(err error) bool {
return isSpecificError(err, "InvalidArgument")
}
// IsInvalidCredentials tests whether err wraps a TritonError with
// code InvalidCredentials
func IsInvalidCredentials(err error) bool {
return isSpecificError(err, "InvalidCredentials")
}
// IsInvalidHeader tests whether err wraps a TritonError with
// code InvalidHeader
func IsInvalidHeader(err error) bool {
return isSpecificError(err, "InvalidHeader")
}
// IsInvalidVersion tests whether err wraps a TritonError with
// code InvalidVersion
func IsInvalidVersion(err error) bool {
return isSpecificError(err, "InvalidVersion")
}
// IsMissingParameter tests whether err wraps a TritonError with
// code MissingParameter
func IsMissingParameter(err error) bool {
return isSpecificError(err, "MissingParameter")
}
// IsNotAuthorized tests whether err wraps a TritonError with
// code NotAuthorized
func IsNotAuthorized(err error) bool {
return isSpecificError(err, "NotAuthorized")
}
// IsRequestThrottled tests whether err wraps a TritonError with
// code RequestThrottled
func IsRequestThrottled(err error) bool {
return isSpecificError(err, "RequestThrottled")
}
// IsRequestTooLarge tests whether err wraps a TritonError with
// code RequestTooLarge
func IsRequestTooLarge(err error) bool {
return isSpecificError(err, "RequestTooLarge")
}
// IsRequestMoved tests whether err wraps a TritonError with
// code RequestMoved
func IsRequestMoved(err error) bool {
return isSpecificError(err, "RequestMoved")
}
// IsResourceNotFound tests whether err wraps a TritonError with
// code ResourceNotFound
func IsResourceNotFound(err error) bool {
return isSpecificError(err, "ResourceNotFound")
}
// IsUnknownError tests whether err wraps a TritonError with
// code UnknownError
func IsUnknownError(err error) bool {
return isSpecificError(err, "UnknownError")
}
// isSpecificError checks whether the error represented by err wraps
// an underlying TritonError with code errorCode.
func isSpecificError(err error, errorCode string) bool {
if err == nil {
return false
}
tritonErrorInterface := errwrap.GetType(err.(error), &TritonError{})
if tritonErrorInterface == nil {
return false
}
tritonErr := tritonErrorInterface.(*TritonError)
if tritonErr.Code == errorCode {
return true
}
return false
}