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

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