Skip to content

Commit

Permalink
Adds Firewall datasource (#594)
Browse files Browse the repository at this point in the history
* Adds new firewall datasource

* Adds datasource test (not working)

* Fixes firewall datasource test

* Clean up

* Addresses suggestions from code review

* Remove optional/required attribute details
  • Loading branch information
scotchneat committed Mar 2, 2021
1 parent 786f7c1 commit 1f77729
Show file tree
Hide file tree
Showing 7 changed files with 513 additions and 339 deletions.
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build: fmtcheck
go install

test: fmtcheck
go test -i $(TEST) || exit 1
go test $(TEST) || exit 1
echo $(TEST) | \
xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4

Expand Down
33 changes: 33 additions & 0 deletions digitalocean/datasource_digitalocean_firewall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package digitalocean

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceDigitalOceanFirewall() *schema.Resource {
fwSchema := firewallSchema()

for _, f := range fwSchema {
f.Computed = true
f.Required = false
}

fwSchema["name"].ValidateFunc = nil

fwSchema["firewall_id"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
}

return &schema.Resource{
ReadContext: dataSourceDigitalOceanFirewallRead,
Schema: fwSchema,
}
}

func dataSourceDigitalOceanFirewallRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
d.SetId(d.Get("firewall_id").(string))
return resourceDigitalOceanFirewallRead(ctx, d, meta)
}
77 changes: 77 additions & 0 deletions digitalocean/datasource_digitalocean_firewall_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package digitalocean

import (
"fmt"
"github.com/digitalocean/godo"
"testing"

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

func TestAccDataSourceDigitalOceanFirewall_Basic(t *testing.T) {
fwDataConfig := `
data "digitalocean_firewall" "foobar" {
firewall_id = digitalocean_firewall.foobar.id
}`

var firewall godo.Firewall
fwName := randomTestName()

fwCreateConfig := fmt.Sprintf(testAccDigitalOceanFirewallConfig_OnlyInbound(fwName))
updatedFWCreateConfig := testAccDigitalOceanFirewallConfig_OnlyMultipleInbound(fwName)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: fwCreateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanFirewallExists("digitalocean_firewall.foobar", &firewall),
),
},
{
Config: fwCreateConfig + fwDataConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.digitalocean_firewall.foobar", "name", "foobar-"+fwName),
resource.TestCheckResourceAttrPair("digitalocean_firewall.foobar", "id",
"data.digitalocean_firewall.foobar", "firewall_id"),
resource.TestCheckResourceAttrPair("digitalocean_firewall.foobar", "droplet_ids",
"data.digitalocean_firewall.foobar", "droplet_ids"),
resource.TestCheckResourceAttrPair("digitalocean_firewall.foobar", "inbound_rule",
"data.digitalocean_firewall.foobar", "inbound_rule"),
resource.TestCheckResourceAttrPair("digitalocean_firewall.foobar", "outbound_rule",
"data.digitalocean_firewall.foobar", "outbound_rule"),
resource.TestCheckResourceAttrPair("digitalocean_firewall.foobar", "status",
"data.digitalocean_firewall.foobar", "status"),
resource.TestCheckResourceAttrPair("digitalocean_firewall.foobar", "created_at",
"data.digitalocean_firewall.foobar", "created_at"),
resource.TestCheckResourceAttrPair("digitalocean_firewall.foobar", "pending_changes",
"data.digitalocean_firewall.foobar", "pending_changes"),
resource.TestCheckResourceAttrPair("digitalocean_firewall.foobar", "tags",
"data.digitalocean_firewall.foobar", "tags"),
resource.TestCheckResourceAttr("data.digitalocean_firewall.foobar", "inbound_rule.0.protocol", "tcp"),
resource.TestCheckResourceAttr("data.digitalocean_firewall.foobar", "inbound_rule.0.port_range", "22"),
resource.TestCheckResourceAttr("data.digitalocean_firewall.foobar", "inbound_rule.0.source_addresses.0", "0.0.0.0/0"),
resource.TestCheckResourceAttr("data.digitalocean_firewall.foobar", "inbound_rule.0.source_addresses.1", "::/0"),
),
},
{
Config: updatedFWCreateConfig,
},
{
Config: updatedFWCreateConfig + fwDataConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.digitalocean_firewall.foobar", "inbound_rule.0.protocol", "tcp"),
resource.TestCheckResourceAttr("data.digitalocean_firewall.foobar", "inbound_rule.0.port_range", "22"),
resource.TestCheckResourceAttr("data.digitalocean_firewall.foobar", "inbound_rule.0.source_addresses.0", "0.0.0.0/0"),
resource.TestCheckResourceAttr("data.digitalocean_firewall.foobar", "inbound_rule.0.source_addresses.1", "::/0"),
resource.TestCheckResourceAttr("data.digitalocean_firewall.foobar", "inbound_rule.1.protocol", "tcp"),
resource.TestCheckResourceAttr("data.digitalocean_firewall.foobar", "inbound_rule.1.port_range", "80"),
resource.TestCheckResourceAttr("data.digitalocean_firewall.foobar", "inbound_rule.1.source_addresses.0", "1.2.3.0/24"),
resource.TestCheckResourceAttr("data.digitalocean_firewall.foobar", "inbound_rule.1.source_addresses.1", "2002::/16"),
),
},
},
})
}
Loading

0 comments on commit 1f77729

Please sign in to comment.