Skip to content

Commit

Permalink
Merge pull request #212 from haiwu/issue_211
Browse files Browse the repository at this point in the history
fix harbor project quota update bug
  • Loading branch information
wrighbr authored May 12, 2022
2 parents efa380a + eba5bb6 commit baa0015
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
56 changes: 42 additions & 14 deletions client/project.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"fmt"
"encoding/json"
"log"
"strconv"
Expand Down Expand Up @@ -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), &quotaResponse); 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++
}
}

Expand Down
17 changes: 17 additions & 0 deletions models/quota.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit baa0015

Please sign in to comment.