-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add Firewall - Add loadbalancder - Fix instance to work with the firewall and loadbalancer BREAKING CHANGE: No Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
- Loading branch information
1 parent
73ebfd3
commit 834be2e
Showing
8 changed files
with
605 additions
and
33 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
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,90 @@ | ||
package civo | ||
|
||
import ( | ||
"fmt" | ||
"github.com/civo/civogo" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"log" | ||
) | ||
|
||
func resourceFirewall() *schema.Resource { | ||
fmt.Print() | ||
return &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validateName, | ||
}, | ||
"region": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
Create: resourceFirewallCreate, | ||
Read: resourceFirewallRead, | ||
Update: resourceFirewallUpdate, | ||
Delete: resourceFirewallDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
} | ||
} | ||
|
||
func resourceFirewallCreate(d *schema.ResourceData, m interface{}) error { | ||
apiClient := m.(*civogo.Client) | ||
|
||
firewall, err := apiClient.NewFirewall(d.Get("name").(string)) | ||
if err != nil { | ||
fmt.Errorf("failed to create a new firewall: %s", err) | ||
return err | ||
} | ||
|
||
d.SetId(firewall.ID) | ||
|
||
return resourceFirewallRead(d, m) | ||
} | ||
|
||
func resourceFirewallRead(d *schema.ResourceData, m interface{}) error { | ||
apiClient := m.(*civogo.Client) | ||
|
||
resp, err := apiClient.FindFirewall(d.Id()) | ||
if err != nil { | ||
if resp != nil { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("[ERR] error retrieving firewall: %s", err) | ||
} | ||
|
||
d.Set("name", resp.Name) | ||
d.Set("region", resp.Region) | ||
|
||
return nil | ||
} | ||
|
||
func resourceFirewallUpdate(d *schema.ResourceData, m interface{}) error { | ||
apiClient := m.(*civogo.Client) | ||
|
||
if d.HasChange("name") { | ||
if d.Get("name").(string) != "" { | ||
_, err := apiClient.RenameFirewall(d.Id(), d.Get("name").(string)) | ||
if err != nil { | ||
log.Printf("[WARN] an error occurred while trying to rename the firewall (%s)", d.Id()) | ||
} | ||
} | ||
} | ||
|
||
return resourceFirewallRead(d, m) | ||
} | ||
|
||
func resourceFirewallDelete(d *schema.ResourceData, m interface{}) error { | ||
apiClient := m.(*civogo.Client) | ||
|
||
_, err := apiClient.DeleteFirewall(d.Id()) | ||
if err != nil { | ||
log.Printf("[INFO] civo firewall (%s) was delete", d.Id()) | ||
} | ||
return nil | ||
} |
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,147 @@ | ||
package civo | ||
|
||
import ( | ||
"fmt" | ||
"github.com/civo/civogo" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
"log" | ||
) | ||
|
||
func resourceFirewallRule() *schema.Resource { | ||
fmt.Print() | ||
return &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"firewall_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateName, | ||
}, | ||
"protocol": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "", | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
"tcp", | ||
"udp", | ||
"icmp", | ||
}, false), | ||
}, | ||
"start_port": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "", | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
"end_port": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "", | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
"cird": { | ||
Type: schema.TypeSet, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "", | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"direction": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "", | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
"inbound", | ||
"outbound", | ||
}, false), | ||
}, | ||
"label": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
}, | ||
Create: resourceFirewallRuleCreate, | ||
Read: resourceFirewallRuleRead, | ||
Delete: resourceFirewallRuleDelete, | ||
//Importer: &schema.ResourceImporter{ | ||
// State: schema.ImportStatePassthrough, | ||
//}, | ||
} | ||
} | ||
|
||
func resourceFirewallRuleCreate(d *schema.ResourceData, m interface{}) error { | ||
apiClient := m.(*civogo.Client) | ||
|
||
tfCidrs := d.Get("cird").(*schema.Set).List() | ||
cird := make([]string, len(tfCidrs)) | ||
for i, tfCird := range tfCidrs { | ||
cird[i] = tfCird.(string) | ||
} | ||
|
||
config := &civogo.FirewallRuleConfig{ | ||
FirewallID: d.Get("firewall_id").(string), | ||
Protocol: d.Get("protocol").(string), | ||
StartPort: d.Get("start_port").(string), | ||
Direction: d.Get("direction").(string), | ||
Cidr: cird, | ||
} | ||
|
||
if attr, ok := d.GetOk("end_port"); ok { | ||
config.EndPort = attr.(string) | ||
} | ||
|
||
if attr, ok := d.GetOk("label"); ok { | ||
config.Label = attr.(string) | ||
} | ||
|
||
firewallRule, err := apiClient.NewFirewallRule(config) | ||
if err != nil { | ||
fmt.Errorf("[ERR] failed to create a new firewall: %s", err) | ||
return err | ||
} | ||
|
||
d.SetId(firewallRule.ID) | ||
|
||
return resourceFirewallRuleRead(d, m) | ||
} | ||
|
||
func resourceFirewallRuleRead(d *schema.ResourceData, m interface{}) error { | ||
apiClient := m.(*civogo.Client) | ||
|
||
resp, err := apiClient.FindFirewallRule(d.Get("firewall_id").(string), d.Id()) | ||
if err != nil { | ||
if resp != nil { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("[ERR] error retrieving firewall Rule: %s", err) | ||
} | ||
|
||
d.Set("firewall_id", resp.FirewallID) | ||
d.Set("protocol", resp.Protocol) | ||
d.Set("start_port", resp.StartPort) | ||
d.Set("end_port", resp.EndPort) | ||
d.Set("cird", resp.Cidr) | ||
d.Set("direction", resp.Direction) | ||
d.Set("label", resp.Label) | ||
|
||
return nil | ||
} | ||
|
||
func resourceFirewallRuleDelete(d *schema.ResourceData, m interface{}) error { | ||
apiClient := m.(*civogo.Client) | ||
|
||
_, err := apiClient.DeleteFirewallRule(d.Get("firewall_id").(string), d.Id()) | ||
if err != nil { | ||
log.Printf("[INFO] civo firewall rule (%s) was delete", d.Id()) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.