Skip to content

Commit 3bb876e

Browse files
feat(network) add dhcp property, default true (#1211)
1 parent 52a3cc9 commit 3bb876e

8 files changed

Lines changed: 42 additions & 0 deletions

File tree

docs/data-sources/network.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ data "stackit_network" "example" {
3333

3434
### Read-Only
3535

36+
- `dhcp` (Boolean) Shows if DHCP is enabled for the network.
3637
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`network_id`".
3738
- `ipv4_gateway` (String) The IPv4 gateway of a network. If not specified, the first IP of the network will be assigned as the gateway.
3839
- `ipv4_nameservers` (List of String) The IPv4 nameservers of the network.

docs/resources/network.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464

6565
### Optional
6666

67+
- `dhcp` (Boolean) If the network has DHCP enabled. Default value is `true`.
6768
- `ipv4_gateway` (String) The IPv4 gateway of a network. If not specified, the first IP of the network will be assigned as the gateway.
6869
- `ipv4_nameservers` (List of String) The IPv4 nameservers of the network.
6970
- `ipv4_prefix` (String) The IPv4 prefix of the network (CIDR).

stackit/internal/services/iaas/iaas_acc_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ var testConfigNetworkVarsMax = config.Variables{
286286
"label": config.StringVariable("label"),
287287
"organization_id": config.StringVariable(testutil.OrganizationId),
288288
"service_account_mail": config.StringVariable(testutil.TestProjectServiceAccountEmail),
289+
"dhcp": config.BoolVariable(true),
289290
}
290291

291292
var testConfigNetworkVarsMaxUpdated = func() config.Variables {
@@ -295,6 +296,7 @@ var testConfigNetworkVarsMaxUpdated = func() config.Variables {
295296
updatedConfig["ipv4_gateway"] = config.StringVariable("")
296297
updatedConfig["ipv4_nameserver_0"] = config.StringVariable("10.2.2.10")
297298
updatedConfig["label"] = config.StringVariable("updated")
299+
updatedConfig["dhcp"] = config.BoolVariable(false)
298300
return updatedConfig
299301
}()
300302

@@ -656,6 +658,7 @@ func TestAccNetworkMin(t *testing.T) {
656658
resource.TestCheckResourceAttrSet("stackit_network.network", "public_ip"),
657659
resource.TestCheckResourceAttrSet("stackit_network.network", "region"),
658660
resource.TestCheckNoResourceAttr("stackit_network.network", "routing_table_id"),
661+
resource.TestCheckResourceAttr("stackit_network.network", "dhcp", "true"),
659662
),
660663
},
661664
// Data source
@@ -682,6 +685,7 @@ func TestAccNetworkMin(t *testing.T) {
682685
resource.TestCheckResourceAttrSet("data.stackit_network.network", "public_ip"),
683686
resource.TestCheckResourceAttrSet("data.stackit_network.network", "region"),
684687
resource.TestCheckNoResourceAttr("data.stackit_network.network", "routing_table_id"),
688+
resource.TestCheckResourceAttr("stackit_network.network", "dhcp", "true"),
685689
),
686690
},
687691

@@ -710,6 +714,7 @@ func TestAccNetworkMin(t *testing.T) {
710714
resource.TestCheckResourceAttrSet("stackit_network.network", "public_ip"),
711715
resource.TestCheckResourceAttrSet("stackit_network.network", "region"),
712716
resource.TestCheckNoResourceAttr("stackit_network.network", "routing_table_id"),
717+
resource.TestCheckResourceAttr("stackit_network.network", "dhcp", "true"),
713718
),
714719
},
715720
// Update
@@ -725,6 +730,7 @@ func TestAccNetworkMin(t *testing.T) {
725730
resource.TestCheckResourceAttrSet("stackit_network.network", "public_ip"),
726731
resource.TestCheckResourceAttrSet("stackit_network.network", "region"),
727732
resource.TestCheckNoResourceAttr("stackit_network.network", "routing_table_id"),
733+
resource.TestCheckResourceAttr("stackit_network.network", "dhcp", "true"),
728734
),
729735
},
730736
// Deletion is done by the framework implicitly
@@ -762,6 +768,7 @@ func TestAccNetworkMax(t *testing.T) {
762768
resource.TestCheckResourceAttr("stackit_network.network_prefix", "routed", testutil.ConvertConfigVariable(testConfigNetworkVarsMax["routed"])),
763769
resource.TestCheckResourceAttr("stackit_network.network_prefix", "labels.acc-test", testutil.ConvertConfigVariable(testConfigNetworkVarsMax["label"])),
764770
resource.TestCheckResourceAttrSet("stackit_network.network_prefix", "public_ip"),
771+
resource.TestCheckResourceAttr("stackit_network.network_prefix", "dhcp", testutil.ConvertConfigVariable(testConfigNetworkVarsMax["dhcp"])),
765772

766773
// Network with prefix_length
767774
resource.TestCheckResourceAttrSet("stackit_network.network_prefix_length", "network_id"),
@@ -848,6 +855,7 @@ func TestAccNetworkMax(t *testing.T) {
848855
resource.TestCheckResourceAttrSet("data.stackit_network.network_prefix", "ipv6_prefixes.#"),
849856
resource.TestCheckResourceAttr("data.stackit_network.network_prefix", "routed", testutil.ConvertConfigVariable(testConfigNetworkVarsMax["routed"])),
850857
resource.TestCheckResourceAttr("data.stackit_network.network_prefix", "labels.acc-test", testutil.ConvertConfigVariable(testConfigNetworkVarsMax["label"])),
858+
resource.TestCheckResourceAttr("data.stackit_network.network_prefix", "dhcp", testutil.ConvertConfigVariable(testConfigNetworkVarsMax["dhcp"])),
851859

852860
// Network with prefix_length
853861
resource.TestCheckResourceAttrSet("data.stackit_network.network_prefix_length", "network_id"),
@@ -931,6 +939,7 @@ func TestAccNetworkMax(t *testing.T) {
931939
resource.TestCheckResourceAttr("stackit_network.network_prefix", "ipv4_prefixes.#", "1"),
932940
resource.TestCheckResourceAttr("stackit_network.network_prefix", "ipv4_prefixes.0", testutil.ConvertConfigVariable(testConfigNetworkVarsMax["ipv4_prefix"])),
933941
resource.TestCheckResourceAttr("stackit_network.network_prefix", "routed", testutil.ConvertConfigVariable(testConfigNetworkVarsMax["routed"])),
942+
resource.TestCheckResourceAttr("stackit_network.network_prefix", "dhcp", testutil.ConvertConfigVariable(testConfigNetworkVarsMax["dhcp"])),
934943
),
935944
},
936945
{
@@ -998,6 +1007,7 @@ func TestAccNetworkMax(t *testing.T) {
9981007
resource.TestCheckResourceAttr("stackit_network.network_prefix", "routed", testutil.ConvertConfigVariable(testConfigNetworkVarsMaxUpdated["routed"])),
9991008
resource.TestCheckResourceAttr("stackit_network.network_prefix", "labels.acc-test", testutil.ConvertConfigVariable(testConfigNetworkVarsMaxUpdated["label"])),
10001009
resource.TestCheckResourceAttrSet("stackit_network.network_prefix", "public_ip"),
1010+
resource.TestCheckResourceAttr("stackit_network.network_prefix", "dhcp", testutil.ConvertConfigVariable(testConfigNetworkVarsMaxUpdated["dhcp"])),
10011011

10021012
resource.TestCheckResourceAttrSet("stackit_network.network_prefix_length", "network_id"),
10031013
resource.TestCheckResourceAttrPair(

stackit/internal/services/iaas/network/datasource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type DataSourceModel struct {
4848
Routed types.Bool `tfsdk:"routed"`
4949
Region types.String `tfsdk:"region"`
5050
RoutingTableID types.String `tfsdk:"routing_table_id"`
51+
DHCP types.Bool `tfsdk:"dhcp"`
5152
}
5253

5354
// NewNetworkDataSource is a helper function to simplify the provider implementation.
@@ -198,6 +199,10 @@ func (d *networkDataSource) Schema(_ context.Context, _ datasource.SchemaRequest
198199
validate.NoSeparator(),
199200
},
200201
},
202+
"dhcp": schema.BoolAttribute{
203+
Description: "Shows if DHCP is enabled for the network.",
204+
Computed: true,
205+
},
201206
},
202207
}
203208
}
@@ -397,6 +402,7 @@ func mapDataSourceFields(ctx context.Context, networkResp *iaas.Network, model *
397402
model.Labels = labels
398403
model.Routed = types.BoolPointerValue(networkResp.Routed)
399404
model.Region = types.StringValue(region)
405+
model.DHCP = types.BoolPointerValue(networkResp.Dhcp)
400406

401407
return nil
402408
}

stackit/internal/services/iaas/network/datasource_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func TestMapDataSourceFields(t *testing.T) {
9898
"key": "value",
9999
},
100100
Routed: utils.Ptr(true),
101+
Dhcp: utils.Ptr(true),
101102
},
102103
testRegion,
103104
DataSourceModel{
@@ -141,6 +142,7 @@ func TestMapDataSourceFields(t *testing.T) {
141142
IPv4Gateway: types.StringValue("gateway"),
142143
IPv6Gateway: types.StringValue("gateway"),
143144
Region: types.StringValue(testRegion),
145+
DHCP: types.BoolValue(true),
144146
},
145147
true,
146148
},

stackit/internal/services/iaas/network/resource.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/hashicorp/terraform-plugin-framework/path"
1515
"github.com/hashicorp/terraform-plugin-framework/resource"
1616
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
17+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
1718
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
1819
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
1920
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
@@ -71,6 +72,7 @@ type Model struct {
7172
NoIPv6Gateway types.Bool `tfsdk:"no_ipv6_gateway"`
7273
Region types.String `tfsdk:"region"`
7374
RoutingTableID types.String `tfsdk:"routing_table_id"`
75+
DHCP types.Bool `tfsdk:"dhcp"`
7476
}
7577

7678
// NewNetworkResource is a helper function to simplify the provider implementation.
@@ -378,6 +380,15 @@ func (r *networkResource) Schema(_ context.Context, _ resource.SchemaRequest, re
378380
stringplanmodifier.RequiresReplaceIfConfigured(),
379381
},
380382
},
383+
"dhcp": schema.BoolAttribute{
384+
Optional: true,
385+
Computed: true,
386+
Description: "If the network has DHCP enabled. Default value is `true`.",
387+
Default: booldefault.StaticBool(true),
388+
PlanModifiers: []planmodifier.Bool{
389+
boolplanmodifier.UseStateForUnknown(),
390+
},
391+
},
381392
},
382393
}
383394
}
@@ -747,6 +758,7 @@ func mapFields(ctx context.Context, networkResp *iaas.Network, model *Model, reg
747758
model.Labels = labels
748759
model.Routed = types.BoolPointerValue(networkResp.Routed)
749760
model.Region = types.StringValue(region)
761+
model.DHCP = types.BoolPointerValue(networkResp.Dhcp)
750762

751763
return nil
752764
}
@@ -862,6 +874,7 @@ func toCreatePayload(ctx context.Context, model *Model) (*iaas.CreateNetworkPayl
862874
Ipv4: ipv4Body,
863875
Ipv6: ipv6Body,
864876
RoutingTableId: conversion.StringValueToPointer(model.RoutingTableID),
877+
Dhcp: conversion.BoolValueToPointer(model.DHCP),
865878
}
866879

867880
return &payload, nil
@@ -944,6 +957,7 @@ func toUpdatePayload(ctx context.Context, model, stateModel *Model) (*iaas.Parti
944957
Ipv4: ipv4Body,
945958
Ipv6: ipv6Body,
946959
RoutingTableId: conversion.StringValueToPointer(model.RoutingTableID),
960+
Dhcp: conversion.BoolValueToPointer(model.DHCP),
947961
}
948962

949963
return &payload, nil

stackit/internal/services/iaas/network/resource_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func TestMapFields(t *testing.T) {
9090
"key": "value",
9191
},
9292
Routed: utils.Ptr(true),
93+
Dhcp: utils.Ptr(true),
9394
},
9495
testRegion,
9596
Model{
@@ -133,6 +134,7 @@ func TestMapFields(t *testing.T) {
133134
IPv4Gateway: types.StringValue("gateway"),
134135
IPv6Gateway: types.StringValue("gateway"),
135136
Region: types.StringValue(testRegion),
137+
DHCP: types.BoolValue(true),
136138
},
137139
true,
138140
},
@@ -403,6 +405,7 @@ func TestToCreatePayload(t *testing.T) {
403405
Routed: types.BoolValue(false),
404406
IPv4Gateway: types.StringValue("gateway"),
405407
IPv4Prefix: types.StringValue("prefix"),
408+
DHCP: types.BoolValue(true),
406409
},
407410
&iaas.CreateNetworkPayload{
408411
Name: utils.Ptr("name"),
@@ -420,6 +423,7 @@ func TestToCreatePayload(t *testing.T) {
420423
"key": "value",
421424
},
422425
Routed: utils.Ptr(false),
426+
Dhcp: utils.Ptr(true),
423427
},
424428
true,
425429
},
@@ -588,6 +592,7 @@ func TestToUpdatePayload(t *testing.T) {
588592
}),
589593
Routed: types.BoolValue(true),
590594
IPv4Gateway: types.StringValue("gateway"),
595+
DHCP: types.BoolValue(true),
591596
},
592597
Model{
593598
ProjectId: types.StringValue("pid"),
@@ -606,6 +611,7 @@ func TestToUpdatePayload(t *testing.T) {
606611
Labels: &map[string]interface{}{
607612
"key": "value",
608613
},
614+
Dhcp: utils.Ptr(true),
609615
},
610616
true,
611617
},

stackit/internal/services/iaas/testdata/resource-network-max.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ variable "ipv4_prefix_length" {}
88
variable "routed" {}
99
variable "label" {}
1010
variable "service_account_mail" {}
11+
variable "dhcp" {}
1112

1213
# no test candidate, just needed for the testing setup
1314
resource "stackit_network_area" "network_area" {
@@ -57,6 +58,7 @@ resource "stackit_network" "network_prefix" {
5758
labels = {
5859
"acc-test" : var.label
5960
}
61+
dhcp = var.dhcp
6062

6163
depends_on = [stackit_network_area_region.network_area_region]
6264
}

0 commit comments

Comments
 (0)