vpc.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  type VPC struct {
  12  	ID          int    `json:"id"`
  13  	Label       string `json:"label"`
  14  	Description string `json:"description"`
  15  	Region      string `json:"region"`
  16  
  17  	// NOTE: IPv6 VPCs may not currently be available to all users.
  18  	IPv6 []VPCIPv6Range `json:"ipv6"`
  19  
  20  	Subnets []VPCSubnet `json:"subnets"`
  21  	Created *time.Time  `json:"-"`
  22  	Updated *time.Time  `json:"-"`
  23  }
  24  
  25  // VPCIPv6Range represents a single IPv6 range assigned to a VPC.
  26  // NOTE: IPv6 VPCs may not currently be available to all users.
  27  type VPCIPv6Range struct {
  28  	Range string `json:"range"`
  29  }
  30  
  31  type VPCCreateOptions struct {
  32  	Label       string `json:"label"`
  33  	Description string `json:"description,omitempty"`
  34  	Region      string `json:"region"`
  35  
  36  	// NOTE: IPv6 VPCs may not currently be available to all users.
  37  	IPv6 []VPCCreateOptionsIPv6 `json:"ipv6,omitempty"`
  38  
  39  	Subnets []VPCSubnetCreateOptions `json:"subnets,omitempty"`
  40  }
  41  
  42  // VPCCreateOptionsIPv6 represents a single IPv6 range assigned to a VPC
  43  // which is specified during a VPC's creation.
  44  // NOTE: IPv6 VPCs may not currently be available to all users.
  45  type VPCCreateOptionsIPv6 struct {
  46  	Range           *string `json:"range,omitempty"`
  47  	AllocationClass *string `json:"allocation_class,omitempty"`
  48  }
  49  
  50  type VPCUpdateOptions struct {
  51  	Label       string `json:"label,omitempty"`
  52  	Description string `json:"description,omitempty"`
  53  }
  54  
  55  func (v VPC) GetCreateOptions() VPCCreateOptions {
  56  	subnetCreations := make([]VPCSubnetCreateOptions, len(v.Subnets))
  57  	for i, s := range v.Subnets {
  58  		subnetCreations[i] = s.GetCreateOptions()
  59  	}
  60  
  61  	return VPCCreateOptions{
  62  		Label:       v.Label,
  63  		Description: v.Description,
  64  		Region:      v.Region,
  65  		Subnets:     subnetCreations,
  66  		IPv6: mapSlice(v.IPv6, func(i VPCIPv6Range) VPCCreateOptionsIPv6 {
  67  			return VPCCreateOptionsIPv6{
  68  				Range: copyValue(&i.Range),
  69  			}
  70  		}),
  71  	}
  72  }
  73  
  74  func (v VPC) GetUpdateOptions() VPCUpdateOptions {
  75  	return VPCUpdateOptions{
  76  		Label:       v.Label,
  77  		Description: v.Description,
  78  	}
  79  }
  80  
  81  func (v *VPC) UnmarshalJSON(b []byte) error {
  82  	type Mask VPC
  83  
  84  	p := struct {
  85  		*Mask
  86  
  87  		Created *parseabletime.ParseableTime `json:"created"`
  88  		Updated *parseabletime.ParseableTime `json:"updated"`
  89  	}{
  90  		Mask: (*Mask)(v),
  91  	}
  92  	if err := json.Unmarshal(b, &p); err != nil {
  93  		return err
  94  	}
  95  
  96  	v.Created = (*time.Time)(p.Created)
  97  	v.Updated = (*time.Time)(p.Updated)
  98  
  99  	return nil
 100  }
 101  
 102  func (c *Client) CreateVPC(
 103  	ctx context.Context,
 104  	opts VPCCreateOptions,
 105  ) (*VPC, error) {
 106  	return doPOSTRequest[VPC](ctx, c, "vpcs", opts)
 107  }
 108  
 109  func (c *Client) GetVPC(ctx context.Context, vpcID int) (*VPC, error) {
 110  	e := formatAPIPath("/vpcs/%d", vpcID)
 111  	return doGETRequest[VPC](ctx, c, e)
 112  }
 113  
 114  func (c *Client) ListVPCs(ctx context.Context, opts *ListOptions) ([]VPC, error) {
 115  	return getPaginatedResults[VPC](ctx, c, "vpcs", opts)
 116  }
 117  
 118  func (c *Client) UpdateVPC(
 119  	ctx context.Context,
 120  	vpcID int,
 121  	opts VPCUpdateOptions,
 122  ) (*VPC, error) {
 123  	e := formatAPIPath("vpcs/%d", vpcID)
 124  	return doPUTRequest[VPC](ctx, c, e, opts)
 125  }
 126  
 127  func (c *Client) DeleteVPC(ctx context.Context, vpcID int) error {
 128  	e := formatAPIPath("vpcs/%d", vpcID)
 129  	return doDELETERequest(ctx, c, e)
 130  }
 131