Skip to content

Commit

Permalink
Ability to define keys and segment for split_definition.treatment (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidji99 authored Jan 23, 2023
1 parent 27007a3 commit 67ed325
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
19 changes: 11 additions & 8 deletions api/api-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/split_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type Treatment struct {
Name *string `json:"name"`
Configurations *string `json:"configurations"`
Description *string `json:"description"`
Keys *string `json:"keys,omitempty"`
Segment []string `json:"segment,omitempty"`
Keys []string `json:"keys,omitempty"`
Segments []string `json:"segments,omitempty"`
}

// Bucket represents a sticky distribution of customers into treatments of a Split.
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/split_definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ explain the difference between each treatment. This attribute block supports the
* `configurations` - (Required) `<string>` Dynamically configure components of your feature (e.g. A button's color or backend API pagination).
This attribute's value must be a valid JSON string.
* `description` - (Optional) `<string>` Description of the treatment.
* `keys` - (Optional) `<list(string)>` List of target key ids.
* `segments` - (Optional) `<list(string)>` List of segments.

### `default_rule`

Expand Down
52 changes: 50 additions & 2 deletions split/resource_split_split_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ func resourceSplitSplitDefinition() *schema.Resource {
Optional: true,
Computed: true,
},

"keys": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},

"segments": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
},
},
},
Expand Down Expand Up @@ -375,6 +391,24 @@ func constructSplitDefinitionRequestOpts(d *schema.ResourceData) (*api.SplitDefi
t.Description = &v
}

if v, ok := vt["keys"]; ok {
vL := v.(*schema.Set).List()
keys := make([]string, 0)
for _, e := range vL {
keys = append(keys, e.(string))
}
t.Keys = keys
}

if v, ok := vt["segments"]; ok {
vL := v.(*schema.Set).List()
segments := make([]string, 0)
for _, e := range vL {
segments = append(segments, e.(string))
}
t.Segments = segments
}

treatments = append(treatments, t)
}

Expand Down Expand Up @@ -484,9 +518,23 @@ func constructSplitDefinitionRequestOpts(d *schema.ResourceData) (*api.SplitDefi
}

func setTreatmentInState(d *schema.ResourceData, sd *api.SplitDefinition) {
treatments := make([]map[string]string, 0)
treatments := make([]map[string]interface{}, 0)
for _, t := range sd.Treatments {
treatments = append(treatments, map[string]string{
treatment := map[string]interface{}{
"name": t.GetName(),
"configurations": t.GetConfigurations(),
"description": t.GetDescription(),
}

if t.HasKeys() {
treatment["keys"] = t.Keys
}

if t.HasSegments() {
treatment["segments"] = t.Segments
}

treatments = append(treatments, map[string]interface{}{
"name": t.GetName(),
"configurations": t.GetConfigurations(),
"description": t.GetDescription(),
Expand Down

0 comments on commit 67ed325

Please sign in to comment.