diff --git a/client/project.go b/client/project.go index 39d1a85..d27f678 100644 --- a/client/project.go +++ b/client/project.go @@ -1,6 +1,7 @@ package client import ( + "fmt" "encoding/json" "log" "strconv" @@ -75,20 +76,47 @@ func (client *Client) UpdateStorageQuota(d *schema.ResourceData) (err error) { if jsonData.QuotaPerProjectEnable.Value == true { if d.HasChange("storage_quota") { - quotaID := "/quotas/" + strings.Replace(d.Id(), "/projects", "", -1) - - storage := d.Get("storage_quota").(int) - if storage > 0 { - storage *= 1073741824 // GB - } - quota := models.Hard{ - Storage: int64(storage), - } - body := models.StorageQuota{quota} - - _, _, err := client.SendRequest("PUT", quotaID, body, 200) - if err != nil { - return err + projectID := strings.Replace(d.Id(), "/projects/", "", -1) + page := 1 + + for { + quotasPath := fmt.Sprintf("/quotas/?page=%d&page_size=100", page) + + resp, _, err := client.SendRequest("GET", quotasPath, nil, 200) + if err != nil { + return err + } + + var quotaResponse []models.QuotaResponse + if err := json.Unmarshal([]byte(resp), "aResponse); err != nil { + return err + } + + if len(quotaResponse) == 0 { + return nil + } + + for _, q := range quotaResponse { + pid := strconv.Itoa(q.Ref.ID) + if pid == projectID { + quotaID := "/quotas/" + strconv.Itoa(q.ID) + storage := d.Get("storage_quota").(int) + if storage > 0 { + storage *= 1073741824 // GB + } + quota := models.Hard{ + Storage: int64(storage), + } + body := models.StorageQuota{quota} + + _, _, err = client.SendRequest("PUT", quotaID, body, 200) + if err != nil { + return err + } + break + } + } + page++ } } diff --git a/models/quota.go b/models/quota.go index 4ecf145..2789627 100644 --- a/models/quota.go +++ b/models/quota.go @@ -1,8 +1,25 @@ package models +import "time" + type StorageQuota struct { Hard Hard `json:"hard"` } type Hard struct { Storage int64 `json:"storage"` } +type Used struct { + Storage int64 `json:"storage"` +} +type QuotaResponse struct { + CreationTime time.Time `json:"creation_time"` + Hard Hard `json:"hard"` + ID int `json:"id"` + Ref struct { + ID int `json:"id"` + Name string `json:"name"` + Owner string `json:"owner"` + } `json:"ref"` + UpdateTime time.Time `json:"update_time"` + Used Used `json:"used"` +}