github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/api/v2/changefeed.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package v2
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  
    20  	v2 "github.com/pingcap/tiflow/cdc/api/v2"
    21  	"github.com/pingcap/tiflow/cdc/model"
    22  	"github.com/pingcap/tiflow/pkg/api/internal/rest"
    23  )
    24  
    25  // ChangefeedsGetter has a method to return a ChangefeedInterface.
    26  type ChangefeedsGetter interface {
    27  	Changefeeds() ChangefeedInterface
    28  }
    29  
    30  // ChangefeedInterface has methods to work with Changefeed items.
    31  // We can also mock the changefeed operations by implement this interface.
    32  type ChangefeedInterface interface {
    33  	// Create creates a changefeed
    34  	Create(ctx context.Context, cfg *v2.ChangefeedConfig) (*v2.ChangeFeedInfo, error)
    35  	// VerifyTable verifies table for a changefeed
    36  	VerifyTable(ctx context.Context, cfg *v2.VerifyTableConfig) (*v2.Tables, error)
    37  	// Update updates a changefeed
    38  	Update(ctx context.Context, cfg *v2.ChangefeedConfig,
    39  		namespace string, name string) (*v2.ChangeFeedInfo, error)
    40  	// Resume resumes a changefeed with given config
    41  	Resume(ctx context.Context, cfg *v2.ResumeChangefeedConfig, namespace string, name string) error
    42  	// Delete deletes a changefeed by name
    43  	Delete(ctx context.Context, namespace string, name string) error
    44  	// Pause pauses a changefeed with given name
    45  	Pause(ctx context.Context, namespace string, name string) error
    46  	// Get gets a changefeed detaail info
    47  	Get(ctx context.Context, namespace string, name string) (*v2.ChangeFeedInfo, error)
    48  	// List lists all changefeeds
    49  	List(ctx context.Context, namespace string, state string) ([]v2.ChangefeedCommonInfo, error)
    50  }
    51  
    52  // changefeeds implements ChangefeedInterface
    53  type changefeeds struct {
    54  	client rest.CDCRESTInterface
    55  }
    56  
    57  // newChangefeed returns changefeeds
    58  func newChangefeeds(c *APIV2Client) *changefeeds {
    59  	return &changefeeds{
    60  		client: c.RESTClient(),
    61  	}
    62  }
    63  
    64  func (c *changefeeds) Create(ctx context.Context,
    65  	cfg *v2.ChangefeedConfig,
    66  ) (*v2.ChangeFeedInfo, error) {
    67  	result := &v2.ChangeFeedInfo{}
    68  	err := c.client.Post().
    69  		WithURI("changefeeds").
    70  		WithBody(cfg).
    71  		Do(ctx).Into(result)
    72  	return result, err
    73  }
    74  
    75  func (c *changefeeds) VerifyTable(ctx context.Context,
    76  	cfg *v2.VerifyTableConfig,
    77  ) (*v2.Tables, error) {
    78  	result := &v2.Tables{}
    79  	err := c.client.Post().
    80  		WithURI("verify_table").
    81  		WithBody(cfg).
    82  		Do(ctx).
    83  		Into(result)
    84  	return result, err
    85  }
    86  
    87  func (c *changefeeds) Update(ctx context.Context,
    88  	cfg *v2.ChangefeedConfig, namespace string, name string,
    89  ) (*v2.ChangeFeedInfo, error) {
    90  	result := &v2.ChangeFeedInfo{}
    91  	u := fmt.Sprintf("changefeeds/%s?namespace=%s", name, namespace)
    92  	err := c.client.Put().
    93  		WithURI(u).
    94  		WithBody(cfg).
    95  		Do(ctx).
    96  		Into(result)
    97  	return result, err
    98  }
    99  
   100  // Resume a changefeed
   101  func (c *changefeeds) Resume(ctx context.Context,
   102  	cfg *v2.ResumeChangefeedConfig, namespace string, name string,
   103  ) error {
   104  	u := fmt.Sprintf("changefeeds/%s/resume?namespace=%s", name, namespace)
   105  	return c.client.Post().
   106  		WithURI(u).
   107  		WithBody(cfg).
   108  		Do(ctx).Error()
   109  }
   110  
   111  // Delete a changefeed
   112  func (c *changefeeds) Delete(ctx context.Context,
   113  	namespace string, name string,
   114  ) error {
   115  	u := fmt.Sprintf("changefeeds/%s?namespace=%s", name, namespace)
   116  	return c.client.Delete().
   117  		WithURI(u).
   118  		Do(ctx).Error()
   119  }
   120  
   121  // Pause a changefeed
   122  func (c *changefeeds) Pause(ctx context.Context,
   123  	namespace string, name string,
   124  ) error {
   125  	u := fmt.Sprintf("changefeeds/%s/pause?namespace=%s", name, namespace)
   126  	return c.client.Post().
   127  		WithURI(u).
   128  		Do(ctx).Error()
   129  }
   130  
   131  // Get gets a changefeed detaail info
   132  func (c *changefeeds) Get(ctx context.Context,
   133  	namespace string, name string,
   134  ) (*v2.ChangeFeedInfo, error) {
   135  	err := model.ValidateChangefeedID(name)
   136  	if err != nil {
   137  		return nil, err
   138  	}
   139  	result := new(v2.ChangeFeedInfo)
   140  	u := fmt.Sprintf("changefeeds/%s?namespace=%s", name, namespace)
   141  	err = c.client.Get().
   142  		WithURI(u).
   143  		Do(ctx).
   144  		Into(result)
   145  	return result, err
   146  }
   147  
   148  // List lists all changefeeds
   149  func (c *changefeeds) List(ctx context.Context,
   150  	namespace string, state string,
   151  ) ([]v2.ChangefeedCommonInfo, error) {
   152  	result := &v2.ListResponse[v2.ChangefeedCommonInfo]{}
   153  	err := c.client.Get().
   154  		WithURI("changefeeds?namespace="+namespace).
   155  		WithParam("state", state).
   156  		Do(ctx).
   157  		Into(result)
   158  	return result.Items, err
   159  }