Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement setting bandwidth_limit on a servers network interface #206

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ If the change isn't user-facing but still relevant enough for a changelog entry,

* (internal)? scope: short description (#pr, @author)
-->
## Added
* Add ability to set the bandwidth limit per network interface on the server resource (#206 @89q12)

### Fixed
* resource/anxcloud_virtual_server: Handle missing VM info more gracefully to prevent division by zero panic (#170, @anx-mschaefer)
Expand Down
6 changes: 6 additions & 0 deletions anxcloud/resource_virtual_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ The virtual_server resource allows you to configure and run virtual machines.
log.Fatalf("[ERROR] unable to force new '%s': %v", key, err)
}
}
if newNet.BandwidthLimit != oldNets[i].BandwidthLimit {
key := fmt.Sprintf("network.%d.bandwidth_limit", i)
if err := d.ForceNew(key); err != nil {
log.Fatalf("[ERROR] unable to force new '%s': %v", key, err)
}
nachtjasmin marked this conversation as resolved.
Show resolved Hide resolved
}

if len(newNet.IPs) < len(oldNets[i].IPs) {
// IPs are missing
Expand Down
4 changes: 2 additions & 2 deletions anxcloud/resource_virtual_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,10 @@ func testAccCheckAnxCloudVirtualServerDisks(n string, expectedDisks []vm.Disk) r

func generateNetworkSubResourceString(networks []vm.Network) string {
var output string
template := "\nnetwork {\n\tvlan_id = \"%s\"\n\tnic_type = \"%s\"\n\tips = [\"%s\"]\n}\n"
template := "\nnetwork {\n\tvlan_id = \"%s\"\n\tnic_type = \"%s\"\n\tbandwidth_limit = \"%s\"\n\tips = [\"%s\"]\n}\n"

for _, n := range networks {
output += fmt.Sprintf(template, n.VLAN, n.NICType, n.IPs[0])
output += fmt.Sprintf(template, n.VLAN, n.NICType, fmt.Sprint(n.BandwidthLimit), n.IPs[0])
}

return output
Expand Down
11 changes: 11 additions & 0 deletions anxcloud/schema_virtual_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ func schemaVirtualServer() map[string]*schema.Schema {
Required: true,
Description: "Network interface card type.",
},
"bandwidth_limit": {
Type: schema.TypeInt,
Optional: true,
Default: 1000,
Description: "Network interface bandwidth limit in Megabit/s, default: 1000",
},
"ips": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -328,6 +334,11 @@ func schemaVirtualServer() map[string]*schema.Schema {
Computed: true,
Description: "NIC type number.",
},
"bandwidth_limit": {
Type: schema.TypeInt,
Computed: true,
Description: "Bandwidth limit of the interface in Megabit/s",
},
"vlan": {
Type: schema.TypeString,
Computed: true,
Expand Down
3 changes: 3 additions & 0 deletions anxcloud/struct_virtual_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func expandVirtualServerNetworks(p []interface{}) []vm.Network {
if v, ok := in["nic_type"]; ok {
network.NICType = v.(string)
}
if v, ok := in["bandwidth_limit"]; ok {
network.BandwidthLimit = v.(int)
}
if v, ok := in["ips"]; ok {
ips := v.(*schema.Set)
for _, ip := range ips.List() {
Expand Down
10 changes: 6 additions & 4 deletions anxcloud/struct_virtual_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ func TestExpanderVirtualServerNetworks(t *testing.T) {
{
[]interface{}{
map[string]interface{}{
"vlan_id": "38f8561acfe34qc49c336d2af31a5cc3",
"nic_type": "virtio",
"vlan_id": "38f8561acfe34qc49c336d2af31a5cc3",
"nic_type": "virtio",
"bandwidth_limit": 1000,
"ips": schema.NewSet(schema.HashSchema(&schema.Schema{Type: schema.TypeString}), []interface{}{
"identifier1",
"identifier2",
Expand All @@ -31,8 +32,9 @@ func TestExpanderVirtualServerNetworks(t *testing.T) {
},
[]vm.Network{
{
VLAN: "38f8561acfe34qc49c336d2af31a5cc3",
NICType: "virtio",
VLAN: "38f8561acfe34qc49c336d2af31a5cc3",
NICType: "virtio",
BandwidthLimit: 1000,
IPs: []string{
"10.11.12.13",
"1.0.0.1",
Expand Down
12 changes: 7 additions & 5 deletions docs/resources/virtual_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ resource "anxcloud_virtual_server" "example" {
# Set network interface
network {
vlan_id = anxcloud_vlan.example.id
ips = [anxcloud_ip_address.v4.id, anxcloud_ip_address.v6.id]
nic_type = "virtio"
vlan_id = anxcloud_vlan.example.id
ips = [anxcloud_ip_address.v4.id, anxcloud_ip_address.v6.id]
nic_type = "virtio"
bandwidth_limit = 1000
}
# Disk 1
Expand Down Expand Up @@ -158,8 +159,8 @@ Required:

Optional:

- `ips` (List of String) Requested list of IPs and IPs identifiers. IPs are ignored when using template_type 'from_scratch'. Defaults to free IPs from IP pool attached to VLAN.

- `bandwidth_limit` (Number) Network interface bandwidth limit in Megabit/s, default: 1000
- `ips` (Set of String) Requested set of IPs and IPs identifiers. IPs are ignored when using template_type 'from_scratch'. Defaults to free IPs from IP pool attached to VLAN.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`
Expand Down Expand Up @@ -220,5 +221,6 @@ Read-Only:
- `mac_address` (String) MAC address of the NIC.
- `nic` (Number) NIC type number.
- `vlan` (String) VLAN identifier.
- `bandwidth_limit` (Number) Network interface bandwidth limit in Megabit/s, default: 1000


1 change: 1 addition & 0 deletions examples/resources/anxcloud_virtual_server/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ resource "anxcloud_virtual_server" "example" {
vlan_id = anxcloud_vlan.example.id
ips = [anxcloud_ip_address.v4.id, anxcloud_ip_address.v6.id]
nic_type = "virtio"
bandwidth_limit = 1000
}

# Disk 1
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ require (
github.com/onsi/gomega v1.34.2
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b
github.com/stretchr/testify v1.9.0
go.anx.io/go-anxcloud v0.7.3
go.anx.io/go-anxcloud v0.7.4
k8s.io/client-go v0.31.1
)

require (
github.com/go-logr/stdr v1.2.2 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
)

require (
Expand Down Expand Up @@ -79,11 +79,11 @@ require (
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/zclconf/go-cty v1.14.4 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/term v0.25.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.24.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ github.com/zclconf/go-cty v1.14.4 h1:uXXczd9QDGsgu0i/QFR/hzI5NYCHLf6NQw/atrbnhq8
github.com/zclconf/go-cty v1.14.4/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b h1:FosyBZYxY34Wul7O/MSKey3txpPYyCqVO5ZyceuQJEI=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
go.anx.io/go-anxcloud v0.7.3 h1:NWkm4KAg0GyJALBbSgp++J2K563lHQGDDVJAcM6CgUU=
go.anx.io/go-anxcloud v0.7.3/go.mod h1:RpJvC8ZmXNu9dSygIgZ0ossqPz0+6n9xDX9weeATmSo=
go.anx.io/go-anxcloud v0.7.4 h1:+/J66wm299WnwvBd0pMC4qc8Nxvvij0cxUyzpSq1jqo=
go.anx.io/go-anxcloud v0.7.4/go.mod h1:4OObxqH+9mkf4i4C6VG7u6HCG4qTiMNsj+2kNP60WcE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down Expand Up @@ -265,19 +265,19 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
golang.org/x/term v0.25.0 h1:WtHI/ltw4NvSUig5KARz9h521QvRC8RmF/cuYqifU24=
golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
Loading