Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Data source for civo_firewall #113

Merged
merged 1 commit into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions civo/datasource_firewall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package civo

import (
"fmt"
"log"
"strings"

"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

// Data source to get from the api a specific firewall
// using the id or the label
func dataSourceFirewall() *schema.Resource {
return &schema.Resource{
Description: strings.Join([]string{
"Retrieve information about a firewall for use in other resources.",
"This data source provides all of the firewall's properties as configured on your Civo account.",
"Firewalls may be looked up by id or label, and you can optionally pass region if you want to make a lookup for an expecific firewall inside that region.",
}, "\n\n"),
Read: dataSourceFirewallRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.NoZeroValues,
ExactlyOneOf: []string{"id", "name"},
},
"name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.NoZeroValues,
ExactlyOneOf: []string{"id", "name"},
Description: "The name of the Kubernetes Cluster",
},
"region": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.NoZeroValues,
Description: "The region where cluster is running",
},
// Computed resource
"network_id": {
Type: schema.TypeString,
Computed: true,
Description: "The id of the associated network",
},
},
}
}

func dataSourceFirewallRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

// overwrite the region if is define in the datasource
if region, ok := d.GetOk("region"); ok {
apiClient.Region = region.(string)
}

var foundFirewall *civogo.Firewall

if id, ok := d.GetOk("id"); ok {
log.Printf("[INFO] Getting the firewall by id")
firewall, err := apiClient.FindFirewall(id.(string))
if err != nil {
return fmt.Errorf("[ERR] failed to retrive firewall: %s", err)
}

foundFirewall = firewall
} else if name, ok := d.GetOk("name"); ok {
log.Printf("[INFO] Getting the firewall by name")
firewall, err := apiClient.FindFirewall(name.(string))
if err != nil {
return fmt.Errorf("[ERR] failed to retrive firewall: %s", err)
}

foundFirewall = firewall
}

d.SetId(foundFirewall.ID)
d.Set("name", foundFirewall.Name)
d.Set("network_id", foundFirewall.NetworkID)
d.Set("region", apiClient.Region)

return nil
}
39 changes: 39 additions & 0 deletions civo/datasource_firewall_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package civo

import (
"fmt"
"testing"

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

func TestAccDataSourceCivoFirewall_basic(t *testing.T) {
datasourceName := "data.civo_firewall.foobar"
name := acctest.RandomWithPrefix("net-test")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceCivoFirewallConfig(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "name", name),
),
},
},
})
}

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

data "civo_firewall" "foobar" {
label = civo_firewall.foobar.name
}
`, name)
}
1 change: 1 addition & 0 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func Provider() *schema.Provider {
"civo_dns_domain_record": dataSourceDNSDomainRecord(),
"civo_network": dataSourceNetwork(),
"civo_volume": dataSourceVolume(),
"civo_firewall": dataSourceFirewall(),
// "civo_loadbalancer": dataSourceLoadBalancer(),
"civo_ssh_key": dataSourceSSHKey(),
// "civo_snapshot": dataSourceSnapshot(),
Expand Down