|
| 1 | +package linodego |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | +) |
| 8 | + |
| 9 | +// InstanceConfigInterface contains information about a configuration's network interface |
| 10 | +type InstanceConfigInterface struct { |
| 11 | + ID int `json:"id"` |
| 12 | + IPAMAddress string `json:"ipam_address"` |
| 13 | + Label string `json:"label"` |
| 14 | + Purpose ConfigInterfacePurpose `json:"purpose"` |
| 15 | + Primary bool `json:"primary"` |
| 16 | + Active bool `json:"active"` |
| 17 | + VPCID *int `json:"vpc_id"` |
| 18 | + SubnetID *int `json:"subnet_id"` |
| 19 | + IPv4 VPCIPv4 `json:"ipv4"` |
| 20 | + IPRanges []string `json:"ip_ranges"` |
| 21 | +} |
| 22 | + |
| 23 | +type VPCIPv4 struct { |
| 24 | + VPC string `json:"vpc,omitempty"` |
| 25 | + NAT1To1 string `json:"nat_1_1,omitempty"` |
| 26 | +} |
| 27 | + |
| 28 | +type InstanceConfigInterfaceCreateOptions struct { |
| 29 | + IPAMAddress string `json:"ipam_address,omitempty"` |
| 30 | + Label string `json:"label,omitempty"` |
| 31 | + Purpose ConfigInterfacePurpose `json:"purpose,omitempty"` |
| 32 | + Primary bool `json:"primary,omitempty"` |
| 33 | + SubnetID *int `json:"subnet_id,omitempty"` |
| 34 | + IPv4 *VPCIPv4 `json:"ipv4,omitempty"` |
| 35 | + IPRanges []string `json:"ip_ranges,omitempty"` |
| 36 | +} |
| 37 | + |
| 38 | +type InstanceConfigInterfaceUpdateOptions struct { |
| 39 | + Primary bool `json:"primary,omitempty"` |
| 40 | + IPv4 *VPCIPv4 `json:"ipv4,omitempty"` |
| 41 | + IPRanges []string `json:"ip_ranges,omitempty"` |
| 42 | +} |
| 43 | + |
| 44 | +type InstanceConfigInterfacesReorderOptions struct { |
| 45 | + IDs []int `json:"ids"` |
| 46 | +} |
| 47 | + |
| 48 | +func getInstanceConfigInterfacesCreateOptionsList( |
| 49 | + interfaces []InstanceConfigInterface, |
| 50 | +) []InstanceConfigInterfaceCreateOptions { |
| 51 | + interfaceOptsList := make([]InstanceConfigInterfaceCreateOptions, len(interfaces)) |
| 52 | + for index, configInterface := range interfaces { |
| 53 | + interfaceOptsList[index] = configInterface.GetCreateOptions() |
| 54 | + } |
| 55 | + return interfaceOptsList |
| 56 | +} |
| 57 | + |
| 58 | +func (i InstanceConfigInterface) GetCreateOptions() InstanceConfigInterfaceCreateOptions { |
| 59 | + opts := InstanceConfigInterfaceCreateOptions{ |
| 60 | + Label: i.Label, |
| 61 | + Purpose: i.Purpose, |
| 62 | + Primary: i.Primary, |
| 63 | + SubnetID: i.SubnetID, |
| 64 | + } |
| 65 | + |
| 66 | + if len(i.IPRanges) > 0 { |
| 67 | + opts.IPRanges = i.IPRanges |
| 68 | + } |
| 69 | + |
| 70 | + if i.Purpose == InterfacePurposeVPC && |
| 71 | + i.IPv4.NAT1To1 != "" && i.IPv4.VPC != "" { |
| 72 | + opts.IPv4 = &VPCIPv4{ |
| 73 | + VPC: i.IPv4.VPC, |
| 74 | + NAT1To1: i.IPv4.NAT1To1, |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // workaround for API issue |
| 79 | + if i.IPAMAddress == "222" { |
| 80 | + opts.IPAMAddress = "" |
| 81 | + } else { |
| 82 | + opts.IPAMAddress = i.IPAMAddress |
| 83 | + } |
| 84 | + |
| 85 | + return opts |
| 86 | +} |
| 87 | + |
| 88 | +func (i InstanceConfigInterface) GetUpdateOptions() InstanceConfigInterfaceUpdateOptions { |
| 89 | + opts := InstanceConfigInterfaceUpdateOptions{ |
| 90 | + Primary: i.Primary, |
| 91 | + } |
| 92 | + |
| 93 | + if i.Purpose == InterfacePurposeVPC { |
| 94 | + opts.IPv4 = &VPCIPv4{ |
| 95 | + VPC: i.IPv4.VPC, |
| 96 | + NAT1To1: i.IPv4.NAT1To1, |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if len(i.IPRanges) > 0 { |
| 101 | + opts.IPRanges = i.IPRanges |
| 102 | + } |
| 103 | + |
| 104 | + return opts |
| 105 | +} |
| 106 | + |
| 107 | +func (c *Client) AppendInstanceConfigInterface( |
| 108 | + ctx context.Context, |
| 109 | + linodeID int, |
| 110 | + configID int, |
| 111 | + opts InstanceConfigInterfaceCreateOptions, |
| 112 | +) (*InstanceConfigInterface, error) { |
| 113 | + body, err := json.Marshal(opts) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + req := c.R(ctx).SetResult(&InstanceConfigInterface{}).SetBody(string(body)) |
| 119 | + e := fmt.Sprintf("/linode/instances/%d/configs/%d/interfaces", linodeID, configID) |
| 120 | + r, err := coupleAPIErrors(req.Post(e)) |
| 121 | + if err != nil { |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + |
| 125 | + return r.Result().(*InstanceConfigInterface), nil |
| 126 | +} |
| 127 | + |
| 128 | +func (c *Client) GetInstanceConfigInterface( |
| 129 | + ctx context.Context, |
| 130 | + linodeID int, |
| 131 | + configID int, |
| 132 | + interfaceID int, |
| 133 | +) (*InstanceConfigInterface, error) { |
| 134 | + e := fmt.Sprintf( |
| 135 | + "linode/instances/%d/configs/%d/interfaces/%d", |
| 136 | + linodeID, |
| 137 | + configID, |
| 138 | + interfaceID, |
| 139 | + ) |
| 140 | + req := c.R(ctx).SetResult(&InstanceConfigInterface{}) |
| 141 | + r, err := coupleAPIErrors(req.Get(e)) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + return r.Result().(*InstanceConfigInterface), nil |
| 146 | +} |
| 147 | + |
| 148 | +func (c *Client) ListInstanceConfigInterfaces( |
| 149 | + ctx context.Context, |
| 150 | + linodeID int, |
| 151 | + configID int, |
| 152 | +) ([]InstanceConfigInterface, error) { |
| 153 | + e := fmt.Sprintf( |
| 154 | + "linode/instances/%d/configs/%d/interfaces", |
| 155 | + linodeID, |
| 156 | + configID, |
| 157 | + ) |
| 158 | + req := c.R(ctx).SetResult([]InstanceConfigInterface{}) |
| 159 | + r, err := coupleAPIErrors(req.Get(e)) |
| 160 | + if err != nil { |
| 161 | + return nil, err |
| 162 | + } |
| 163 | + return *r.Result().(*[]InstanceConfigInterface), nil |
| 164 | +} |
| 165 | + |
| 166 | +func (c *Client) UpdateInstanceConfigInterface( |
| 167 | + ctx context.Context, |
| 168 | + linodeID int, |
| 169 | + configID int, |
| 170 | + interfaceID int, |
| 171 | + opts InstanceConfigInterfaceUpdateOptions, |
| 172 | +) (*InstanceConfigInterface, error) { |
| 173 | + body, err := json.Marshal(opts) |
| 174 | + if err != nil { |
| 175 | + return nil, err |
| 176 | + } |
| 177 | + |
| 178 | + e := fmt.Sprintf( |
| 179 | + "linode/instances/%d/configs/%d/interfaces/%d", |
| 180 | + linodeID, |
| 181 | + configID, |
| 182 | + interfaceID, |
| 183 | + ) |
| 184 | + req := c.R(ctx).SetResult(&InstanceConfigInterface{}).SetBody(string(body)) |
| 185 | + r, err := coupleAPIErrors(req.Put(e)) |
| 186 | + if err != nil { |
| 187 | + return nil, err |
| 188 | + } |
| 189 | + return r.Result().(*InstanceConfigInterface), nil |
| 190 | +} |
| 191 | + |
| 192 | +func (c *Client) DeleteInstanceConfigInterface( |
| 193 | + ctx context.Context, |
| 194 | + linodeID int, |
| 195 | + configID int, |
| 196 | + interfaceID int, |
| 197 | +) error { |
| 198 | + e := fmt.Sprintf( |
| 199 | + "linode/instances/%d/configs/%d/interfaces/%d", |
| 200 | + linodeID, |
| 201 | + configID, |
| 202 | + interfaceID, |
| 203 | + ) |
| 204 | + _, err := coupleAPIErrors(c.R(ctx).Delete(e)) |
| 205 | + return err |
| 206 | +} |
| 207 | + |
| 208 | +func (c *Client) ReorderInstanceConfigInterfaces( |
| 209 | + ctx context.Context, |
| 210 | + linodeID int, |
| 211 | + configID int, |
| 212 | + opts InstanceConfigInterfacesReorderOptions, |
| 213 | +) error { |
| 214 | + body, err := json.Marshal(opts) |
| 215 | + if err != nil { |
| 216 | + return err |
| 217 | + } |
| 218 | + e := fmt.Sprintf( |
| 219 | + "linode/instances/%d/configs/%d/interfaces/order", |
| 220 | + linodeID, |
| 221 | + configID, |
| 222 | + ) |
| 223 | + |
| 224 | + req := c.R(ctx).SetBody(string(body)) |
| 225 | + _, err = coupleAPIErrors(req.Post(e)) |
| 226 | + |
| 227 | + return err |
| 228 | +} |
0 commit comments