...

Source file src/github.com/LINBIT/golinstor/client/remote.go

Documentation: github.com/LINBIT/golinstor/client

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/google/go-querystring/query"
     8  )
     9  
    10  type LinstorRemote struct {
    11  	RemoteName string `json:"remote_name,omitempty"`
    12  	Url        string `json:"url,omitempty"`
    13  	Passphrase string `json:"passphrase,omitempty"`
    14  	ClusterId  string `json:"cluster_id,omitempty"`
    15  }
    16  
    17  type RemoteList struct {
    18  	S3Remotes      []S3Remote      `json:"s3_remotes,omitempty"`
    19  	LinstorRemotes []LinstorRemote `json:"linstor_remotes,omitempty"`
    20  	EbsRemotes     []EbsRemote     `json:"ebs_remotes,omitempty"`
    21  }
    22  
    23  type S3Remote struct {
    24  	RemoteName   string `json:"remote_name,omitempty"`
    25  	Endpoint     string `json:"endpoint,omitempty"`
    26  	Bucket       string `json:"bucket,omitempty"`
    27  	Region       string `json:"region,omitempty"`
    28  	AccessKey    string `json:"access_key,omitempty"`
    29  	SecretKey    string `json:"secret_key,omitempty"`
    30  	UsePathStyle bool   `json:"use_path_style,omitempty"`
    31  }
    32  
    33  type EbsRemote struct {
    34  	RemoteName       string `json:"remote_name,omitempty"`
    35  	Endpoint         string `json:"endpoint,omitempty"`
    36  	Region           string `json:"region,omitempty"`
    37  	AvailabilityZone string `json:"availability_zone,omitempty"`
    38  	AccessKey        string `json:"access_key,omitempty"`
    39  	SecretKey        string `json:"secret_key,omitempty"`
    40  }
    41  
    42  type RemoteProvider interface {
    43  	// GetAll returns the list of all registered remotes.
    44  	GetAll(ctx context.Context, opts ...*ListOpts) (RemoteList, error)
    45  	// GetAllLinstor returns the list of LINSTOR remotes.
    46  	GetAllLinstor(ctx context.Context, opts ...*ListOpts) ([]LinstorRemote, error)
    47  	// GetAllS3 returns the list of S3 remotes.
    48  	GetAllS3(ctx context.Context, opts ...*ListOpts) ([]S3Remote, error)
    49  	// GetAllEbs returns the list of EBS remotes.
    50  	GetAllEbs(ctx context.Context, opts ...*ListOpts) ([]EbsRemote, error)
    51  	// CreateLinstor creates a new LINSTOR remote.
    52  	CreateLinstor(ctx context.Context, create LinstorRemote) error
    53  	// CreateS3 creates a new S3 remote.
    54  	CreateS3(ctx context.Context, create S3Remote) error
    55  	// CreateEbs creates a new EBS remote.
    56  	CreateEbs(ctx context.Context, create EbsRemote) error
    57  	// Delete a named remote.
    58  	Delete(ctx context.Context, remoteName string) error
    59  	// ModifyLinstor modifies the given LINSTOR remote.
    60  	ModifyLinstor(ctx context.Context, remoteName string, modify LinstorRemote) error
    61  	// ModifyS3 modifies the given S3 remote.
    62  	ModifyS3(ctx context.Context, remoteName string, modify S3Remote) error
    63  	// ModifyEbs modifies the given EBS remote.
    64  	ModifyEbs(ctx context.Context, remoteName string, modify EbsRemote) error
    65  }
    66  
    67  var _ RemoteProvider = &RemoteService{}
    68  
    69  type RemoteService struct {
    70  	client *Client
    71  }
    72  
    73  func (r *RemoteService) GetAll(ctx context.Context, opts ...*ListOpts) (RemoteList, error) {
    74  	var list RemoteList
    75  	_, err := r.client.doGET(ctx, "/v1/remotes", &list, opts...)
    76  	return list, err
    77  }
    78  
    79  func (r *RemoteService) GetAllLinstor(ctx context.Context, opts ...*ListOpts) ([]LinstorRemote, error) {
    80  	var list []LinstorRemote
    81  	_, err := r.client.doGET(ctx, "/v1/remotes/linstor", &list, opts...)
    82  	return list, err
    83  }
    84  
    85  func (r *RemoteService) GetAllS3(ctx context.Context, opts ...*ListOpts) ([]S3Remote, error) {
    86  	var list []S3Remote
    87  	_, err := r.client.doGET(ctx, "/v1/remotes/s3", &list, opts...)
    88  	return list, err
    89  }
    90  
    91  func (r *RemoteService) GetAllEbs(ctx context.Context, opts ...*ListOpts) ([]EbsRemote, error) {
    92  	var list []EbsRemote
    93  	_, err := r.client.doGET(ctx, "/v1/remotes/ebs", &list, opts...)
    94  	return list, err
    95  }
    96  
    97  func (r *RemoteService) CreateLinstor(ctx context.Context, create LinstorRemote) error {
    98  	_, err := r.client.doPOST(ctx, "/v1/remotes/linstor", create)
    99  	return err
   100  }
   101  
   102  func (r *RemoteService) CreateS3(ctx context.Context, create S3Remote) error {
   103  	_, err := r.client.doPOST(ctx, "/v1/remotes/s3", create)
   104  	return err
   105  }
   106  
   107  func (r *RemoteService) CreateEbs(ctx context.Context, create EbsRemote) error {
   108  	_, err := r.client.doPOST(ctx, "/v1/remotes/ebs", create)
   109  	return err
   110  }
   111  
   112  func (r *RemoteService) Delete(ctx context.Context, remoteName string) error {
   113  	vals, err := query.Values(&struct {
   114  		RemoteName string `url:"remote_name"`
   115  	}{RemoteName: remoteName})
   116  	if err != nil {
   117  		return fmt.Errorf("failed to encode remote name: %w", err)
   118  	}
   119  
   120  	_, err = r.client.doDELETE(ctx, "/v1/remotes?"+vals.Encode(), nil)
   121  	return err
   122  }
   123  
   124  func (r *RemoteService) ModifyLinstor(ctx context.Context, remoteName string, modify LinstorRemote) error {
   125  	_, err := r.client.doPUT(ctx, "/v1/remotes/linstor/"+remoteName, modify)
   126  	return err
   127  }
   128  
   129  func (r *RemoteService) ModifyS3(ctx context.Context, remoteName string, modify S3Remote) error {
   130  	_, err := r.client.doPUT(ctx, "/v1/remotes/s3/"+remoteName, modify)
   131  	return err
   132  }
   133  
   134  func (r *RemoteService) ModifyEbs(ctx context.Context, remoteName string, modify EbsRemote) error {
   135  	_, err := r.client.doPUT(ctx, "/v1/remotes/ebs/"+remoteName, modify)
   136  	return err
   137  }
   138  

View as plain text