Skip to content

Commit

Permalink
fix: add UntaggedNetwork and remove Custom
Browse files Browse the repository at this point in the history
Signed-off-by: PoAn Yang <[email protected]>
  • Loading branch information
FrankYang0529 committed Feb 27, 2024
1 parent 0f6a4de commit 3a136ac
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 26 deletions.
14 changes: 13 additions & 1 deletion docs/resources/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
page_title: "harvester_network Resource - terraform-provider-harvester"
subcategory: ""
description: |-
---

# harvester_network (Resource)
Expand All @@ -13,6 +13,18 @@ description: |-
## Example Usage

```terraform
resource "harvester_network" "mgmt-untagged-network" {
name = "mgmt-untagged-network"
namespace = "harvester-public"
vlan_id = 0
route_mode = "auto"
route_dhcp_server_ip = ""
cluster_network_name = data.harvester_clusternetwork.mgmt.name
}
resource "harvester_network" "mgmt-vlan1" {
name = "mgmt-vlan1"
namespace = "harvester-public"
Expand Down
28 changes: 7 additions & 21 deletions internal/provider/network/resource_network_constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,15 @@ func (c *Constructor) Setup() util.Processors {
vlanID = i.(int)
)
if vlanID != 0 {
networkType = builder.NetworkTypeVLAN
clusterNetworkName := c.Network.Labels[networkutils.KeyClusterNetworkLabel]
c.Network.Spec.Config = fmt.Sprintf(builder.NetworkVLANConfigTemplate, c.Network.Name, clusterNetworkName, vlanID)
} else {
networkType = builder.NetworkTypeCustom
networkType = string(networkutils.L2VlanNetwork)
} else if vlanID == 0 {
networkType = string(networkutils.UntaggedNetwork)
}
clusterNetworkName := c.Network.Labels[networkutils.KeyClusterNetworkLabel]
c.Network.Spec.Config = fmt.Sprintf(builder.NetworkVLANConfigTemplate, c.Network.Name, clusterNetworkName, vlanID)

c.Network.Labels[networkutils.KeyVlanLabel] = strconv.Itoa(vlanID)
c.Network.Labels[builder.LabelKeyNetworkType] = networkType
return nil
},
Required: true,
},
{
Field: constants.FieldNetworkConfig,
Parser: func(i interface{}) error {
if c.Network.Labels[builder.LabelKeyNetworkType] == builder.NetworkTypeVLAN {
return nil
}
config := i.(string)
if config == "" {
return errors.New("must specify config in custom network type")
}
c.Network.Spec.Config = config
c.Network.Labels[networkutils.KeyNetworkType] = networkType
return nil
},
Required: true,
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/network/schema_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func Schema() map[string]*schema.Schema {
constants.FieldNetworkVlanID: {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(1, 4094),
Description: "e.g. 1-4094",
ValidateFunc: validation.IntBetween(0, 4094),
Description: "e.g. 0-4094",
},
constants.FieldNetworkConfig: {
Type: schema.TypeString,
Expand Down
4 changes: 2 additions & 2 deletions internal/tests/resource_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func TestAccNetwork_basic(t *testing.T) {
CheckDestroy: testAccCheckNetworkDestroy(ctx),
Steps: []resource.TestStep{
{
Config: buildNetworkConfig(testAccNetworkName, testAccNetworkDescription, "0"),
ExpectError: regexp.MustCompile(fmt.Sprintf(`expected %s to be in the range \(1 - 4094\)`, constants.FieldNetworkVlanID)),
Config: buildNetworkConfig(testAccNetworkName, testAccNetworkDescription, "4045"),
ExpectError: regexp.MustCompile(fmt.Sprintf(`expected %s to be in the range \(0 - 4094\)`, constants.FieldNetworkVlanID)),
},
{
Config: buildNetworkConfig(testAccNetworkName, testAccNetworkDescription, testAccNetworkVlanID),
Expand Down

0 comments on commit 3a136ac

Please sign in to comment.