Skip to content

Commit

Permalink
Fixed error in the instances creation
Browse files Browse the repository at this point in the history
Fixed error message
Added the load balancer basic test

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Jun 22, 2020
1 parent 961f283 commit bd3664d
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 8 deletions.
2 changes: 1 addition & 1 deletion civo/resource_dns_domain_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func resourceDNSDomainRecordRead(d *schema.ResourceData, m interface{}) error {
return nil
}

return fmt.Errorf("[WARN] domain record (%s) not found", d.Id())
return fmt.Errorf("[WARN] error retrieving domain record: %s", err)
}

d.Set("name", resp.Name)
Expand Down
2 changes: 1 addition & 1 deletion civo/resource_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func resourceFirewallRead(d *schema.ResourceData, m interface{}) error {
return nil
}

return fmt.Errorf("[ERR] error retrieving firewall %s, %s", d.Id(), err)
return fmt.Errorf("[ERR] error retrieving firewall: %s", err)
}

d.Set("name", resp.Name)
Expand Down
2 changes: 1 addition & 1 deletion civo/resource_firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func resourceFirewallRuleRead(d *schema.ResourceData, m interface{}) error {
return nil
}

return fmt.Errorf("[ERR] error retrieving firewall Rule: %s", err)
return fmt.Errorf("[ERR] error retrieving firewall rule: %s", err)
}

d.Set("firewall_id", resp.FirewallID)
Expand Down
11 changes: 7 additions & 4 deletions civo/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,13 @@ func resourceInstanceRead(d *schema.ResourceData, m interface{}) error {

log.Printf("[INFO] retriving the instance %s", d.Id())
resp, err := apiClient.GetInstance(d.Id())
if err == nil {
d.SetId("")
// check if the instance no longer exists.
return fmt.Errorf("[ERR] instance (%s) not found", d.Id())
if err != nil {
if resp == nil {
d.SetId("")
return nil
}

return fmt.Errorf("[ERR] failed to retriving the instance: %s", err)
}

d.Set("hostname", resp.Hostname)
Expand Down
182 changes: 182 additions & 0 deletions civo/resource_loadbalancer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package civo

import (
"fmt"
"testing"

"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

// example.Widget represents a concrete Go type that represents an API resource
func TestAccCivoLoadBalancer_basic(t *testing.T) {
var loadBalancer civogo.LoadBalancer

// generate a random name for each test run
resName := "civo_loadbalancer.foobar"
var domainName = acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCivoLoadBalancerDestroy,
Steps: []resource.TestStep{
{
// use a dynamic configuration with the random name from above
Config: testAccCheckCivoLoadBalancerConfigBasic(domainName),
// compose a basic test, checking both remote and local values
Check: resource.ComposeTestCheckFunc(
// query the API to retrieve the widget object
testAccCheckCivoLoadBalancerResourceExists(resName, &loadBalancer),
// verify remote values
testAccCheckCivoLoadBalancerValues(&loadBalancer, domainName),
// verify local values
resource.TestCheckResourceAttr(resName, "protocol", "http"),
resource.TestCheckResourceAttr(resName, "port", "80"),
),
},
},
})
}

// func TestAccCivoLoadBalancer_update(t *testing.T) {
// var firewallRule civogo.FirewallRule

// // generate a random name for each test run
// resName := "civo_firewall_rule.testrule"
// var firewallRuleName = acctest.RandomWithPrefix("rename-fw-rule")

// resource.Test(t, resource.TestCase{
// PreCheck: func() { testAccPreCheck(t) },
// Providers: testAccProviders,
// CheckDestroy: testAccCheckCivoFirewallRuleDestroy,
// Steps: []resource.TestStep{
// {
// Config: testAccCheckCivoFirewallRuleConfigUpdates(firewallRuleName),
// Check: resource.ComposeTestCheckFunc(
// testAccCheckCivoFirewallRuleResourceExists(resName, &firewallRule),
// resource.TestCheckResourceAttr(resName, "protocol", "tcp"),
// resource.TestCheckResourceAttr(resName, "start_port", "443"),
// ),
// },
// {
// // use a dynamic configuration with the random name from above
// Config: testAccCheckCivoFirewallRuleConfigUpdates(firewallRuleName),
// Check: resource.ComposeTestCheckFunc(
// testAccCheckCivoFirewallRuleResourceExists(resName, &firewallRule),
// testAccCheckCivoFirewallRuleUpdated(&firewallRule),
// resource.TestCheckResourceAttr(resName, "protocol", "tcp"),
// resource.TestCheckResourceAttr(resName, "start_port", "443"),
// ),
// },
// },
// })
// }

func testAccCheckCivoLoadBalancerValues(loadBalancer *civogo.LoadBalancer, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if loadBalancer.Hostname != name {
return fmt.Errorf("bad protocol, expected \"%s\", got: %#v", name, loadBalancer.Hostname)
}
return nil
}
}

// testAccCheckExampleResourceExists queries the API and retrieves the matching Widget.
func testAccCheckCivoLoadBalancerResourceExists(n string, loadBalancer *civogo.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
// find the corresponding state object
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

// retrieve the configured client from the test setup
client := testAccProvider.Meta().(*civogo.Client)
resp, err := client.FindLoadBalancer(rs.Primary.ID)
if err != nil {
return fmt.Errorf("LoadBalancer not found: (%s) %s", rs.Primary.ID, err)
}

// If no error, assign the response Widget attribute to the widget pointer
*loadBalancer = *resp

// return fmt.Errorf("Domain (%s) not found", rs.Primary.ID)
return nil
}
}

// func testAccCheckCivoLoadBalancerUpdated(firewall *civogo.FirewallRule) resource.TestCheckFunc {
// return func(s *terraform.State) error {
// if firewall.Protocol != "tcp" {
// return fmt.Errorf("bad protocol, expected \"%s\", got: %#v", "tcp", firewall.Protocol)
// }
// if firewall.StartPort != "443" {
// return fmt.Errorf("bad port, expected \"%s\", got: %#v", "443", firewall.StartPort)
// }
// return nil
// }
// }

func testAccCheckCivoLoadBalancerDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*civogo.Client)

for _, rs := range s.RootModule().Resources {
if rs.Type != "civo_loadbalancer" {
continue
}

_, err := client.FindLoadBalancer(rs.Primary.ID)
if err == nil {
return fmt.Errorf("LoadBlanacer still exists")
}
}

return nil
}

func testAccCheckCivoLoadBalancerConfigBasic(name string) string {
return fmt.Sprintf(`
resource "civo_instance" "vm" {
hostname = "instance-%s"
}
resource "civo_loadbalancer" "foobar" {
hostname = "%s"
protocol = "http"
port = 80
max_request_size = 30
policy = "round_robin"
health_check_path = "/"
max_conns = 10
fail_timeout = 40
depends_on = [civo_instance.vm]
backend {
instance_id = civo_instance.vm.id
protocol = "http"
port = 80
}
}
`, name, name)
}

// func testAccCheckCivoLoadBalancerConfigUpdates(name string) string {
// return fmt.Sprintf(`
// resource "civo_firewall" "foobar" {
// name = "%s"
// }

// resource "civo_firewall_rule" "testrule" {
// firewall_id = civo_firewall.foobar.id
// protocol = "tcp"
// start_port = "443"
// end_port = "443"
// cidr = ["192.168.1.2/32"]
// direction = "inbound"
// label = "web"
// }
// `, name)
// }
2 changes: 1 addition & 1 deletion civo/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func resourceNetworkRead(d *schema.ResourceData, m interface{}) error {
return nil
}

return fmt.Errorf("[ERR] failed to list all network %s", err)
return fmt.Errorf("[ERR] failed to list the network: %s", err)
}

for _, net := range resp {
Expand Down

0 comments on commit bd3664d

Please sign in to comment.