namespace.go 2.5 KB

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