/
resource_quota.go
75 lines (62 loc) · 1.85 KB
/
resource_quota.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package management
import (
"fmt"
"github.com/IBM-Cloud/bluemix-go/bmxerror"
"github.com/IBM-Cloud/bluemix-go/client"
"github.com/IBM-Cloud/bluemix-go/models"
)
type quotaDefinitionQueryResult struct {
QuotaDefinitions []models.QuotaDefinition `json:"resources"`
}
//ErrCodeResourceQuotaDoesnotExist ...
const ErrCodeResourceQuotaDoesnotExist = "ResourceQuotaDoesnotExist"
type ResourceQuotaRepository interface {
// List all quota definitions
List() ([]models.QuotaDefinition, error)
// Query quota definitions having specific name
FindByName(name string) ([]models.QuotaDefinition, error)
// Get quota definition by ID
Get(id string) (*models.QuotaDefinition, error)
}
type resourceQuota struct {
client *client.Client
}
func newResourceQuotaAPI(c *client.Client) ResourceQuotaRepository {
return &resourceQuota{
client: c,
}
}
func (r *resourceQuota) List() ([]models.QuotaDefinition, error) {
resp := quotaDefinitionQueryResult{}
// TODO: change to use pagination if it's available on backend
_, err := r.client.Get("/v1/quota_definitions", &resp)
if err != nil {
return []models.QuotaDefinition{}, err
}
return resp.QuotaDefinitions, nil
}
func (r *resourceQuota) FindByName(name string) ([]models.QuotaDefinition, error) {
allQuotas, err := r.List()
if err != nil {
return []models.QuotaDefinition{}, err
}
quotas := []models.QuotaDefinition{}
for _, quota := range allQuotas {
if quota.Name == name {
quotas = append(quotas, quota)
}
}
if len(quotas) == 0 {
return quotas, bmxerror.New(ErrCodeResourceQuotaDoesnotExist,
fmt.Sprintf("Given quota : %q doesn't exist", name))
}
return quotas, nil
}
func (r *resourceQuota) Get(id string) (*models.QuotaDefinition, error) {
quota := models.QuotaDefinition{}
_, err := r.client.Get("/v1/quota_definitions/"+id, "a)
if err != nil {
return nil, err
}
return "a, nil
}