diff --git a/api/split.go b/api/split.go index 3890dda..cc05dac 100644 --- a/api/split.go +++ b/api/split.go @@ -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"` } @@ -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 @@ -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 +} diff --git a/api/tags.go b/api/tags.go index 234db86..8f9e342 100644 --- a/api/tags.go +++ b/api/tags.go @@ -2,4 +2,4 @@ package api type Tag struct { Name *string `json:"name"` -} +} \ No newline at end of file diff --git a/split/resource_split_split.go b/split/resource_split_split.go index 63180e1..528bb03 100644 --- a/split/resource_split_split.go +++ b/split/resource_split_split.go @@ -50,6 +50,15 @@ func resourceSplitSplit() *schema.Resource { Optional: true, Computed: true, }, + + "tags":{ + Type: schema.TypeList, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Optional: true, + ForceNew: true, + }, }, } } @@ -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", + 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()) + } + + d.SetId(s.GetID()) d.Set("workspace_id", workspaceID) diff --git a/split/resource_split_split_test.go b/split/resource_split_split_test.go index 1eec6af..21500b6 100644 --- a/split/resource_split_split_test.go +++ b/split/resource_split_split_test.go @@ -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( + ["split.testtag"], "tags"), ), }, { diff --git a/terraform-provider-split b/terraform-provider-split new file mode 100755 index 0000000..a219a9f Binary files /dev/null and b/terraform-provider-split differ