prefixlists.go raw

   1  package linodego
   2  
   3  import (
   4  	"context"
   5  	"encoding/json"
   6  	"time"
   7  
   8  	"github.com/linode/linodego/internal/parseabletime"
   9  )
  10  
  11  // PrefixList represents a network prefix list returned by the API.
  12  type PrefixList struct {
  13  	ID                 int       `json:"id"`
  14  	Name               string    `json:"name"`
  15  	Description        string    `json:"description"`
  16  	Visibility         string    `json:"visibility"`
  17  	SourcePrefixListID *int      `json:"source_prefixlist_id"`
  18  	IPv4               *[]string `json:"ipv4"`
  19  	IPv6               *[]string `json:"ipv6"`
  20  	Version            int       `json:"version"`
  21  
  22  	Created *time.Time `json:"-"`
  23  	Updated *time.Time `json:"-"`
  24  	Deleted *time.Time `json:"-"`
  25  }
  26  
  27  // UnmarshalJSON implements custom timestamp parsing for PrefixList values.
  28  func (p *PrefixList) UnmarshalJSON(data []byte) error {
  29  	type Mask PrefixList
  30  
  31  	aux := struct {
  32  		*Mask
  33  
  34  		Created *parseabletime.ParseableTime `json:"created"`
  35  		Updated *parseabletime.ParseableTime `json:"updated"`
  36  		Deleted *parseabletime.ParseableTime `json:"deleted"`
  37  	}{
  38  		Mask: (*Mask)(p),
  39  	}
  40  
  41  	if err := json.Unmarshal(data, &aux); err != nil {
  42  		return err
  43  	}
  44  
  45  	p.Created = (*time.Time)(aux.Created)
  46  	p.Updated = (*time.Time)(aux.Updated)
  47  	p.Deleted = (*time.Time)(aux.Deleted)
  48  
  49  	return nil
  50  }
  51  
  52  // ListPrefixLists returns a paginated collection of Prefix Lists.
  53  func (c *Client) ListPrefixLists(ctx context.Context, opts *ListOptions) ([]PrefixList, error) {
  54  	return getPaginatedResults[PrefixList](ctx, c, "networking/prefixlists", opts)
  55  }
  56  
  57  // GetPrefixList fetches a single Prefix List by its ID.
  58  func (c *Client) GetPrefixList(ctx context.Context, id int) (*PrefixList, error) {
  59  	endpoint := formatAPIPath("networking/prefixlists/%d", id)
  60  	return doGETRequest[PrefixList](ctx, c, endpoint)
  61  }
  62