instance_ips.go raw

   1  package linodego
   2  
   3  import (
   4  	"context"
   5  )
   6  
   7  // InstanceIPAddressResponse contains the IPv4 and IPv6 details for an Instance
   8  type InstanceIPAddressResponse struct {
   9  	IPv4 *InstanceIPv4Response `json:"ipv4"`
  10  	IPv6 *InstanceIPv6Response `json:"ipv6"`
  11  }
  12  
  13  // InstanceIPv4Response contains the details of all IPv4 addresses associated with an Instance
  14  type InstanceIPv4Response struct {
  15  	Public   []*InstanceIP `json:"public"`
  16  	Private  []*InstanceIP `json:"private"`
  17  	Shared   []*InstanceIP `json:"shared"`
  18  	Reserved []*InstanceIP `json:"reserved"`
  19  	VPC      []*VPCIP      `json:"vpc"`
  20  }
  21  
  22  // InstanceIP represents an Instance IP with additional DNS and networking details
  23  type InstanceIP struct {
  24  	Address     string             `json:"address"`
  25  	Gateway     string             `json:"gateway"`
  26  	SubnetMask  string             `json:"subnet_mask"`
  27  	Prefix      int                `json:"prefix"`
  28  	Type        InstanceIPType     `json:"type"`
  29  	Public      bool               `json:"public"`
  30  	RDNS        string             `json:"rdns"`
  31  	LinodeID    int                `json:"linode_id"`
  32  	InterfaceID *int               `json:"interface_id"`
  33  	Region      string             `json:"region"`
  34  	VPCNAT1To1  *InstanceIPNAT1To1 `json:"vpc_nat_1_1"`
  35  	Reserved    bool               `json:"reserved"`
  36  }
  37  
  38  // VPCIP represents a private IP address in a VPC subnet with additional networking details
  39  type VPCIP struct {
  40  	Address      *string `json:"address"`
  41  	AddressRange *string `json:"address_range"`
  42  	Gateway      string  `json:"gateway"`
  43  	SubnetMask   string  `json:"subnet_mask"`
  44  	Prefix       int     `json:"prefix"`
  45  	LinodeID     int     `json:"linode_id"`
  46  	Region       string  `json:"region"`
  47  	Active       bool    `json:"active"`
  48  	NAT1To1      *string `json:"nat_1_1"`
  49  	VPCID        int     `json:"vpc_id"`
  50  	SubnetID     int     `json:"subnet_id"`
  51  	InterfaceID  int     `json:"interface_id"`
  52  	// NOTE: NodeBalancerID and DatabaseID may not currently be available to all users.
  53  	NodeBalancerID *int `json:"nodebalancer_id"`
  54  	DatabaseID     *int `json:"database_id"`
  55  	// NOTE: IPv6 VPCs may not currently be available to all users.
  56  	IPv6Range     *string            `json:"ipv6_range"`
  57  	IPv6IsPublic  *bool              `json:"ipv6_is_public"`
  58  	IPv6Addresses []VPCIPIPv6Address `json:"ipv6_addresses"`
  59  
  60  	// The type of this field will be made a pointer in the next major release of linodego.
  61  	ConfigID int `json:"config_id"`
  62  }
  63  
  64  // VPCIPIPv6Address represents a single IPv6 address under a VPCIP.
  65  // NOTE: IPv6 VPCs may not currently be available to all users.
  66  type VPCIPIPv6Address struct {
  67  	SLAACAddress string `json:"slaac_address"`
  68  }
  69  
  70  // InstanceIPv6Response contains the IPv6 addresses and ranges for an Instance
  71  type InstanceIPv6Response struct {
  72  	LinkLocal *InstanceIP `json:"link_local"`
  73  	SLAAC     *InstanceIP `json:"slaac"`
  74  	Global    []IPv6Range `json:"global"`
  75  	// NOTE: IPv6 VPCs may not currently be available to all users.
  76  	VPC []VPCIP `json:"vpc"`
  77  }
  78  
  79  // InstanceIPNAT1To1 contains information about the NAT 1:1 mapping
  80  // of a public IP address to a VPC subnet.
  81  type InstanceIPNAT1To1 struct {
  82  	Address  string `json:"address"`
  83  	SubnetID int    `json:"subnet_id"`
  84  	VPCID    int    `json:"vpc_id"`
  85  }
  86  
  87  // IPv6Range represents a range of IPv6 addresses routed to a single Linode in a given Region
  88  type IPv6Range struct {
  89  	Range  string `json:"range"`
  90  	Region string `json:"region"`
  91  	Prefix int    `json:"prefix"`
  92  
  93  	RouteTarget string `json:"route_target"`
  94  
  95  	// These fields are only returned by GetIPv6Range(...)
  96  	IsBGP   bool  `json:"is_bgp"`
  97  	Linodes []int `json:"linodes"`
  98  }
  99  
 100  type InstanceReserveIPOptions struct {
 101  	Type    string `json:"type"`
 102  	Public  bool   `json:"public"`
 103  	Address string `json:"address"`
 104  }
 105  
 106  // InstanceIPType constants start with IPType and include Linode Instance IP Types
 107  type InstanceIPType string
 108  
 109  // InstanceIPType constants represent the IP types an Instance IP may be
 110  const (
 111  	IPTypeIPv4      InstanceIPType = "ipv4"
 112  	IPTypeIPv6      InstanceIPType = "ipv6"
 113  	IPTypeIPv6Pool  InstanceIPType = "ipv6/pool"
 114  	IPTypeIPv6Range InstanceIPType = "ipv6/range"
 115  )
 116  
 117  // GetInstanceIPAddresses gets the IPAddresses for a Linode instance
 118  func (c *Client) GetInstanceIPAddresses(ctx context.Context, linodeID int) (*InstanceIPAddressResponse, error) {
 119  	e := formatAPIPath("linode/instances/%d/ips", linodeID)
 120  	return doGETRequest[InstanceIPAddressResponse](ctx, c, e)
 121  }
 122  
 123  // GetInstanceIPAddress gets the IPAddress for a Linode instance matching a supplied IP address
 124  func (c *Client) GetInstanceIPAddress(ctx context.Context, linodeID int, ipaddress string) (*InstanceIP, error) {
 125  	e := formatAPIPath("linode/instances/%d/ips/%s", linodeID, ipaddress)
 126  	return doGETRequest[InstanceIP](ctx, c, e)
 127  }
 128  
 129  // AddInstanceIPAddress adds a public or private IP to a Linode instance
 130  func (c *Client) AddInstanceIPAddress(ctx context.Context, linodeID int, public bool) (*InstanceIP, error) {
 131  	instanceipRequest := struct {
 132  		Type   string `json:"type"`
 133  		Public bool   `json:"public"`
 134  	}{"ipv4", public}
 135  
 136  	e := formatAPIPath("linode/instances/%d/ips", linodeID)
 137  
 138  	return doPOSTRequest[InstanceIP](ctx, c, e, instanceipRequest)
 139  }
 140  
 141  // UpdateInstanceIPAddress updates the IPAddress with the specified instance id and IP address
 142  func (c *Client) UpdateInstanceIPAddress(ctx context.Context, linodeID int, ipAddress string, opts IPAddressUpdateOptions) (*InstanceIP, error) {
 143  	e := formatAPIPath("linode/instances/%d/ips/%s", linodeID, ipAddress)
 144  	return doPUTRequest[InstanceIP](ctx, c, e, opts)
 145  }
 146  
 147  func (c *Client) DeleteInstanceIPAddress(ctx context.Context, linodeID int, ipAddress string) error {
 148  	e := formatAPIPath("linode/instances/%d/ips/%s", linodeID, ipAddress)
 149  	return doDELETERequest(ctx, c, e)
 150  }
 151  
 152  // AssignInstanceReservedIP adds additional reserved IPV4 addresses to an existing linode
 153  func (c *Client) AssignInstanceReservedIP(ctx context.Context, linodeID int, opts InstanceReserveIPOptions) (*InstanceIP, error) {
 154  	endpoint := formatAPIPath("linode/instances/%d/ips", linodeID)
 155  	return doPOSTRequest[InstanceIP](ctx, c, endpoint, opts)
 156  }
 157