github.com/fastly/go-fastly/v6@v6.8.0/fastly/ftp.go (about)

     1  package fastly
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"sort"
     7  	"time"
     8  )
     9  
    10  // FTP represents an FTP logging response from the Fastly API.
    11  type FTP struct {
    12  	ServiceID      string `mapstructure:"service_id"`
    13  	ServiceVersion int    `mapstructure:"version"`
    14  
    15  	Name              string     `mapstructure:"name"`
    16  	Address           string     `mapstructure:"address"`
    17  	Port              uint       `mapstructure:"port"`
    18  	Username          string     `mapstructure:"user"`
    19  	Password          string     `mapstructure:"password"`
    20  	PublicKey         string     `mapstructure:"public_key"`
    21  	Path              string     `mapstructure:"path"`
    22  	Period            uint       `mapstructure:"period"`
    23  	CompressionCodec  string     `mapstructure:"compression_codec"`
    24  	GzipLevel         uint8      `mapstructure:"gzip_level"`
    25  	Format            string     `mapstructure:"format"`
    26  	FormatVersion     uint       `mapstructure:"format_version"`
    27  	ResponseCondition string     `mapstructure:"response_condition"`
    28  	TimestampFormat   string     `mapstructure:"timestamp_format"`
    29  	MessageType       string     `mapstructure:"message_type"`
    30  	Placement         string     `mapstructure:"placement"`
    31  	CreatedAt         *time.Time `mapstructure:"created_at"`
    32  	UpdatedAt         *time.Time `mapstructure:"updated_at"`
    33  	DeletedAt         *time.Time `mapstructure:"deleted_at"`
    34  }
    35  
    36  // ftpsByName is a sortable list of ftps.
    37  type ftpsByName []*FTP
    38  
    39  // Len, Swap, and Less implement the sortable interface.
    40  func (s ftpsByName) Len() int      { return len(s) }
    41  func (s ftpsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
    42  func (s ftpsByName) Less(i, j int) bool {
    43  	return s[i].Name < s[j].Name
    44  }
    45  
    46  // ListFTPsInput is used as input to the ListFTPs function.
    47  type ListFTPsInput struct {
    48  	// ServiceID is the ID of the service (required).
    49  	ServiceID string
    50  
    51  	// ServiceVersion is the specific configuration version (required).
    52  	ServiceVersion int
    53  }
    54  
    55  // ListFTPs returns the list of ftps for the configuration version.
    56  func (c *Client) ListFTPs(i *ListFTPsInput) ([]*FTP, error) {
    57  	if i.ServiceID == "" {
    58  		return nil, ErrMissingServiceID
    59  	}
    60  
    61  	if i.ServiceVersion == 0 {
    62  		return nil, ErrMissingServiceVersion
    63  	}
    64  
    65  	path := fmt.Sprintf("/service/%s/version/%d/logging/ftp", i.ServiceID, i.ServiceVersion)
    66  	resp, err := c.Get(path, nil)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	defer resp.Body.Close()
    71  
    72  	var ftps []*FTP
    73  	if err := decodeBodyMap(resp.Body, &ftps); err != nil {
    74  		return nil, err
    75  	}
    76  	sort.Stable(ftpsByName(ftps))
    77  	return ftps, nil
    78  }
    79  
    80  // CreateFTPInput is used as input to the CreateFTP function.
    81  type CreateFTPInput struct {
    82  	// ServiceID is the ID of the service (required).
    83  	ServiceID string
    84  
    85  	// ServiceVersion is the specific configuration version (required).
    86  	ServiceVersion int
    87  
    88  	Name              string `url:"name,omitempty"`
    89  	Address           string `url:"address,omitempty"`
    90  	Port              uint   `url:"port,omitempty"`
    91  	Username          string `url:"user,omitempty"`
    92  	Password          string `url:"password,omitempty"`
    93  	PublicKey         string `url:"public_key,omitempty"`
    94  	Path              string `url:"path,omitempty"`
    95  	Period            uint   `url:"period,omitempty"`
    96  	FormatVersion     uint   `url:"format_version,omitempty"`
    97  	CompressionCodec  string `url:"compression_codec,omitempty"`
    98  	GzipLevel         uint8  `url:"gzip_level,omitempty"`
    99  	Format            string `url:"format,omitempty"`
   100  	ResponseCondition string `url:"response_condition,omitempty"`
   101  	MessageType       string `url:"message_type,omitempty"`
   102  	TimestampFormat   string `url:"timestamp_format,omitempty"`
   103  	Placement         string `url:"placement,omitempty"`
   104  }
   105  
   106  // CreateFTP creates a new Fastly FTP.
   107  func (c *Client) CreateFTP(i *CreateFTPInput) (*FTP, error) {
   108  	if i.ServiceID == "" {
   109  		return nil, ErrMissingServiceID
   110  	}
   111  
   112  	if i.ServiceVersion == 0 {
   113  		return nil, ErrMissingServiceVersion
   114  	}
   115  
   116  	path := fmt.Sprintf("/service/%s/version/%d/logging/ftp", i.ServiceID, i.ServiceVersion)
   117  	resp, err := c.PostForm(path, i, nil)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  	defer resp.Body.Close()
   122  
   123  	var ftp *FTP
   124  	if err := decodeBodyMap(resp.Body, &ftp); err != nil {
   125  		return nil, err
   126  	}
   127  	return ftp, nil
   128  }
   129  
   130  // GetFTPInput is used as input to the GetFTP function.
   131  type GetFTPInput struct {
   132  	// ServiceID is the ID of the service (required).
   133  	ServiceID string
   134  
   135  	// ServiceVersion is the specific configuration version (required).
   136  	ServiceVersion int
   137  
   138  	// Name is the name of the FTP to fetch.
   139  	Name string
   140  }
   141  
   142  // GetFTP gets the FTP configuration with the given parameters.
   143  func (c *Client) GetFTP(i *GetFTPInput) (*FTP, error) {
   144  	if i.ServiceID == "" {
   145  		return nil, ErrMissingServiceID
   146  	}
   147  
   148  	if i.ServiceVersion == 0 {
   149  		return nil, ErrMissingServiceVersion
   150  	}
   151  
   152  	if i.Name == "" {
   153  		return nil, ErrMissingName
   154  	}
   155  
   156  	path := fmt.Sprintf("/service/%s/version/%d/logging/ftp/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   157  	resp, err := c.Get(path, nil)
   158  	if err != nil {
   159  		return nil, err
   160  	}
   161  	defer resp.Body.Close()
   162  
   163  	var b *FTP
   164  	if err := decodeBodyMap(resp.Body, &b); err != nil {
   165  		return nil, err
   166  	}
   167  	return b, nil
   168  }
   169  
   170  // UpdateFTPInput is used as input to the UpdateFTP function.
   171  type UpdateFTPInput struct {
   172  	// ServiceID is the ID of the service (required).
   173  	ServiceID string
   174  
   175  	// ServiceVersion is the specific configuration version (required).
   176  	ServiceVersion int
   177  
   178  	// Name is the name of the FTP to update.
   179  	Name string
   180  
   181  	NewName           *string `url:"name,omitempty"`
   182  	Address           *string `url:"address,omitempty"`
   183  	Port              *uint   `url:"port,omitempty"`
   184  	PublicKey         *string `url:"public_key,omitempty"`
   185  	Username          *string `url:"user,omitempty"`
   186  	Password          *string `url:"password,omitempty"`
   187  	Path              *string `url:"path,omitempty"`
   188  	Period            *uint   `url:"period,omitempty"`
   189  	FormatVersion     *uint   `url:"format_version,omitempty"`
   190  	CompressionCodec  *string `url:"compression_codec,omitempty"`
   191  	GzipLevel         *uint8  `url:"gzip_level,omitempty"`
   192  	Format            *string `url:"format,omitempty"`
   193  	ResponseCondition *string `url:"response_condition,omitempty"`
   194  	MessageType       *string `url:"message_type,omitempty"`
   195  	TimestampFormat   *string `url:"timestamp_format,omitempty"`
   196  	Placement         *string `url:"placement,omitempty"`
   197  }
   198  
   199  // UpdateFTP updates a specific FTP.
   200  func (c *Client) UpdateFTP(i *UpdateFTPInput) (*FTP, error) {
   201  	if i.ServiceID == "" {
   202  		return nil, ErrMissingServiceID
   203  	}
   204  
   205  	if i.ServiceVersion == 0 {
   206  		return nil, ErrMissingServiceVersion
   207  	}
   208  
   209  	if i.Name == "" {
   210  		return nil, ErrMissingName
   211  	}
   212  
   213  	path := fmt.Sprintf("/service/%s/version/%d/logging/ftp/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   214  	resp, err := c.PutForm(path, i, nil)
   215  	if err != nil {
   216  		return nil, err
   217  	}
   218  	defer resp.Body.Close()
   219  
   220  	var b *FTP
   221  	if err := decodeBodyMap(resp.Body, &b); err != nil {
   222  		return nil, err
   223  	}
   224  	return b, nil
   225  }
   226  
   227  // DeleteFTPInput is the input parameter to DeleteFTP.
   228  type DeleteFTPInput struct {
   229  	// ServiceID is the ID of the service (required).
   230  	ServiceID string
   231  
   232  	// ServiceVersion is the specific configuration version (required).
   233  	ServiceVersion int
   234  
   235  	// Name is the name of the FTP to delete (required).
   236  	Name string
   237  }
   238  
   239  // DeleteFTP deletes the given FTP version.
   240  func (c *Client) DeleteFTP(i *DeleteFTPInput) error {
   241  	if i.ServiceID == "" {
   242  		return ErrMissingServiceID
   243  	}
   244  
   245  	if i.ServiceVersion == 0 {
   246  		return ErrMissingServiceVersion
   247  	}
   248  
   249  	if i.Name == "" {
   250  		return ErrMissingName
   251  	}
   252  
   253  	path := fmt.Sprintf("/service/%s/version/%d/logging/ftp/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   254  	resp, err := c.Delete(path, nil)
   255  	if err != nil {
   256  		return err
   257  	}
   258  	defer resp.Body.Close()
   259  
   260  	var r *statusResp
   261  	if err := decodeBodyMap(resp.Body, &r); err != nil {
   262  		return err
   263  	}
   264  	if !r.Ok() {
   265  		return ErrNotOK
   266  	}
   267  	return nil
   268  }