Skip to content

Commit

Permalink
💾 allow customising server memory and disk (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarhermoso authored Dec 8, 2024
1 parent 4ef9df6 commit 7f5bae6
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 14 deletions.
2 changes: 2 additions & 0 deletions docs/data-sources/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ Provides a Binary Lane Server data source. This can be used to read existing ser
### Read-Only

- `backups` (Boolean) If true this will enable two daily backups for the server. `options.daily_backups` will override this value if provided. Setting this to false has no effect.
- `disk` (Number) The amount of storage in GB assigned to the server.
- `image` (String) The slug of the selected operating system.
- `memory` (Number) The amount of memory in MB assigned to the server.
- `name` (String) The hostname of your server, such as vps01.yourcompany.com. If not provided, the server will be created with a random name.
- `permalink` (String) A randomly generated two-word identifier assigned to servers in regions that support this feature
- `port_blocking` (Boolean) Port blocking of outgoing connections for email, SSH and Remote Desktop (TCP ports 22, 25, and 3389) is enabled by default for all new servers. If this is false port blocking will be disabled. Disabling port blocking is only available to reviewed accounts.
Expand Down
14 changes: 14 additions & 0 deletions docs/resources/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ Provides a Binary Lane Server resource. This can be used to create and delete se
### Optional

- `backups` (Boolean) If true this will enable two daily backups for the server. `options.daily_backups` will override this value if provided. Setting this to false has no effect.
- `disk` (Number) The total storage in GB for this server. If specified this is the absolute value, not just the additional storage above what is included in the size.
Leave null to accept the default for the size if this is a new server or a resize to a different base size,
or to keep the current value if this a resize with the same base size but different options. Valid values:
- must be a multiple of 5
- \> 60GB must be a multiple of 10
- \> 200GB must be a multiple of 100
- `memory` (Number) The total memory in MB for this server. If specified this is the absolute value, not just the
additional memory above what is included in the size. Leave null to accept the default for the size if this is a new
server or a resize to a different base size, or to keep the current value if this a resize with the same base size
but different options. Valid values:
- must be a multiple of 128
- \> 2048MB must be a multiple of 1024
- \> 16384MB must be a multiple of 2048
- \> 24576MB must be a multiple of 4096
- `name` (String) The hostname of your server, such as vps01.yourcompany.com. If not provided, the server will be created with a random name.
- `password` (String, Sensitive) If this is provided the specified or default remote user's account password will be set to this value. Only valid if the server supports password change actions. If omitted and the server supports password change actions a random password will be generated and emailed to the account email address.
- `port_blocking` (Boolean) Port blocking of outgoing connections for email, SSH and Remote Desktop (TCP ports 22, 25, and 3389) is enabled by default for all new servers. If this is false port blocking will be disabled. Disabling port blocking is only available to reviewed accounts.
Expand Down
18 changes: 18 additions & 0 deletions internal/provider/server_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type serverDataModel struct {
PublicIpv4Addresses types.List `tfsdk:"public_ipv4_addresses"`
PrivateIPv4Addresses types.List `tfsdk:"private_ipv4_addresses"`
Permalink types.String `tfsdk:"permalink"`
Memory types.Int32 `tfsdk:"memory"`
Disk types.Int32 `tfsdk:"disk"`
}

func (d *serverDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
Expand Down Expand Up @@ -98,6 +100,20 @@ func (d *serverDataSource) Schema(ctx context.Context, req datasource.SchemaRequ
ElementType: types.StringType,
Computed: true,
}

memoryDescription := "The amount of memory in MB assigned to the server."
resp.Schema.Attributes["memory"] = &schema.Int32Attribute{
Description: memoryDescription,
MarkdownDescription: memoryDescription,
Computed: true,
}

diskDescription := "The amount of storage in GB assigned to the server."
resp.Schema.Attributes["disk"] = &schema.Int32Attribute{
Description: diskDescription,
MarkdownDescription: diskDescription,
Computed: true,
}
}

func (d *serverDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
Expand Down Expand Up @@ -137,6 +153,8 @@ func (d *serverDataSource) Read(ctx context.Context, req datasource.ReadRequest,
data.PortBlocking = types.BoolValue(serverResp.JSON200.Server.Networks.PortBlocking)
data.VpcId = types.Int64PointerValue(serverResp.JSON200.Server.VpcId)
data.Permalink = types.StringValue(*serverResp.JSON200.Server.Permalink)
data.Memory = types.Int32Value(*serverResp.JSON200.Server.Memory)
data.Disk = types.Int32Value(*serverResp.JSON200.Server.Disk)
publicIpv4Addresses := []string{}
privateIpv4Addresses := []string{}

Expand Down
119 changes: 105 additions & 14 deletions internal/provider/server_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func (r *serverResource) Schema(ctx context.Context, _ resource.SchemaRequest, r
// Default: int32default.StaticInt32(0), // TODO: Uncomment with 1.0 release (see issue #30)
Validators: []validator.Int32{
int32validator.AtLeast(0),
int32validator.AtMost(8),
},
}

Expand Down Expand Up @@ -260,6 +261,58 @@ func (r *serverResource) Schema(ctx context.Context, _ resource.SchemaRequest, r
},
}

memoryDescription := `The total memory in MB for this server. If specified this is the absolute value, not just the
additional memory above what is included in the size. Leave null to accept the default for the size if this is a new
server or a resize to a different base size, or to keep the current value if this a resize with the same base size
but different options.`
memoryValidValues := "Valid values must be a multiple of 128. If the value is greater than 2048MB, it must be a " +
"multiple of 1024. If the value is greater than 16384MB, it must be a multiple of 2048. If the value is greater " +
"than 24576MB, it must be a multiple of 4096."
memoryValidValuesMarkdown := ` Valid values:
- must be a multiple of 128
- \> 2048MB must be a multiple of 1024
- \> 16384MB must be a multiple of 2048
- \> 24576MB must be a multiple of 4096`

resp.Schema.Attributes["memory"] = &schema.Int32Attribute{
Description: memoryDescription + memoryValidValues,
MarkdownDescription: memoryDescription + memoryValidValuesMarkdown,
Optional: true,
Required: false,
Computed: true,
Validators: []validator.Int32{
int32validator.AtLeast(128),
MultipleOfValidator{Multiple: 128},
MultipleOfValidator{Multiple: 1024, RangeFrom: 2048, RangeTo: 16384},
MultipleOfValidator{Multiple: 2048, RangeFrom: 16384, RangeTo: 24576},
MultipleOfValidator{Multiple: 4096, RangeFrom: 24576},
},
}

diskDescription := `The total storage in GB for this server. If specified this is the absolute value, not just the additional storage above what is included in the size.
Leave null to accept the default for the size if this is a new server or a resize to a different base size,
or to keep the current value if this a resize with the same base size but different options.`
diskValidValues := "Valid values must be a multiple of 5. If the value is greater than 60GB, it must be a multiple of 10. " +
"if the value is greater than 200GB, it must be a multiple of 100. "
diskValidValuesMarkdown := ` Valid values:
- must be a multiple of 5
- \> 60GB must be a multiple of 10
- \> 200GB must be a multiple of 100`

resp.Schema.Attributes["disk"] = &schema.Int32Attribute{
Description: diskDescription + diskValidValues,
MarkdownDescription: diskDescription + diskValidValuesMarkdown,
Optional: true,
Required: false,
Computed: true,
Validators: []validator.Int32{
int32validator.AtLeast(20),
MultipleOfValidator{Multiple: 5},
MultipleOfValidator{Multiple: 10, RangeFrom: 60, RangeTo: 200},
MultipleOfValidator{Multiple: 100, RangeFrom: 200},
},
}

resp.Schema.Attributes["timeouts"] =
timeouts.Attributes(ctx, timeouts.Opts{
Create: true,
Expand All @@ -274,6 +327,7 @@ func (r *serverResource) ModifyPlan(ctx context.Context, req resource.ModifyPlan
// Destruction plan, no modification needed
return
}

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
Expand All @@ -293,6 +347,7 @@ func (r *serverResource) ModifyPlan(ctx context.Context, req resource.ModifyPlan
// Creation plan, no further modification needed
return
}

// Read Terraform state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -335,6 +390,14 @@ func (r *serverResource) ModifyPlan(ctx context.Context, req resource.ModifyPlan
)
}

// Use state for unknown disk/memory values, as long as server size is the same
if (plan.Memory.IsNull() || plan.Memory.IsUnknown()) && plan.Size.Equal(state.Size) {
plan.Memory = state.Memory
}
if (plan.Disk.IsNull() || plan.Disk.IsUnknown()) && plan.Size.Equal(state.Size) {
plan.Disk = state.Disk
}

// Save data into Terraform state
resp.Diagnostics.Append(resp.Plan.Set(ctx, &plan)...)
}
Expand Down Expand Up @@ -378,6 +441,13 @@ func (r *serverResource) Create(ctx context.Context, req resource.CreateRequest,
},
}

if !data.Memory.IsNull() && !data.Memory.IsUnknown() {
body.Options.Memory = data.Memory.ValueInt32Pointer()
}
if !data.Disk.IsNull() && !data.Disk.IsUnknown() {
body.Options.Disk = data.Disk.ValueInt32Pointer()
}

if data.Password.IsNull() {
data.Password = types.StringNull()
} else {
Expand Down Expand Up @@ -430,6 +500,8 @@ func (r *serverResource) Create(ctx context.Context, req resource.CreateRequest,
data.VpcId = types.Int64PointerValue(serverResp.JSON200.Server.VpcId)
data.Permalink = types.StringValue(*serverResp.JSON200.Server.Permalink)
data.PasswordChangeSupported = types.BoolValue(*serverResp.JSON200.Server.PasswordChangeSupported)
data.Memory = types.Int32Value(*serverResp.JSON200.Server.Memory)
data.Disk = types.Int32Value(*serverResp.JSON200.Server.Disk)
plannedSourceDestCheck := data.SourceAndDestinationCheck
serverRespSourceDestCheck := types.BoolPointerValue(serverResp.JSON200.Server.Networks.SourceAndDestinationCheck)
data.SourceAndDestinationCheck = serverRespSourceDestCheck
Expand Down Expand Up @@ -636,18 +708,42 @@ func (r *serverResource) Update(ctx context.Context, req resource.UpdateRequest,
}

// Resize operation
if !plan.Size.Equal(state.Size) || !plan.PublicIpv4Count.Equal(state.PublicIpv4Count) || !plan.Image.Equal(state.Image) {
if !plan.Size.Equal(state.Size) ||
!plan.Memory.IsNull() && !plan.Memory.IsUnknown() && !plan.Memory.Equal(state.Memory) ||
!plan.Disk.IsNull() && !plan.Disk.IsUnknown() && !plan.Disk.Equal(state.Disk) ||
!plan.Image.Equal(state.Image) ||
!plan.PublicIpv4Count.Equal(state.PublicIpv4Count) {

resizeReq := &binarylane.PostServersServerIdActionsResizeJSONRequestBody{
Type: "resize",
Type: "resize",
Options: &binarylane.ChangeSizeOptionsRequest{},
}
if !plan.Size.Equal(state.Size) {

if !plan.Size.Equal(state.Size) ||
!plan.Memory.IsNull() && !plan.Memory.IsUnknown() && !plan.Memory.Equal(state.Memory) ||
!plan.Disk.IsNull() && !plan.Disk.IsUnknown() && !plan.Disk.Equal(state.Disk) {

resizeReq.Size = plan.Size.ValueStringPointer()
if !plan.Memory.IsUnknown() && !plan.Memory.IsNull() {
resizeReq.Options.Memory = plan.Memory.ValueInt32Pointer()
}
if !plan.Disk.IsNull() && !plan.Disk.IsUnknown() {
resizeReq.Options.Disk = plan.Disk.ValueInt32Pointer()
}
state.Size = plan.Size
state.Memory = plan.Memory
state.Disk = plan.Disk
}
if !plan.PublicIpv4Count.Equal(state.PublicIpv4Count) {
resizeReq.Options = &binarylane.ChangeSizeOptionsRequest{
Ipv4Addresses: plan.PublicIpv4Count.ValueInt32Pointer(),

if !plan.Image.Equal(state.Image) {
resizeReq.ChangeImage = &binarylane.ChangeImage{
Image: plan.Image.ValueStringPointer(),
}
state.Image = plan.Image
}

if !plan.PublicIpv4Count.Equal(state.PublicIpv4Count) {
resizeReq.Options.Ipv4Addresses = plan.PublicIpv4Count.ValueInt32Pointer()
if plan.PublicIpv4Count.ValueInt32() < state.PublicIpv4Count.ValueInt32() {
currentIps := []string{}
diags := state.PublicIpv4Addresses.ElementsAs(ctx, &currentIps, false)
Expand All @@ -665,12 +761,6 @@ func (r *serverResource) Update(ctx context.Context, req resource.UpdateRequest,
state.PublicIpv4Count = plan.PublicIpv4Count
state.PublicIpv4Addresses = plan.PublicIpv4Addresses
}
if !plan.Image.Equal(state.Image) {
resizeReq.ChangeImage = &binarylane.ChangeImage{
Image: plan.Image.ValueStringPointer(),
}
state.Image = plan.Image
}

tflog.Info(ctx, fmt.Sprintf("Resizing server: server_id=%s", state.Id.String()))

Expand Down Expand Up @@ -699,8 +789,7 @@ func (r *serverResource) Update(ctx context.Context, req resource.UpdateRequest,
return
}

if state.PublicIpv4Addresses.IsUnknown() || listContainsUnknown(ctx, state.PublicIpv4Addresses) {
// New IPs may have been allocated, so we need to check the server again
if state.PublicIpv4Addresses.IsUnknown() || listContainsUnknown(ctx, state.PublicIpv4Addresses) || state.Memory.IsNull() || state.Memory.IsUnknown() {
refreshNeeded = true
}
}
Expand Down Expand Up @@ -949,6 +1038,8 @@ func setServerResourceState(ctx context.Context, data *serverResourceModel, serv
data.Permalink = types.StringValue(*serverResp.Server.Permalink)
data.PasswordChangeSupported = types.BoolValue(*serverResp.Server.PasswordChangeSupported)
data.SourceAndDestinationCheck = types.BoolPointerValue(serverResp.Server.Networks.SourceAndDestinationCheck)
data.Memory = types.Int32Value(*serverResp.Server.Memory)
data.Disk = types.Int32Value(*serverResp.Server.Disk)

publicIpv4Addresses := []string{}
privateIpv4Addresses := []string{}
Expand Down
8 changes: 8 additions & 0 deletions internal/provider/server_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ resource "binarylane_server" "test" {
region = "per"
image = "debian-11"
size = "std-min"
memory = 1152
password = "` + password + `"
vpc_id = binarylane_vpc.test.id
public_ipv4_count = 1
Expand All @@ -80,6 +81,8 @@ data "binarylane_server" "test" {
resource.TestCheckResourceAttr("binarylane_server.test", "region", "per"),
resource.TestCheckResourceAttr("binarylane_server.test", "image", "debian-11"),
resource.TestCheckResourceAttr("binarylane_server.test", "size", "std-min"),
resource.TestCheckResourceAttr("binarylane_server.test", "memory", "1152"),
resource.TestCheckResourceAttr("binarylane_server.test", "disk", "20"),
resource.TestCheckResourceAttrSet("binarylane_server.test", "vpc_id"),
resource.TestCheckResourceAttr("binarylane_server.test", "public_ipv4_count", "1"),
resource.TestCheckResourceAttr("binarylane_server.test", "password", password),
Expand All @@ -105,6 +108,8 @@ echo "Hello World" > /var/tmp/output.txt
resource.TestCheckResourceAttr("data.binarylane_server.test", "user_data", `#cloud-config
echo "Hello World" > /var/tmp/output.txt
`),
resource.TestCheckResourceAttr("data.binarylane_server.test", "memory", "1152"),
resource.TestCheckResourceAttr("data.binarylane_server.test", "disk", "20"),
),
},
// Test import by ID
Expand Down Expand Up @@ -147,6 +152,7 @@ resource "binarylane_server" "test" {
region = "per"
image = "debian-12"
size = "std-1vcpu"
disk = "45"
password = "` + password + `"
vpc_id = null
public_ipv4_count = 0
Expand All @@ -163,6 +169,8 @@ EOT
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("binarylane_server.test", "name", "tf-test-server-resource-2"),
resource.TestCheckResourceAttr("binarylane_server.test", "size", "std-1vcpu"),
resource.TestCheckResourceAttr("binarylane_server.test", "memory", "2048"),
resource.TestCheckResourceAttr("binarylane_server.test", "disk", "45"),
resource.TestCheckResourceAttr("binarylane_server.test", "public_ipv4_count", "0"),
resource.TestCheckResourceAttr("binarylane_server.test", "public_ipv4_addresses.#", "0"),
resource.TestCheckResourceAttr("binarylane_server.test", "image", "debian-12"),
Expand Down
55 changes: 55 additions & 0 deletions internal/provider/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

var (
_ validator.Int32 = &MultipleOfValidator{}
)

type MultipleOfValidator struct {
Multiple, RangeFrom, RangeTo int32
}

func (v MultipleOfValidator) Description(ctx context.Context) string {
if v.RangeFrom == 0 {
return fmt.Sprintf(`must be a multiple of %d`, v.Multiple)
}
return fmt.Sprintf("when greater than %d, must be a multiple of %d", v.RangeFrom, v.Multiple)
}

func (v MultipleOfValidator) MarkdownDescription(ctx context.Context) string {
if v.RangeFrom == 0 {
return fmt.Sprintf(`must be a multiple of %d`, v.Multiple)
}
return fmt.Sprintf("when greater than %d, must be a multiple of %d", v.RangeFrom, v.Multiple)
}

func (v MultipleOfValidator) ValidateInt32(ctx context.Context, req validator.Int32Request, resp *validator.Int32Response) {
if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() {
return
}
memory := req.ConfigValue.ValueInt32()

if memory%v.Multiple == 0 || v.RangeFrom != 0 && memory < v.RangeFrom || v.RangeTo != 0 && memory >= v.RangeTo {
return
}

if v.RangeFrom != 0 {
resp.Diagnostics.AddAttributeError(
req.Path,
"Not a Multiple",
fmt.Sprintf("if greater than %d, value must be a multiple of %d", v.RangeFrom, v.Multiple),
)
} else {
resp.Diagnostics.AddAttributeError(
req.Path,
"Not a Multiple",
fmt.Sprintf("value must be a multiple of %d", v.Multiple),
)
}
}

0 comments on commit 7f5bae6

Please sign in to comment.