Skip to content

Commit

Permalink
Make CIDR required in 'civo_firewall_rule' (#75)
Browse files Browse the repository at this point in the history
* Make CIDR required in 'civo_firewall_rule'

* Add CIDR validation to firewall rule resource

* Remove CIDR validation because ValidateFunc only works for primitive types
  • Loading branch information
zulh-civo authored Sep 2, 2021
1 parent b176aef commit 0bbeab8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
19 changes: 7 additions & 12 deletions civo/resource_firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ func resourceFirewallRule() *schema.Resource {
},
"cidr": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Required: true,
ForceNew: true,
Description: "The IP address of the other end (i.e. not your instance) to affect, or a valid network CIDR (defaults to being globally applied, i.e. 0.0.0.0/0)",
Description: "The CIDR notation of the other end to affect, or a valid network CIDR (e.g. 0.0.0.0/0 to open for everyone or 1.2.3.4/32 to open just for a specific IP address)",
Elem: &schema.Schema{Type: schema.TypeString},
},
"direction": {
Expand Down Expand Up @@ -127,15 +126,13 @@ func resourceFirewallRuleCreate(d *schema.ResourceData, m interface{}) error {
config.Label = attr.(string)
}

log.Printf("[INFO] Config: %+v", config)

log.Printf("[INFO] creating a new firewall rule for firewall %s", d.Get("firewall_id").(string))
log.Printf("[INFO] Creating a new firewall rule for firewall %s with config: %+v", d.Get("firewall_id").(string), config)
firewallRule, err := apiClient.NewFirewallRule(config)
if err != nil {
return fmt.Errorf("[ERR] failed to create a new firewall: %s", err)
return fmt.Errorf("[ERR] failed to create a new firewall rule: %s", err)
}

log.Printf("[INFO] RuleID: %s", firewallRule.ID)
log.Printf("[INFO] Firewall rule created with ID: %s", firewallRule.ID)

d.SetId(firewallRule.ID)

Expand All @@ -151,10 +148,8 @@ func resourceFirewallRuleRead(d *schema.ResourceData, m interface{}) error {
apiClient.Region = region.(string)
}

log.Printf("[INFO] firewallID: %s", d.Get("firewall_id").(string))
log.Printf("[INFO] RuleID: %s", d.Id())
log.Printf("[INFO] Reading firewall rule %s from firewall %s", d.Id(), d.Get("firewall_id").(string))

log.Printf("[INFO] retriving the firewall rule %s", d.Id())
resp, err := apiClient.FindFirewallRule(d.Get("firewall_id").(string), d.Id())
if err != nil {
if resp == nil {
Expand All @@ -165,7 +160,7 @@ func resourceFirewallRuleRead(d *schema.ResourceData, m interface{}) error {
return fmt.Errorf("[ERR] error retrieving firewall rule: %s", err)
}

log.Printf("[INFO] rules %+v", resp)
log.Printf("[INFO] Rules response: %+v", resp)

d.Set("firewall_id", resp.FirewallID)
d.Set("protocol", resp.Protocol)
Expand Down
4 changes: 2 additions & 2 deletions docs/resources/firewall_rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ The following arguments are supported:
* `protocol` (Required) This may be one of "tcp", "udp", or "icmp".
* `start_port` (Required) The start port where traffic to be allowed.
* `end_port` (Required) The end port where traffic to be allowed.
* `cidr` (Required) the IP address of the other end (i.e. not your instance) to affect, or a valid network CIDR (defaults to being globally applied, i.e. 0.0.0.0/0).
* `cidr` (Required) The CIDR notation of the other end to affect, or a valid network CIDR (e.g. 0.0.0.0/0 to open for everyone or 1.2.3.4/32 to open just for a specific IP address.
* `direction` (Required) will this rule affect ingress traffic
* `label` (Optional) a string that will be the displayed name/reference for this rule (optional)

Expand All @@ -53,7 +53,7 @@ The following attributes are exported:
* `protocol` This may be one of "tcp", "udp", or "icmp".
* `start_port` The start port where traffic to be allowed.
* `end_port` The end port where traffic to be allowed.
* `cidr` A list of IP address of the other end (i.e. not your instance) to affect, or a valid network CIDR.
* `cidr` A list of CIDR notations of the other end to affect.
* `direction` Will this rule affect ingress traffic
* `label` A string that will be the displayed name/reference for this rule (optional)

Expand Down

0 comments on commit 0bbeab8

Please sign in to comment.