1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
package linodego
import (
"context"
"encoding/json"
"time"
"github.com/linode/linodego/internal/parseabletime"
)
type VPC struct {
ID int `json:"id"`
Label string `json:"label"`
Description string `json:"description"`
Region string `json:"region"`
Subnets []VPCSubnet `json:"subnets"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
}
type VPCCreateOptions struct {
Label string `json:"label"`
Description string `json:"description,omitempty"`
Region string `json:"region"`
Subnets []VPCSubnetCreateOptions `json:"subnets,omitempty"`
}
type VPCUpdateOptions struct {
Label string `json:"label,omitempty"`
Description string `json:"description,omitempty"`
}
func (v VPC) GetCreateOptions() VPCCreateOptions {
subnetCreations := make([]VPCSubnetCreateOptions, len(v.Subnets))
for i, s := range v.Subnets {
subnetCreations[i] = s.GetCreateOptions()
}
return VPCCreateOptions{
Label: v.Label,
Description: v.Description,
Region: v.Region,
Subnets: subnetCreations,
}
}
func (v VPC) GetUpdateOptions() VPCUpdateOptions {
return VPCUpdateOptions{
Label: v.Label,
Description: v.Description,
}
}
func (v *VPC) UnmarshalJSON(b []byte) error {
type Mask VPC
p := struct {
*Mask
Created *parseabletime.ParseableTime `json:"created"`
Updated *parseabletime.ParseableTime `json:"updated"`
}{
Mask: (*Mask)(v),
}
if err := json.Unmarshal(b, &p); err != nil {
return err
}
v.Created = (*time.Time)(p.Created)
v.Updated = (*time.Time)(p.Updated)
return nil
}
func (c *Client) CreateVPC(
ctx context.Context,
opts VPCCreateOptions,
) (*VPC, error) {
e := "vpcs"
response, err := doPOSTRequest[VPC](ctx, c, e, opts)
return response, err
}
func (c *Client) GetVPC(ctx context.Context, vpcID int) (*VPC, error) {
e := formatAPIPath("/vpcs/%d", vpcID)
response, err := doGETRequest[VPC](ctx, c, e)
return response, err
}
func (c *Client) ListVPCs(ctx context.Context, opts *ListOptions) ([]VPC, error) {
response, err := getPaginatedResults[VPC](ctx, c, "vpcs", opts)
return response, err
}
func (c *Client) UpdateVPC(
ctx context.Context,
vpcID int,
opts VPCUpdateOptions,
) (*VPC, error) {
e := formatAPIPath("vpcs/%d", vpcID)
response, err := doPUTRequest[VPC](ctx, c, e, opts)
return response, err
}
func (c *Client) DeleteVPC(ctx context.Context, vpcID int) error {
e := formatAPIPath("vpcs/%d", vpcID)
err := doDELETERequest(ctx, c, e)
return err
}
|