Skip to content

Commit

Permalink
Merge pull request #24 from quobyte/update_api
Browse files Browse the repository at this point in the history
Full API for Quobyte 2.x
  • Loading branch information
venkatsc authored Jul 15, 2020
2 parents a7c71d2 + 31297e2 commit d57088b
Show file tree
Hide file tree
Showing 6 changed files with 3,700 additions and 180 deletions.
33 changes: 25 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,41 @@ import (
)

func main() {
client := quobyte_api.NewQuobyteClient("http://apiserver:7860", "user", "password")
url := flag.String("url", "", "URL of Quobyte API")
username := flag.String("username", "", "username")
password := flag.String("password", "", "password")
flag.Parse()

if *url == "" || *username == "" || *password == "" {
flag.PrintDefaults()
os.Exit(1)
}

client := quobyte_api.NewQuobyteClient(*url, *username, *password)
client.SetAPIRetryPolicy(quobyte_api.RetryInfinitely) // Default quobyte_api.RetryInteractive
req := &quobyte_api.CreateVolumeRequest{
Name: "MyVolume",
RootUserID: "root",
RootGroupID: "root",
TenantId: "32edb36d-badc-affe-b44a-4ab749af4d9a",
RootUserId: "root",
RootGroupId: "root",
ConfigurationName: "BASE",
Labels: []quobyte_api.Label{
Label: []*quobyte_api.Label{
{Name: "label1", Value: "value1"},
{Name: "label2", Value: "value2"},
},
}

volumeUUID, err := client.CreateVolume(req)
response, err := client.CreateVolume(req)
if err != nil {
log.Fatalf("Error: %v", err)
}

capactiy := int64(1024 * 1024 * 1024)
err = client.SetVolumeQuota(response.VolumeUuid, uint64(capactiy))
if err != nil {
log.Fatalf("Error:", err)
log.Fatalf("Error: %v", err)
}

log.Printf("%s", volumeUUID)
log.Printf("%s", response.VolumeUuid)
}
```
```
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/quobyte/api

go 1.13
Empty file added go.sum
Empty file.
124 changes: 26 additions & 98 deletions quobyte.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Package quobyte represents a golang API for the Quobyte Storage System
package quobyte

import (
Expand All @@ -8,10 +7,8 @@ import (

// retry policy codes
const (
RetryNever string = "NEVER"
RetryInteractive string = "INTERACTIVE"
RetryInfinitely string = "INFINITELY"
RetryOncePerTarget string = "ONCE_PER_TARGET"
RetryInteractive string = "INTERACTIVE"
RetryInfinitely string = "INFINITELY"
)

var UUIDValidator = 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}$")
Expand Down Expand Up @@ -47,25 +44,9 @@ func NewQuobyteClient(url string, username string, password string) *QuobyteClie
}
}

// CreateVolume creates a new Quobyte volume. Its root directory will be owned by given user and group
func (client QuobyteClient) CreateVolume(request *CreateVolumeRequest) (string, error) {
var response volumeUUID
tenantUUID, err := client.GetTenantUUID(request.TenantID)
if err != nil {
return "", err
}
request.TenantID = tenantUUID

if err = client.sendRequest("createVolume", request, &response); err != nil {
return "", err
}

return response.VolumeUUID, nil
}

// GetVolumeUUID resolves the volumeUUID for the given volume and tenant name.
// This method should be used when it is not clear if the given string is volume UUID or Name.
func (client QuobyteClient) GetVolumeUUID(volume, tenant string) (string, error) {
func (client *QuobyteClient) GetVolumeUUID(volume, tenant string) (string, error) {
if len(volume) != 0 && !IsValidUUID(volume) {
tenantUUID, err := client.GetTenantUUID(tenant)
if err != nil {
Expand All @@ -83,7 +64,7 @@ func (client QuobyteClient) GetVolumeUUID(volume, tenant string) (string, error)

// GetTenantUUID resolves the tenatnUUID for the given name
// This method should be used when it is not clear if the given string is Tenant UUID or Name.
func (client QuobyteClient) GetTenantUUID(tenant string) (string, error) {
func (client *QuobyteClient) GetTenantUUID(tenant string) (string, error) {
if len(tenant) != 0 && !IsValidUUID(tenant) {
tenantUUID, err := client.ResolveTenantNameToUUID(tenant)
if err != nil {
Expand All @@ -96,16 +77,16 @@ func (client QuobyteClient) GetTenantUUID(tenant string) (string, error) {

// ResolveVolumeNameToUUID resolves a volume name to a UUID
func (client *QuobyteClient) ResolveVolumeNameToUUID(volumeName, tenant string) (string, error) {
request := &resolveVolumeNameRequest{
request := &ResolveVolumeNameRequest{
VolumeName: volumeName,
TenantDomain: tenant,
}
var response volumeUUID
var response ResolveVolumeNameResponse
if err := client.sendRequest("resolveVolumeName", request, &response); err != nil {
return "", err
}

return response.VolumeUUID, nil
return response.VolumeUuid, nil
}

// DeleteVolumeByResolvingNamesToUUID deletes the volume by resolving the volume name and tenant name to
Expand All @@ -117,17 +98,8 @@ func (client *QuobyteClient) DeleteVolumeByResolvingNamesToUUID(volume, tenant s
return err
}

return client.DeleteVolume(volumeUUID)
}

// DeleteVolume deletes a Quobyte volume
func (client *QuobyteClient) DeleteVolume(UUID string) error {
return client.sendRequest(
"deleteVolume",
&volumeUUID{
VolumeUUID: UUID,
},
nil)
_, err = client.DeleteVolume(&DeleteVolumeRequest{VolumeUuid: volumeUUID})
return err
}

// DeleteVolumeByName deletes a volume by a given name
Expand All @@ -137,37 +109,24 @@ func (client *QuobyteClient) DeleteVolumeByName(volumeName, tenant string) error
return err
}

return client.DeleteVolume(uuid)
}

// GetClientList returns a list of all active clients
func (client *QuobyteClient) GetClientList(tenant string) (GetClientListResponse, error) {
request := &getClientListRequest{
TenantDomain: tenant,
}

var response GetClientListResponse
if err := client.sendRequest("getClientListRequest", request, &response); err != nil {
return response, err
}

return response, nil
_, err = client.DeleteVolume(&DeleteVolumeRequest{VolumeUuid: uuid})
return err
}

// SetVolumeQuota sets a Quota to the specified Volume
func (client *QuobyteClient) SetVolumeQuota(volumeUUID string, quotaSize uint64) error {
request := &setQuotaRequest{
Quotas: []*quota{
&quota{
Consumer: []*consumingEntity{
&consumingEntity{
Type: "VOLUME",
request := &SetQuotaRequest{
Quotas: []*Quota{
&Quota{
Consumer: []*ConsumingEntity{
&ConsumingEntity{
Type: ConsumingEntity_Type_VOLUME,
Identifier: volumeUUID,
},
},
Limits: []*resource{
&resource{
Type: "LOGICAL_DISK_SPACE",
Limits: []*Resource{
&Resource{
Type: Resource_Type_LOGICAL_DISK_SPACE,
Value: quotaSize,
},
},
Expand All @@ -178,68 +137,37 @@ func (client *QuobyteClient) SetVolumeQuota(volumeUUID string, quotaSize uint64)
return client.sendRequest("setQuota", request, nil)
}

// GetTenant returns the Tenant configuration for all specified tenants
func (client *QuobyteClient) GetTenant(tenantIDs []string) (GetTenantResponse, error) {
request := &getTenantRequest{TenantIDs: tenantIDs}

var response GetTenantResponse
err := client.sendRequest("getTenant", request, &response)
if err != nil {
return response, err
}

return response, nil
}

// GetTenantMap returns a map that contains all tenant names and there ID's
func (client *QuobyteClient) GetTenantMap() (map[string]string, error) {
result := map[string]string{}
response, err := client.GetTenant([]string{})
response, err := client.GetTenant(&GetTenantRequest{})

if err != nil {
return result, err
}

for _, tenant := range response.Tenants {
result[tenant.Name] = tenant.TenantID
for _, tenant := range response.Tenant {
result[tenant.Name] = tenant.TenantId
}

return result, nil
}

// SetTenant creates a Tenant with the specified name
func (client *QuobyteClient) SetTenant(tenantName string) (string, error) {
request := &setTenantRequest{
&TenantDomainConfiguration{
Name: tenantName,
},
retryPolicy{client.GetAPIRetryPolicy()},
}

var response setTenantResponse
err := client.sendRequest("setTenant", request, &response)
if err != nil {
return "", err
}

return response.TenantID, nil
}

// IsValidUUID Validates the given uuid
func IsValidUUID(uuid string) bool {
return UUIDValidator.MatchString(uuid)
}

// ResolveTenantNameToUUID Returns UUID for given name, error if not found.
func (client *QuobyteClient) ResolveTenantNameToUUID(name string) (string, error) {
request := &resolveTenantNameRequest{
request := &ResolveTenantNameRequest{
TenantName: name,
}

var response resolveTenantNameResponse
var response ResolveTenantNameResponse
err := client.sendRequest("resolveTenantName", request, &response)
if err != nil {
return "", err
}
return response.TenantID, nil
return response.TenantId, nil
}
16 changes: 8 additions & 8 deletions rpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

func TestSuccesfullEncodeRequest(t *testing.T) {
req := &CreateVolumeRequest{
RootUserID: "root",
RootGroupID: "root",
RootUserId: "root",
RootGroupId: "root",
Name: "test",
}

Expand Down Expand Up @@ -59,15 +59,15 @@ func TestSuccesfullDecodeResponse(t *testing.T) {

res, _ := json.Marshal(expectedResult)

var resp volumeUUID
var resp CreateVolumeResponse
err := decodeResponse(bytes.NewReader(res), &resp)
if err != nil {
t.Log(err)
t.Fail()
}

if "1234" != resp.VolumeUUID {
t.Logf("Expected Volume UUID: %v got %v\n", "1234", resp.VolumeUUID)
if "1234" != resp.VolumeUuid {
t.Logf("Expected Volume UUID: %v got %v\n", "1234", resp.VolumeUuid)
t.Fail()
}
}
Expand All @@ -88,7 +88,7 @@ func TestSuccesfullDecodeResponseWithErrorMessage(t *testing.T) {

res, _ := json.Marshal(expectedResult)

var resp volumeUUID
var resp CreateVolumeResponse
err := decodeResponse(bytes.NewReader(res), &resp)
if err == nil {
t.Log("No error occured")
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestSuccesfullDecodeResponseWithErrorCode(t *testing.T) {

res, _ := json.Marshal(expectedResult)

var resp volumeUUID
var resp CreateVolumeResponse
err := decodeResponse(bytes.NewReader(res), &resp)
if err == nil {
t.Log("No error occured")
Expand All @@ -137,7 +137,7 @@ func TestBadDecodeResponse(t *testing.T) {

res, _ := json.Marshal(expectedResult)

var resp volumeUUID
var resp CreateVolumeResponse
err := decodeResponse(bytes.NewReader(res), &resp)
if err == nil {
t.Log("No error occured")
Expand Down
Loading

0 comments on commit d57088b

Please sign in to comment.