github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/model/upstream.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 model
    15  
    16  import (
    17  	"encoding/json"
    18  
    19  	"github.com/pingcap/errors"
    20  	cerror "github.com/pingcap/tiflow/pkg/errors"
    21  )
    22  
    23  // UpstreamID is the type for upstream ID
    24  type UpstreamID = uint64
    25  
    26  // UpstreamInfo store in etcd.
    27  type UpstreamInfo struct {
    28  	ID            uint64   `json:"id"`
    29  	PDEndpoints   string   `json:"pd-endpoints"`
    30  	KeyPath       string   `json:"key-path"`
    31  	CertPath      string   `json:"cert-path"`
    32  	CAPath        string   `json:"ca-path"`
    33  	CertAllowedCN []string `json:"cert-allowed-cn"`
    34  }
    35  
    36  // Marshal using json.Marshal.
    37  func (c *UpstreamInfo) Marshal() ([]byte, error) {
    38  	data, err := json.Marshal(c)
    39  	if err != nil {
    40  		return nil, cerror.WrapError(cerror.ErrMarshalFailed, err)
    41  	}
    42  
    43  	return data, nil
    44  }
    45  
    46  // Unmarshal from binary data.
    47  func (c *UpstreamInfo) Unmarshal(data []byte) error {
    48  	err := json.Unmarshal(data, c)
    49  	return errors.Annotatef(cerror.WrapError(cerror.ErrUnmarshalFailed, err),
    50  		"unmarshal data: %v", data)
    51  }
    52  
    53  // Clone returns a cloned upstreamInfo
    54  func (c *UpstreamInfo) Clone() (*UpstreamInfo, error) {
    55  	s, err := c.Marshal()
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	cloned := new(UpstreamInfo)
    60  	err = cloned.Unmarshal(s)
    61  	return cloned, err
    62  }