namespace.go 2.5 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. CompartmentId uint32 `json:",omitempty"`
  28. }
  29. func issueNamespaceRequest(id *string, method, subpath string, request interface{}) (*Namespace, error) {
  30. var err error
  31. hnspath := "/namespaces/"
  32. if id != nil {
  33. hnspath = path.Join(hnspath, *id)
  34. }
  35. if subpath != "" {
  36. hnspath = path.Join(hnspath, subpath)
  37. }
  38. var reqJSON []byte
  39. if request != nil {
  40. if reqJSON, err = json.Marshal(request); err != nil {
  41. return nil, err
  42. }
  43. }
  44. var ns Namespace
  45. err = hnsCall(method, hnspath, string(reqJSON), &ns)
  46. if err != nil {
  47. if strings.Contains(err.Error(), "Element not found.") {
  48. return nil, os.ErrNotExist
  49. }
  50. return nil, fmt.Errorf("%s %s: %s", method, hnspath, err)
  51. }
  52. return &ns, err
  53. }
  54. func CreateNamespace() (string, error) {
  55. req := namespaceRequest{}
  56. ns, err := issueNamespaceRequest(nil, "POST", "", &req)
  57. if err != nil {
  58. return "", err
  59. }
  60. return ns.ID, nil
  61. }
  62. func RemoveNamespace(id string) error {
  63. _, err := issueNamespaceRequest(&id, "DELETE", "", nil)
  64. return err
  65. }
  66. func GetNamespaceEndpoints(id string) ([]string, error) {
  67. ns, err := issueNamespaceRequest(&id, "GET", "", nil)
  68. if err != nil {
  69. return nil, err
  70. }
  71. var endpoints []string
  72. for _, rsrc := range ns.ResourceList {
  73. if rsrc.Type == "Endpoint" {
  74. var endpoint namespaceEndpointRequest
  75. err = json.Unmarshal(rsrc.Data, &endpoint)
  76. if err != nil {
  77. return nil, fmt.Errorf("unmarshal endpoint: %s", err)
  78. }
  79. endpoints = append(endpoints, endpoint.ID)
  80. }
  81. }
  82. return endpoints, nil
  83. }
  84. func AddNamespaceEndpoint(id string, endpointID string) error {
  85. resource := namespaceResourceRequest{
  86. Type: "Endpoint",
  87. Data: namespaceEndpointRequest{endpointID},
  88. }
  89. _, err := issueNamespaceRequest(&id, "POST", "addresource", &resource)
  90. return err
  91. }
  92. func RemoveNamespaceEndpoint(id string, endpointID string) error {
  93. resource := namespaceResourceRequest{
  94. Type: "Endpoint",
  95. Data: namespaceEndpointRequest{endpointID},
  96. }
  97. _, err := issueNamespaceRequest(&id, "POST", "removeresource", &resource)
  98. return err
  99. }