gopkg.in/goose.v2@v2.0.1/testservices/errors.go (about)

     1  package testservices
     2  
     3  import "fmt"
     4  
     5  // This map is copied from nova python client
     6  // https://github.com/openstack/nova/blob/master/nova/api/openstack/wsgi.py#L1185
     7  var nameReference = map[int]string{
     8  	400: "badRequest",
     9  	401: "unauthorized",
    10  	403: "forbidden",
    11  	404: "itemNotFound",
    12  	405: "badMethod",
    13  	409: "conflictingRequest",
    14  	413: "overLimit",
    15  	415: "badMediaType",
    16  	429: "overLimit",
    17  	501: "notImplemented",
    18  	503: "serviceUnavailable",
    19  }
    20  
    21  type ServerError struct {
    22  	message string
    23  	code    int
    24  }
    25  
    26  func serverErrorf(code int, message string, args ...interface{}) *ServerError {
    27  	return &ServerError{code: code, message: fmt.Sprintf(message, args...)}
    28  }
    29  
    30  func (n *ServerError) Code() int {
    31  	return n.code
    32  }
    33  
    34  func (n *ServerError) AsJSON() string {
    35  	return fmt.Sprintf(`{%q:{"message":%q, "code":%d}}`, n.Name(), n.message, n.code)
    36  }
    37  
    38  func (n *ServerError) Error() string {
    39  	return fmt.Sprintf("%s: %s", n.Name(), n.message)
    40  }
    41  
    42  func (n *ServerError) Name() string {
    43  	name, ok := nameReference[n.code]
    44  	if !ok {
    45  		return "computeFault"
    46  	}
    47  	return name
    48  }
    49  
    50  func NewInternalServerError(message string) *ServerError {
    51  	return serverErrorf(500, message)
    52  }
    53  
    54  func NewNotFoundError(message string) *ServerError {
    55  	return serverErrorf(404, message)
    56  }
    57  
    58  func NewNoMoreFloatingIpsError() *ServerError {
    59  	return serverErrorf(404, "Zero floating ips available")
    60  }
    61  
    62  func NewIPLimitExceededError() *ServerError {
    63  	return serverErrorf(413, "Maximum number of floating ips exceeded")
    64  }
    65  
    66  func NewRateLimitExceededError() *ServerError {
    67  	// This is an undocumented error
    68  	return serverErrorf(413, "Retry limit exceeded")
    69  }
    70  
    71  func NewTooManyRequestsError() *ServerError {
    72  	return serverErrorf(429, "Too many requests")
    73  }
    74  
    75  func NewForbiddenRateLimitError() *ServerError {
    76  	return serverErrorf(403, "User Rate Limit Exceeded.")
    77  }
    78  
    79  func NewServiceUnavailRateLimitError() *ServerError {
    80  	return serverErrorf(503, "The maximum request receiving rate is exceeded.")
    81  }
    82  
    83  func NewAvailabilityZoneIsNotAvailableError() *ServerError {
    84  	return serverErrorf(400, "The requested availability zone is not available")
    85  }
    86  
    87  func NewAddFlavorError(id string) *ServerError {
    88  	return serverErrorf(409, "A flavor with id %q already exists", id)
    89  }
    90  
    91  func NewNoSuchFlavorError(id string) *ServerError {
    92  	return serverErrorf(404, "No such flavor %q", id)
    93  }
    94  
    95  func NewServerByIDNotFoundError(id string) *ServerError {
    96  	return serverErrorf(404, "No such server %q", id)
    97  }
    98  
    99  func NewServerByNameNotFoundError(name string) *ServerError {
   100  	return serverErrorf(404, "No such server named %q", name)
   101  }
   102  
   103  func NewServerAlreadyExistsError(id string) *ServerError {
   104  	return serverErrorf(409, "A server with id %q already exists", id)
   105  }
   106  
   107  func NewPortAlreadyExistsError(id string) *ServerError {
   108  	return serverErrorf(409, "A port with id %s already exists", id)
   109  }
   110  
   111  func NewPortByIDNotFoundError(portId string) *ServerError {
   112  	return serverErrorf(404, "No such port %s", portId)
   113  }
   114  
   115  func NewSecurityGroupAlreadyExistsError(id string) *ServerError {
   116  	return serverErrorf(409, "A security group with id %s already exists", id)
   117  }
   118  
   119  func NewSecurityGroupByIDNotFoundError(groupId string) *ServerError {
   120  	return serverErrorf(404, "No such security group %s", groupId)
   121  }
   122  
   123  func NewSecurityGroupByNameNotFoundError(name string) *ServerError {
   124  	return serverErrorf(404, "No such security group named %q", name)
   125  }
   126  
   127  func NewSecurityGroupRuleAlreadyExistsError(id string) *ServerError {
   128  	return serverErrorf(409, "A security group rule with id %s already exists", id)
   129  }
   130  
   131  func NewNeutronSecurityGroupRuleAlreadyExistsError(parentId string) *ServerError {
   132  	return serverErrorf(409, "Security group rule already exists. Group id is %s.", parentId)
   133  }
   134  
   135  func NewCannotAddTwiceRuleToGroupError(ruleId, groupId string) *ServerError {
   136  	return serverErrorf(409, "Cannot add twice rule %s to security group %s", ruleId, groupId)
   137  }
   138  
   139  func NewUnknownSecurityGroupError(groupId string) *ServerError {
   140  	return serverErrorf(409, "Unknown source security group %s", groupId)
   141  }
   142  
   143  func NewSecurityGroupRuleNotFoundError(ruleId string) *ServerError {
   144  	return serverErrorf(404, "No such security group rule %s", ruleId)
   145  }
   146  
   147  func NewInvalidDirectionSecurityGroupError(direction string) *ServerError {
   148  	return serverErrorf(400, "Invalid input for direction. Reason: %s is not ingress or egress.", direction)
   149  }
   150  
   151  func NewSecurityGroupRuleInvalidEthernetType(ethernetType string) *ServerError {
   152  	return serverErrorf(400, "Invalid input for ethertype. Reason: %s is not '', 'IPv4' or 'IPv6'.", ethernetType)
   153  }
   154  
   155  func NewSecurityGroupRuleParameterConflict(param1 string, value1 string, param2 string, value2 string) *ServerError {
   156  	return serverErrorf(400, "Conflicting value %s %s for %s %s", param1, value1, param2, value2)
   157  }
   158  
   159  func NewSecurityGroupRuleInvalidCIDR(cidr string) *ServerError {
   160  	return serverErrorf(400, "Invalid CIDR %s given as IP prefix.", cidr)
   161  }
   162  
   163  func NewServerBelongsToGroupError(serverId, groupId string) *ServerError {
   164  	return serverErrorf(409, "Server %q already belongs to group %s", serverId, groupId)
   165  }
   166  
   167  func NewServerDoesNotBelongToGroupsError(serverId string) *ServerError {
   168  	return serverErrorf(400, "Server %q does not belong to any groups", serverId)
   169  }
   170  
   171  func NewServerDoesNotBelongToGroupError(serverId, groupId string) *ServerError {
   172  	return serverErrorf(400, "Server %q does not belong to group %s", serverId, groupId)
   173  }
   174  
   175  func NewFloatingIPExistsError(ipID string) *ServerError {
   176  	return serverErrorf(409, "A floating IP with id %s already exists", ipID)
   177  }
   178  
   179  func NewFloatingIPNotFoundError(address string) *ServerError {
   180  	return serverErrorf(404, "No such floating IP %q", address)
   181  }
   182  
   183  func NewServerHasFloatingIPError(serverId, ipId string) *ServerError {
   184  	return serverErrorf(409, "Server %q already has floating IP %s", serverId, ipId)
   185  }
   186  
   187  func NewNoFloatingIPsToRemoveError(serverId string) *ServerError {
   188  	return serverErrorf(409, "Server %q does not have any floating IPs to remove", serverId)
   189  }
   190  
   191  func NewNoFloatingIPsError(serverId, ipId string) *ServerError {
   192  	return serverErrorf(404, "Server %q does not have floating IP %s", serverId, ipId)
   193  }
   194  
   195  func NewNetworkNotFoundError(network string) *ServerError {
   196  	return serverErrorf(404, "No such network %q", network)
   197  }
   198  
   199  func NewNetworkAlreadyExistsError(id string) *ServerError {
   200  	return serverErrorf(409, "A network with id %q already exists", id)
   201  }
   202  
   203  func NewSubnetNotFoundError(subnet string) *ServerError {
   204  	return serverErrorf(404, "No such subnet %q", subnet)
   205  }
   206  
   207  func NewSubnetAlreadyExistsError(id string) *ServerError {
   208  	return serverErrorf(409, "A subnet with id %q already exists", id)
   209  }
   210  
   211  func NewNoSuchOSInterfaceError(id string) *ServerError {
   212  	return serverErrorf(404, "No such os interface %q", id)
   213  }