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

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