Skip to content

Commit

Permalink
rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
RobsonSutton committed Dec 6, 2022
1 parent 65fb740 commit 11777b3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
6 changes: 4 additions & 2 deletions internal/clients/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (a *ApiClient) PutWatch(ctx context.Context, watch *models.Watch) diag.Diag
body := a.es.Watcher.PutWatch.WithBody(bytes.NewReader(watchBytes))
active := a.es.Watcher.PutWatch.WithActive(watch.Active)

res, err := a.es.Watcher.PutWatch(watch.WatchID, body, active, a.es.Watcher.PutWatch.WithContext(ctx))
res, err := a.es.Watcher.PutWatch(watch.WatchID, active, body, a.es.Watcher.PutWatch.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -49,12 +49,14 @@ func (a *ApiClient) GetWatch(ctx context.Context, watchID string) (*models.Watch
}

watch := make(map[string]models.Watch)
if err := json.NewDecoder(res.Body).Decode(&watch); err != nil {
watchBody := make(map[string]interface{})
if err := json.NewDecoder(res.Body).Decode(&watchBody); err != nil {
return nil, diag.FromErr(err)
}

if watch, ok := watch[watchID]; ok {
watch.WatchID = watchID
watch.Body = watchBody
return &watch, diags
}

Expand Down
13 changes: 10 additions & 3 deletions internal/elasticsearch/watcher/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package watcher
import (
"context"
"encoding/json"
"strings"

"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/models"
Expand Down Expand Up @@ -72,7 +71,7 @@ func resourceWatchPut(ctx context.Context, d *schema.ResourceData, meta interfac
}

watchBody := make(map[string]interface{})
if err := json.NewDecoder(strings.NewReader(d.Get("body").(string))).Decode(&watchBody); err != nil {
if err := json.Unmarshal([]byte(d.Get("body").(string)), &watchBody); err != nil {
return diag.FromErr(err)
}

Expand Down Expand Up @@ -115,10 +114,18 @@ func resourceWatchRead(ctx context.Context, d *schema.ResourceData, meta interfa
if err := d.Set("active", watch.Active); err != nil {
return diag.FromErr(err)
}
if err := d.Set("body", watch.Body); err != nil {
body, err := json.Marshal(watch.Body)
if err != nil {
return diag.FromErr(err)
}
if err := d.Set("body", string(body)); err != nil {
return diag.FromErr(err)
}

println(watch.WatchID)
println(watch.Active)
println(watch.Body)

return nil
}

Expand Down
5 changes: 2 additions & 3 deletions internal/elasticsearch/watcher/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestResourceWatch(t *testing.T) {
Config: testAccResourceWatchCreate(watchID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("elasticstack_elasticsearch_watcher_watch.test", "watch_id", watchID),
resource.TestCheckResourceAttr("elasticstack_elasticsearch_watcher_watch.test", "active", "false"),
resource.TestCheckResourceAttr("elasticstack_elasticsearch_watcher_watch.test", "body", `"trigger":{"schedule":{"0 0/1 * * * ?"}}`),
//resource.TestCheckResourceAttr("elasticstack_elasticsearch_watcher_watch.test", "active", "false"),
resource.TestCheckResourceAttr("elasticstack_elasticsearch_watcher_watch.test", "body", `{"trigger":{"schedule":{"0 0/1 * * * ?"}}}`),
),
},
},
Expand All @@ -38,7 +38,6 @@ func testAccResourceWatchCreate(watchID string) string {
}
resource "elasticstack_elasticsearch_watcher_watch" "test" {
watch_id = "%s"
active = false
body = <<EOF
{
"trigger" : {
Expand Down

0 comments on commit 11777b3

Please sign in to comment.