github.com/fastly/go-fastly/v5@v5.3.0/fastly/logentries.go (about)

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