diff --git a/pkg/models/cluster.go b/pkg/models/cluster.go index 2684159c..7da9e738 100644 --- a/pkg/models/cluster.go +++ b/pkg/models/cluster.go @@ -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' diff --git a/pkg/models/common/api/backup_schedule.go b/pkg/models/common/api/backup_schedule.go new file mode 100644 index 00000000..e27daf2a --- /dev/null +++ b/pkg/models/common/api/backup_schedule.go @@ -0,0 +1,6 @@ +package api + +type BackupSchedule struct { + StartDay *float64 `json:"startDay,omitempty"` + StartTime *string `json:"startTime,omitempty"` +} diff --git a/pkg/models/common/terraform/backup_schedule.go b/pkg/models/common/terraform/backup_schedule.go new file mode 100644 index 00000000..30722847 --- /dev/null +++ b/pkg/models/common/terraform/backup_schedule.go @@ -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"` +} diff --git a/pkg/models/pgd/api/data_group.go b/pkg/models/pgd/api/data_group.go index 8821c892..8d86f658 100644 --- a/pkg/models/pgd/api/data_group.go +++ b/pkg/models/pgd/api/data_group.go @@ -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"` @@ -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"` } diff --git a/pkg/models/pgd/terraform/data_group.go b/pkg/models/pgd/terraform/data_group.go index 4756d334..b8169b3b 100644 --- a/pkg/models/pgd/terraform/data_group.go +++ b/pkg/models/pgd/terraform/data_group.go @@ -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"` } diff --git a/pkg/provider/common.go b/pkg/provider/common.go index 212db12e..88936ab3 100644 --- a/pkg/provider/common.go +++ b/pkg/provider/common.go @@ -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{} @@ -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, + }, + }, +} diff --git a/pkg/provider/resource_analytics_cluster.go b/pkg/provider/resource_analytics_cluster.go index 51ec7a29..f6804e7a 100644 --- a/pkg/provider/resource_analytics_cluster.go +++ b/pkg/provider/resource_analytics_cluster.go @@ -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"` } @@ -301,6 +302,7 @@ func (r *analyticsClusterResource) Schema(ctx context.Context, req resource.Sche plan_modifier.CustomAssignTags(), }, }, + "backup_schedule": resourceBackupSchedule, }, } } @@ -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 } @@ -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 } diff --git a/pkg/provider/resource_cluster.go b/pkg/provider/resource_cluster.go index 83023578..e6fa516b 100644 --- a/pkg/provider/resource_cluster.go +++ b/pkg/provider/resource_cluster.go @@ -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"` } @@ -575,6 +576,7 @@ func (c *clusterResource) Schema(ctx context.Context, req resource.SchemaRequest Computed: true, PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, }, + "backup_schedule": resourceBackupSchedule, }, } } @@ -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 } @@ -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 } diff --git a/pkg/provider/resource_fareplica.go b/pkg/provider/resource_fareplica.go index 25012d6b..50ef2ac8 100644 --- a/pkg/provider/resource_fareplica.go +++ b/pkg/provider/resource_fareplica.go @@ -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"` } @@ -422,6 +423,7 @@ func (r *FAReplicaResource) Schema(ctx context.Context, req resource.SchemaReque plan_modifier.CustomAssignTags(), }, }, + "backup_schedule": resourceBackupSchedule, }, } } @@ -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 } @@ -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 } diff --git a/pkg/provider/resource_pgd.go b/pkg/provider/resource_pgd.go index cc1792c7..10886d7b 100644 --- a/pkg/provider/resource_pgd.go +++ b/pkg/provider/resource_pgd.go @@ -417,6 +417,7 @@ func PgdSchema(ctx context.Context) schema.Schema { Optional: true, Computed: true, }, + "backup_schedule": resourceBackupSchedule, }, }, }, @@ -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) } @@ -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) } @@ -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, @@ -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)