-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
9d2f1ed
commit d3b55ce
Showing
7 changed files
with
513 additions
and
339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.