/src/kubernetes-1.13.4/vendor/github.com/quobyte/api/quobyte.go

https://github.com/trailofbits/audit-kubernetes · Go · 203 lines · 155 code · 34 blank · 14 comment · 22 complexity · 24c8d3865d44b2b973c1b075d36d4c27 MD5 · raw file

  1. // Package quobyte represents a golang API for the Quobyte Storage System
  2. package quobyte
  3. import (
  4. "log"
  5. "net/http"
  6. "regexp"
  7. )
  8. // retry policy codes
  9. const (
  10. RetryNever string = "NEVER"
  11. RetryInteractive string = "INTERACTIVE"
  12. RetryInfinitely string = "INFINITELY"
  13. RetryOncePerTarget string = "ONCE_PER_TARGET"
  14. )
  15. type QuobyteClient struct {
  16. client *http.Client
  17. url string
  18. username string
  19. password string
  20. apiRetryPolicy string
  21. }
  22. func (client *QuobyteClient) SetAPIRetryPolicy(retry string) {
  23. client.apiRetryPolicy = retry
  24. }
  25. func (client *QuobyteClient) GetAPIRetryPolicy() string {
  26. return client.apiRetryPolicy
  27. }
  28. // NewQuobyteClient creates a new Quobyte API client
  29. func NewQuobyteClient(url string, username string, password string) *QuobyteClient {
  30. return &QuobyteClient{
  31. client: &http.Client{},
  32. url: url,
  33. username: username,
  34. password: password,
  35. apiRetryPolicy: RetryInteractive,
  36. }
  37. }
  38. // CreateVolume creates a new Quobyte volume. Its root directory will be owned by given user and group
  39. func (client QuobyteClient) CreateVolume(request *CreateVolumeRequest) (string, error) {
  40. var response volumeUUID
  41. if request.TenantID != "" && !IsValidUUID(request.TenantID) {
  42. log.Printf("Tenant name resolution: Resolving %s to UUID\n", request.TenantID)
  43. tenantUUID, err := client.ResolveTenantNameToUUID(request.TenantID)
  44. if err != nil {
  45. return "", err
  46. }
  47. request.TenantID = tenantUUID
  48. }
  49. if err := client.sendRequest("createVolume", request, &response); err != nil {
  50. return "", err
  51. }
  52. return response.VolumeUUID, nil
  53. }
  54. // ResolveVolumeNameToUUID resolves a volume name to a UUID
  55. func (client *QuobyteClient) ResolveVolumeNameToUUID(volumeName, tenant string) (string, error) {
  56. request := &resolveVolumeNameRequest{
  57. VolumeName: volumeName,
  58. TenantDomain: tenant,
  59. }
  60. var response volumeUUID
  61. if err := client.sendRequest("resolveVolumeName", request, &response); err != nil {
  62. return "", err
  63. }
  64. return response.VolumeUUID, nil
  65. }
  66. // DeleteVolume deletes a Quobyte volume
  67. func (client *QuobyteClient) DeleteVolume(UUID string) error {
  68. return client.sendRequest(
  69. "deleteVolume",
  70. &volumeUUID{
  71. VolumeUUID: UUID,
  72. },
  73. nil)
  74. }
  75. // DeleteVolumeByName deletes a volume by a given name
  76. func (client *QuobyteClient) DeleteVolumeByName(volumeName, tenant string) error {
  77. uuid, err := client.ResolveVolumeNameToUUID(volumeName, tenant)
  78. if err != nil {
  79. return err
  80. }
  81. return client.DeleteVolume(uuid)
  82. }
  83. // GetClientList returns a list of all active clients
  84. func (client *QuobyteClient) GetClientList(tenant string) (GetClientListResponse, error) {
  85. request := &getClientListRequest{
  86. TenantDomain: tenant,
  87. }
  88. var response GetClientListResponse
  89. if err := client.sendRequest("getClientListRequest", request, &response); err != nil {
  90. return response, err
  91. }
  92. return response, nil
  93. }
  94. // SetVolumeQuota sets a Quota to the specified Volume
  95. func (client *QuobyteClient) SetVolumeQuota(volumeUUID string, quotaSize uint64) error {
  96. request := &setQuotaRequest{
  97. Quotas: []*quota{
  98. &quota{
  99. Consumer: []*consumingEntity{
  100. &consumingEntity{
  101. Type: "VOLUME",
  102. Identifier: volumeUUID,
  103. },
  104. },
  105. Limits: []*resource{
  106. &resource{
  107. Type: "LOGICAL_DISK_SPACE",
  108. Value: quotaSize,
  109. },
  110. },
  111. },
  112. },
  113. }
  114. return client.sendRequest("setQuota", request, nil)
  115. }
  116. // GetTenant returns the Tenant configuration for all specified tenants
  117. func (client *QuobyteClient) GetTenant(tenantIDs []string) (GetTenantResponse, error) {
  118. request := &getTenantRequest{TenantIDs: tenantIDs}
  119. var response GetTenantResponse
  120. err := client.sendRequest("getTenant", request, &response)
  121. if err != nil {
  122. return response, err
  123. }
  124. return response, nil
  125. }
  126. // GetTenantMap returns a map that contains all tenant names and there ID's
  127. func (client *QuobyteClient) GetTenantMap() (map[string]string, error) {
  128. result := map[string]string{}
  129. response, err := client.GetTenant([]string{})
  130. if err != nil {
  131. return result, err
  132. }
  133. for _, tenant := range response.Tenants {
  134. result[tenant.Name] = tenant.TenantID
  135. }
  136. return result, nil
  137. }
  138. // SetTenant creates a Tenant with the specified name
  139. func (client *QuobyteClient) SetTenant(tenantName string) (string, error) {
  140. request := &setTenantRequest{
  141. &TenantDomainConfiguration{
  142. Name: tenantName,
  143. },
  144. retryPolicy{client.GetAPIRetryPolicy()},
  145. }
  146. var response setTenantResponse
  147. err := client.sendRequest("setTenant", request, &response)
  148. if err != nil {
  149. return "", err
  150. }
  151. return response.TenantID, nil
  152. }
  153. // IsValidUUID Validates given uuid
  154. func IsValidUUID(uuid string) bool {
  155. r := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
  156. return r.MatchString(uuid)
  157. }
  158. // ResolveTenantNameToUUID Returns UUID for given name, error if not found.
  159. func (client *QuobyteClient) ResolveTenantNameToUUID(name string) (string, error) {
  160. request := &resolveTenantNameRequest{
  161. TenantName: name,
  162. }
  163. var response resolveTenantNameResponse
  164. err := client.sendRequest("resolveTenantName", request, &response)
  165. if err != nil {
  166. return "", err
  167. }
  168. return response.TenantID, nil
  169. }