hcnerrors.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Package hcn is a shim for the Host Compute Networking (HCN) service, which manages networking for Windows Server
  2. // containers and Hyper-V containers. Previous to RS5, HCN was referred to as Host Networking Service (HNS).
  3. package hcn
  4. import (
  5. "fmt"
  6. "github.com/Microsoft/hcsshim/internal/hcserror"
  7. "github.com/Microsoft/hcsshim/internal/interop"
  8. "github.com/sirupsen/logrus"
  9. )
  10. func checkForErrors(methodName string, hr error, resultBuffer *uint16) error {
  11. errorFound := false
  12. if hr != nil {
  13. errorFound = true
  14. }
  15. result := ""
  16. if resultBuffer != nil {
  17. result = interop.ConvertAndFreeCoTaskMemString(resultBuffer)
  18. if result != "" {
  19. errorFound = true
  20. }
  21. }
  22. if errorFound {
  23. returnError := hcserror.New(hr, methodName, result)
  24. logrus.Debugf(returnError.Error()) // HCN errors logged for debugging.
  25. return returnError
  26. }
  27. return nil
  28. }
  29. // NetworkNotFoundError results from a failed seach for a network by Id or Name
  30. type NetworkNotFoundError struct {
  31. NetworkName string
  32. NetworkID string
  33. }
  34. func (e NetworkNotFoundError) Error() string {
  35. if e.NetworkName == "" {
  36. return fmt.Sprintf("Network Name %s not found", e.NetworkName)
  37. }
  38. return fmt.Sprintf("Network Id %s not found", e.NetworkID)
  39. }
  40. // EndpointNotFoundError results from a failed seach for an endpoint by Id or Name
  41. type EndpointNotFoundError struct {
  42. EndpointName string
  43. EndpointID string
  44. }
  45. func (e EndpointNotFoundError) Error() string {
  46. if e.EndpointName == "" {
  47. return fmt.Sprintf("Endpoint Name %s not found", e.EndpointName)
  48. }
  49. return fmt.Sprintf("Endpoint Id %s not found", e.EndpointID)
  50. }
  51. // NamespaceNotFoundError results from a failed seach for a namsepace by Id
  52. type NamespaceNotFoundError struct {
  53. NamespaceID string
  54. }
  55. func (e NamespaceNotFoundError) Error() string {
  56. return fmt.Sprintf("Namespace %s not found", e.NamespaceID)
  57. }
  58. // LoadBalancerNotFoundError results from a failed seach for a loadbalancer by Id
  59. type LoadBalancerNotFoundError struct {
  60. LoadBalancerId string
  61. }
  62. func (e LoadBalancerNotFoundError) Error() string {
  63. return fmt.Sprintf("LoadBalancer %s not found", e.LoadBalancerId)
  64. }
  65. // IsNotFoundError returns a boolean indicating whether the error was caused by
  66. // a resource not being found.
  67. func IsNotFoundError(err error) bool {
  68. switch err.(type) {
  69. case NetworkNotFoundError:
  70. return true
  71. case EndpointNotFoundError:
  72. return true
  73. case NamespaceNotFoundError:
  74. return true
  75. case LoadBalancerNotFoundError:
  76. return true
  77. }
  78. return false
  79. }