From 56b371c055ce90013b6e739589c63a072d6e1c06 Mon Sep 17 00:00:00 2001 From: safboukhari Date: Tue, 23 May 2023 11:57:06 +0200 Subject: [PATCH] feat(DocDB): implement restorefrom for docdb --- apis/docdb/v1alpha1/custom_types.go | 167 ++++++++++++++ apis/docdb/v1alpha1/zz_generated.deepcopy.go | 218 ++++++++++++++++++ .../docdb.aws.crossplane.io_dbclusters.yaml | 106 +++++++++ .../docdb.aws.crossplane.io_dbinstances.yaml | 101 ++++++++ pkg/controller/docdb/dbcluster/setup.go | 116 +++++++++- 5 files changed, 704 insertions(+), 4 deletions(-) diff --git a/apis/docdb/v1alpha1/custom_types.go b/apis/docdb/v1alpha1/custom_types.go index aa08e99360..ecaaef93a5 100644 --- a/apis/docdb/v1alpha1/custom_types.go +++ b/apis/docdb/v1alpha1/custom_types.go @@ -18,6 +18,7 @@ package v1alpha1 import ( xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // RDS instance states. @@ -56,6 +57,23 @@ type CustomDBInstanceParameters struct { DBClusterIdentifier *string `json:"dbClusterIdentifier,omitempty"` DBClusterIdentifierRef *xpv1.Reference `json:"dbClusterIdentifierRef,omitempty"` DBClusterIdentifierSelector *xpv1.Selector `json:"dbClusterIdentifierSelector,omitempty"` + + // RestoreFrom specifies the details of the backup to restore when creating a new DBInstance. + // +optional + RestoreFrom *RestoreDBInstanceBackupConfiguration `json:"restoreFrom,omitempty"` + + // A list of Amazon EC2 VPC security groups to authorize on this DB instance. + // This change is asynchronously applied as soon as possible. + // + // This setting doesn't apply to RDS Custom. + // + // Amazon Aurora + // Not applicable. The associated list of EC2 VPC security groups is managed + // by the DB cluster. For more information, see ModifyDBCluster. + // + // Constraints: + // * If supplied, must match existing VpcSecurityGroupIds. + VPCSecurityGroupIDs []string `json:"vpcSecurityGroupIDs,omitempty"` } // CustomDBSubnetGroupParameters for DBSubnetGroupParameters @@ -156,6 +174,155 @@ type CustomDBClusterParameters struct { VPCSecurityGroupIDsRefs []xpv1.Reference `json:"vpcSecurityGroupIDsRefs,omitempty"` // TODO(haarchri): when resource is bumped to beta we will convert this field to vpcSecurityGroupIdSelector VPCSecurityGroupIDsSelector *xpv1.Selector `json:"vpcSecurityGroupIDsSelector,omitempty"` + + // RestoreFrom specifies the details of the backup to restore when creating a new DBCluster. + // +optional + RestoreFrom *RestoreDBClusterBackupConfiguration `json:"restoreFrom,omitempty"` +} + +// S3RestoreBackupConfiguration defines the details of the S3 backup to restore from. +type S3RestoreBackupConfiguration struct { + // BucketName is the name of the S3 bucket containing the backup to restore. + BucketName *string `json:"bucketName"` + + // IngestionRoleARN is the IAM role RDS can assume that will allow it to access the contents of the S3 bucket. + IngestionRoleARN *string `json:"ingestionRoleARN"` + + // Prefix is the path prefix of the S3 bucket within which the backup to restore is located. + // +optional + Prefix *string `json:"prefix,omitempty"` + + // SourceEngine is the engine used to create the backup. + // Must be "mysql". + SourceEngine *string `json:"sourceEngine"` + + // SourceEngineVersion is the version of the engine used to create the backup. + // Example: "5.7.30" + SourceEngineVersion *string `json:"sourceEngineVersion"` +} + +// SnapshotRestoreBackupConfiguration defines the details of the snapshot to restore from. +type SnapshotRestoreBackupConfiguration struct { + // SnapshotIdentifier is the identifier of the snapshot to restore. + SnapshotIdentifier *string `json:"snapshotIdentifier"` +} + +// PointInTimeRestoreBackupConfiguration defines the details of the time to restore from +type PointInTimeRestoreBackupConfiguration struct { + // RestoreTime is the date and time (UTC) to restore from. + // Must be before the latest restorable time for the DB instance. + // Can't be specified if the useLatestRestorableTime parameter is enabled. + // Example: 2011-09-07T23:45:00Z + // +optional + RestoreTime *metav1.Time `json:"restoreTime"` + + // UseLatestRestorableTime indicates that the DB instance is restored from the latest backup + // Can't be specified if the restoreTime parameter is provided. + // +optional + UseLatestRestorableTime bool `json:"useLatestRestorableTime"` + + // SourceDBInstanceAutomatedBackupsArn specifies the Amazon Resource Name (ARN) of the replicated automated backups + // from which to restore. Example: arn:aws:rds:useast-1:123456789012:auto-backup:ab-L2IJCEXJP7XQ7HOJ4SIEXAMPLE + // +optional + SourceDBInstanceAutomatedBackupsArn *string `json:"sourceDBInstanceAutomatedBackupsArn"` + + // SourceDBInstanceIdentifier specifies the identifier of the source DB instance from which to restore. Constraints: + // Must match the identifier of an existing DB instance. + // +optional + SourceDBInstanceIdentifier *string `json:"sourceDBInstanceIdentifier"` + + // SourceDbiResourceID specifies the resource ID of the source DB instance from which to restore. + // +optional + SourceDbiResourceID *string `json:"sourceDbiResourceId"` +} + +// PointInTimeRestoreDBClusterBackupConfiguration defines the details of the time to restore from +type PointInTimeRestoreDBClusterBackupConfiguration struct { + // RestoreTime is the date and time (UTC) to restore from. + // Must be before the latest restorable time for the DB instance. + // Can't be specified if the useLatestRestorableTime parameter is enabled. + // Example: 2011-09-07T23:45:00Z + // +optional + RestoreTime *metav1.Time `json:"restoreTime"` + + // UseLatestRestorableTime indicates that the DB instance is restored from the latest backup + // Can't be specified if the restoreTime parameter is provided. + // +optional + UseLatestRestorableTime bool `json:"useLatestRestorableTime"` + + // SourceDBInstanceAutomatedBackupsArn specifies the Amazon Resource Name (ARN) of the replicated automated backups + // from which to restore. Example: arn:aws:rds:useast-1:123456789012:auto-backup:ab-L2IJCEXJP7XQ7HOJ4SIEXAMPLE + // +optional + SourceDBInstanceAutomatedBackupsArn *string `json:"sourceDBInstanceAutomatedBackupsArn"` + + // SourceDBClusterIdentifier specifies the identifier of the source DB cluster from which to restore. Constraints: + // Must match the identifier of an existing DB instance. + // +optional + SourceDBClusterIdentifier *string `json:"sourceDBClusterIdentifier"` + + // SourceDbiResourceID specifies the resource ID of the source DB instance from which to restore. + // +optional + SourceDbiResourceID *string `json:"sourceDbiResourceId"` + + // The type of restore to be performed. You can specify one of the following + // values: + // + // * full-copy - The new DB cluster is restored as a full copy of the source + // DB cluster. + // + // * copy-on-write - The new DB cluster is restored as a clone of the source + // DB cluster. + // + // Constraints: You can't specify copy-on-write if the engine version of the + // source DB cluster is earlier than 1.11. + // + // If you don't specify a RestoreType value, then the new DB cluster is restored + // as a full copy of the source DB cluster. + // + // Valid for: Aurora DB clusters and Multi-AZ DB clusters + // +optional + // +kubebuilder:validation:Enum=full-copy;copy-on-write + RestoreType *string `json:"restoreType"` +} + +// RestoreDBInstanceBackupConfiguration defines the backup to restore a new DBCluster from. +type RestoreDBInstanceBackupConfiguration struct { + // S3 specifies the details of the S3 backup to restore from. + // +optional + S3 *S3RestoreBackupConfiguration `json:"s3,omitempty"` + + // Snapshot specifies the details of the snapshot to restore from. + // +optional + Snapshot *SnapshotRestoreBackupConfiguration `json:"snapshot,omitempty"` + + // PointInTime specifies the details of the point in time restore. + // +optional + PointInTime *PointInTimeRestoreBackupConfiguration `json:"pointInTime,omitempty"` + + // Source is the type of the backup to restore when creating a new DBCluster or DBInstance. + // S3, Snapshot and PointInTime are supported. + // +kubebuilder:validation:Enum=S3;Snapshot;PointInTime + Source *string `json:"source"` +} + +// RestoreDBClusterBackupConfiguration defines the backup to restore a new DBCluster from. +type RestoreDBClusterBackupConfiguration struct { + // S3 specifies the details of the S3 backup to restore from. + // +optional + S3 *S3RestoreBackupConfiguration `json:"s3,omitempty"` + + // Snapshot specifies the details of the snapshot to restore from. + // +optional + Snapshot *SnapshotRestoreBackupConfiguration `json:"snapshot,omitempty"` + + // PointInTime specifies the details of the point in time restore. + // +optional + PointInTime *PointInTimeRestoreDBClusterBackupConfiguration `json:"pointInTime,omitempty"` + + // Source is the type of the backup to restore when creating a new DBCluster or DBInstance. + // S3, Snapshot and PointInTime are supported. + // +kubebuilder:validation:Enum=S3;Snapshot;PointInTime + Source *string `json:"source"` } // CustomParameter are custom parameters for the Parameter diff --git a/apis/docdb/v1alpha1/zz_generated.deepcopy.go b/apis/docdb/v1alpha1/zz_generated.deepcopy.go index 2b9d61bed3..1a728f61c0 100644 --- a/apis/docdb/v1alpha1/zz_generated.deepcopy.go +++ b/apis/docdb/v1alpha1/zz_generated.deepcopy.go @@ -217,6 +217,11 @@ func (in *CustomDBClusterParameters) DeepCopyInto(out *CustomDBClusterParameters *out = new(v1.Selector) (*in).DeepCopyInto(*out) } + if in.RestoreFrom != nil { + in, out := &in.RestoreFrom, &out.RestoreFrom + *out = new(RestoreDBClusterBackupConfiguration) + (*in).DeepCopyInto(*out) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CustomDBClusterParameters. @@ -257,6 +262,16 @@ func (in *CustomDBInstanceParameters) DeepCopyInto(out *CustomDBInstanceParamete *out = new(v1.Selector) (*in).DeepCopyInto(*out) } + if in.RestoreFrom != nil { + in, out := &in.RestoreFrom, &out.RestoreFrom + *out = new(RestoreDBInstanceBackupConfiguration) + (*in).DeepCopyInto(*out) + } + if in.VPCSecurityGroupIDs != nil { + in, out := &in.VPCSecurityGroupIDs, &out.VPCSecurityGroupIDs + *out = make([]string, len(*in)) + copy(*out, *in) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CustomDBInstanceParameters. @@ -2654,6 +2669,79 @@ func (in *PendingModifiedValues) DeepCopy() *PendingModifiedValues { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *PointInTimeRestoreBackupConfiguration) DeepCopyInto(out *PointInTimeRestoreBackupConfiguration) { + *out = *in + if in.RestoreTime != nil { + in, out := &in.RestoreTime, &out.RestoreTime + *out = (*in).DeepCopy() + } + if in.SourceDBInstanceAutomatedBackupsArn != nil { + in, out := &in.SourceDBInstanceAutomatedBackupsArn, &out.SourceDBInstanceAutomatedBackupsArn + *out = new(string) + **out = **in + } + if in.SourceDBInstanceIdentifier != nil { + in, out := &in.SourceDBInstanceIdentifier, &out.SourceDBInstanceIdentifier + *out = new(string) + **out = **in + } + if in.SourceDbiResourceID != nil { + in, out := &in.SourceDbiResourceID, &out.SourceDbiResourceID + *out = new(string) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PointInTimeRestoreBackupConfiguration. +func (in *PointInTimeRestoreBackupConfiguration) DeepCopy() *PointInTimeRestoreBackupConfiguration { + if in == nil { + return nil + } + out := new(PointInTimeRestoreBackupConfiguration) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *PointInTimeRestoreDBClusterBackupConfiguration) DeepCopyInto(out *PointInTimeRestoreDBClusterBackupConfiguration) { + *out = *in + if in.RestoreTime != nil { + in, out := &in.RestoreTime, &out.RestoreTime + *out = (*in).DeepCopy() + } + if in.SourceDBInstanceAutomatedBackupsArn != nil { + in, out := &in.SourceDBInstanceAutomatedBackupsArn, &out.SourceDBInstanceAutomatedBackupsArn + *out = new(string) + **out = **in + } + if in.SourceDBClusterIdentifier != nil { + in, out := &in.SourceDBClusterIdentifier, &out.SourceDBClusterIdentifier + *out = new(string) + **out = **in + } + if in.SourceDbiResourceID != nil { + in, out := &in.SourceDbiResourceID, &out.SourceDbiResourceID + *out = new(string) + **out = **in + } + if in.RestoreType != nil { + in, out := &in.RestoreType, &out.RestoreType + *out = new(string) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PointInTimeRestoreDBClusterBackupConfiguration. +func (in *PointInTimeRestoreDBClusterBackupConfiguration) DeepCopy() *PointInTimeRestoreDBClusterBackupConfiguration { + if in == nil { + return nil + } + out := new(PointInTimeRestoreDBClusterBackupConfiguration) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ResourcePendingMaintenanceActions) DeepCopyInto(out *ResourcePendingMaintenanceActions) { *out = *in @@ -2674,6 +2762,136 @@ func (in *ResourcePendingMaintenanceActions) DeepCopy() *ResourcePendingMaintena return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *RestoreDBClusterBackupConfiguration) DeepCopyInto(out *RestoreDBClusterBackupConfiguration) { + *out = *in + if in.S3 != nil { + in, out := &in.S3, &out.S3 + *out = new(S3RestoreBackupConfiguration) + (*in).DeepCopyInto(*out) + } + if in.Snapshot != nil { + in, out := &in.Snapshot, &out.Snapshot + *out = new(SnapshotRestoreBackupConfiguration) + (*in).DeepCopyInto(*out) + } + if in.PointInTime != nil { + in, out := &in.PointInTime, &out.PointInTime + *out = new(PointInTimeRestoreDBClusterBackupConfiguration) + (*in).DeepCopyInto(*out) + } + if in.Source != nil { + in, out := &in.Source, &out.Source + *out = new(string) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RestoreDBClusterBackupConfiguration. +func (in *RestoreDBClusterBackupConfiguration) DeepCopy() *RestoreDBClusterBackupConfiguration { + if in == nil { + return nil + } + out := new(RestoreDBClusterBackupConfiguration) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *RestoreDBInstanceBackupConfiguration) DeepCopyInto(out *RestoreDBInstanceBackupConfiguration) { + *out = *in + if in.S3 != nil { + in, out := &in.S3, &out.S3 + *out = new(S3RestoreBackupConfiguration) + (*in).DeepCopyInto(*out) + } + if in.Snapshot != nil { + in, out := &in.Snapshot, &out.Snapshot + *out = new(SnapshotRestoreBackupConfiguration) + (*in).DeepCopyInto(*out) + } + if in.PointInTime != nil { + in, out := &in.PointInTime, &out.PointInTime + *out = new(PointInTimeRestoreBackupConfiguration) + (*in).DeepCopyInto(*out) + } + if in.Source != nil { + in, out := &in.Source, &out.Source + *out = new(string) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RestoreDBInstanceBackupConfiguration. +func (in *RestoreDBInstanceBackupConfiguration) DeepCopy() *RestoreDBInstanceBackupConfiguration { + if in == nil { + return nil + } + out := new(RestoreDBInstanceBackupConfiguration) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *S3RestoreBackupConfiguration) DeepCopyInto(out *S3RestoreBackupConfiguration) { + *out = *in + if in.BucketName != nil { + in, out := &in.BucketName, &out.BucketName + *out = new(string) + **out = **in + } + if in.IngestionRoleARN != nil { + in, out := &in.IngestionRoleARN, &out.IngestionRoleARN + *out = new(string) + **out = **in + } + if in.Prefix != nil { + in, out := &in.Prefix, &out.Prefix + *out = new(string) + **out = **in + } + if in.SourceEngine != nil { + in, out := &in.SourceEngine, &out.SourceEngine + *out = new(string) + **out = **in + } + if in.SourceEngineVersion != nil { + in, out := &in.SourceEngineVersion, &out.SourceEngineVersion + *out = new(string) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new S3RestoreBackupConfiguration. +func (in *S3RestoreBackupConfiguration) DeepCopy() *S3RestoreBackupConfiguration { + if in == nil { + return nil + } + out := new(S3RestoreBackupConfiguration) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *SnapshotRestoreBackupConfiguration) DeepCopyInto(out *SnapshotRestoreBackupConfiguration) { + *out = *in + if in.SnapshotIdentifier != nil { + in, out := &in.SnapshotIdentifier, &out.SnapshotIdentifier + *out = new(string) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SnapshotRestoreBackupConfiguration. +func (in *SnapshotRestoreBackupConfiguration) DeepCopy() *SnapshotRestoreBackupConfiguration { + if in == nil { + return nil + } + out := new(SnapshotRestoreBackupConfiguration) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Subnet) DeepCopyInto(out *Subnet) { *out = *in diff --git a/package/crds/docdb.aws.crossplane.io_dbclusters.yaml b/package/crds/docdb.aws.crossplane.io_dbclusters.yaml index 651db64128..62ac4732ce 100644 --- a/package/crds/docdb.aws.crossplane.io_dbclusters.yaml +++ b/package/crds/docdb.aws.crossplane.io_dbclusters.yaml @@ -437,6 +437,112 @@ spec: region: description: Region is which region the DBCluster will be created. type: string + restoreFrom: + description: RestoreFrom specifies the details of the backup to + restore when creating a new DBCluster. + properties: + pointInTime: + description: PointInTime specifies the details of the point + in time restore. + properties: + restoreTime: + description: 'RestoreTime is the date and time (UTC) to + restore from. Must be before the latest restorable time + for the DB instance. Can''t be specified if the useLatestRestorableTime + parameter is enabled. Example: 2011-09-07T23:45:00Z' + format: date-time + type: string + restoreType: + description: "The type of restore to be performed. You + can specify one of the following values: \n * full-copy + - The new DB cluster is restored as a full copy of the + source DB cluster. \n * copy-on-write - The new DB cluster + is restored as a clone of the source DB cluster. \n + Constraints: You can't specify copy-on-write if the + engine version of the source DB cluster is earlier than + 1.11. \n If you don't specify a RestoreType value, then + the new DB cluster is restored as a full copy of the + source DB cluster. \n Valid for: Aurora DB clusters + and Multi-AZ DB clusters" + enum: + - full-copy + - copy-on-write + type: string + sourceDBClusterIdentifier: + description: 'SourceDBClusterIdentifier specifies the + identifier of the source DB cluster from which to restore. + Constraints: Must match the identifier of an existing + DB instance.' + type: string + sourceDBInstanceAutomatedBackupsArn: + description: 'SourceDBInstanceAutomatedBackupsArn specifies + the Amazon Resource Name (ARN) of the replicated automated + backups from which to restore. Example: arn:aws:rds:useast-1:123456789012:auto-backup:ab-L2IJCEXJP7XQ7HOJ4SIEXAMPLE' + type: string + sourceDbiResourceId: + description: SourceDbiResourceID specifies the resource + ID of the source DB instance from which to restore. + type: string + useLatestRestorableTime: + description: UseLatestRestorableTime indicates that the + DB instance is restored from the latest backup Can't + be specified if the restoreTime parameter is provided. + type: boolean + type: object + s3: + description: S3 specifies the details of the S3 backup to + restore from. + properties: + bucketName: + description: BucketName is the name of the S3 bucket containing + the backup to restore. + type: string + ingestionRoleARN: + description: IngestionRoleARN is the IAM role RDS can + assume that will allow it to access the contents of + the S3 bucket. + type: string + prefix: + description: Prefix is the path prefix of the S3 bucket + within which the backup to restore is located. + type: string + sourceEngine: + description: SourceEngine is the engine used to create + the backup. Must be "mysql". + type: string + sourceEngineVersion: + description: 'SourceEngineVersion is the version of the + engine used to create the backup. Example: "5.7.30"' + type: string + required: + - bucketName + - ingestionRoleARN + - sourceEngine + - sourceEngineVersion + type: object + snapshot: + description: Snapshot specifies the details of the snapshot + to restore from. + properties: + snapshotIdentifier: + description: SnapshotIdentifier is the identifier of the + snapshot to restore. + type: string + required: + - snapshotIdentifier + type: object + source: + description: Source is the type of the backup to restore when + creating a new DBCluster or DBInstance. S3, Snapshot and + PointInTime are supported. + enum: + - S3 + - Snapshot + - PointInTime + type: string + required: + - source + type: object skipFinalSnapshot: description: "Determines whether a final cluster snapshot is created before the cluster is deleted. If true is specified, no cluster diff --git a/package/crds/docdb.aws.crossplane.io_dbinstances.yaml b/package/crds/docdb.aws.crossplane.io_dbinstances.yaml index 8d0c15e780..e015746d9d 100644 --- a/package/crds/docdb.aws.crossplane.io_dbinstances.yaml +++ b/package/crds/docdb.aws.crossplane.io_dbinstances.yaml @@ -211,6 +211,96 @@ spec: region: description: Region is which region the DBInstance will be created. type: string + restoreFrom: + description: RestoreFrom specifies the details of the backup to + restore when creating a new DBInstance. + properties: + pointInTime: + description: PointInTime specifies the details of the point + in time restore. + properties: + restoreTime: + description: 'RestoreTime is the date and time (UTC) to + restore from. Must be before the latest restorable time + for the DB instance. Can''t be specified if the useLatestRestorableTime + parameter is enabled. Example: 2011-09-07T23:45:00Z' + format: date-time + type: string + sourceDBInstanceAutomatedBackupsArn: + description: 'SourceDBInstanceAutomatedBackupsArn specifies + the Amazon Resource Name (ARN) of the replicated automated + backups from which to restore. Example: arn:aws:rds:useast-1:123456789012:auto-backup:ab-L2IJCEXJP7XQ7HOJ4SIEXAMPLE' + type: string + sourceDBInstanceIdentifier: + description: 'SourceDBInstanceIdentifier specifies the + identifier of the source DB instance from which to restore. + Constraints: Must match the identifier of an existing + DB instance.' + type: string + sourceDbiResourceId: + description: SourceDbiResourceID specifies the resource + ID of the source DB instance from which to restore. + type: string + useLatestRestorableTime: + description: UseLatestRestorableTime indicates that the + DB instance is restored from the latest backup Can't + be specified if the restoreTime parameter is provided. + type: boolean + type: object + s3: + description: S3 specifies the details of the S3 backup to + restore from. + properties: + bucketName: + description: BucketName is the name of the S3 bucket containing + the backup to restore. + type: string + ingestionRoleARN: + description: IngestionRoleARN is the IAM role RDS can + assume that will allow it to access the contents of + the S3 bucket. + type: string + prefix: + description: Prefix is the path prefix of the S3 bucket + within which the backup to restore is located. + type: string + sourceEngine: + description: SourceEngine is the engine used to create + the backup. Must be "mysql". + type: string + sourceEngineVersion: + description: 'SourceEngineVersion is the version of the + engine used to create the backup. Example: "5.7.30"' + type: string + required: + - bucketName + - ingestionRoleARN + - sourceEngine + - sourceEngineVersion + type: object + snapshot: + description: Snapshot specifies the details of the snapshot + to restore from. + properties: + snapshotIdentifier: + description: SnapshotIdentifier is the identifier of the + snapshot to restore. + type: string + required: + - snapshotIdentifier + type: object + source: + description: Source is the type of the backup to restore when + creating a new DBCluster or DBInstance. S3, Snapshot and + PointInTime are supported. + enum: + - S3 + - Snapshot + - PointInTime + type: string + required: + - source + type: object tags: description: The tags to be assigned to the instance. You can assign up to 10 tags to an instance. @@ -222,6 +312,17 @@ spec: type: string type: object type: array + vpcSecurityGroupIDs: + description: "A list of Amazon EC2 VPC security groups to authorize + on this DB instance. This change is asynchronously applied as + soon as possible. \n This setting doesn't apply to RDS Custom. + \n Amazon Aurora Not applicable. The associated list of EC2 + VPC security groups is managed by the DB cluster. For more information, + see ModifyDBCluster. \n Constraints: * If supplied, must match + existing VpcSecurityGroupIds." + items: + type: string + type: array required: - dbInstanceClass - engine diff --git a/pkg/controller/docdb/dbcluster/setup.go b/pkg/controller/docdb/dbcluster/setup.go index be0e4fc99a..743c813411 100644 --- a/pkg/controller/docdb/dbcluster/setup.go +++ b/pkg/controller/docdb/dbcluster/setup.go @@ -48,10 +48,12 @@ import ( ) const ( - errNotDBCluster = "managed resource is not a DB Cluster custom resource" - errKubeUpdateFailed = "cannot update DBCluster instance custom resource" - errGetPasswordSecretFailed = "cannot get password secret" - errSaveSecretFailed = "failed to save generated password to Kubernetes secret" + errNotDBCluster = "managed resource is not a DB Cluster custom resource" + errKubeUpdateFailed = "cannot update DBCluster instance custom resource" + errGetPasswordSecretFailed = "cannot get password secret" + errSaveSecretFailed = "failed to save generated password to Kubernetes secret" + errRestore = "cannot restore DBCluster in AWS" + errUnknownRestoreFromSource = "unknown restoreFrom source" ) // SetupDBCluster adds a controller that reconciles a DBCluster. @@ -227,6 +229,28 @@ func (e *hooks) preCreate(ctx context.Context, cr *svcapitypes.DBCluster, obj *s } obj.MasterUserPassword = awsclient.String(pw) + if cr.Spec.ForProvider.RestoreFrom != nil { + switch *cr.Spec.ForProvider.RestoreFrom.Source { + case "Snapshot": + input := generateRestoreDBClusterFromSnapshotInput(cr) + input.DBClusterIdentifier = obj.DBClusterIdentifier + input.VpcSecurityGroupIds = obj.VpcSecurityGroupIds + + if _, err = e.client.RestoreDBClusterFromSnapshotWithContext(ctx, input); err != nil { + return errors.Wrap(err, errRestore) + } + case "PointInTime": + input := generateRestoreDBClusterToPointInTimeInput(cr) + input.DBClusterIdentifier = obj.DBClusterIdentifier + input.VpcSecurityGroupIds = obj.VpcSecurityGroupIds + + if _, err = e.client.RestoreDBClusterToPointInTimeWithContext(ctx, input); err != nil { + return errors.Wrap(err, errRestore) + } + default: + return errors.New(errUnknownRestoreFromSource) + } + } return nil } @@ -249,6 +273,90 @@ func (e *hooks) postCreate(ctx context.Context, cr *svcapitypes.DBCluster, resp return cre, nil } +func generateRestoreDBClusterFromSnapshotInput(cr *svcapitypes.DBCluster) *svcsdk.RestoreDBClusterFromSnapshotInput { // nolint:gocyclo + res := &svcsdk.RestoreDBClusterFromSnapshotInput{} + + if cr.Spec.ForProvider.AvailabilityZones != nil { + res.SetAvailabilityZones(cr.Spec.ForProvider.AvailabilityZones) + } + + if cr.Spec.ForProvider.DBSubnetGroupName != nil { + res.SetDBSubnetGroupName(*cr.Spec.ForProvider.DBSubnetGroupName) + } + + if cr.Spec.ForProvider.DeletionProtection != nil { + res.SetDeletionProtection(*cr.Spec.ForProvider.DeletionProtection) + } + + if cr.Spec.ForProvider.EnableCloudwatchLogsExports != nil { + res.SetEnableCloudwatchLogsExports(cr.Spec.ForProvider.EnableCloudwatchLogsExports) + } + + if cr.Spec.ForProvider.Engine != nil { + res.SetEngine(*cr.Spec.ForProvider.Engine) + } + + if cr.Spec.ForProvider.EngineVersion != nil { + res.SetEngineVersion(*cr.Spec.ForProvider.EngineVersion) + } + + if cr.Spec.ForProvider.KMSKeyID != nil { + res.SetKmsKeyId(*cr.Spec.ForProvider.KMSKeyID) + } + + if cr.Spec.ForProvider.Port != nil { + res.SetPort(*cr.Spec.ForProvider.Port) + } + + if cr.Spec.ForProvider.RestoreFrom != nil && cr.Spec.ForProvider.RestoreFrom.Snapshot != nil { + res.SetSnapshotIdentifier(*cr.Spec.ForProvider.RestoreFrom.Snapshot.SnapshotIdentifier) + } + + if cr.Spec.ForProvider.Tags != nil { + var tags []*svcsdk.Tag + for _, tag := range cr.Spec.ForProvider.Tags { + tags = append(tags, &svcsdk.Tag{Key: tag.Key, Value: tag.Value}) + } + + res.SetTags(tags) + } + + return res +} + +func generateRestoreDBClusterToPointInTimeInput(cr *svcapitypes.DBCluster) *svcsdk.RestoreDBClusterToPointInTimeInput { // nolint:gocyclo + + p := cr.Spec.ForProvider + res := &svcsdk.RestoreDBClusterToPointInTimeInput{ + DBSubnetGroupName: p.DBSubnetGroupName, + DeletionProtection: p.DeletionProtection, + EnableCloudwatchLogsExports: p.EnableCloudwatchLogsExports, + KmsKeyId: p.KMSKeyID, + Port: p.Port, + UseLatestRestorableTime: &p.RestoreFrom.PointInTime.UseLatestRestorableTime, + VpcSecurityGroupIds: p.VPCSecurityGroupIDs, + } + if p.RestoreFrom.PointInTime != nil && p.RestoreFrom.PointInTime.RestoreTime != nil { + res.RestoreToTime = &p.RestoreFrom.PointInTime.RestoreTime.Time + } + if p.RestoreFrom.PointInTime != nil && p.RestoreFrom.PointInTime.RestoreTime != nil { + res.RestoreType = p.RestoreFrom.PointInTime.RestoreType + } + if p.RestoreFrom.PointInTime != nil && p.RestoreFrom.PointInTime.SourceDBClusterIdentifier != nil { + res.SourceDBClusterIdentifier = p.RestoreFrom.PointInTime.SourceDBClusterIdentifier + } + if cr.Spec.ForProvider.Tags != nil { + var tags []*svcsdk.Tag + for _, tag := range cr.Spec.ForProvider.Tags { + tags = append(tags, &svcsdk.Tag{Key: tag.Key, Value: tag.Value}) + } + + res.SetTags(tags) + } + + return res +} + func preDelete(_ context.Context, cr *svcapitypes.DBCluster, obj *svcsdk.DeleteDBClusterInput) (bool, error) { obj.DBClusterIdentifier = awsclient.String(meta.GetExternalName(cr)) obj.FinalDBSnapshotIdentifier = cr.Spec.ForProvider.FinalDBSnapshotIdentifier