Skip to content

Commit

Permalink
refactor(server): migrate server resource to plugin framework (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
kangasta authored Jan 16, 2025
1 parent 0a88bf0 commit 45f62ab
Show file tree
Hide file tree
Showing 20 changed files with 1,718 additions and 1,238 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]

### Changed

- upcloud_server: make template storage tier configurable.

### Fixed

- upcloud_server: mark network interface IP address values as unknown during planning. This ensures that IP addresses have known values after apply.

## [5.16.0] - 2024-12-03

### Added

- upcloud_managed_database_\*: add `termination_protection` field.

## [5.15.0] - 2024-11-14
Expand Down
97 changes: 97 additions & 0 deletions internal/service/server/create_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package server

import (
"context"
"fmt"

"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/hashicorp/terraform-plugin-framework/diag"
)

func buildLoginOpts(ctx context.Context, data loginModel) (*request.LoginUser, string, diag.Diagnostics) {
r := &request.LoginUser{}

r.Username = data.User.ValueString()

if !data.CreatePassword.IsNull() {
if data.CreatePassword.ValueBool() {
r.CreatePassword = "yes"
} else {
r.CreatePassword = "no"
}
}

var keys []string
diags := data.Keys.ElementsAs(ctx, &keys, false)
for _, key := range keys {
r.SSHKeys = append(r.SSHKeys, key)
}

return r, data.PasswordDelivery.ValueString(), diags
}

func buildSimpleBackupOpts(data *simpleBackupModel) string {
if data == nil {
return "no"
}

time := data.Time.ValueString()
plan := data.Plan.ValueString()

if time != "" && plan != "" {
return fmt.Sprintf("%s,%s", time, plan)
}

return "no"
}

func buildStorageDeviceAddress(address, position string) string {
if position != "" {
return fmt.Sprintf("%s:%s", address, position)
}

return address
}

func buildNetworkOpts(ctx context.Context, data serverModel) (req []request.CreateServerInterface, diags diag.Diagnostics) {
var ifaces []networkInterfaceModel
diags.Append(data.NetworkInterfaces.ElementsAs(context.Background(), &ifaces, false)...)

for _, iface := range ifaces {
r := request.CreateServerInterface{
Bootable: upcloud.FromBool(iface.Bootable.ValueBool()),
Index: int(iface.Index.ValueInt64()),
IPAddresses: []request.CreateServerIPAddress{
{
Family: iface.IPAddressFamily.ValueString(),
Address: iface.IPAddress.ValueString(),
},
},
Network: iface.Network.ValueString(),
SourceIPFiltering: upcloud.FromBool(iface.SourceIPFiltering.ValueBool()),
Type: iface.Type.ValueString(),
}

if !iface.AdditionalIPAddresses.IsNull() {
if r.Type != upcloud.NetworkTypePrivate {
diags.AddError("Invalid configuration", "additional_ip_address can only be set for private network interfaces")
return
}

var additionalIPAddresses []additionalIPAddressModel
diags.Append(iface.AdditionalIPAddresses.ElementsAs(ctx, &additionalIPAddresses, false)...)

for _, ipAddress := range additionalIPAddresses {
r.IPAddresses = append(r.IPAddresses, request.CreateServerIPAddress{
Family: ipAddress.IPAddressFamily.ValueString(),
Address: ipAddress.IPAddress.ValueString(),
})
}
}

req = append(req, r)
}

return
}
Loading

0 comments on commit 45f62ab

Please sign in to comment.