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

     1  package fastly
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"sort"
     7  	"time"
     8  )
     9  
    10  // Dictionary represents a dictionary response from the Fastly API.
    11  type Dictionary struct {
    12  	ServiceID      string `mapstructure:"service_id"`
    13  	ServiceVersion int    `mapstructure:"version"`
    14  
    15  	ID        string     `mapstructure:"id"`
    16  	Name      string     `mapstructure:"name"`
    17  	WriteOnly bool       `mapstructure:"write_only"`
    18  	CreatedAt *time.Time `mapstructure:"created_at"`
    19  	UpdatedAt *time.Time `mapstructure:"updated_at"`
    20  	DeletedAt *time.Time `mapstructure:"deleted_at"`
    21  }
    22  
    23  // dictionariesByName is a sortable list of dictionaries.
    24  type dictionariesByName []*Dictionary
    25  
    26  // Len, Swap, and Less implement the sortable interface.
    27  func (s dictionariesByName) Len() int      { return len(s) }
    28  func (s dictionariesByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
    29  func (s dictionariesByName) Less(i, j int) bool {
    30  	return s[i].Name < s[j].Name
    31  }
    32  
    33  // ListDictionariesInput is used as input to the ListDictionaries function.
    34  type ListDictionariesInput struct {
    35  	// ServiceID is the ID of the service (required).
    36  	ServiceID string
    37  
    38  	// ServiceVersion is the specific configuration version (required).
    39  	ServiceVersion int
    40  }
    41  
    42  // ListDictionaries returns the list of dictionaries for the configuration version.
    43  func (c *Client) ListDictionaries(i *ListDictionariesInput) ([]*Dictionary, error) {
    44  	if i.ServiceID == "" {
    45  		return nil, ErrMissingServiceID
    46  	}
    47  
    48  	if i.ServiceVersion == 0 {
    49  		return nil, ErrMissingServiceVersion
    50  	}
    51  
    52  	path := fmt.Sprintf("/service/%s/version/%d/dictionary", i.ServiceID, i.ServiceVersion)
    53  	resp, err := c.Get(path, nil)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	var bs []*Dictionary
    59  	if err := decodeBodyMap(resp.Body, &bs); err != nil {
    60  		return nil, err
    61  	}
    62  	sort.Stable(dictionariesByName(bs))
    63  	return bs, nil
    64  }
    65  
    66  // CreateDictionaryInput is used as input to the CreateDictionary function.
    67  type CreateDictionaryInput struct {
    68  	// ServiceID is the ID of the service (required).
    69  	ServiceID string
    70  
    71  	// ServiceVersion is the specific configuration version (required).
    72  	ServiceVersion int
    73  
    74  	Name      string      `url:"name,omitempty"`
    75  	WriteOnly Compatibool `url:"write_only,omitempty"`
    76  }
    77  
    78  // CreateDictionary creates a new Fastly dictionary.
    79  func (c *Client) CreateDictionary(i *CreateDictionaryInput) (*Dictionary, error) {
    80  	if i.ServiceID == "" {
    81  		return nil, ErrMissingServiceID
    82  	}
    83  
    84  	if i.ServiceVersion == 0 {
    85  		return nil, ErrMissingServiceVersion
    86  	}
    87  
    88  	path := fmt.Sprintf("/service/%s/version/%d/dictionary", i.ServiceID, i.ServiceVersion)
    89  	resp, err := c.PostForm(path, i, nil)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  
    94  	var b *Dictionary
    95  	if err := decodeBodyMap(resp.Body, &b); err != nil {
    96  		return nil, err
    97  	}
    98  	return b, nil
    99  }
   100  
   101  // GetDictionaryInput is used as input to the GetDictionary function.
   102  type GetDictionaryInput struct {
   103  	// ServiceID is the ID of the service (required).
   104  	ServiceID string
   105  
   106  	// ServiceVersion is the specific configuration version (required).
   107  	ServiceVersion int
   108  
   109  	// Name is the name of the dictionary to fetch.
   110  	Name string
   111  }
   112  
   113  // GetDictionary gets the dictionary configuration with the given parameters.
   114  func (c *Client) GetDictionary(i *GetDictionaryInput) (*Dictionary, error) {
   115  	if i.ServiceID == "" {
   116  		return nil, ErrMissingServiceID
   117  	}
   118  
   119  	if i.ServiceVersion == 0 {
   120  		return nil, ErrMissingServiceVersion
   121  	}
   122  
   123  	if i.Name == "" {
   124  		return nil, ErrMissingName
   125  	}
   126  
   127  	path := fmt.Sprintf("/service/%s/version/%d/dictionary/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   128  	resp, err := c.Get(path, nil)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  
   133  	var b *Dictionary
   134  	if err := decodeBodyMap(resp.Body, &b); err != nil {
   135  		return nil, err
   136  	}
   137  	return b, nil
   138  }
   139  
   140  // UpdateDictionaryInput is used as input to the UpdateDictionary function.
   141  type UpdateDictionaryInput struct {
   142  	// ServiceID is the ID of the service (required).
   143  	ServiceID string
   144  
   145  	// ServiceVersion is the specific configuration version (required).
   146  	ServiceVersion int
   147  
   148  	// Name is the name of the dictionary to update.
   149  	Name string
   150  
   151  	NewName   *string      `url:"name,omitempty"`
   152  	WriteOnly *Compatibool `url:"write_only,omitempty"`
   153  }
   154  
   155  // UpdateDictionary updates a specific dictionary.
   156  func (c *Client) UpdateDictionary(i *UpdateDictionaryInput) (*Dictionary, error) {
   157  	if i.ServiceID == "" {
   158  		return nil, ErrMissingServiceID
   159  	}
   160  
   161  	if i.ServiceVersion == 0 {
   162  		return nil, ErrMissingServiceVersion
   163  	}
   164  
   165  	if i.Name == "" {
   166  		return nil, ErrMissingName
   167  	}
   168  
   169  	path := fmt.Sprintf("/service/%s/version/%d/dictionary/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   170  	resp, err := c.PutForm(path, i, nil)
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  
   175  	var b *Dictionary
   176  	if err := decodeBodyMap(resp.Body, &b); err != nil {
   177  		return nil, err
   178  	}
   179  	return b, nil
   180  }
   181  
   182  // DeleteDictionaryInput is the input parameter to DeleteDictionary.
   183  type DeleteDictionaryInput struct {
   184  	// ServiceID is the ID of the service (required).
   185  	ServiceID string
   186  
   187  	// ServiceVersion is the specific configuration version (required).
   188  	ServiceVersion int
   189  
   190  	// Name is the name of the dictionary to delete (required).
   191  	Name string
   192  }
   193  
   194  // DeleteDictionary deletes the given dictionary version.
   195  func (c *Client) DeleteDictionary(i *DeleteDictionaryInput) error {
   196  	if i.ServiceID == "" {
   197  		return ErrMissingServiceID
   198  	}
   199  
   200  	if i.ServiceVersion == 0 {
   201  		return ErrMissingServiceVersion
   202  	}
   203  
   204  	if i.Name == "" {
   205  		return ErrMissingName
   206  	}
   207  
   208  	path := fmt.Sprintf("/service/%s/version/%d/dictionary/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   209  	resp, err := c.Delete(path, nil)
   210  	if err != nil {
   211  		return err
   212  	}
   213  	defer resp.Body.Close()
   214  
   215  	// Unlike other endpoints, the dictionary endpoint does not return a status
   216  	// response - it just returns a 200 OK.
   217  	return nil
   218  }