Repository: honeycombio/terraform-provider-honeycombio
Pull request #185 (2022-07-13 18:14:43)

[datasets] Add additional fields description and ExpandJSON Depth
Adding the following field for Honeycomb Datasets:
- `description`
-  `ExpandJSONDepth`

These changes are currently be added to the shep API seperately.

Asana Tracking:
https://app.asana.com/0/1202083175313322/1202555362164075

codecov-commenter: # [Codecov](https://codecov.io/gh/honeycombio/terraform-provider-honeycombio/pull/185?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=honeycombio) Report
> Merging [#185](https://codecov.io/gh/honeycombio/terraform-provider-honeycombio/pull/185?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=honeycombio) (47a0a0d) into [main](https://codecov.io/gh/honeycombio/terraform-provider-honeycombio/commit/b9d024ea5382f3f5a8402deecf1be26de64525c1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=honeycombio) (b9d024e) will **decrease** coverage by `0.32%`.
> The diff coverage is `63.63%`.

```diff
@@            Coverage Diff             @@
##             main     #185      +/-   ##
==========================================
- Coverage   84.62%   84.29%   -0.33%     
==========================================
  Files          38       38              
  Lines        2926     2968      +42     
==========================================
+ Hits         2476     2502      +26     
- Misses        357      373      +16     
  Partials       93       93              
```


| [Impacted Files](https://codecov.io/gh/honeycombio/terraform-provider-honeycombio/pull/185?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=honeycombio) | Coverage Δ | |
|---|---|---|
| [honeycombio/resource\_dataset.go](https://codecov.io/gh/honeycombio/terraform-provider-honeycombio/pull/185/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=honeycombio#diff-aG9uZXljb21iaW8vcmVzb3VyY2VfZGF0YXNldC5nbw==) | `70.23% <60.00%> (-10.20%)` | :arrow_down: |
| [client/dataset.go](https://codecov.io/gh/honeycombio/terraform-provider-honeycombio/pull/185/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=honeycombio#diff-Y2xpZW50L2RhdGFzZXQuZ28=) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=honeycombio).


Patch:
diff --git a/client/dataset.go b/client/dataset.go
index aa61afd..b0ba9a1 100644
--- a/client/dataset.go
+++ b/client/dataset.go
@@ -20,6 +20,10 @@ type Datasets interface {
 	// Create a new dataset. Only name should be set when creating a dataset,
 	// all other fields are ignored.
 	Create(ctx context.Context, dataset *Dataset) (*Dataset, error)
+
+	// Update an existing dataset. Missing (optional) fields will set to their
+	// respective defaults and not the currently existing values.
+	Update(ctx context.Context, dataset *Dataset) (*Dataset, error)
 }
 
 // datasets implements Datasets.
@@ -34,9 +38,10 @@ var _ Datasets = (*datasets)(nil)
 //
 // API docs: https://docs.honeycomb.io/api/dataset
 type Dataset struct {
-	Name string `json:"name"`
-	Slug string `json:"slug,omitempty"`
-
+	Name            string `json:"name"`
+	Description     string `json:"description,omitempty"`
+	Slug            string `json:"slug,omitempty"`
+	ExpandJSONDepth int    `json:"expand_json_depth,omitempty"`
 	// Read only
 	LastWrittenAt time.Time `json:"last_written_at,omitempty"`
 	CreatedAt     time.Time `json:"created_at,omitempty"`
@@ -59,3 +64,9 @@ func (s datasets) Create(ctx context.Context, data *Dataset) (*Dataset, error) {
 	err := s.client.performRequest(ctx, "POST", "/1/datasets", data, &dataset)
 	return &dataset, err
 }
+
+func (s datasets) Update(ctx context.Context, data *Dataset) (*Dataset, error) {
+	var dataset Dataset
+	err := s.client.performRequest(ctx, "POST", "/1/datasets", data, &dataset)
+	return &dataset, err
+}
diff --git a/docs/resources/dataset.md b/docs/resources/dataset.md
index 6bb7ad2..8843f4f 100644
--- a/docs/resources/dataset.md
+++ b/docs/resources/dataset.md
@@ -11,6 +11,7 @@ Creates a dataset.
 ```hcl
 resource "honeycombio_dataset" "my_dataset" {
   name = "My dataset"
+  description = "buzzing with data"
 }
 ```
 
@@ -19,6 +20,8 @@ resource "honeycombio_dataset" "my_dataset" {
 The following arguments are supported:
 
 * `name` - (Required) The name of the dataset.
+* `description` - (Optional) A longer description for dataset.
+* `expand_json_depth` - (Optional) The maximum unpacking depth of nested JSON fields.
 
 ## Attribute Reference
 
diff --git a/honeycombio/resource_dataset.go b/honeycombio/resource_dataset.go
index ba50ca9..b7f6256 100644
--- a/honeycombio/resource_dataset.go
+++ b/honeycombio/resource_dataset.go
@@ -14,7 +14,7 @@ func newDataset() *schema.Resource {
 	return &schema.Resource{
 		CreateContext: resourceDatasetCreate,
 		ReadContext:   resourceDatasetRead,
-		UpdateContext: nil,
+		UpdateContext: resourceDatasetUpdate,
 		DeleteContext: schema.NoopContext,
 
 		Schema: map[string]*schema.Schema{
@@ -24,10 +24,20 @@ func newDataset() *schema.Resource {
 				ForceNew:     true,
 				ValidateFunc: validation.StringLenBetween(1, 255),
 			},
+			"description": {
+				Type:         schema.TypeString,
+				Optional:     true,
+				ValidateFunc: validation.StringLenBetween(0, 1024),
+			},
 			"slug": {
 				Type:     schema.TypeString,
 				Computed: true,
 			},
+			"expand_json_depth": {
+				Type:         schema.TypeInt,
+				Optional:     true,
+				ValidateFunc: validation.IntBetween(0, 10),
+			},
 			"created_at": {
 				Type:     schema.TypeString,
 				Computed: true,
@@ -47,8 +57,13 @@ func newDataset() *schema.Resource {
 func resourceDatasetCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
 	client := meta.(*honeycombio.Client)
 
+	name := d.Get("name").(string)
+	description := d.Get("description").(string)
+	expandJSONDepth := d.Get("expand_json_depth").(int)
 	data := &honeycombio.Dataset{
-		Name: d.Get("name").(string),
+		Name:            name,
+		Description:     description,
+		ExpandJSONDepth: expandJSONDepth,
 	}
 	dataset, err := client.Datasets.Create(ctx, data)
 	if err != nil {
@@ -72,8 +87,30 @@ func resourceDatasetRead(ctx context.Context, d *schema.ResourceData, meta inter
 
 	d.SetId(dataset.Slug)
 	d.Set("name", dataset.Name)
+	d.Set("description", dataset.Description)
+	d.Set("expand_json_depth", dataset.ExpandJSONDepth)
 	d.Set("slug", dataset.Slug)
 	d.Set("created_at", dataset.CreatedAt.UTC().Format(time.RFC3339))
 	d.Set("last_written_at", dataset.CreatedAt.UTC().Format(time.RFC3339))
 	return nil
 }
+
+func resourceDatasetUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
+	client := meta.(*honeycombio.Client)
+
+	name := d.Get("name").(string)
+	description := d.Get("description").(string)
+	expandJSONDepth := d.Get("expand_json_depth").(int)
+	data := &honeycombio.Dataset{
+		Name:            name,
+		Description:     description,
+		ExpandJSONDepth: expandJSONDepth,
+	}
+	dataset, err := client.Datasets.Update(ctx, data)
+	if err != nil {
+		return diag.FromErr(err)
+	}
+
+	d.SetId(dataset.Slug)
+	return resourceDatasetRead(ctx, d, meta)
+}


Test patch:
diff --git a/client/dataset_test.go b/client/dataset_test.go
index 1ba71fa..686c036 100644
--- a/client/dataset_test.go
+++ b/client/dataset_test.go
@@ -12,12 +12,28 @@ func TestDatasets(t *testing.T) {
 
 	c := newTestClient(t)
 	datasetName := testDataset(t)
+	updatedDescription := "buzzing with data"
+	updatedExpandJSONDepth := 3
 
 	currentDataset := &Dataset{
 		Name: datasetName,
 		Slug: urlEncodeDataset(datasetName),
 	}
 
+	// create a new dataset with the parameters above
+	t.Run("Create", func(t *testing.T) {
+		d, err := c.Datasets.Create(ctx, currentDataset)
+		assert.NoError(t, err)
+		assert.NotNil(t, d.LastWrittenAt, "last written at is empty")
+		assert.NotNil(t, d.CreatedAt, "created at is empty")
+		// copy dynamic fields before asserting - will be skipped if expected dataset not found
+		if d.Name == currentDataset.Name {
+			currentDataset.LastWrittenAt = d.LastWrittenAt
+			currentDataset.CreatedAt = d.CreatedAt
+		}
+		assert.Equal(t, *currentDataset, *d)
+	})
+
 	t.Run("List", func(t *testing.T) {
 		d, err := c.Datasets.List(ctx)
 
@@ -71,4 +87,30 @@ func TestDatasets(t *testing.T) {
 
 		assert.Equal(t, currentDataset, d)
 	})
+
+	t.Run("Update", func(t *testing.T) {
+		updateDataset := &Dataset{
+			Name:            datasetName,
+			Slug:            urlEncodeDataset(datasetName),
+			Description:     updatedDescription,
+			ExpandJSONDepth: updatedExpandJSONDepth,
+		}
+		d, err := c.Datasets.Update(ctx, updateDataset)
+
+		assert.NoError(t, err)
+
+		assert.NotNil(t, d.LastWrittenAt, "last written at is empty")
+		assert.NotNil(t, d.CreatedAt, "created at is empty")
+		// copy dynamic fields before asserting - will be skipped if expected dataset not found
+		if currentDataset.Name == updateDataset.Name {
+			currentDataset.Description = updateDataset.Description
+			currentDataset.ExpandJSONDepth = updateDataset.ExpandJSONDepth
+			currentDataset.LastWrittenAt = updateDataset.LastWrittenAt
+			currentDataset.CreatedAt = updateDataset.CreatedAt
+		}
+
+		assert.Equal(t, *updateDataset, *currentDataset)
+		//assert.Equal(t, currentDataset.Description, updatedDescription)
+		//assert.Equal(t, currentDataset.ExpandJSONDepth, updatedExpandJSONDepth)
+	})
 }
diff --git a/honeycombio/resource_dataset_test.go b/honeycombio/resource_dataset_test.go
index 8b3f545..85ed873 100644
--- a/honeycombio/resource_dataset_test.go
+++ b/honeycombio/resource_dataset_test.go
@@ -8,11 +8,17 @@ import (
 
 	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
 	"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
+	honeycombio "github.com/honeycombio/terraform-provider-honeycombio/client"
 	"github.com/stretchr/testify/assert"
 )
 
 func TestAccHoneycombioDataset_basic(t *testing.T) {
-	testDataset := testAccDataset()
+
+	testDatasetName := testAccDataset()
+
+	testDataset := honeycombio.Dataset{
+		Name: testDatasetName,
+	}
 
 	resource.Test(t, resource.TestCase{
 		PreCheck:          testAccPreCheck(t),
@@ -22,24 +28,28 @@ func TestAccHoneycombioDataset_basic(t *testing.T) {
 				Config: testAccDatasetConfig(testDataset),
 				Check: resource.ComposeTestCheckFunc(
 					testAccCheckDatasetExists(t, "honeycombio_dataset.test", testDataset),
-					resource.TestCheckResourceAttr("honeycombio_dataset.test", "name", testDataset),
-					resource.TestCheckResourceAttr("honeycombio_dataset.test", "slug", urlEncodeDataset(testDataset)),
+					resource.TestCheckResourceAttr("honeycombio_dataset.test", "name", testDataset.Name),
+					resource.TestCheckResourceAttr("honeycombio_dataset.test", "description", testDataset.Description),
+					resource.TestCheckResourceAttr("honeycombio_dataset.test", "slug", urlEncodeDataset(testDataset.Name)),
+					resource.TestCheckResourceAttr("honeycombio_dataset.test", "expand_json_depth", fmt.Sprintf("%d", testDataset.ExpandJSONDepth)),
 				),
 			},
 		},
 	})
 }
 
-func testAccDatasetConfig(dataset string) string {
+func testAccDatasetConfig(dataset honeycombio.Dataset) string {
 	return fmt.Sprintf(`
 resource "honeycombio_dataset" "test" {
   name = "%s"
-}`, dataset)
+  description = "%s"
+  expand_json_depth = "%d"
+}`, dataset.Name, dataset.Description, dataset.ExpandJSONDepth)
 }
 
 // testAccCheckDatasetExists queries the API to verify the Dataset exists and
 // matches with the Terraform state.
-func testAccCheckDatasetExists(t *testing.T, name, testDataset string) resource.TestCheckFunc {
+func testAccCheckDatasetExists(t *testing.T, name string, testDataset honeycombio.Dataset) resource.TestCheckFunc {
 	return func(s *terraform.State) error {
 		resourceState, ok := s.RootModule().Resources[name]
 		if !ok {
@@ -52,8 +62,8 @@ func testAccCheckDatasetExists(t *testing.T, name, testDataset string) resource.
 			return fmt.Errorf("could not retrieve dataset: %w", err)
 		}
 
-		assert.Equal(t, testDataset, d.Name)
-		assert.Equal(t, urlEncodeDataset(testDataset), d.Slug)
+		assert.Equal(t, testDataset.Name, d.Name)
+		assert.Equal(t, urlEncodeDataset(d.Name), d.Slug)
 
 		return nil
 	}