Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import by name for v2 ingest budgets #474

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions sumologic/resource_sumologic_ingest_budget_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ func resourceSumologicIngestBudgetV2Update(d *schema.ResourceData, meta interfac
return resourceSumologicIngestBudgetV2Read(d, meta)
}

func resourceSumologicIngestBudgetV2Import(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
c := meta.(*Client)

name := d.Id()

ingestBudgetV2, err := c.FindIngestBudgetV2(name)

if err != nil {
return nil, err
}

d.SetId(ingestBudgetV2.ID)

return []*schema.ResourceData{d}, nil
}

func resourceToIngestBudgetV2(d *schema.ResourceData) IngestBudgetV2 {

return IngestBudgetV2{
Expand Down
41 changes: 41 additions & 0 deletions sumologic/sumologic_ingest_budget_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,47 @@ func (s *Client) UpdateIngestBudgetV2(ingestBudgetV2 IngestBudgetV2) error {

}

func (s *Client) FindIngestBudgetV2(name string) (*IngestBudget, error) {
type IngestBudgetV2List struct {
Next string `json:"next"`
Data []IngestBudget `json:"data"`
}

next := ""

for {
body, _, err := s.Get(fmt.Sprintf("v2/ingestBudgets?next=%s", next))
if err != nil {
return nil, err
}

if body == nil {
return nil, nil
}

var response IngestBudgetV2List

err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}

for _, budgetV2 := range response.Data {
if budgetV2.Name == name {
return &budgetV2, nil
}
}

if response.Next == "" {
break
}

next = response.Next
}

return nil, fmt.Errorf("unable to find ingest budget '%s'", name)
}

type IngestBudgetV2 struct {
AuditThreshold int `json:"auditThreshold,omitempty"`
Action string `json:"action"`
Expand Down