Skip to content

Commit

Permalink
feat: backup schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
wai-wong-edb committed Nov 12, 2024
1 parent c609e0f commit b5078f3
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 27 deletions.
1 change: 1 addition & 0 deletions pkg/models/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ type Cluster struct {
EncryptionKeyIdReq *string `json:"keyId,omitempty"`
EncryptionKeyResp *EncryptionKey `json:"encryptionKey,omitempty"`
PgIdentity *string `json:"pgIdentity,omitempty"`
BackupSchedule *commonApi.BackupSchedule `json:"backupSchedule,omitempty"`
}

// IsHealthy checks to see if the cluster has the right condition 'biganimal.com/deployed'
Expand Down
6 changes: 6 additions & 0 deletions pkg/models/common/api/backup_schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package api

type BackupSchedule struct {
StartDay *float64 `json:"startDay,omitempty"`
StartTime *string `json:"startTime,omitempty"`
}
8 changes: 8 additions & 0 deletions pkg/models/common/terraform/backup_schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package terraform

import "github.com/hashicorp/terraform-plugin-framework/types"

type BackupSchedule struct {
StartDay types.String `tfsdk:"start_day"`
StartTime types.String `tfsdk:"start_time"`
}
6 changes: 5 additions & 1 deletion pkg/models/pgd/api/data_group.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package api

import "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models"
import (
"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models"
commonApi "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models/common/api"
)

type DataGroup struct {
GroupId *string `json:"groupId,omitempty"`
Expand Down Expand Up @@ -30,4 +33,5 @@ type DataGroup struct {
PeAllowedPrincipalIds *[]string `json:"peAllowedPrincipalIds,omitempty"`
RoConnectionUri *string `json:"roConnectionUri,omitempty"`
ReadOnlyConnections *bool `json:"readOnlyConnections,omitempty"`
BackupSchedule *commonApi.BackupSchedule `json:"backupSchedule,omitempty"`
}
54 changes: 28 additions & 26 deletions pkg/models/pgd/terraform/data_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,37 @@ package terraform

import (
"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models"
commonTerraform "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models/common/terraform"
"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models/pgd/api"
"github.com/hashicorp/terraform-plugin-framework/types"
)

type DataGroup struct {
GroupId types.String `tfsdk:"group_id"`
AllowedIpRanges types.Set `tfsdk:"allowed_ip_ranges"`
BackupRetentionPeriod *string `tfsdk:"backup_retention_period"`
ClusterArchitecture *ClusterArchitecture `tfsdk:"cluster_architecture"`
ClusterName types.String `tfsdk:"cluster_name"`
ClusterType types.String `tfsdk:"cluster_type"`
Connection types.String `tfsdk:"connection_uri"`
CreatedAt types.String `tfsdk:"created_at"`
CspAuth *bool `tfsdk:"csp_auth"`
InstanceType *api.InstanceType `tfsdk:"instance_type"`
LogsUrl types.String `tfsdk:"logs_url"`
MetricsUrl types.String `tfsdk:"metrics_url"`
PgConfig *[]models.KeyValue `tfsdk:"pg_config"`
PgType *api.PgType `tfsdk:"pg_type"`
PgVersion *api.PgVersion `tfsdk:"pg_version"`
Phase types.String `tfsdk:"phase"`
PrivateNetworking *bool `tfsdk:"private_networking"`
Provider *api.CloudProvider `tfsdk:"cloud_provider"`
Region *api.Region `tfsdk:"region"`
ResizingPvc types.Set `tfsdk:"resizing_pvc"`
Storage *Storage `tfsdk:"storage"`
MaintenanceWindow *models.MaintenanceWindow `tfsdk:"maintenance_window"`
ServiceAccountIds types.Set `tfsdk:"service_account_ids"`
PeAllowedPrincipalIds types.Set `tfsdk:"pe_allowed_principal_ids"`
RoConnectionUri types.String `tfsdk:"ro_connection_uri"`
ReadOnlyConnections *bool `tfsdk:"read_only_connections"`
GroupId types.String `tfsdk:"group_id"`
AllowedIpRanges types.Set `tfsdk:"allowed_ip_ranges"`
BackupRetentionPeriod *string `tfsdk:"backup_retention_period"`
ClusterArchitecture *ClusterArchitecture `tfsdk:"cluster_architecture"`
ClusterName types.String `tfsdk:"cluster_name"`
ClusterType types.String `tfsdk:"cluster_type"`
Connection types.String `tfsdk:"connection_uri"`
CreatedAt types.String `tfsdk:"created_at"`
CspAuth *bool `tfsdk:"csp_auth"`
InstanceType *api.InstanceType `tfsdk:"instance_type"`
LogsUrl types.String `tfsdk:"logs_url"`
MetricsUrl types.String `tfsdk:"metrics_url"`
PgConfig *[]models.KeyValue `tfsdk:"pg_config"`
PgType *api.PgType `tfsdk:"pg_type"`
PgVersion *api.PgVersion `tfsdk:"pg_version"`
Phase types.String `tfsdk:"phase"`
PrivateNetworking *bool `tfsdk:"private_networking"`
Provider *api.CloudProvider `tfsdk:"cloud_provider"`
Region *api.Region `tfsdk:"region"`
ResizingPvc types.Set `tfsdk:"resizing_pvc"`
Storage *Storage `tfsdk:"storage"`
MaintenanceWindow *models.MaintenanceWindow `tfsdk:"maintenance_window"`
ServiceAccountIds types.Set `tfsdk:"service_account_ids"`
PeAllowedPrincipalIds types.Set `tfsdk:"pe_allowed_principal_ids"`
RoConnectionUri types.String `tfsdk:"ro_connection_uri"`
ReadOnlyConnections *bool `tfsdk:"read_only_connections"`
BackupSchedule *commonTerraform.BackupSchedule `tfsdk:"backup_schedule"`
}
36 changes: 36 additions & 0 deletions pkg/provider/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,31 @@ package provider
import (
commonApi "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models/common/api"
commonTerraform "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models/common/terraform"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

var WeekdaysNumber = map[string]float64{
"monday": 1.0,
"tuesday": 2.0,
"wednesday": 3.0,
"thursday": 4.0,
"friday": 5.0,
"saturday": 6.0,
"sunday": 0.0,
}

var WeekdaysName = map[float64]string{
1.0: "Monday",
2.0: "Tuesday",
3.0: "Wednesday",
4.0: "Thursday",
5.0: "Friday",
6.0: "Saturday",
0.0: "Sunday",
}

// build tag assign terraform resource as, using api response as input
func buildTFRsrcAssignTagsAs(tfRsrcTagsOut *[]commonTerraform.Tag, apiRespTags []commonApi.Tag) {
*tfRsrcTagsOut = []commonTerraform.Tag{}
Expand All @@ -31,3 +52,18 @@ func buildAPIReqAssignTags(tfRsrcTags []commonTerraform.Tag) []commonApi.Tag {
}
return tags
}

var resourceBackupSchedule = schema.SingleNestedAttribute{
Description: "Backup schedule.",
Optional: true,
Attributes: map[string]schema.Attribute{
"start_day": schema.StringAttribute{
Description: "Backup schedule start day.",
Required: true,
},
"start_time": schema.StringAttribute{
Description: "Backup schedule start time.",
Required: true,
},
},
}
16 changes: 16 additions & 0 deletions pkg/provider/resource_analytics_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type analyticsClusterResourceModel struct {
PeAllowedPrincipalIds types.Set `tfsdk:"pe_allowed_principal_ids"`
Pause types.Bool `tfsdk:"pause"`
Tags []commonTerraform.Tag `tfsdk:"tags"`
BackupSchedule *commonTerraform.BackupSchedule `tfsdk:"backup_schedule"`

Timeouts timeouts.Value `tfsdk:"timeouts"`
}
Expand Down Expand Up @@ -301,6 +302,7 @@ func (r *analyticsClusterResource) Schema(ctx context.Context, req resource.Sche
plan_modifier.CustomAssignTags(),
},
},
"backup_schedule": resourceBackupSchedule,
},
}
}
Expand Down Expand Up @@ -478,6 +480,13 @@ func generateAnalyticsClusterModelCreate(ctx context.Context, client *api.Cluste

cluster.Tags = buildAPIReqAssignTags(clusterResource.Tags)

if clusterResource.BackupSchedule != nil {
cluster.BackupSchedule = &commonApi.BackupSchedule{
StartDay: utils.ToPointer(WeekdaysNumber[clusterResource.BackupSchedule.StartDay.ValueString()]),
StartTime: clusterResource.BackupSchedule.StartTime.ValueStringPointer(),
}
}

return cluster, nil
}

Expand Down Expand Up @@ -546,6 +555,13 @@ func readAnalyticsCluster(ctx context.Context, client *api.ClusterClient, tfClus

buildTFRsrcAssignTagsAs(&tfClusterResource.Tags, responseCluster.Tags)

if responseCluster.BackupSchedule != nil {
tfClusterResource.BackupSchedule = &commonTerraform.BackupSchedule{
StartDay: types.StringValue(WeekdaysName[*responseCluster.BackupSchedule.StartDay]),
StartTime: types.StringPointerValue(responseCluster.BackupSchedule.StartTime),
}
}

return nil
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/provider/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type ClusterResourceModel struct {
VolumeSnapshot types.Bool `tfsdk:"volume_snapshot_backup"`
Tags []commonTerraform.Tag `tfsdk:"tags"`
ServiceName types.String `tfsdk:"service_name"`
BackupSchedule *commonTerraform.BackupSchedule `tfsdk:"backup_schedule"`

Timeouts timeouts.Value `tfsdk:"timeouts"`
}
Expand Down Expand Up @@ -575,6 +576,7 @@ func (c *clusterResource) Schema(ctx context.Context, req resource.SchemaRequest
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"backup_schedule": resourceBackupSchedule,
},
}
}
Expand Down Expand Up @@ -979,6 +981,13 @@ func readCluster(ctx context.Context, client *api.ClusterClient, tfClusterResour
tfClusterResource.TransparentDataEncryption.Status = types.StringValue(responseCluster.EncryptionKeyResp.Status)
}

if responseCluster.BackupSchedule != nil {
tfClusterResource.BackupSchedule = &commonTerraform.BackupSchedule{
StartDay: types.StringValue(WeekdaysName[*responseCluster.BackupSchedule.StartDay]),
StartTime: types.StringPointerValue(responseCluster.BackupSchedule.StartTime),
}
}

return nil
}

Expand Down Expand Up @@ -1188,6 +1197,13 @@ func (c *clusterResource) generateGenericClusterModel(ctx context.Context, clust
}
}

if clusterResource.BackupSchedule != nil {
cluster.BackupSchedule = &commonApi.BackupSchedule{
StartDay: utils.ToPointer(WeekdaysNumber[clusterResource.BackupSchedule.StartDay.ValueString()]),
StartTime: clusterResource.BackupSchedule.StartTime.ValueStringPointer(),
}
}

return cluster, nil
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/provider/resource_fareplica.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type FAReplicaResourceModel struct {
TransparentDataEncryptionAction types.String `tfsdk:"transparent_data_encryption_action"`
VolumeSnapshot types.Bool `tfsdk:"volume_snapshot_backup"`
Tags []commonTerraform.Tag `tfsdk:"tags"`
BackupSchedule *commonTerraform.BackupSchedule `tfsdk:"backup_schedule"`

Timeouts timeouts.Value `tfsdk:"timeouts"`
}
Expand Down Expand Up @@ -422,6 +423,7 @@ func (r *FAReplicaResource) Schema(ctx context.Context, req resource.SchemaReque
plan_modifier.CustomAssignTags(),
},
},
"backup_schedule": resourceBackupSchedule,
},
}
}
Expand Down Expand Up @@ -701,6 +703,13 @@ func readFAReplica(ctx context.Context, client *api.ClusterClient, fAReplicaReso
})
}

if responseCluster.BackupSchedule != nil {
fAReplicaResourceModel.BackupSchedule = &commonTerraform.BackupSchedule{
StartDay: types.StringValue(WeekdaysName[*responseCluster.BackupSchedule.StartDay]),
StartTime: types.StringPointerValue(responseCluster.BackupSchedule.StartTime),
}
}

return nil
}

Expand Down Expand Up @@ -812,6 +821,13 @@ func (r *FAReplicaResource) generateGenericFAReplicaModel(ctx context.Context, f
}
cluster.Tags = tags

if fAReplicaResourceModel.BackupSchedule != nil {
cluster.BackupSchedule = &commonApi.BackupSchedule{
StartDay: utils.ToPointer(WeekdaysNumber[fAReplicaResourceModel.BackupSchedule.StartDay.ValueString()]),
StartTime: fAReplicaResourceModel.BackupSchedule.StartTime.ValueStringPointer(),
}
}

return cluster, nil
}

Expand Down
25 changes: 25 additions & 0 deletions pkg/provider/resource_pgd.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ func PgdSchema(ctx context.Context) schema.Schema {
Optional: true,
Computed: true,
},
"backup_schedule": resourceBackupSchedule,
},
},
},
Expand Down Expand Up @@ -705,6 +706,14 @@ func (p pgdResource) Create(ctx context.Context, req resource.CreateRequest, res
PeAllowedPrincipalIds: principalIds,
ReadOnlyConnections: v.ReadOnlyConnections,
}

if v.BackupSchedule != nil {
apiDGModel.BackupSchedule = &commonApi.BackupSchedule{
StartDay: utils.ToPointer(WeekdaysNumber[v.BackupSchedule.StartDay.ValueString()]),
StartTime: v.BackupSchedule.StartTime.ValueStringPointer(),
}
}

*clusterReqBody.Groups = append(*clusterReqBody.Groups, apiDGModel)
}

Expand Down Expand Up @@ -993,6 +1002,13 @@ func (p pgdResource) Update(ctx context.Context, req resource.UpdateRequest, res
reqDg.PeAllowedPrincipalIds = principalIds
}

if v.BackupSchedule != nil {
reqDg.BackupSchedule = &commonApi.BackupSchedule{
StartDay: utils.ToPointer(WeekdaysNumber[v.BackupSchedule.StartDay.ValueString()]),
StartTime: v.BackupSchedule.StartTime.ValueStringPointer(),
}
}

*clusterReqBody.Groups = append(*clusterReqBody.Groups, reqDg)
}

Expand Down Expand Up @@ -1394,6 +1410,14 @@ func buildTFGroupsAs(ctx context.Context, diags *diag.Diagnostics, state tfsdk.S
allwdIpRngsSet = types.SetValueMust(allwdIpRngsElemType, allowedIpRanges)
}

var backupSchedule *commonTerraform.BackupSchedule
if apiRespDgModel.BackupSchedule != nil {
backupSchedule = &commonTerraform.BackupSchedule{
StartDay: types.StringValue(WeekdaysName[*apiRespDgModel.BackupSchedule.StartDay]),
StartTime: types.StringPointerValue(apiRespDgModel.BackupSchedule.StartTime),
}
}

tfDGModel := terraform.DataGroup{
GroupId: types.StringPointerValue(apiRespDgModel.GroupId),
AllowedIpRanges: allwdIpRngsSet,
Expand Down Expand Up @@ -1421,6 +1445,7 @@ func buildTFGroupsAs(ctx context.Context, diags *diag.Diagnostics, state tfsdk.S
PeAllowedPrincipalIds: types.SetValueMust(types.StringType, principalIds),
RoConnectionUri: types.StringPointerValue(apiRespDgModel.RoConnectionUri),
ReadOnlyConnections: apiRespDgModel.ReadOnlyConnections,
BackupSchedule: backupSchedule,
}

outPgdTFResource.DataGroups = append(outPgdTFResource.DataGroups, tfDGModel)
Expand Down

0 comments on commit b5078f3

Please sign in to comment.