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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
package linodego
import (
"context"
)
// InstanceConfigInterface contains information about a configuration's network interface
type InstanceConfigInterface struct {
ID int `json:"id"`
IPAMAddress string `json:"ipam_address"`
Label string `json:"label"`
Purpose ConfigInterfacePurpose `json:"purpose"`
Primary bool `json:"primary"`
Active bool `json:"active"`
VPCID *int `json:"vpc_id"`
SubnetID *int `json:"subnet_id"`
IPv4 *VPCIPv4 `json:"ipv4"`
IPRanges []string `json:"ip_ranges"`
}
type VPCIPv4 struct {
VPC string `json:"vpc,omitempty"`
NAT1To1 *string `json:"nat_1_1,omitempty"`
}
type InstanceConfigInterfaceCreateOptions struct {
IPAMAddress string `json:"ipam_address,omitempty"`
Label string `json:"label,omitempty"`
Purpose ConfigInterfacePurpose `json:"purpose,omitempty"`
Primary bool `json:"primary,omitempty"`
SubnetID *int `json:"subnet_id,omitempty"`
IPv4 *VPCIPv4 `json:"ipv4,omitempty"`
IPRanges []string `json:"ip_ranges,omitempty"`
}
type InstanceConfigInterfaceUpdateOptions struct {
Primary bool `json:"primary,omitempty"`
IPv4 *VPCIPv4 `json:"ipv4,omitempty"`
IPRanges *[]string `json:"ip_ranges,omitempty"`
}
type InstanceConfigInterfacesReorderOptions struct {
IDs []int `json:"ids"`
}
func getInstanceConfigInterfacesCreateOptionsList(
interfaces []InstanceConfigInterface,
) []InstanceConfigInterfaceCreateOptions {
interfaceOptsList := make([]InstanceConfigInterfaceCreateOptions, len(interfaces))
for index, configInterface := range interfaces {
interfaceOptsList[index] = configInterface.GetCreateOptions()
}
return interfaceOptsList
}
func (i InstanceConfigInterface) GetCreateOptions() InstanceConfigInterfaceCreateOptions {
opts := InstanceConfigInterfaceCreateOptions{
Label: i.Label,
Purpose: i.Purpose,
Primary: i.Primary,
SubnetID: i.SubnetID,
}
if len(i.IPRanges) > 0 {
opts.IPRanges = i.IPRanges
}
if i.Purpose == InterfacePurposeVPC && i.IPv4 != nil {
opts.IPv4 = &VPCIPv4{
VPC: i.IPv4.VPC,
NAT1To1: i.IPv4.NAT1To1,
}
}
opts.IPAMAddress = i.IPAMAddress
return opts
}
func (i InstanceConfigInterface) GetUpdateOptions() InstanceConfigInterfaceUpdateOptions {
opts := InstanceConfigInterfaceUpdateOptions{
Primary: i.Primary,
}
if i.Purpose == InterfacePurposeVPC && i.IPv4 != nil {
opts.IPv4 = &VPCIPv4{
VPC: i.IPv4.VPC,
NAT1To1: i.IPv4.NAT1To1,
}
}
if i.IPRanges != nil {
// Copy the slice to prevent accidental
// mutations
copiedIPRanges := make([]string, len(i.IPRanges))
copy(copiedIPRanges, i.IPRanges)
opts.IPRanges = &copiedIPRanges
}
return opts
}
func (c *Client) AppendInstanceConfigInterface(
ctx context.Context,
linodeID int,
configID int,
opts InstanceConfigInterfaceCreateOptions,
) (*InstanceConfigInterface, error) {
e := formatAPIPath("/linode/instances/%d/configs/%d/interfaces", linodeID, configID)
response, err := doPOSTRequest[InstanceConfigInterface](ctx, c, e, opts)
if err != nil {
return nil, err
}
return response, nil
}
func (c *Client) GetInstanceConfigInterface(
ctx context.Context,
linodeID int,
configID int,
interfaceID int,
) (*InstanceConfigInterface, error) {
e := formatAPIPath(
"linode/instances/%d/configs/%d/interfaces/%d",
linodeID,
configID,
interfaceID,
)
response, err := doGETRequest[InstanceConfigInterface](ctx, c, e)
if err != nil {
return nil, err
}
return response, nil
}
func (c *Client) ListInstanceConfigInterfaces(
ctx context.Context,
linodeID int,
configID int,
) ([]InstanceConfigInterface, error) {
e := formatAPIPath(
"linode/instances/%d/configs/%d/interfaces",
linodeID,
configID,
)
response, err := doGETRequest[[]InstanceConfigInterface](ctx, c, e)
if err != nil {
return nil, err
}
return *response, nil
}
func (c *Client) UpdateInstanceConfigInterface(
ctx context.Context,
linodeID int,
configID int,
interfaceID int,
opts InstanceConfigInterfaceUpdateOptions,
) (*InstanceConfigInterface, error) {
e := formatAPIPath(
"linode/instances/%d/configs/%d/interfaces/%d",
linodeID,
configID,
interfaceID,
)
response, err := doPUTRequest[InstanceConfigInterface](ctx, c, e, opts)
if err != nil {
return nil, err
}
return response, nil
}
func (c *Client) DeleteInstanceConfigInterface(
ctx context.Context,
linodeID int,
configID int,
interfaceID int,
) error {
e := formatAPIPath(
"linode/instances/%d/configs/%d/interfaces/%d",
linodeID,
configID,
interfaceID,
)
err := doDELETERequest(ctx, c, e)
return err
}
func (c *Client) ReorderInstanceConfigInterfaces(
ctx context.Context,
linodeID int,
configID int,
opts InstanceConfigInterfacesReorderOptions,
) error {
e := formatAPIPath(
"linode/instances/%d/configs/%d/interfaces/order",
linodeID,
configID,
)
_, err := doPOSTRequest[OAuthClient](ctx, c, e, opts)
return err
}
|