From 2d8980c25e8b59baff3616ff933101f1fc6176c5 Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Wed, 23 Oct 2024 22:51:46 +0530 Subject: [PATCH 1/4] initial commit --- spectrocloud/resource_cluster_edge_native.go | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/spectrocloud/resource_cluster_edge_native.go b/spectrocloud/resource_cluster_edge_native.go index b6a4c7cc..838311f2 100644 --- a/spectrocloud/resource_cluster_edge_native.go +++ b/spectrocloud/resource_cluster_edge_native.go @@ -234,6 +234,35 @@ func resourceClusterEdgeNative() *schema.Resource { Description: "Edge host static IP", Optional: true, }, + "nic_name": { + Type: schema.TypeString, + Description: "Edge host IP Address", + Optional: true, + }, + "ip": { + Type: schema.TypeString, + Description: "Edge host IP Address", + Optional: true, + }, + "default_gateway": { + Type: schema.TypeString, + Description: "Edge host default gateway", + Optional: true, + }, + "subnet_mask": { + Type: schema.TypeString, + Description: "Edge host subnet mask", + Optional: true, + }, + "dns_servers": { + Type: schema.TypeSet, + Required: true, + ForceNew: true, + Set: schema.HashString, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, "two_node_role": { Type: schema.TypeString, Description: "Two node role for edge host. Valid values are `primary` and `secondary`.", From 4453f1b2051ef9ff24d5e07472bf775931aad921 Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Fri, 25 Oct 2024 19:47:58 +0530 Subject: [PATCH 2/4] PLT-1451: Added network related setting in machine_pool for edgenative cluster --- docs/resources/cluster_edge_native.md | 6 +- .../providers.tf | 14 ++ .../resource_cluster.tf | 43 ++++++ .../terraform.template.tfvars | 4 + .../variables.tf | 13 ++ spectrocloud/resource_cluster_edge_native.go | 42 ++++-- .../resource_cluster_edge_native_test.go | 134 +++++++++++++----- 7 files changed, 205 insertions(+), 51 deletions(-) create mode 100644 examples/resources/spectrocloud_cluster_edge_native/providers.tf create mode 100644 examples/resources/spectrocloud_cluster_edge_native/resource_cluster.tf create mode 100644 examples/resources/spectrocloud_cluster_edge_native/terraform.template.tfvars create mode 100644 examples/resources/spectrocloud_cluster_edge_native/variables.tf diff --git a/docs/resources/cluster_edge_native.md b/docs/resources/cluster_edge_native.md index d6758f1d..16c6c84b 100644 --- a/docs/resources/cluster_edge_native.md +++ b/docs/resources/cluster_edge_native.md @@ -152,8 +152,12 @@ Required: Optional: +- `default_gateway` (String) Edge host default gateway +- `dns_servers` (Set of String) - `host_name` (String) Edge host name -- `static_ip` (String) Edge host static IP +- `nic_name` (String) Edge host IP Address +- `static_ip` (String) Edge host static IP address +- `subnet_mask` (String) Edge host subnet mask - `two_node_role` (String) Two node role for edge host. Valid values are `primary` and `secondary`. diff --git a/examples/resources/spectrocloud_cluster_edge_native/providers.tf b/examples/resources/spectrocloud_cluster_edge_native/providers.tf new file mode 100644 index 00000000..4c109161 --- /dev/null +++ b/examples/resources/spectrocloud_cluster_edge_native/providers.tf @@ -0,0 +1,14 @@ +terraform { + required_providers { + spectrocloud = { + version = ">= 0.1" + source = "spectrocloud/spectrocloud" + } + } +} + +provider "spectrocloud" { + host = var.sc_host + api_key = var.sc_api_key + project_name = var.sc_project_name +} diff --git a/examples/resources/spectrocloud_cluster_edge_native/resource_cluster.tf b/examples/resources/spectrocloud_cluster_edge_native/resource_cluster.tf new file mode 100644 index 00000000..d0fa47f3 --- /dev/null +++ b/examples/resources/spectrocloud_cluster_edge_native/resource_cluster.tf @@ -0,0 +1,43 @@ +resource "spectrocloud_cluster_edge_native" "cluster" { + name = "ran-edge-tf" + + cluster_profile { + id = "test-profile-id" + } + + cloud_config { + ssh_keys = ["spectro2023"] + vip = "10.10.232.57" + # overlay_cidr_range = "100.64.192.0/23" + } + + machine_pool { + control_plane = true + control_plane_as_worker = true + name = "cp-pool" + + edge_host { + host_uid = "edge-fsdsdedadfasdtest" + static_ip = "10.10.32.12" + default_gateway = "10.10.12.1" + dns_servers = ["tf.test.com"] + host_name = "test-test" + nic_name = "auto162" + subnet_mask = "255.255.12.0" + } + } + + machine_pool { + name = "wp-pool" + + edge_host { + host_uid = "edge-bef8384adfasdtest" + default_gateway = "10.10.12.1" + dns_servers = ["tf.test.com"] + host_name = "test-test" + nic_name = "auto160" + static_ip = "10.10.132449.22" + subnet_mask = "255.255.92.0" + } + } +} diff --git a/examples/resources/spectrocloud_cluster_edge_native/terraform.template.tfvars b/examples/resources/spectrocloud_cluster_edge_native/terraform.template.tfvars new file mode 100644 index 00000000..c7e9d50b --- /dev/null +++ b/examples/resources/spectrocloud_cluster_edge_native/terraform.template.tfvars @@ -0,0 +1,4 @@ +# Spectro Cloud credentials +sc_host = "{Enter Spectro Cloud API Host}" #e.g: api.spectrocloud.com (for SaaS) +sc_api_key = "{Enter Spectro Cloud API Key}" +sc_project_name = "{Enter Spectro Cloud Project Name}" #e.g: Default \ No newline at end of file diff --git a/examples/resources/spectrocloud_cluster_edge_native/variables.tf b/examples/resources/spectrocloud_cluster_edge_native/variables.tf new file mode 100644 index 00000000..e89ad263 --- /dev/null +++ b/examples/resources/spectrocloud_cluster_edge_native/variables.tf @@ -0,0 +1,13 @@ +variable "sc_host" { + description = "Spectro Cloud Endpoint" + default = "api.spectrocloud.com" +} + +variable "sc_api_key" { + description = "Spectro Cloud API key" +} + +variable "sc_project_name" { + description = "Spectro Cloud Project (e.g: Default)" + default = "Default" +} \ No newline at end of file diff --git a/spectrocloud/resource_cluster_edge_native.go b/spectrocloud/resource_cluster_edge_native.go index 838311f2..8ba5ff5b 100644 --- a/spectrocloud/resource_cluster_edge_native.go +++ b/spectrocloud/resource_cluster_edge_native.go @@ -231,7 +231,7 @@ func resourceClusterEdgeNative() *schema.Resource { }, "static_ip": { Type: schema.TypeString, - Description: "Edge host static IP", + Description: "Edge host static IP address", Optional: true, }, "nic_name": { @@ -239,11 +239,6 @@ func resourceClusterEdgeNative() *schema.Resource { Description: "Edge host IP Address", Optional: true, }, - "ip": { - Type: schema.TypeString, - Description: "Edge host IP Address", - Optional: true, - }, "default_gateway": { Type: schema.TypeString, Description: "Edge host default gateway", @@ -256,8 +251,7 @@ func resourceClusterEdgeNative() *schema.Resource { }, "dns_servers": { Type: schema.TypeSet, - Required: true, - ForceNew: true, + Optional: true, Set: schema.HashString, Elem: &schema.Schema{ Type: schema.TypeString, @@ -447,12 +441,16 @@ func flattenMachinePoolConfigsEdgeNative(machinePools []*models.V1EdgeNativeMach oi["control_plane_as_worker"] = machinePool.UseControlPlaneAsWorker oi["name"] = machinePool.Name - var hosts []map[string]string + var hosts []map[string]interface{} for _, host := range machinePool.Hosts { - rawHost := map[string]string{ - "host_name": host.HostName, - "host_uid": *host.HostUID, - "static_ip": host.StaticIP, + rawHost := map[string]interface{}{ + "host_name": host.HostName, + "host_uid": *host.HostUID, + "static_ip": host.Nic.IP, + "nic_name": host.Nic.NicName, + "default_gateway": host.Nic.Gateway, + "subnet_mask": host.Nic.Subnet, + "dns_servers": host.Nic.DNS, } if host.TwoNodeCandidatePriority != "" { rawHost["two_node_role"] = host.TwoNodeCandidatePriority @@ -723,7 +721,23 @@ func toEdgeHosts(m map[string]interface{}) (*models.V1EdgeNativeMachinePoolCloud edgeHost := &models.V1EdgeNativeMachinePoolHostEntity{ HostName: hostName, HostUID: &hostId, - StaticIP: host.(map[string]interface{})["static_ip"].(string), + Nic: &models.V1Nic{ + Gateway: host.(map[string]interface{})["default_gateway"].(string), + IP: host.(map[string]interface{})["static_ip"].(string), + NicName: host.(map[string]interface{})["nic_name"].(string), + Subnet: host.(map[string]interface{})["subnet_mask"].(string), + }, + // Hubble deprecated it and need to set it inside nic + // StaticIP: host.(map[string]interface{})["static_ip"].(string), + } + if v, ok := host.(map[string]interface{})["dns_servers"].(*schema.Set); ok { + if v.Len() > 0 { + var result []string + for _, val := range v.List() { + result = append(result, val.(string)) // Type assertion + } + edgeHost.Nic.DNS = result + } } if v, ok := host.(map[string]interface{})["two_node_role"].(string); ok { if v != "" { diff --git a/spectrocloud/resource_cluster_edge_native_test.go b/spectrocloud/resource_cluster_edge_native_test.go index f470887e..31e00eab 100644 --- a/spectrocloud/resource_cluster_edge_native_test.go +++ b/spectrocloud/resource_cluster_edge_native_test.go @@ -33,14 +33,22 @@ func TestToEdgeHosts(t *testing.T) { input: map[string]interface{}{ "edge_host": []interface{}{ map[string]interface{}{ - "host_name": "host1", - "host_uid": "uid1", - "static_ip": "ip1", + "host_name": "host1", + "host_uid": "uid1", + "static_ip": "ip1", + "nic_name": "test_nic", + "default_gateway": "1.1.1.1", + "subnet_mask": "2.2.2.2", + "dns_servers": []string{"t.t.com"}, }, map[string]interface{}{ - "host_name": "host2", - "host_uid": "uid2", - "static_ip": "ip2", + "host_name": "host2", + "host_uid": "uid2", + "static_ip": "ip2", + "nic_name": "test_nic", + "default_gateway": "1.1.1.1", + "subnet_mask": "2.2.2.2", + "dns_servers": []string{"t.t.com"}, }, }, }, @@ -49,12 +57,24 @@ func TestToEdgeHosts(t *testing.T) { { HostName: "host1", HostUID: &hostUI1, - StaticIP: "ip1", + Nic: &models.V1Nic{ + IP: "ip1", + NicName: "test_nic", + Gateway: "1.1.1.1", + Subnet: "2.2.2.2", + DNS: []string{"t.t.com"}, + }, }, { HostName: "host2", HostUID: &hostUI2, - StaticIP: "ip2", + Nic: &models.V1Nic{ + IP: "ip2", + NicName: "test_nic", + Gateway: "1.1.1.1", + Subnet: "2.2.2.2", + DNS: []string{"t.t.com"}, + }, }, }, }, @@ -64,14 +84,22 @@ func TestToEdgeHosts(t *testing.T) { input: map[string]interface{}{ "edge_host": []interface{}{ map[string]interface{}{ - "host_name": "", - "host_uid": "uid1", - "static_ip": "ip1", + "host_name": "", + "host_uid": "uid1", + "static_ip": "ip1", + "nic_name": "test_nic", + "default_gateway": "1.1.1.1", + "subnet_mask": "2.2.2.2", + "dns_servers": []string{"t.t.com"}, }, map[string]interface{}{ - "host_name": "", - "host_uid": "uid2", - "static_ip": "ip2", + "host_name": "", + "host_uid": "uid2", + "static_ip": "ip2", + "nic_name": "test_nic", + "default_gateway": "1.1.1.1", + "subnet_mask": "2.2.2.2", + "dns_servers": []string{"t.t.com"}, }, }, }, @@ -80,12 +108,24 @@ func TestToEdgeHosts(t *testing.T) { { HostName: "", HostUID: &hostUI1, - StaticIP: "ip1", + Nic: &models.V1Nic{ + IP: "ip1", + NicName: "test_nic", + Gateway: "1.1.1.1", + Subnet: "2.2.2.2", + DNS: []string{"t.t.com"}, + }, }, { HostName: "", HostUID: &hostUI2, - StaticIP: "ip2", + Nic: &models.V1Nic{ + IP: "ip2", + NicName: "test_nic", + Gateway: "1.1.1.1", + Subnet: "2.2.2.2", + DNS: []string{"t.t.com"}, + }, }, }, }, @@ -111,15 +151,19 @@ func TestToEdgeHosts(t *testing.T) { expected: &models.V1EdgeNativeMachinePoolCloudConfigEntity{ EdgeHosts: []*models.V1EdgeNativeMachinePoolHostEntity{ { - HostName: "", - HostUID: &hostUI1, - StaticIP: "ip1", + HostName: "", + HostUID: &hostUI1, + Nic: &models.V1Nic{ + IP: "ip1", + }, TwoNodeCandidatePriority: "primary", }, { - HostName: "", - HostUID: &hostUI2, - StaticIP: "ip2", + HostName: "", + HostUID: &hostUI2, + Nic: &models.V1Nic{ + IP: "ip2", + }, TwoNodeCandidatePriority: "secondary", }, }, @@ -323,12 +367,16 @@ func TestFlattenMachinePoolConfigsEdgeNative(t *testing.T) { { HostName: "host1", HostUID: &hui1, - StaticIP: "ip1", + Nic: &models.V1Nic{ + IP: "ip1", + }, }, { HostName: "host2", HostUID: &huid2, - StaticIP: "ip2", + Nic: &models.V1Nic{ + IP: "ip2", + }, }, }, UpdateStrategy: &models.V1UpdateStrategy{Type: "strategy1"}, @@ -342,7 +390,9 @@ func TestFlattenMachinePoolConfigsEdgeNative(t *testing.T) { { HostName: "host3", HostUID: &huid3, - StaticIP: "ip3", + Nic: &models.V1Nic{ + IP: "ip3", + }, }, }, UpdateStrategy: &models.V1UpdateStrategy{Type: "strategy2"}, @@ -355,16 +405,24 @@ func TestFlattenMachinePoolConfigsEdgeNative(t *testing.T) { "control_plane": false, "node_repave_interval": int32(0), "name": "pool1", - "edge_host": []map[string]string{ + "edge_host": []map[string]interface{}{ { - "host_name": "host1", - "host_uid": "uid1", - "static_ip": "ip1", + "host_name": "host1", + "host_uid": "uid1", + "static_ip": "ip1", + "nic_name": "", + "default_gateway": "", + "subnet_mask": "", + "dns_servers": []string(nil), }, { - "host_name": "host2", - "host_uid": "uid2", - "static_ip": "ip2", + "host_name": "host2", + "host_uid": "uid2", + "static_ip": "ip2", + "nic_name": "", + "default_gateway": "", + "subnet_mask": "", + "dns_servers": []string(nil), }, }, "update_strategy": "strategy1", @@ -375,11 +433,15 @@ func TestFlattenMachinePoolConfigsEdgeNative(t *testing.T) { "control_plane": false, "node_repave_interval": int32(0), "name": "pool2", - "edge_host": []map[string]string{ + "edge_host": []map[string]interface{}{ { - "host_name": "host3", - "host_uid": "uid3", - "static_ip": "ip3", + "host_name": "host3", + "host_uid": "uid3", + "static_ip": "ip3", + "nic_name": "", + "default_gateway": "", + "subnet_mask": "", + "dns_servers": []string(nil), }, }, "update_strategy": "strategy2", From cf64944226b57ffe1ea41a206fa7b6f3114b474d Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Fri, 25 Oct 2024 19:52:43 +0530 Subject: [PATCH 3/4] docs fix --- docs/resources/cluster_edge_native.md | 9 +++++++-- spectrocloud/resource_cluster_edge_native.go | 9 +++++---- templates/resources/cluster_edge_native.md.tmpl | 5 +++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/docs/resources/cluster_edge_native.md b/docs/resources/cluster_edge_native.md index 16c6c84b..f287fd28 100644 --- a/docs/resources/cluster_edge_native.md +++ b/docs/resources/cluster_edge_native.md @@ -48,6 +48,11 @@ resource "spectrocloud_cluster_edge_native" "cluster" { edge_host { host_uid = spectrocloud_appliance.appliance1.uid static_ip = "1.2.3.4" + dns_servers = ["tf.test.com"] + host_name = "test-test" + nic_name = "auto160" + static_ip = "112.21.12.21" + subnet_mask = "2.2.1.0" } } @@ -153,9 +158,9 @@ Required: Optional: - `default_gateway` (String) Edge host default gateway -- `dns_servers` (Set of String) +- `dns_servers` (Set of String) Edge host DNS servers - `host_name` (String) Edge host name -- `nic_name` (String) Edge host IP Address +- `nic_name` (String) NIC Name for edge host. - `static_ip` (String) Edge host static IP address - `subnet_mask` (String) Edge host subnet mask - `two_node_role` (String) Two node role for edge host. Valid values are `primary` and `secondary`. diff --git a/spectrocloud/resource_cluster_edge_native.go b/spectrocloud/resource_cluster_edge_native.go index 8ba5ff5b..fc677140 100644 --- a/spectrocloud/resource_cluster_edge_native.go +++ b/spectrocloud/resource_cluster_edge_native.go @@ -236,7 +236,7 @@ func resourceClusterEdgeNative() *schema.Resource { }, "nic_name": { Type: schema.TypeString, - Description: "Edge host IP Address", + Description: "NIC Name for edge host.", Optional: true, }, "default_gateway": { @@ -250,9 +250,10 @@ func resourceClusterEdgeNative() *schema.Resource { Optional: true, }, "dns_servers": { - Type: schema.TypeSet, - Optional: true, - Set: schema.HashString, + Type: schema.TypeSet, + Optional: true, + Set: schema.HashString, + Description: "Edge host DNS servers", Elem: &schema.Schema{ Type: schema.TypeString, }, diff --git a/templates/resources/cluster_edge_native.md.tmpl b/templates/resources/cluster_edge_native.md.tmpl index 050469ba..e26d965f 100644 --- a/templates/resources/cluster_edge_native.md.tmpl +++ b/templates/resources/cluster_edge_native.md.tmpl @@ -48,6 +48,11 @@ resource "spectrocloud_cluster_edge_native" "cluster" { edge_host { host_uid = spectrocloud_appliance.appliance1.uid static_ip = "1.2.3.4" + dns_servers = ["tf.test.com"] + host_name = "test-test" + nic_name = "auto160" + static_ip = "112.21.12.21" + subnet_mask = "2.2.1.0" } } From aad0ed6848652b491349f11b85be58a4457aa975 Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Fri, 25 Oct 2024 21:03:57 +0530 Subject: [PATCH 4/4] unitest fix --- spectrocloud/resource_cluster_edge_native.go | 20 +++++++++++++------ .../resource_cluster_edge_native_test.go | 17 ++++++++++++---- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/spectrocloud/resource_cluster_edge_native.go b/spectrocloud/resource_cluster_edge_native.go index fc677140..30c97441 100644 --- a/spectrocloud/resource_cluster_edge_native.go +++ b/spectrocloud/resource_cluster_edge_native.go @@ -722,12 +722,7 @@ func toEdgeHosts(m map[string]interface{}) (*models.V1EdgeNativeMachinePoolCloud edgeHost := &models.V1EdgeNativeMachinePoolHostEntity{ HostName: hostName, HostUID: &hostId, - Nic: &models.V1Nic{ - Gateway: host.(map[string]interface{})["default_gateway"].(string), - IP: host.(map[string]interface{})["static_ip"].(string), - NicName: host.(map[string]interface{})["nic_name"].(string), - Subnet: host.(map[string]interface{})["subnet_mask"].(string), - }, + Nic: &models.V1Nic{}, // Hubble deprecated it and need to set it inside nic // StaticIP: host.(map[string]interface{})["static_ip"].(string), } @@ -740,6 +735,19 @@ func toEdgeHosts(m map[string]interface{}) (*models.V1EdgeNativeMachinePoolCloud edgeHost.Nic.DNS = result } } + if v, ok := host.(map[string]interface{})["default_gateway"]; ok { + edgeHost.Nic.Gateway = v.(string) + } + if v, ok := host.(map[string]interface{})["static_ip"]; ok { + edgeHost.Nic.IP = v.(string) + } + if v, ok := host.(map[string]interface{})["nic_name"]; ok { + edgeHost.Nic.NicName = v.(string) + } + if v, ok := host.(map[string]interface{})["subnet_mask"]; ok { + edgeHost.Nic.Subnet = v.(string) + } + if v, ok := host.(map[string]interface{})["two_node_role"].(string); ok { if v != "" { if _, ok := twoNodeHostRoles[v]; ok { diff --git a/spectrocloud/resource_cluster_edge_native_test.go b/spectrocloud/resource_cluster_edge_native_test.go index 31e00eab..87ff9a55 100644 --- a/spectrocloud/resource_cluster_edge_native_test.go +++ b/spectrocloud/resource_cluster_edge_native_test.go @@ -1,6 +1,7 @@ package spectrocloud import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "reflect" "testing" @@ -13,6 +14,14 @@ import ( "github.com/spectrocloud/terraform-provider-spectrocloud/types" ) +func ToSchemaSetFromStrings(strings []string) *schema.Set { + set := schema.NewSet(schema.HashString, nil) + for _, v := range strings { + set.Add(v) + } + return set +} + func TestToEdgeHosts(t *testing.T) { hostUI1 := "uid1" hostUI2 := "uid2" @@ -39,7 +48,7 @@ func TestToEdgeHosts(t *testing.T) { "nic_name": "test_nic", "default_gateway": "1.1.1.1", "subnet_mask": "2.2.2.2", - "dns_servers": []string{"t.t.com"}, + "dns_servers": ToSchemaSetFromStrings([]string{"t.t.com"}), }, map[string]interface{}{ "host_name": "host2", @@ -48,7 +57,7 @@ func TestToEdgeHosts(t *testing.T) { "nic_name": "test_nic", "default_gateway": "1.1.1.1", "subnet_mask": "2.2.2.2", - "dns_servers": []string{"t.t.com"}, + "dns_servers": ToSchemaSetFromStrings([]string{"t.t.com"}), }, }, }, @@ -90,7 +99,7 @@ func TestToEdgeHosts(t *testing.T) { "nic_name": "test_nic", "default_gateway": "1.1.1.1", "subnet_mask": "2.2.2.2", - "dns_servers": []string{"t.t.com"}, + "dns_servers": ToSchemaSetFromStrings([]string{"t.t.com"}), }, map[string]interface{}{ "host_name": "", @@ -99,7 +108,7 @@ func TestToEdgeHosts(t *testing.T) { "nic_name": "test_nic", "default_gateway": "1.1.1.1", "subnet_mask": "2.2.2.2", - "dns_servers": []string{"t.t.com"}, + "dns_servers": ToSchemaSetFromStrings([]string{"t.t.com"}), }, }, },