Skip to content

Commit

Permalink
feat: Add firewall and loadbalancer
Browse files Browse the repository at this point in the history
- 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
alejandrojnm committed Mar 23, 2020
1 parent 73ebfd3 commit 834be2e
Show file tree
Hide file tree
Showing 8 changed files with 605 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ Progress
- Regions
- Quotas
- Sizes
- Domain names
- Domain records
- ~~Domain names~~
- ~~Domain records~~
- Kubernetes Clusters
- Kubernetes Applications
- Load balancers
Expand Down
3 changes: 3 additions & 0 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func Provider() terraform.ResourceProvider {
"civo_volume": resourceVolume(),
"civo_dns_domain_name": resourceDnsDomainName(),
"civo_dns_domain_record": resourceDnsDomainRecord(),
"civo_firewall": resourceFirewall(),
"civo_firewall_rule": resourceFirewallRule(),
"civo_loadbalancer": resourceLoadBalancer(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
90 changes: 90 additions & 0 deletions civo/resource_firewall.go
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
}
147 changes: 147 additions & 0 deletions civo/resource_firewall_rule.go
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
}
Loading

0 comments on commit 834be2e

Please sign in to comment.