Skip to content

Commit

Permalink
feat: Added one feature to the snapshot resource
Browse files Browse the repository at this point in the history
- Update the resource_firewall_rule.go resource
- Add next_execution option to the snapshot resource
- Add cronexpr to parse the cron_time and say the next execution

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Apr 16, 2020
1 parent e4dc011 commit 52bf3e0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ Progress resource
-~~Domain records~~
-~~Templates~~
-~~Snapshots~~
- Regions
- Quotas
- Sizes
- Kubernetes Clusters
- Kubernetes Applications
-~~Regions~~
-~~Quotas~~
-~~Sizes~~
-~~Kubernetes Clusters~~

Progress data source
----------------------
-~~Templates~~
-~~Templates~~
-~~Instances Size~~
-~~Kubernetes Version~~
16 changes: 8 additions & 8 deletions civo/resource_firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// Firewall Rule resource represent you can create and manage all firewall rules
// this resource don't have a update option because the backend don't have the
// this resource don't have an update option because the backend don't have the
// support for that, so in this case we use ForceNew for all object in the resource
func resourceFirewallRule() *schema.Resource {
fmt.Print()
Expand All @@ -25,7 +25,7 @@ func resourceFirewallRule() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "",
Description: "The protocol choice from tcp, udp or icmp (the default if unspecified is tcp)",
ValidateFunc: validation.StringInSlice([]string{
"tcp",
"udp",
Expand All @@ -36,28 +36,28 @@ func resourceFirewallRule() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "",
Description: "The start of the port range to configure for this rule (or the single port if required)",
ValidateFunc: validation.NoZeroValues,
},
"end_port": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "",
Description: "The end of the port range (this is optional, by default it will only apply to the single port listed in start_port)",
ValidateFunc: validation.NoZeroValues,
},
"cird": {
Type: schema.TypeSet,
Required: true,
ForceNew: true,
Description: "",
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)",
Elem: &schema.Schema{Type: schema.TypeString},
},
"direction": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "",
Description: "Will this rule affect inbound or outbound traffic (by default this is inbound)",
ValidateFunc: validation.StringInSlice([]string{
"inbound",
"outbound",
Expand All @@ -67,6 +67,7 @@ func resourceFirewallRule() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "A string that will be the displayed name/reference for this rule (optional)",
ValidateFunc: validation.StringIsNotEmpty,
},
},
Expand Down Expand Up @@ -109,8 +110,7 @@ func resourceFirewallRuleCreate(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] creating a new firewall rule for firewall %s", d.Get("firewall_id").(string))
firewallRule, err := apiClient.NewFirewallRule(config)
if err != nil {
fmt.Errorf("[ERR] failed to create a new firewall: %s", err)
return err
return fmt.Errorf("[ERR] failed to create a new firewall: %s", err)
}

d.SetId(firewallRule.ID)
Expand Down
12 changes: 12 additions & 0 deletions civo/resource_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package civo
import (
"fmt"
"github.com/civo/civogo"
"github.com/gorhill/cronexpr"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"log"
"time"
)

// Snapshot resource, with this we can create and manage all Snapshot
Expand Down Expand Up @@ -69,6 +71,10 @@ func resourceSnapshot() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"next_execution": {
Type: schema.TypeString,
Computed: true,
},
"requested_at": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -151,11 +157,16 @@ func resourceSnapshotRead(d *schema.ResourceData, m interface{}) error {
}

safeValue := false
nextExecution := time.Time{}

if resp.Safe == 1 {
safeValue = true
}

if resp.Cron != "" {
nextExecution = cronexpr.MustParse(resp.Cron).Next(time.Now().UTC())
}

d.Set("instance_id", resp.InstanceID)
d.Set("hostname", resp.Hostname)
d.Set("template_id", resp.Template)
Expand All @@ -165,6 +176,7 @@ func resourceSnapshotRead(d *schema.ResourceData, m interface{}) error {
d.Set("size_gb", resp.SizeGigabytes)
d.Set("state", resp.State)
d.Set("cron_timing", resp.Cron)
d.Set("next_execution", nextExecution.String())
d.Set("requested_at", resp.RequestedAt.UTC().String())
d.Set("completed_at", resp.CompletedAt.UTC().String())

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require (
github.com/aws/aws-sdk-go v1.29.22 // indirect
github.com/civo/civogo v0.2.5
github.com/fatih/color v1.9.0 // indirect
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75
github.com/hashicorp/go-getter v1.4.1 // indirect
github.com/hashicorp/go-hclog v0.12.1 // indirect
github.com/hashicorp/go-plugin v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75 h1:f0n1xnMSmBLzVfsMMvriDyA75NB/oBgILX2GcHXIQzY=
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75/go.mod h1:g2644b03hfBX9Ov0ZBDgXXens4rxSxmqFBbhvKv2yVA=
github.com/hashicorp/errwrap v0.0.0-20180715044906-d6c0cd880357/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down

0 comments on commit 52bf3e0

Please sign in to comment.