Skip to content

Commit

Permalink
Add missing docs about the network attribute/argument on hcloud_serve…
Browse files Browse the repository at this point in the history
…r and implement the datatransformation of the network for the attribute

Signed-off-by: Lukas Kämmerling <lukas.kaemmerling@hetzner-cloud.de>
  • Loading branch information
LKaemmerling committed Jun 26, 2021
1 parent 4027dd6 commit ad88a85
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/e2etests/server/resource_test.go
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/hetznercloud/terraform-provider-hcloud/internal/firewall"
"github.com/hetznercloud/terraform-provider-hcloud/internal/network"
"github.com/hetznercloud/terraform-provider-hcloud/internal/sshkey"

"github.com/stretchr/testify/assert"

"github.com/hetznercloud/terraform-provider-hcloud/internal/server"
Expand Down Expand Up @@ -301,6 +300,9 @@ func TestServerResource_DirectAttachToNetwork(t *testing.T) {
testsupport.CheckResourceExists(nwRes.TFID(), network.ByID(t, &nw)),
testsupport.CheckResourceExists(sResWithNet.TFID(), server.ByID(t, &s)),
testsupport.LiftTCF(hasServerNetwork(t, &s, &nw, "10.0.1.5", "10.0.1.6", "10.0.1.7")),
resource.TestCheckResourceAttr(sResWithNet.TFID(), "network.#", "1"),
resource.TestCheckResourceAttr(sResWithNet.TFID(), "network.0.ip", "10.0.1.5"),
resource.TestCheckResourceAttr(sResWithNet.TFID(), "network.0.alias_ips.#", "2"),
),
},
{
Expand Down
35 changes: 35 additions & 0 deletions internal/server/resource.go
Expand Up @@ -131,6 +131,12 @@ func Resource() *schema.Resource {
"network": {
Type: schema.TypeSet,
Optional: true,
DiffSuppressFunc: func(_, _, _ string, d *schema.ResourceData) bool {
// Diff is only valid if "network" resource is set in
// terraform configuration.
_, ok := d.GetOk("network")
return !ok // Negate because we do **not** want to suppress the diff.
},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"network_id": {
Expand Down Expand Up @@ -721,4 +727,33 @@ func setServerSchema(d *schema.ResourceData, s *hcloud.Server) {
firewallIDs[i] = firewall.Firewall.ID
}
d.Set("firewall_ids", firewallIDs)

// Only write the networks to the resource data if it already contains
// such an entry. This avoids setting the "network" property which is not
// marked as "computed" if the user uses the "server_network_subnet"
// resource. Setting the "network" property as computed is not possible
// because this would lead to loosing any updates.
//
// The easiest would be to use schema.ComputedWhen but this is marked
// as currently not working.
if _, ok := d.GetOk("network"); ok {
d.Set("network", networkToTerraformNetworks(s.PrivateNet))
}
}

func networkToTerraformNetworks(privateNetworks []hcloud.ServerPrivateNet) []map[string]interface{} {
tfPrivateNetworks := make([]map[string]interface{}, len(privateNetworks))
for i, privateNetwork := range privateNetworks {
tfPrivateNetwork := make(map[string]interface{})
tfPrivateNetwork["network_id"] = privateNetwork.Network.ID
tfPrivateNetwork["ip"] = privateNetwork.IP.String()
tfPrivateNetwork["mac_address"] = privateNetwork.MACAddress
aliasIPs := make([]string, len(privateNetwork.Aliases))
for in, ip := range privateNetwork.Aliases {
aliasIPs[in] = ip.String()
}
tfPrivateNetwork["alias_ips"] = aliasIPs
tfPrivateNetworks[i] = tfPrivateNetwork
}
return tfPrivateNetworks
}
15 changes: 15 additions & 0 deletions website/docs/r/server.html.md
Expand Up @@ -80,6 +80,13 @@ The following arguments are supported:
- `labels` - (Optional, map) User-defined labels (key-value pairs) should be created with.
- `backups` - (Optional, boolean) Enable or disable backups.
- `firewall_ids` - (Optional, list) Firewall IDs the server should be attached to on creation.
- `network` - (Optional) Network the server should be attached to on creation. (Can be specified multiple times)

`network` support the following fields:
- `network_id` - (Required, int) ID of the network
- `ip` - (Optional, string) Specify the IP the server should get in the network
- `alias_ips` - (Optional, list) Alias IPs the server should have in the Network.


## Attributes Reference

Expand Down Expand Up @@ -107,6 +114,14 @@ The following attributes are exported:
issue. It is therefore necessary to use `depends_on` to link the server
to the respective subnetwork. See examples.
- `firewall_ids` - (Optional, list) Firewall IDs the server is attached to.
- `network` - (Optional, list) Network the server should be attached to on creation. (Can be specified multiple times)

a single entry in `network` support the following fields:
- `network_id` - (Required, int) ID of the network
- `ip` - (Optional, string) Specify the IP the server should get in the network
- `alias_ips` - (Optional, list) Alias IPs the server should have in the Network.
- `mac_address` - (Optional, string) The MAC address the private interface of the server has


## Import

Expand Down

0 comments on commit ad88a85

Please sign in to comment.