Skip to content

Commit

Permalink
fix: fixes #25 correctly set of dhcp + nil check + added firewall_typ…
Browse files Browse the repository at this point in the history
…e field in server resource
  • Loading branch information
iblindu committed Jun 24, 2021
1 parent fb6a3cf commit ce2376a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 11 deletions.
11 changes: 6 additions & 5 deletions ionoscloud/resource_k8s_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
62 changes: 62 additions & 0 deletions ionoscloud/resource_k8s_node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
}`
17 changes: 16 additions & 1 deletion ionoscloud/resource_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
13 changes: 8 additions & 5 deletions ionoscloud/resource_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ce2376a

Please sign in to comment.