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

Map additional fields for BigSegments. #187

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions docs/data-sources/segment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions launchdarkly/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 8 additions & 4 deletions launchdarkly/resource_launchdarkly_segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
43 changes: 43 additions & 0 deletions launchdarkly/resource_launchdarkly_segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
},
},
})
}
Expand Down
10 changes: 10 additions & 0 deletions launchdarkly/segments_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down