Skip to content

Commit

Permalink
Add pagination when listing immutableTagRules (#444)
Browse files Browse the repository at this point in the history
immutableTagRule ressource didn't handle pagination. This commit fixes
it.
  • Loading branch information
gaglimax authored Jun 5, 2024
1 parent 115d4f7 commit dafd683
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions provider/resource_immutable_tag_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,34 @@ func resourceImmutableTagRuleRead(d *schema.ResourceData, m interface{}) error {

lastSlashIndex := strings.LastIndex(d.Id(), "/")
projectImmutableTagRulePath := d.Id()[0:lastSlashIndex]
tagId, err := strconv.Atoi(d.Id()[lastSlashIndex+1:])
immutableTagRuleId, err := strconv.Atoi(d.Id()[lastSlashIndex+1:])
if err != nil {
return err
}
log.Printf("[DEBUG] Path to immutable tag rules: %+v\n", projectImmutableTagRulePath)

resp, _, _, err := apiClient.SendRequest("GET", projectImmutableTagRulePath, nil, 200)
if err != nil {
return fmt.Errorf("Resource not found %s", projectImmutableTagRulePath)
}

var immutableTagRuleModels []models.ImmutableTagRule
err = json.Unmarshal([]byte(resp), &immutableTagRuleModels)
if err != nil {
return err
page := 1
pageSize := 15
for {
resp, _, _, err := apiClient.SendRequest("GET", fmt.Sprintf("%s?page=%d&page_size=%d", projectImmutableTagRulePath, page, pageSize), nil, 200)
if err != nil {
return fmt.Errorf("Resource not found %s", projectImmutableTagRulePath)
}
var pageModels []models.ImmutableTagRule
err = json.Unmarshal([]byte(resp), &pageModels)
if err != nil {
return err
}
immutableTagRuleModels = append(immutableTagRuleModels, pageModels...)
if len(pageModels) < pageSize {
break
}
page++
}

for _, rule := range immutableTagRuleModels {
if rule.Id == tagId {
log.Printf("[DEBUG] found tag id %d", tagId)
if rule.Id == immutableTagRuleId {
log.Printf("[DEBUG] found tag id %d", immutableTagRuleId)
d.Set("disabled", rule.Disabled)
d.Set("project_id", strings.ReplaceAll(projectImmutableTagRulePath, models.PathImmutableTagRules, ""))

Expand Down

0 comments on commit dafd683

Please sign in to comment.