From ce2376a2f44668a41eea9d7d991aefd705144ac2 Mon Sep 17 00:00:00 2001 From: IuliaBlindu Date: Thu, 24 Jun 2021 19:55:11 +0300 Subject: [PATCH] fix: fixes #25 correctly set of dhcp + nil check + added firewall_type field in server resource --- ionoscloud/resource_k8s_node_pool.go | 11 ++-- ionoscloud/resource_k8s_node_pool_test.go | 62 +++++++++++++++++++++++ ionoscloud/resource_server.go | 17 ++++++- ionoscloud/resource_server_test.go | 13 +++-- 4 files changed, 92 insertions(+), 11 deletions(-) diff --git a/ionoscloud/resource_k8s_node_pool.go b/ionoscloud/resource_k8s_node_pool.go index 47732706b..375a50158 100644 --- a/ionoscloud/resource_k8s_node_pool.go +++ b/ionoscloud/resource_k8s_node_pool.go @@ -70,6 +70,7 @@ func resourcek8sNodePool() *schema.Resource { Type: schema.TypeBool, Description: "Indicates if the Kubernetes Node Pool LAN will reserve an IP using DHCP", Optional: true, + Default: true, }, "routes": { Type: schema.TypeList, @@ -269,10 +270,10 @@ func resourcek8sNodePoolCreate(ctx context.Context, d *schema.ResourceData, meta lan.Id = &lanID addLan = true } - if lanDhcp, lanDhcpOk := d.GetOk(fmt.Sprintf("lans.%d.dhcp", lanIndex)); lanDhcpOk { - lanDhcp := lanDhcp.(bool) - lan.Dhcp = &lanDhcp - } + + lanDhcp := d.Get(fmt.Sprintf("lans.%d.dhcp", lanIndex)).(bool) + lan.Dhcp = &lanDhcp + if lanRoutes, lanRoutesOk := d.GetOk(fmt.Sprintf("lans.%d.routes", lanIndex)); lanRoutesOk { if lanRoutes.([]interface{}) != nil { updateRoutes := false @@ -535,7 +536,7 @@ func resourcek8sNodePoolRead(ctx context.Context, d *schema.ResourceData, meta i } nodePoolRoutes := make([]interface{}, 0) - if len(*nodePoolLan.Routes) > 0 { + if nodePoolLan.Routes != nil && len(*nodePoolLan.Routes) > 0 { nodePoolRoutes = make([]interface{}, len(*nodePoolLan.Routes)) for routeIndex, nodePoolRoute := range *nodePoolLan.Routes { routeEntry := make(map[string]string) diff --git a/ionoscloud/resource_k8s_node_pool_test.go b/ionoscloud/resource_k8s_node_pool_test.go index b8b7c0d13..b41de8e8e 100644 --- a/ionoscloud/resource_k8s_node_pool_test.go +++ b/ionoscloud/resource_k8s_node_pool_test.go @@ -64,6 +64,28 @@ func TestAcck8sNodepool_Basic(t *testing.T) { }) } +func TestAcck8sNodepool_Lan(t *testing.T) { + var k8sNodepool ionoscloud.KubernetesNodePool + k8sNodepoolName := "terraform_acctest" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccCheckk8sNodepoolDestroyCheck, + Steps: []resource.TestStep{ + { + Config: fmt.Sprintf(testAccCheckk8sNodepoolConfigLan, k8sNodepoolName), + Check: resource.ComposeTestCheckFunc( + testAccCheckk8sNodepoolExists("ionoscloud_k8s_node_pool.terraform_acctest", &k8sNodepool), + resource.TestCheckResourceAttr("ionoscloud_k8s_node_pool.terraform_acctest", "name", k8sNodepoolName), + ), + }, + }, + }) +} + func testAccCheckk8sNodepoolDestroyCheck(s *terraform.State) error { client := testAccProvider.Meta().(*ionoscloud.APIClient) @@ -202,3 +224,43 @@ resource "ionoscloud_k8s_node_pool" "terraform_acctest" { storage_size = 40 public_ips = [ "%s", "%s", "%s" ] }` + +const testAccCheckk8sNodepoolConfigLan = ` +resource "ionoscloud_datacenter" "terraform_acctest" { + name = "terraform_acctest_lan" + location = "us/las" + description = "Datacenter created through terraform" +} + +resource "ionoscloud_lan" "terraform_acctest" { + datacenter_id = "${ionoscloud_datacenter.terraform_acctest.id}" + public = false + name = "terraform_acctest_lan" +} + +resource "ionoscloud_k8s_cluster" "terraform_acctest" { + name = "terraform_acctest_lan" + k8s_version = "1.20.6" + maintenance_window { + day_of_the_week = "Monday" + time = "09:00:00Z" + } +} + +resource "ionoscloud_k8s_node_pool" "terraform_acctest" { + name = "%s" + datacenter_id = ionoscloud_datacenter.terraform_acctest.id + k8s_cluster_id = ionoscloud_k8s_cluster.terraform_acctest.id + k8s_version = ionoscloud_k8s_cluster.terraform_acctest.k8s_version + lans { + id = ionoscloud_lan.terraform_acctest.id + dhcp = false + } + cpu_family = "AMD_OPTERON" + availability_zone = "AUTO" + storage_type = "SSD" + node_count = 1 + cores_count = 2 + ram_size = 2048 + storage_size = 40 +}` diff --git a/ionoscloud/resource_server.go b/ionoscloud/resource_server.go index 84f0d83ec..014ac3980 100644 --- a/ionoscloud/resource_server.go +++ b/ionoscloud/resource_server.go @@ -239,6 +239,11 @@ func resourceServer() *schema.Resource { Type: schema.TypeBool, Optional: true, }, + "firewall_type": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, "firewall": { Type: schema.TypeList, Optional: true, @@ -572,6 +577,11 @@ func resourceServerCreate(ctx context.Context, d *schema.ResourceData, meta inte nic.Properties.Dhcp = boolAddr(d.Get("nic.0.dhcp").(bool)) nic.Properties.FirewallActive = boolAddr(d.Get("nic.0.firewall_active").(bool)) + if v, ok := d.GetOk("nic.0.firewall_type"); ok { + v := v.(string) + nic.Properties.FirewallType = &v + } + if v, ok := d.GetOk("nic.0.ip"); ok { ips := strings.Split(v.(string), ",") if len(ips) > 0 { @@ -884,7 +894,7 @@ func resourceServerRead(ctx context.Context, d *schema.ResourceData, meta interf setPropWithNilCheck(network, "dhcp", nic.Properties.Dhcp) setPropWithNilCheck(network, "firewall_active", nic.Properties.FirewallActive) - + setPropWithNilCheck(network, "firewall_type", nic.Properties.FirewallType) setPropWithNilCheck(network, "lan", nic.Properties.Lan) setPropWithNilCheck(network, "name", nic.Properties.Name) setPropWithNilCheck(network, "ips", nic.Properties.Ips) @@ -1117,6 +1127,11 @@ func resourceServerUpdate(ctx context.Context, d *schema.ResourceData, meta inte properties.FirewallActive = boolAddr(d.Get("nic.0.firewall_active").(bool)) + if v, ok := d.GetOk("nic.0.firewall_type"); ok { + vStr := v.(string) + properties.FirewallType = &vStr + } + if d.HasChange("nic.0.firewall") { firewall := GetFirewallResource(d, "nic.0.firewall") diff --git a/ionoscloud/resource_server_test.go b/ionoscloud/resource_server_test.go index 57a122915..7f47d1c74 100644 --- a/ionoscloud/resource_server_test.go +++ b/ionoscloud/resource_server_test.go @@ -208,7 +208,8 @@ resource "ionoscloud_server" "webserver" { lan = "${ionoscloud_lan.webserver_lan.id}" dhcp = true firewall_active = true - firewall { + firewall_type = "BIDIRECTIONAL" + firewall { protocol = "TCP" name = "SSH" port_range_start = 22 @@ -247,7 +248,8 @@ resource "ionoscloud_server" "webserver" { lan = "${ionoscloud_lan.webserver_lan.id}" dhcp = true firewall_active = true - firewall { + firewall_type = "BIDIRECTIONAL" + firewall { protocol = "TCP" name = "SSH" port_range_start = 22 @@ -286,7 +288,8 @@ resource "ionoscloud_server" "webserver" { lan = "${ionoscloud_lan.webserver_lan.id}" dhcp = false firewall_active = false - firewall { + firewall_type = "BIDIRECTIONAL" + firewall { protocol = "TCP" name = "SSH" port_range_start = 22 @@ -358,8 +361,8 @@ resource "ionoscloud_server" "webserver" { nic { lan = "${ionoscloud_lan.webserver_lan.id}" dhcp = true - firewall_active = true - firewall { + firewall_active = true + firewall { protocol = "TCP" name = "SSH" port_range_start = 22