namespace.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package hns
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "path"
  7. "strings"
  8. )
  9. type namespaceRequest struct {
  10. IsDefault bool `json:",omitempty"`
  11. }
  12. type namespaceEndpointRequest struct {
  13. ID string `json:"Id"`
  14. }
  15. type NamespaceResource struct {
  16. Type string
  17. Data json.RawMessage
  18. }
  19. type namespaceResourceRequest struct {
  20. Type string
  21. Data interface{}
  22. }
  23. type Namespace struct {
  24. ID string
  25. IsDefault bool `json:",omitempty"`
  26. ResourceList []NamespaceResource `json:",omitempty"`
  27. }
  28. func issueNamespaceRequest(id *string, method, subpath string, request interface{}) (*Namespace, error) {
  29. var err error
  30. hnspath := "/namespaces/"
  31. if id != nil {
  32. hnspath = path.Join(hnspath, *id)
  33. }
  34. if subpath != "" {
  35. hnspath = path.Join(hnspath, subpath)
  36. }
  37. var reqJSON []byte
  38. if request != nil {
  39. if reqJSON, err = json.Marshal(request); err != nil {
  40. return nil, err
  41. }
  42. }
  43. var ns Namespace
  44. err = hnsCall(method, hnspath, string(reqJSON), &ns)
  45. if err != nil {
  46. if strings.Contains(err.Error(), "Element not found.") {
  47. return nil, os.ErrNotExist
  48. }
  49. return nil, fmt.Errorf("%s %s: %s", method, hnspath, err)
  50. }
  51. return &ns, err
  52. }
  53. func CreateNamespace() (string, error) {
  54. req := namespaceRequest{}
  55. ns, err := issueNamespaceRequest(nil, "POST", "", &req)
  56. if err != nil {
  57. return "", err
  58. }
  59. return ns.ID, nil
  60. }
  61. func RemoveNamespace(id string) error {
  62. _, err := issueNamespaceRequest(&id, "DELETE", "", nil)
  63. return err
  64. }
  65. func GetNamespaceEndpoints(id string) ([]string, error) {
  66. ns, err := issueNamespaceRequest(&id, "GET", "", nil)
  67. if err != nil {
  68. return nil, err
  69. }
  70. var endpoints []string
  71. for _, rsrc := range ns.ResourceList {
  72. if rsrc.Type == "Endpoint" {
  73. var endpoint namespaceEndpointRequest
  74. err = json.Unmarshal(rsrc.Data, &endpoint)
  75. if err != nil {
  76. return nil, fmt.Errorf("unmarshal endpoint: %s", err)
  77. }
  78. endpoints = append(endpoints, endpoint.ID)
  79. }
  80. }
  81. return endpoints, nil
  82. }
  83. func AddNamespaceEndpoint(id string, endpointID string) error {
  84. resource := namespaceResourceRequest{
  85. Type: "Endpoint",
  86. Data: namespaceEndpointRequest{endpointID},
  87. }
  88. _, err := issueNamespaceRequest(&id, "POST", "addresource", &resource)
  89. return err
  90. }
  91. func RemoveNamespaceEndpoint(id string, endpointID string) error {
  92. resource := namespaceResourceRequest{
  93. Type: "Endpoint",
  94. Data: namespaceEndpointRequest{endpointID},
  95. }
  96. _, err := issueNamespaceRequest(&id, "POST", "removeresource", &resource)
  97. return err
  98. }