Skip to content

Commit

Permalink
[droplets]: add support for backup policy
Browse files Browse the repository at this point in the history
  • Loading branch information
loosla committed Oct 31, 2024
1 parent 549f5e8 commit 73fca2f
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 36 deletions.
85 changes: 80 additions & 5 deletions digitalocean/droplet/resource_droplet.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,35 @@ func ResourceDigitalOceanDroplet() *schema.Resource {
Default: false,
},

"backup_policy": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"plan": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"daily",
"weekly",
}, false),
},
"weekday": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT",
}, false),
},
"hour": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(0, 20),
},
},
},
},

"ipv6": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -255,11 +284,12 @@ func resourceDigitalOceanDropletCreate(ctx context.Context, d *schema.ResourceDa

// Build up our creation options
opts := &godo.DropletCreateRequest{
Image: godo.DropletCreateImage{},
Name: d.Get("name").(string),
Region: d.Get("region").(string),
Size: d.Get("size").(string),
Tags: tag.ExpandTags(d.Get("tags").(*schema.Set).List()),
Image: godo.DropletCreateImage{},
Name: d.Get("name").(string),
Region: d.Get("region").(string),
Size: d.Get("size").(string),
Tags: tag.ExpandTags(d.Get("tags").(*schema.Set).List()),
BackupPolicy: &godo.DropletBackupPolicyRequest{},
}

imageId, err := strconv.Atoi(image)
Expand Down Expand Up @@ -323,6 +353,51 @@ func resourceDigitalOceanDropletCreate(ctx context.Context, d *schema.ResourceDa
opts.SSHKeys = expandedSshKeys
}

// Get configured backup_policy
if policy, ok := d.GetOk("backup_policy"); ok {
objectList := policy.([]interface{})
for _, rawObject := range objectList {
objectMap, ok := rawObject.(map[string]interface{})
if !ok {
return diag.FromErr(err)
}

if planValue, exists := objectMap["plan"]; exists {
if plan, ok := planValue.(string); ok {
opts.BackupPolicy.Plan = plan
} else {
log.Println(">>> Error: plan is not a string")
}
} else {
log.Println(">>> Error: plan key does not exist")
}

if weekdayValue, exists := objectMap["weekday"]; exists {
if weekday, ok := weekdayValue.(string); ok {
fmt.Println("Weekday:", weekday)
opts.BackupPolicy.Weekday = weekday
} else {
log.Println(">>> Error: weekday is not a string")
}
} else {
log.Println(">>> Error: weekday key does not exist")
}

if hourValue, exists := objectMap["hour"]; exists {
if hour, ok := hourValue.(int); ok {
fmt.Println("Hour:", hour)
opts.BackupPolicy.Hour = hour
} else {
log.Println(">>> Error: hour is not a float64")
}
} else {
log.Println(">>> Error: hour key does not exist")
}

log.Println(">>> opts.BackupPolicy: ", opts.BackupPolicy)
}
}

log.Printf("[DEBUG] Droplet create configuration: %#v", opts)

droplet, _, err := client.Droplets.Create(context.Background(), opts)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/digitalocean/terraform-provider-digitalocean

require (
github.com/aws/aws-sdk-go v1.42.18
github.com/digitalocean/godo v1.128.1-0.20241025145008-2654a9d1e887
github.com/digitalocean/godo v1.128.1-0.20241031134431-3a7bfd290da0
github.com/hashicorp/awspolicyequivalence v1.5.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-uuid v1.0.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/digitalocean/godo v1.128.1-0.20241025145008-2654a9d1e887 h1:kdXNbMfHEDbQilcqllKkNrJ85ftyJSvSDpsQvzrhHbg=
github.com/digitalocean/godo v1.128.1-0.20241025145008-2654a9d1e887/go.mod h1:PU8JB6I1XYkQIdHFop8lLAY9ojp6M0XcU0TWaQSxbrc=
github.com/digitalocean/godo v1.128.1-0.20241031134431-3a7bfd290da0 h1:WlqeI3UYtmkg10jNJYr/JwIfnWMomgIycRmhYqqaLB8=
github.com/digitalocean/godo v1.128.1-0.20241031134431-3a7bfd290da0/go.mod h1:PU8JB6I1XYkQIdHFop8lLAY9ojp6M0XcU0TWaQSxbrc=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
Expand Down
172 changes: 145 additions & 27 deletions vendor/github.com/digitalocean/godo/droplets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ github.com/aws/aws-sdk-go/service/sts/stsiface
# github.com/davecgh/go-spew v1.1.1
## explicit
github.com/davecgh/go-spew/spew
# github.com/digitalocean/godo v1.128.1-0.20241025145008-2654a9d1e887
# github.com/digitalocean/godo v1.128.1-0.20241031134431-3a7bfd290da0
## explicit; go 1.22
github.com/digitalocean/godo
github.com/digitalocean/godo/metrics
Expand Down

0 comments on commit 73fca2f

Please sign in to comment.