Skip to content
Open
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
25 changes: 24 additions & 1 deletion api/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ type Splits struct {
type SplitCreateRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Tags []string `json:"tags"`
}

// SplitUpdateRequest represents a request to update a split.
// SplitUpdateRequest represents a request to update a split description.
type SplitUpdateRequest struct {
Description string `json:"description"`
}
Expand All @@ -53,6 +54,13 @@ type SplitTag struct {
Name string `json:"name"`
}

// SplitUpdateFlagRequest represents a request to update a split.
type SplitUpdateFlagRequest struct {
Op string `json:"op"`
Path string `json:"path"`
Value SplitTag `json:"value"`
}

// List all splits.
//
// Reference: https://docs.split.io/reference/list-splits
Expand Down Expand Up @@ -126,3 +134,18 @@ func (s *SplitsService) Delete(workspaceId, splitName string) (*simpleresty.Resp

return response, createErr
}

// Update an existing split.
//
// Reference: https://docs.split.io/reference/update-feature-flag
func (s *SplitsService) UpdateSplit(workspaceId string, splitName string, opts *[]SplitUpdateFlagRequest) (*Split, *simpleresty.Response, error) {
var result Split

splitNameEncoded := url.QueryEscape(splitName)
urlStr := s.client.http.RequestURL("/splits/ws/%s/%s", workspaceId, splitNameEncoded)

// Execute the request
response, updateErr := s.client.patch(urlStr, &result, opts)

return &result, response, updateErr
}
2 changes: 1 addition & 1 deletion api/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package api

type Tag struct {
Name *string `json:"name"`
}
}
43 changes: 43 additions & 0 deletions split/resource_split_split.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ func resourceSplitSplit() *schema.Resource {
Optional: true,
Computed: true,
},

"tags":{
Type: schema.TypeList,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeSet might better here unless order is important for tags.

Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
ForceNew: true,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ForceNew: true needed here? It does seem you can update the feature flag without needing to recreate the feature flag itself.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cant actually remove tags from a feature flag, only add new ones. At least that's how it was when i tested it.

},
},
}
}
Expand Down Expand Up @@ -110,6 +119,40 @@ func resourceSplitSplitCreate(ctx context.Context, d *schema.ResourceData, meta

log.Printf("[DEBUG] Created split %v", s.GetID())

tagsRaw := d.Get("tags").([]interface{})
tags := make([]string, len(tagsRaw))
log.Printf("[DEBUG] Creating Tag amount: %v", len(tags))

if len(tags) > 0 {
log.Printf("[DEBUG] Creating Tags %v",tags)
// Create object
operations := []api.SplitUpdateFlagRequest{}
for _, tag := range tagsRaw {
operation := api.SplitUpdateFlagRequest{
Op: "add",
Path: "/tags/0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the path always /tags/0?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If doing Tags, yes. For other operations other paths are needed.

Value: api.SplitTag{
Name: tag.(string),
},
}
operations = append(operations, operation)
}

log.Printf("[DEBUG] Operations: %v", &operations)
s, _, updateForTagErr := client.Splits.UpdateSplit(workspaceID, opts.Name, &operations)
if updateForTagErr != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Unable to update split flags %v", opts.Name),
Detail: createErr.Error(),
})
return diags
}

log.Printf("[DEBUG] Updated split %v", s.GetID())
}


Comment on lines +122 to +155
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire method needs to be extracted into a separate method that is called by both resourceSplitSplitUpdate and resourceSplitSplitCreate. With what you have now, the only time tags will be added to the feature flag is during creation.

d.SetId(s.GetID())
d.Set("workspace_id", workspaceID)

Expand Down
2 changes: 2 additions & 0 deletions split/resource_split_split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func TestAccSplitSplit_Basic(t *testing.T) {
"split_split.foobar", "description", "my split description"),
resource.TestCheckResourceAttrSet(
"split_split.foobar", "traffic_type_id"),
resource.TestCheckResourceAttrSet(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to add a testcase in this file that adds tags to the feature flag instead of just checking if it is read.

["split.testtag"], "tags"),
),
},
{
Expand Down
Binary file added terraform-provider-split
Binary file not shown.