From b9943750adf51913c0c0650eae9b1d5c80a5f654 Mon Sep 17 00:00:00 2001 From: Chris Gavin Date: Mon, 2 Oct 2023 21:01:46 +1100 Subject: [PATCH 1/2] Map additional fields for BigSegments. --- launchdarkly/keys.go | 2 + launchdarkly/resource_launchdarkly_segment.go | 12 ++++-- .../resource_launchdarkly_segment_test.go | 43 +++++++++++++++++++ launchdarkly/segments_helper.go | 10 +++++ 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/launchdarkly/keys.go b/launchdarkly/keys.go index 138f14d0..3c23b29e 100644 --- a/launchdarkly/keys.go +++ b/launchdarkly/keys.go @@ -106,6 +106,8 @@ const ( TOKEN = "token" TRACK_EVENTS = "track_events" TRIGGER_URL = "trigger_url" + UNBOUNDED = "unbounded" + UNBOUNDED_CONTEXT_KIND = "unbounded_context_kind" UNIT = "unit" URL = "url" URLS = "urls" diff --git a/launchdarkly/resource_launchdarkly_segment.go b/launchdarkly/resource_launchdarkly_segment.go index f1fd59e0..68011eb8 100644 --- a/launchdarkly/resource_launchdarkly_segment.go +++ b/launchdarkly/resource_launchdarkly_segment.go @@ -62,12 +62,16 @@ func resourceSegmentCreate(ctx context.Context, d *schema.ResourceData, metaRaw description := d.Get(DESCRIPTION).(string) segmentName := d.Get(NAME).(string) tags := stringsFromResourceData(d, TAGS) + unbounded := d.Get(UNBOUNDED).(bool) + unboundedContextKind := d.Get(UNBOUNDED_CONTEXT_KIND).(string) segment := ldapi.SegmentBody{ - Name: segmentName, - Key: key, - Description: &description, - Tags: tags, + Name: segmentName, + Key: key, + Description: &description, + Tags: tags, + Unbounded: &unbounded, + UnboundedContextKind: &unboundedContextKind, } _, _, err := client.ld.SegmentsApi.PostSegment(client.ctx, projectKey, envKey).SegmentBody(segment).Execute() diff --git a/launchdarkly/resource_launchdarkly_segment_test.go b/launchdarkly/resource_launchdarkly_segment_test.go index 96fb46b9..eecefa0c 100644 --- a/launchdarkly/resource_launchdarkly_segment_test.go +++ b/launchdarkly/resource_launchdarkly_segment_test.go @@ -147,6 +147,20 @@ resource "launchdarkly_segment" "test" { context_kind = "eanies" } }` + + testAccSegmentCreateWithUnbounded = ` +resource "launchdarkly_segment" "test" { + key = "segmentKey1" + project_key = launchdarkly_project.test.key + env_key = "test" + name = "segment name" + description = "segment description" + tags = ["segmentTag1", "segmentTag2"] + included = ["user1", "user2"] + excluded = ["user3", "user4"] + unbounded = true + unbounded_context_kind = "device" +}` ) func TestAccSegment_CreateAndUpdate(t *testing.T) { @@ -276,6 +290,35 @@ func TestAccSegment_CreateAndUpdate(t *testing.T) { ImportState: true, ImportStateVerify: true, }, + { + Config: withRandomProject(projectKey, testAccSegmentCreateWithUnbounded), + Check: resource.ComposeTestCheckFunc( + testAccCheckProjectExists("launchdarkly_project.test"), + testAccCheckSegmentExists(resourceName), + resource.TestCheckResourceAttr(resourceName, KEY, "segmentKey1"), + resource.TestCheckResourceAttr(resourceName, PROJECT_KEY, projectKey), + resource.TestCheckResourceAttr(resourceName, ENV_KEY, "test"), + resource.TestCheckResourceAttr(resourceName, NAME, "segment name"), + resource.TestCheckResourceAttr(resourceName, DESCRIPTION, "segment description"), + resource.TestCheckResourceAttr(resourceName, "tags.#", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.0", "segmentTag1"), + resource.TestCheckResourceAttr(resourceName, "tags.1", "segmentTag2"), + resource.TestCheckResourceAttr(resourceName, "included.#", "2"), + resource.TestCheckResourceAttr(resourceName, "included.0", "user1"), + resource.TestCheckResourceAttr(resourceName, "included.1", "user2"), + resource.TestCheckResourceAttr(resourceName, "excluded.#", "2"), + resource.TestCheckResourceAttr(resourceName, "excluded.0", "user3"), + resource.TestCheckResourceAttr(resourceName, "excluded.1", "user4"), + resource.TestCheckResourceAttrSet(resourceName, CREATION_DATE), + resource.TestCheckResourceAttr(resourceName, UNBOUNDED, "true"), + resource.TestCheckResourceAttr(resourceName, UNBOUNDED_CONTEXT_KIND, "device"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, }, }) } diff --git a/launchdarkly/segments_helper.go b/launchdarkly/segments_helper.go index 08e2f72c..462f4962 100644 --- a/launchdarkly/segments_helper.go +++ b/launchdarkly/segments_helper.go @@ -106,6 +106,16 @@ func segmentRead(ctx context.Context, d *schema.ResourceData, raw interface{}, i return diag.Errorf("failed to set tags on segment with key %q: %v", segmentKey, err) } + err = d.Set(UNBOUNDED, segment.Unbounded) + if err != nil { + return diag.Errorf("failed to set unbounded on segment with key %q: %v", segmentKey, err) + } + + err = d.Set(UNBOUNDED_CONTEXT_KIND, segment.UnboundedContextKind) + if err != nil { + return diag.Errorf("failed to set unboundedContextKind on segment with key %q: %v", segmentKey, err) + } + err = d.Set(INCLUDED, segment.Included) if err != nil { return diag.Errorf("failed to set included on segment with key %q: %v", segmentKey, err) From ae98ac2b747efbbb9727670533ac248af812abfa Mon Sep 17 00:00:00 2001 From: Chris Gavin Date: Mon, 2 Oct 2023 21:04:26 +1100 Subject: [PATCH 2/2] Update docs for BigSegment fields. --- docs/data-sources/segment.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/data-sources/segment.md b/docs/data-sources/segment.md index 4641d1d0..74a95e45 100644 --- a/docs/data-sources/segment.md +++ b/docs/data-sources/segment.md @@ -41,6 +41,10 @@ In addition to the arguments above, the resource exports the following attribute - `tags` - Set of tags for the segment. +- `unbounded` - Whether to create a standard segment (false) or a BigSegment (true). + +- `unbounded_context_kind` - For Big Segments, the targeted context kind. + - `included` - List of user keys included in the segment. - `excluded` - List of user keys excluded from the segment.