diff --git a/docs/data-sources/cbr_backup_ids_v3.md b/docs/data-sources/cbr_backup_ids_v3.md new file mode 100644 index 000000000..580ff2b60 --- /dev/null +++ b/docs/data-sources/cbr_backup_ids_v3.md @@ -0,0 +1,49 @@ +--- +subcategory: "Cloud Backup and Recovery (CBR)" +--- + +# opentelekomcloud_cbr_backup_ids_v3 + +Use this data source to get details about backup resources from OpenTelekomCloud. + +## Example Usage + +```hcl +variable "checkpoint_id" {} + +data "opentelekomcloud_cbr_backup_ids_v3" "cbr_backups" { + checkpoint_id = var.checkpoint_id +} +``` + +## Argument Reference + +The following arguments are supported: + +* `id` - (Optional) Specifies the ID of backup. + +* `chackpoint_id` - (Optional) Specifies the restore point ID. + +* `status` - (Optional) Specifies the backup status. + +* `resource_name` - (Optional) Specifies the backup resource name. + +* `image_type` - (Optional) Specifies the backup type. + +* `resource_type` - (Optional) Specifies the type of backup objects. + +* `resource_id` - (Optional) Specifies the backup object ID. + +* `name` - (Optional) Specifies the backup name + +* `parent_id` - (Optional) Specifies the ID of parent backup. + +* `resource_az` - (Optional) Specifies the AZ of backup. + +* `vault_id` - (Optional) Specifies the ID of backup vault. + +## Attributes Reference + +The following attributes are exported: + +* `ids` - A list of all the backup ids found. This data source will fail if none are found. diff --git a/docs/data-sources/cbr_backup_v3.md b/docs/data-sources/cbr_backup_v3.md new file mode 100644 index 000000000..922a342d0 --- /dev/null +++ b/docs/data-sources/cbr_backup_v3.md @@ -0,0 +1,77 @@ +--- +subcategory: "Cloud Backup and Recovery (CBR)" +--- + +# opentelekomcloud_cbr_backup_v3 + +Use this data source to get details about backup resources from OpenTelekomCloud. + +## Example Usage + +```hcl +variable "backup_id" {} + +data "opentelekomcloud_cbr_backup_v3" "cbr_backup" { + id = var.backup_id +} +``` + +## Argument Reference + +The following arguments are supported: + +* `id` - (Optional) Specifies the ID of backup. + +* `chackpoint_id` - (Optional) Specifies the restore point ID. + +* `status` - (Optional) Specifies the backup status. + +* `resource_name` - (Optional) Specifies the backup resource name. + +* `image_type` - (Optional) Specifies the backup type. + +* `resource_type` - (Optional) Specifies the type of backup objects. + +* `resource_id` - (Optional) Specifies the backup object ID. + +* `name` - (Optional) Specifies the backup name + +* `parent_id` - (Optional) Specifies the ID of parent backup. + +* `resource_az` - (Optional) Specifies the AZ of backup. + +* `vault_id` - (Optional) Specifies the ID of backup vault. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `created_at` - The time the backup was created. + +* `description` - Backup description. + +* `expired_at` - The time the backup will be expired. + +* `project_id` - The project ID of backup. + +* `resource_size` - Backup size in GB. + +* `updated_at` - Indicates the update time. + +* `provider_id` - Backup provider ID which is used to distinguish backup objects. + +* `auto_trigger` - Specifies whether the backup is automatically generated. + +* `bootable` - Specifies whether the backup is a system disk backup. + +* `incremental` - Specifies whether the backup is an incremental backup. + +* `snapshot_id` - The snapshot ID of the disk backup. + +* `support_lld` - Specifies whether to allow lazyloading for fast restoration. + +* `supported_restore_mode` - Restoration mode of the backup. + +* `encrypted` - Specifies whether the backup is encrypted. + +* `system_disk` - Specifies whether a disk is a system disk. diff --git a/go.mod b/go.mod index d2c0acbf2..ab376c994 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/jen20/awspolicyequivalence v1.1.0 github.com/jinzhu/copier v0.3.5 github.com/mitchellh/go-homedir v1.1.0 - github.com/opentelekomcloud/gophertelekomcloud v0.5.14 + github.com/opentelekomcloud/gophertelekomcloud v0.5.15-0.20220706063853-36a17543d6f9 github.com/unknwon/com v1.0.1 golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum index 50f756a2a..597d080bc 100644 --- a/go.sum +++ b/go.sum @@ -201,8 +201,8 @@ github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce/go.mod h1:uFMI8w+ref4v2r9jz+c9i1IfIttS/OkmLfrk1jne5hs= github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= -github.com/opentelekomcloud/gophertelekomcloud v0.5.14 h1:6qnaY47xzzH79zsA8axQj2hNwv/hHQsHGZPCnnGmeJ0= -github.com/opentelekomcloud/gophertelekomcloud v0.5.14/go.mod h1:pzEP1kduNwv+hrI9R6/DFU/NiX7Kr9NiFjpQ7kJQTsM= +github.com/opentelekomcloud/gophertelekomcloud v0.5.15-0.20220706063853-36a17543d6f9 h1:XKv+sTANU9vjZStqYQy9LUCUsvxesItv59QVVqCoA9k= +github.com/opentelekomcloud/gophertelekomcloud v0.5.15-0.20220706063853-36a17543d6f9/go.mod h1:pzEP1kduNwv+hrI9R6/DFU/NiX7Kr9NiFjpQ7kJQTsM= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/opentelekomcloud/acceptance/cbr/data_source_opentelekomcloud_cbr_backup_v3_test.go b/opentelekomcloud/acceptance/cbr/data_source_opentelekomcloud_cbr_backup_v3_test.go new file mode 100644 index 000000000..7e08fc10e --- /dev/null +++ b/opentelekomcloud/acceptance/cbr/data_source_opentelekomcloud_cbr_backup_v3_test.go @@ -0,0 +1,57 @@ +package acceptance + +import ( + "fmt" + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common" +) + +const dataBackupName = "data.opentelekomcloud_cbr_backup_v3.cbr" + +func TestAccCBRBackupV3DataSource_basic(t *testing.T) { + if os.Getenv("CBR_DATA") == "" { + t.Skip("this test is not a stable one") + } + vaultId := "insert_vaultId_here" + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + common.TestAccPreCheck(t) + }, + ProviderFactories: common.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccCBRBackupV3DataSourceBasic(vaultId), + Check: resource.ComposeTestCheckFunc( + testAccCheckCBRBackupV3DataSourceID(dataBackupName), + ), + }, + }, + }) +} + +func testAccCheckCBRBackupV3DataSourceID(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("can't find backup data source: %s ", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("backup data source ID not set ") + } + + return nil + } +} + +func testAccCBRBackupV3DataSourceBasic(vaultId string) string { + return fmt.Sprintf(` +data "opentelekomcloud_cbr_backup_v3" "cbr" { + checkpoint_id = "%s" +} +`, vaultId) +} diff --git a/opentelekomcloud/acceptance/cbr/data_source_opentelekomcloud_cbr_backups_ids_v3_test.go b/opentelekomcloud/acceptance/cbr/data_source_opentelekomcloud_cbr_backups_ids_v3_test.go new file mode 100644 index 000000000..57573641b --- /dev/null +++ b/opentelekomcloud/acceptance/cbr/data_source_opentelekomcloud_cbr_backups_ids_v3_test.go @@ -0,0 +1,57 @@ +package acceptance + +import ( + "fmt" + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common" +) + +const dataBackupIDs = "data.opentelekomcloud_cbr_backup_ids_v3.backup_ids" + +func TestAccCBRBackupV3DataSourceIds_basic(t *testing.T) { + if os.Getenv("CBR_DATA") == "" { + t.Skip("this test is not a stable one") + } + vaultId := "insert_id_here" + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + common.TestAccPreCheck(t) + }, + ProviderFactories: common.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccCBRBackupV3DataSourceIDsBasic(vaultId), + Check: resource.ComposeTestCheckFunc( + testAccCheckCBRBackupV3DataSourceIDs(dataBackupIDs), + ), + }, + }, + }) +} + +func testAccCheckCBRBackupV3DataSourceIDs(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("can't find backup data source: %s ", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("backup data source ID not set ") + } + + return nil + } +} + +func testAccCBRBackupV3DataSourceIDsBasic(vaultId string) string { + return fmt.Sprintf(` +data "opentelekomcloud_cbr_backup_ids_v3" "backup_ids" { + vault_id = "%s" +} +`, vaultId) +} diff --git a/opentelekomcloud/provider.go b/opentelekomcloud/provider.go index 2e90c4fd6..752da7555 100644 --- a/opentelekomcloud/provider.go +++ b/opentelekomcloud/provider.go @@ -230,6 +230,8 @@ func Provider() *schema.Provider { DataSourcesMap: map[string]*schema.Resource{ "opentelekomcloud_antiddos_v1": antiddos.DataSourceAntiDdosV1(), + "opentelekomcloud_cbr_backup_v3": cbr.DataSourceCBRBackupsV3(), + "opentelekomcloud_cbr_backup_ids_v3": cbr.DataSourceCBRBackupsIdsV3(), "opentelekomcloud_cce_cluster_v3": cce.DataSourceCCEClusterV3(), "opentelekomcloud_cce_cluster_kubeconfig_v3": cce.DataSourceCCEClusterKubeConfigV3(), "opentelekomcloud_cce_node_ids_v3": cce.DataSourceCceNodeIdsV3(), diff --git a/opentelekomcloud/services/cbr/data_source_opentelekomcloud_cbr_backups_ids_v3.go b/opentelekomcloud/services/cbr/data_source_opentelekomcloud_cbr_backups_ids_v3.go new file mode 100644 index 000000000..295c648a3 --- /dev/null +++ b/opentelekomcloud/services/cbr/data_source_opentelekomcloud_cbr_backups_ids_v3.go @@ -0,0 +1,127 @@ +package cbr + +import ( + "context" + + "github.com/hashicorp/go-multierror" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/opentelekomcloud/gophertelekomcloud/openstack/cbr/v3/backups" + "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg" + "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/fmterr" +) + +func DataSourceCBRBackupsIdsV3() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceCBRBackupsIdsV3Read, + + Schema: map[string]*schema.Schema{ + "ids": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "checkpoint_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "image_type": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "vault_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "parent_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "resource_az": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "resource_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "resource_name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "resource_type": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + }, + } +} + +func dataSourceCBRBackupsIdsV3Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + config := meta.(*cfg.Config) + cbrClient, err := config.CbrV3Client(config.GetRegion(d)) + if err != nil { + return fmterr.Errorf("unable to create opentelekomcloud CBR client : %s", err) + } + + listOpts := backups.ListOpts{ + ID: d.Get("id").(string), + CheckpointID: d.Get("checkpoint_id").(string), + ImageType: d.Get("image_type").(string), + Name: d.Get("name").(string), + VaultID: d.Get("vault_id").(string), + ParentID: d.Get("parent_id").(string), + ResourceAZ: d.Get("resource_az").(string), + ResourceID: d.Get("resource_id").(string), + ResourceName: d.Get("resource_name").(string), + ResourceType: d.Get("resource_type").(string), + Status: d.Get("status").(string), + } + extractedBackups, err := backups.List(cbrClient, listOpts) + + if err != nil { + return fmterr.Errorf("unable to retrieve Backups: %s", err) + } + + if len(extractedBackups) < 1 { + return fmterr.Errorf("your query returned no results. Please change your search criteria and try again") + } + + allBackups := make([]string, 0) + for _, singleBackup := range extractedBackups { + allBackups = append(allBackups, singleBackup.ID) + } + d.SetId("Filter") + mErr := multierror.Append( + d.Set("ids", allBackups), + ) + + if err := mErr.ErrorOrNil(); err != nil { + return diag.FromErr(err) + } + + return nil +} diff --git a/opentelekomcloud/services/cbr/data_source_opentelekomcloud_cbr_backups_v3.go b/opentelekomcloud/services/cbr/data_source_opentelekomcloud_cbr_backups_v3.go new file mode 100644 index 000000000..bd3ee8801 --- /dev/null +++ b/opentelekomcloud/services/cbr/data_source_opentelekomcloud_cbr_backups_v3.go @@ -0,0 +1,233 @@ +package cbr + +import ( + "context" + "log" + + "github.com/hashicorp/go-multierror" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/opentelekomcloud/gophertelekomcloud/openstack/cbr/v3/backups" + "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common" + "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg" + "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/fmterr" +) + +func DataSourceCBRBackupsV3() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceCBRBackupsV3Read, + + Schema: map[string]*schema.Schema{ + "checkpoint_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "created_at": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "description": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "expired_at": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "image_type": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "parent_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "project_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "resource_az": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "resource_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "resource_name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "resource_size": { + Type: schema.TypeInt, + Optional: true, + Computed: true, + }, + "resource_type": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "updated_at": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "vault_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "provider_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "auto_trigger": { + Type: schema.TypeBool, + Optional: true, + Computed: true, + }, + "bootable": { + Type: schema.TypeBool, + Optional: true, + Computed: true, + }, + "incremental": { + Type: schema.TypeBool, + Optional: true, + Computed: true, + }, + "snapshot_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "support_lld": { + Type: schema.TypeBool, + Optional: true, + Computed: true, + }, + "supported_restore_mode": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "contain_system_disk": { + Type: schema.TypeBool, + Optional: true, + Computed: true, + }, + "encrypted": { + Type: schema.TypeBool, + Optional: true, + Computed: true, + }, + "system_disk": { + Type: schema.TypeBool, + Optional: true, + Computed: true, + }, + }, + } +} + +func dataSourceCBRBackupsV3Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + config := meta.(*cfg.Config) + backupClient, err := config.CbrV3Client(config.GetRegion(d)) + if err != nil { + return fmterr.Errorf("error creating CbrV3Client: %w", err) + } + + listOpts := backups.ListOpts{ + ID: d.Get("id").(string), + CheckpointID: d.Get("checkpoint_id").(string), + Status: d.Get("status").(string), + ResourceName: d.Get("resource_name").(string), + ImageType: d.Get("image_type").(string), + ResourceType: d.Get("resource_type").(string), + ResourceID: d.Get("resource_id").(string), + Name: d.Get("name").(string), + ParentID: d.Get("parent_id").(string), + ResourceAZ: d.Get("resource_az").(string), + VaultID: d.Get("vault_id").(string), + } + + extractedBackups, err := backups.List(backupClient, listOpts) + if err != nil { + return fmterr.Errorf("unable to list all backups pages: %s", err) + } + + if len(extractedBackups) < 1 { + return common.DataSourceTooFewDiag + } + + if len(extractedBackups) > 1 { + return common.DataSourceTooManyDiag + } + + backup := extractedBackups[0] + + log.Printf("[INFO] Retrieved backup policy %s using given filter", backup.ID) + + d.SetId(backup.ID) + mErr := multierror.Append( + d.Set("checkpoint_id", backup.CheckpointID), + d.Set("created_at", backup.CreatedAt), + d.Set("description", backup.Description), + d.Set("expired_at", backup.ExpiredAt), + d.Set("image_type", backup.ImageType), + d.Set("name", backup.Name), + d.Set("parent_id", backup.ParentID), + d.Set("project_id", backup.ProjectID), + d.Set("provider_id", backup.ProviderID), + d.Set("resource_az", backup.ResourceAZ), + d.Set("resource_id", backup.ResourceID), + d.Set("resource_name", backup.ResourceName), + d.Set("resource_size", backup.ResourceSize), + d.Set("resource_type", backup.ResourceType), + d.Set("status", backup.Status), + d.Set("updated_at", backup.UpdatedAt), + d.Set("vault_id", backup.VaultId), + d.Set("auto_trigger", backup.ExtendInfo.AutoTrigger), + d.Set("bootable", backup.ExtendInfo.Bootable), + d.Set("incremental", backup.ExtendInfo.Incremental), + d.Set("snapshot_id", backup.ExtendInfo.SnapshotID), + d.Set("support_lld", backup.ExtendInfo.SupportLld), + d.Set("supported_restore_mode", backup.ExtendInfo.SupportedRestoreMode), + d.Set("contain_system_disk", backup.ExtendInfo.ContainSystemDisk), + d.Set("encrypted", backup.ExtendInfo.Encrypted), + d.Set("system_disk", backup.ExtendInfo.SystemDisk), + ) + + if err := mErr.ErrorOrNil(); err != nil { + return diag.FromErr(err) + } + + return nil +} diff --git a/releasenotes/notes/cbr_backups-c8ab402b0eb93c35.yaml b/releasenotes/notes/cbr_backups-c8ab402b0eb93c35.yaml new file mode 100644 index 000000000..05b980332 --- /dev/null +++ b/releasenotes/notes/cbr_backups-c8ab402b0eb93c35.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + **New Data Source:** ``opentelekomcloud_cbr_backup_v3`` (`#1779 `_) + - | + **New Data Source:** ``opentelekomcloud_cbr_backup_ids_v3`` (`#1779 `_)