From 1e06f654d9f8f8f0dba5d7dda48e6bc29acee9fe Mon Sep 17 00:00:00 2001 From: Tomas Karasek Date: Mon, 23 Mar 2020 16:44:00 +0100 Subject: [PATCH] bump packngo version 23032020 --- go.mod | 2 +- go.sum | 4 +- .../github.com/packethost/packngo/devices.go | 113 ++++--- vendor/github.com/packethost/packngo/ports.go | 287 ++++++++++-------- vendor/modules.txt | 2 +- 5 files changed, 250 insertions(+), 158 deletions(-) diff --git a/go.mod b/go.mod index d652b54..b155dc9 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/hashicorp/vault/api v1.0.5-0.20200215224050-f6547fa8e820 github.com/hashicorp/vault/sdk v0.1.14-0.20200215224050-f6547fa8e820 github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect - github.com/packethost/packngo v0.2.1-0.20200220143658-05f78a847cb6 + github.com/packethost/packngo v0.2.1-0.20200320175010-9cdf732e0c3e golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db // indirect google.golang.org/genproto v0.0.0-20190404172233-64821d5d2107 // indirect diff --git a/go.sum b/go.sum index 5606cf3..b425c77 100644 --- a/go.sum +++ b/go.sum @@ -102,8 +102,8 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= -github.com/packethost/packngo v0.2.1-0.20200220143658-05f78a847cb6 h1:GL+IdflHCr5+5S2wqXOq7F7D/gfIaZJwJWb5ogRxaKc= -github.com/packethost/packngo v0.2.1-0.20200220143658-05f78a847cb6/go.mod h1:lTgI4wr0T6uAIArEQtlx1wu+n0cEy+opDJpIBAHvQ34= +github.com/packethost/packngo v0.2.1-0.20200320175010-9cdf732e0c3e h1:dUPdoO8yliTx2fbWcwT+uP+/qmQ90wdxB1x4TTUr/T0= +github.com/packethost/packngo v0.2.1-0.20200320175010-9cdf732e0c3e/go.mod h1:lTgI4wr0T6uAIArEQtlx1wu+n0cEy+opDJpIBAHvQ34= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM8aXeqhl0I= github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= diff --git a/vendor/github.com/packethost/packngo/devices.go b/vendor/github.com/packethost/packngo/devices.go index 707f62a..8069e60 100644 --- a/vendor/github.com/packethost/packngo/devices.go +++ b/vendor/github.com/packethost/packngo/devices.go @@ -1,7 +1,6 @@ package packngo import ( - "encoding/json" "fmt" ) @@ -28,8 +27,8 @@ type devicesRoot struct { Meta meta `json:"meta"` } -// DeviceRaw represents a Packet device from API -type DeviceRaw struct { +// Device represents a Packet device from API +type Device struct { ID string `json:"id"` Href string `json:"href,omitempty"` Hostname string `json:"hostname,omitempty"` @@ -63,27 +62,6 @@ type DeviceRaw struct { SwitchUUID string `json:"switch_uuid,omitempty"` } -type Device struct { - DeviceRaw - NetworkType string -} - -func (d *Device) UnmarshalJSON(b []byte) error { - dJSON := DeviceRaw{} - if err := json.Unmarshal(b, &dJSON); err != nil { - return err - } - d.DeviceRaw = dJSON - if len(dJSON.NetworkPorts) > 0 { - networkType, err := dJSON.GetNetworkType() - if err != nil { - return err - } - d.NetworkType = networkType - } - return nil -} - type NetworkInfo struct { PublicIPv4 string PublicIPv6 string @@ -113,21 +91,88 @@ func (d Device) String() string { return Stringify(d) } -func (d DeviceRaw) GetNetworkType() (string, error) { - if len(d.NetworkPorts) == 0 { - return "", fmt.Errorf("Device has no network ports listed") - } +func (d *Device) NumOfBonds() int { + numOfBonds := 0 for _, p := range d.NetworkPorts { - if p.Name == "bond0" { - return p.NetworkType, nil + if p.Type == "NetworkBondPort" { + numOfBonds++ + } + } + return numOfBonds +} + +func (d *Device) GetPortsInBond(name string) map[string]*Port { + ports := map[string]*Port{} + for _, port := range d.NetworkPorts { + if port.Bond != nil && port.Bond.Name == name { + p := port + ports[p.Name] = &p + } + } + return ports +} + +func (d *Device) GetBondPorts() map[string]*Port { + ports := map[string]*Port{} + for _, port := range d.NetworkPorts { + if port.Type == "NetworkBondPort" { + p := port + ports[p.Name] = &p + } + } + return ports +} + +func (d *Device) GetPhysicalPorts() map[string]*Port { + ports := map[string]*Port{} + for _, port := range d.NetworkPorts { + if port.Type == "NetworkPort" { + p := port + ports[p.Name] = &p + } + } + return ports +} + +func (d *Device) GetPortByName(name string) (*Port, error) { + for _, port := range d.NetworkPorts { + if port.Name == name { + return &port, nil + } + } + return nil, fmt.Errorf("Port %s not found in device %s", name, d.ID) +} + +func (d *Device) GetNetworkType() (string, error) { + numOfBonds := d.NumOfBonds() + if (numOfBonds < 1) || (numOfBonds > 2) { + return "", fmt.Errorf("Wrong number of Bond ports") + } + bond0, err := d.GetPortByName("bond0") + if err != nil { + return "", err + } + if numOfBonds == 2 { + bond1, err := d.GetPortByName("bond1") + if err != nil { + return "", err + } + if bond0.NetworkType == bond1.NetworkType { + return bond0.NetworkType, nil + } + if (bond0.NetworkType == "layer3") && (bond1.NetworkType == "layer2-individual") { + return "hybrid", nil } + return "", fmt.Errorf("Strange 2-bond ports conf - bond0: %s, bond1: %s", bond0.NetworkType, bond1.NetworkType) } - return "", fmt.Errorf("Bound port not found") + return bond0.NetworkType, nil } type IPAddressCreateRequest struct { - AddressFamily int `json:"address_family"` - Public bool `json:"public"` + AddressFamily int `json:"address_family"` + Public bool `json:"public"` + CIDR int `json:"cidr,omitempty"` + Reservations []string `json:"ip_reservations,omitempty"` } // DeviceCreateRequest type used to create a Packet device @@ -152,7 +197,7 @@ type DeviceCreateRequest struct { // UserSSHKeys is a list of user UUIDs - essentialy a list of // collaborators. The users must be a collaborator in the same project // where the device is created. The user's SSH keys then go to the - // device. + // device UserSSHKeys []string `json:"user_ssh_keys,omitempty"` // Project SSHKeys is a list of SSHKeys resource UUIDs. If this param // is supplied, only the listed SSHKeys will go to the device. diff --git a/vendor/github.com/packethost/packngo/ports.go b/vendor/github.com/packethost/packngo/ports.go index 35cdba2..d4c8a94 100644 --- a/vendor/github.com/packethost/packngo/ports.go +++ b/vendor/github.com/packethost/packngo/ports.go @@ -2,6 +2,7 @@ package packngo import ( "fmt" + "strings" ) const portBasePath = "/ports" @@ -12,16 +13,15 @@ type DevicePortService interface { Unassign(*PortAssignRequest) (*Port, *Response, error) AssignNative(*PortAssignRequest) (*Port, *Response, error) UnassignNative(string) (*Port, *Response, error) - Bond(*BondRequest) (*Port, *Response, error) - Disbond(*DisbondRequest) (*Port, *Response, error) + Bond(*Port, bool) (*Port, *Response, error) + Disbond(*Port, bool) (*Port, *Response, error) DeviceToNetworkType(string, string) (*Device, error) DeviceNetworkType(string) (string, error) - PortToLayerTwo(string) (*Port, *Response, error) - PortToLayerThree(string) (*Port, *Response, error) - DeviceToLayerTwo(string) (*Device, error) - DeviceToLayerThree(string) (*Device, error) - GetBondPort(string) (*Port, error) + PortToLayerTwo(string, string) (*Port, *Response, error) + PortToLayerThree(string, string) (*Port, *Response, error) GetPortByName(string, string) (*Port, error) + Convert1BondDevice(*Device, string) error + Convert2BondDevice(*Device, string) error } type PortData struct { @@ -29,6 +29,11 @@ type PortData struct { Bonded bool `json:"bonded"` } +type BondData struct { + ID string `json:"id"` + Name string `json:"name"` +} + type Port struct { ID string `json:"id"` Type string `json:"type"` @@ -37,6 +42,7 @@ type Port struct { NetworkType string `json:"network_type,omitempty"` NativeVirtualNetwork *VirtualNetwork `json:"native_virtual_network"` AttachedVirtualNetworks []VirtualNetwork `json:"virtual_networks"` + Bond *BondData `json:"bond"` } type AddressRequest struct { @@ -67,32 +73,12 @@ type DisbondRequest struct { BulkDisable bool `json:"bulk_disable"` } -func (i *DevicePortServiceOp) GetBondPort(deviceID string) (*Port, error) { - device, _, err := i.client.Devices.Get(deviceID, nil) - if err != nil { - return nil, err - } - for _, port := range device.NetworkPorts { - if port.Type == "NetworkBondPort" { - return &port, nil - } - } - - return nil, fmt.Errorf("No bonded port found in device %s", deviceID) -} - func (i *DevicePortServiceOp) GetPortByName(deviceID, name string) (*Port, error) { device, _, err := i.client.Devices.Get(deviceID, nil) if err != nil { return nil, err } - for _, port := range device.NetworkPorts { - if port.Name == name { - return &port, nil - } - } - - return nil, fmt.Errorf("Port %s not found in device %s", name, deviceID) + return device.GetPortByName(name) } func (i *DevicePortServiceOp) Assign(par *PortAssignRequest) (*Port, *Response, error) { @@ -122,12 +108,20 @@ func (i *DevicePortServiceOp) Unassign(par *PortAssignRequest) (*Port, *Response return i.portAction(path, par) } -func (i *DevicePortServiceOp) Bond(br *BondRequest) (*Port, *Response, error) { +func (i *DevicePortServiceOp) Bond(p *Port, be bool) (*Port, *Response, error) { + if p.Data.Bonded { + return p, nil, nil + } + br := &BondRequest{PortID: p.ID, BulkEnable: be} path := fmt.Sprintf("%s/%s/bond", portBasePath, br.PortID) return i.portAction(path, br) } -func (i *DevicePortServiceOp) Disbond(dr *DisbondRequest) (*Port, *Response, error) { +func (i *DevicePortServiceOp) Disbond(p *Port, bd bool) (*Port, *Response, error) { + if !p.Data.Bonded { + return p, nil, nil + } + dr := &DisbondRequest{PortID: p.ID, BulkDisable: bd} path := fmt.Sprintf("%s/%s/disbond", portBasePath, dr.PortID) return i.portAction(path, dr) } @@ -143,8 +137,15 @@ func (i *DevicePortServiceOp) portAction(path string, req interface{}) (*Port, * return port, resp, err } -func (i *DevicePortServiceOp) PortToLayerTwo(portID string) (*Port, *Response, error) { - path := fmt.Sprintf("%s/%s/convert/layer-2", portBasePath, portID) +func (i *DevicePortServiceOp) PortToLayerTwo(deviceID, portName string) (*Port, *Response, error) { + p, err := i.client.DevicePorts.GetPortByName(deviceID, portName) + if err != nil { + return nil, nil, err + } + if strings.HasPrefix(p.NetworkType, "layer2") { + return p, nil, nil + } + path := fmt.Sprintf("%s/%s/convert/layer-2", portBasePath, p.ID) port := new(Port) resp, err := i.client.DoRequest("POST", path, nil, port) @@ -155,8 +156,15 @@ func (i *DevicePortServiceOp) PortToLayerTwo(portID string) (*Port, *Response, e return port, resp, err } -func (i *DevicePortServiceOp) PortToLayerThree(portID string) (*Port, *Response, error) { - path := fmt.Sprintf("%s/%s/convert/layer-3", portBasePath, portID) +func (i *DevicePortServiceOp) PortToLayerThree(deviceID, portName string) (*Port, *Response, error) { + p, err := i.client.DevicePorts.GetPortByName(deviceID, portName) + if err != nil { + return nil, nil, err + } + if p.NetworkType == "layer3" { + return p, nil, nil + } + path := fmt.Sprintf("%s/%s/convert/layer-3", portBasePath, p.ID) port := new(Port) req := BackToL3Request{ @@ -180,142 +188,181 @@ func (i *DevicePortServiceOp) DeviceNetworkType(deviceID string) (string, error) if err != nil { return "", err } - return d.NetworkType, nil + return d.GetNetworkType() } -func (i *DevicePortServiceOp) DeviceToNetworkType(deviceID string, nType string) (*Device, error) { - - d, _, err := i.client.Devices.Get(deviceID, nil) - if err != nil { - return nil, err - } - - curType := d.NetworkType +func (i *DevicePortServiceOp) Convert2BondDevice(d *Device, targetType string) error { + bondPorts := d.GetBondPorts() + ethPorts := d.GetPhysicalPorts() - if curType == nType { - return nil, fmt.Errorf("Device already is in state %s", nType) - } - bond0ID := "" - eth1ID := "" - for _, port := range d.NetworkPorts { - if port.Name == "bond0" { - bond0ID = port.ID + if targetType == "layer3" { + for _, p := range ethPorts { + _, _, err := i.client.DevicePorts.Bond(p, false) + if err != nil { + return err + } } - if port.Name == "eth1" { - eth1ID = port.ID + for _, p := range bondPorts { + _, _, err := i.client.DevicePorts.PortToLayerThree(d.ID, p.Name) + if err != nil { + return err + } } } - - if nType == "layer3" { - if curType == "layer2-individual" || curType == "layer2-bonded" { - if curType == "layer2-individual" { - _, _, err := i.client.DevicePorts.Bond( - &BondRequest{PortID: bond0ID, BulkEnable: false}) - if err != nil { - return nil, err - } - - } - _, _, err := i.client.DevicePorts.PortToLayerThree(bond0ID) + if targetType == "hybrid" { + for _, p := range d.GetPortsInBond("bond1") { + _, _, err := i.client.DevicePorts.Disbond(p, false) if err != nil { - return nil, err + return err } } - _, _, err = i.client.DevicePorts.Bond( - &BondRequest{PortID: bond0ID, BulkEnable: true}) + _, _, err := i.client.DevicePorts.PortToLayerThree(d.ID, "bond0") + if err != nil { + return err + } + _, _, err = i.client.DevicePorts.PortToLayerTwo(d.ID, "bond1") if err != nil { - return nil, err + return err } } - if nType == "hybrid" { - if curType == "layer2-individual" || curType == "layer2-bonded" { - if curType == "layer2-individual" { - _, _, err = i.client.DevicePorts.Bond( - &BondRequest{PortID: bond0ID, BulkEnable: false}) - if err != nil { - return nil, err - } - } - _, _, err = i.client.DevicePorts.PortToLayerThree(bond0ID) + if targetType == "layer2-individual" { + for _, p := range bondPorts { + _, _, err := i.client.DevicePorts.PortToLayerTwo(d.ID, p.Name) if err != nil { - return nil, err + return err } } - _, _, err := i.client.DevicePorts.Disbond( - &DisbondRequest{PortID: eth1ID, BulkDisable: false}) - if err != nil { - return nil, err + for _, p := range ethPorts { + _, _, err := i.client.DevicePorts.Disbond(p, false) + if err != nil { + return err + } } } - if nType == "layer2-individual" { - if curType == "hybrid" || curType == "layer3" { - _, _, err = i.client.DevicePorts.PortToLayerTwo(bond0ID) + if targetType == "layer2-bonded" { + for _, p := range bondPorts { + _, _, err := i.client.DevicePorts.PortToLayerTwo(d.ID, p.Name) if err != nil { - return nil, err + return err } + } + for _, p := range ethPorts { + _, _, err := i.client.DevicePorts.Bond(p, false) + if err != nil { + return err + } + } + } + return nil +} + +func (i *DevicePortServiceOp) Convert1BondDevice(d *Device, targetType string) error { + bond0, err := d.GetPortByName("bond0") + if err != nil { + return err + } + bond0ports := d.GetPortsInBond("bond0") + if targetType == "layer3" { + for _, p := range bond0ports { + _, _, err = i.client.DevicePorts.Bond(p, false) + if err != nil { + return err + } } - _, _, err = i.client.DevicePorts.Disbond( - &DisbondRequest{PortID: bond0ID, BulkDisable: true}) + _, _, err = i.client.DevicePorts.PortToLayerThree(d.ID, "bond0") if err != nil { - return nil, err + return err } } - if nType == "layer2-bonded" { - if curType == "hybrid" || curType == "layer3" { - _, _, err = i.client.DevicePorts.PortToLayerTwo(bond0ID) + if targetType == "hybrid" { + bond0, _, err = i.client.DevicePorts.Bond(bond0, false) + if err != nil { + return err + } + bond0, _, err = i.client.DevicePorts.PortToLayerThree(d.ID, "bond0") + if err != nil { + return err + } + eth1, err := i.client.DevicePorts.GetPortByName(d.ID, "eth1") + if err != nil { + return err + } + _, _, err = i.client.DevicePorts.Disbond(eth1, false) + if err != nil { + return err + } + } + if targetType == "layer2-individual" { + bond0, _, err = i.client.DevicePorts.PortToLayerTwo(d.ID, "bond0") + if err != nil { + return err + } + _, _, err = i.client.DevicePorts.Disbond(bond0, true) + if err != nil { + return err + } + } + if targetType == "layer2-bonded" { + for _, p := range bond0ports { + _, _, err = i.client.DevicePorts.Bond(p, false) if err != nil { - return nil, err + return err } } - _, _, err = i.client.DevicePorts.Bond( - &BondRequest{PortID: bond0ID, BulkEnable: false}) + bond0, _, err = i.client.DevicePorts.PortToLayerTwo(d.ID, "bond0") if err != nil { - return nil, err + return err } } + return nil +} - d, _, err = i.client.Devices.Get(deviceID, nil) +func (i *DevicePortServiceOp) DeviceToNetworkType(deviceID string, targetType string) (*Device, error) { + + d, _, err := i.client.Devices.Get(deviceID, nil) if err != nil { return nil, err } - if d.NetworkType != nType { - return nil, fmt.Errorf( - "Failed to convert device %s from %s to %s. New type was %s", - deviceID, curType, nType, d.NetworkType) + curType, err := d.GetNetworkType() + if err != nil { + return nil, err + } + if curType == targetType { + return nil, fmt.Errorf("Device already is in state %s", targetType) } - return d, err -} -func (i *DevicePortServiceOp) DeviceToLayerThree(deviceID string) (*Device, error) { - // hopefull all the VLANs are unassigned at this point - bond0, err := i.client.DevicePorts.GetBondPort(deviceID) - if err != nil { - return nil, err + numOfBonds := d.NumOfBonds() + if (numOfBonds < 1) || (numOfBonds > 2) { + return nil, fmt.Errorf("Strange number of bonds: %d", numOfBonds) } - bond0, _, err = i.client.DevicePorts.PortToLayerThree(bond0.ID) + if numOfBonds == 1 { + err = i.client.DevicePorts.Convert1BondDevice(d, targetType) + } else { + err = i.client.DevicePorts.Convert2BondDevice(d, targetType) + } if err != nil { return nil, err } - d, _, err := i.client.Devices.Get(deviceID, nil) - return d, err -} -// DeviceToLayerTwo converts device to L2 networking. Use bond0 to attach VLAN. -func (i *DevicePortServiceOp) DeviceToLayerTwo(deviceID string) (*Device, error) { - bond0, err := i.client.DevicePorts.GetBondPort(deviceID) + d, _, err = i.client.Devices.Get(deviceID, nil) if err != nil { return nil, err } - bond0, _, err = i.client.DevicePorts.PortToLayerTwo(bond0.ID) + finalType, err := d.GetNetworkType() if err != nil { return nil, err } - d, _, err := i.client.Devices.Get(deviceID, nil) - return d, err + if finalType != targetType { + return nil, fmt.Errorf( + "Failed to convert device %s from %s to %s. New type was %s", + deviceID, curType, targetType, finalType) + + } + return d, err } diff --git a/vendor/modules.txt b/vendor/modules.txt index ce341b8..54de2a1 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -90,7 +90,7 @@ github.com/mitchellh/go-testing-interface github.com/mitchellh/mapstructure # github.com/oklog/run v1.0.0 github.com/oklog/run -# github.com/packethost/packngo v0.2.1-0.20200220143658-05f78a847cb6 +# github.com/packethost/packngo v0.2.1-0.20200320175010-9cdf732e0c3e github.com/packethost/packngo # github.com/pierrec/lz4 v2.0.5+incompatible github.com/pierrec/lz4