diff --git a/ibm/conns/config.go b/ibm/conns/config.go index e6715733d4..e2c4523a75 100644 --- a/ibm/conns/config.go +++ b/ibm/conns/config.go @@ -247,6 +247,8 @@ type ClientSession interface { ResourceControllerAPIV2() (controllerv2.ResourceControllerAPIV2, error) BackupRecoveryV1() (*backuprecoveryv1.BackupRecoveryV1, error) BackupRecoveryV1Connector() (*backuprecoveryv1.BackupRecoveryV1Connector, error) + BackupRecoveryManagerV1() (*backuprecoveryv1.HeliosReportingServiceApIsV1, error) + BackupRecoveryManagerSreV1() (*backuprecoveryv1.HeliosSreApiV2, error) IBMCloudLogsRoutingV0() (*ibmcloudlogsroutingv0.IBMCloudLogsRoutingV0, error) SoftLayerSession() *slsession.Session IBMPISession() (*ibmpisession.IBMPISession, error) @@ -579,6 +581,12 @@ type clientSession struct { backupRecoveryConnectorClient *backuprecoveryv1.BackupRecoveryV1Connector backupRecoveryConnectorClientErr error + backupRecoveryManagerClient *backuprecoveryv1.HeliosReportingServiceApIsV1 + backupRecoveryManagerClientErr error + + backupRecoveryManagerSreClient *backuprecoveryv1.HeliosSreApiV2 + backupRecoveryManagerSreClientErr error + secretsManagerClient *secretsmanagerv2.SecretsManagerV2 secretsManagerClientErr error @@ -1176,6 +1184,14 @@ func (session clientSession) BackupRecoveryV1Connector() (*backuprecoveryv1.Back return session.backupRecoveryConnectorClient, session.backupRecoveryConnectorClientErr } +func (session clientSession) BackupRecoveryManagerV1() (*backuprecoveryv1.HeliosReportingServiceApIsV1, error) { + return session.backupRecoveryManagerClient, session.backupRecoveryManagerClientErr +} + +func (session clientSession) BackupRecoveryManagerSreV1() (*backuprecoveryv1.HeliosSreApiV2, error) { + return session.backupRecoveryManagerSreClient, session.backupRecoveryManagerSreClientErr +} + // IBM Cloud Secrets Manager V2 Basic API func (session clientSession) SecretsManagerV2() (*secretsmanagerv2.SecretsManagerV2, error) { return session.secretsManagerClient, session.secretsManagerClientErr @@ -1374,6 +1390,8 @@ func (c *Config) ClientSession() (interface{}, error) { session.resourceControllerErr = errEmptyBluemixCredentials session.backupRecoveryClientErr = errEmptyBluemixCredentials session.backupRecoveryConnectorClientErr = errEmptyBluemixCredentials + session.backupRecoveryManagerClientErr = errEmptyBluemixCredentials + session.backupRecoveryManagerSreClientErr = errEmptyBluemixCredentials session.catalogManagementClientErr = errEmptyBluemixCredentials session.partnerCenterSellClientErr = errEmptyBluemixCredentials session.ibmpiConfigErr = errEmptyBluemixCredentials @@ -1640,6 +1658,8 @@ func (c *Config) ClientSession() (interface{}, error) { // Construct the service options. var backupRecoveryURL string var backupRecoveryConnectorURL string + var backupRecoveryManagerURL string + var backupRecoveryManagerSreURL string if fileMap != nil && c.Visibility != "public-and-private" { backupRecoveryURL = fileFallBack(fileMap, c.Visibility, "IBMCLOUD_BACKUP_RECOVERY_ENDPOINT", c.Region, backupRecoveryURL) @@ -1689,6 +1709,48 @@ func (c *Config) ClientSession() (interface{}, error) { }) } + backupRecoveryManagerClientOptions := &backuprecoveryv1.HeliosReportingServiceApIsV1Options{ + Authenticator: authenticator, + URL: EnvFallBack([]string{"IBMCLOUD_BACKUP_RECOVERY_MANAGER_ENDPOINT"}, backupRecoveryManagerURL), + } + if backupRecoveryManagerClientOptions.URL == "" { + session.backupRecoveryManagerClientErr = fmt.Errorf("IBMCLOUD_BACKUP_RECOVERY_MANAGER_ENDPOINT not set in env or endpoints file") + } + // Construct the service client. + session.backupRecoveryManagerClient, err = backuprecoveryv1.NewHeliosReportingServiceApIsV1(backupRecoveryManagerClientOptions) + if err != nil { + session.backupRecoveryManagerClientErr = fmt.Errorf("Error occurred while configuring IBM Backup recovery API service: %q", err) + } + if session.backupRecoveryManagerClient != nil && session.backupRecoveryManagerClient.Service != nil { + // Enable retries for API calls + session.backupRecoveryManagerClient.Service.EnableRetries(c.RetryCount, c.RetryDelay) + // Add custom header for analytics + session.backupRecoveryManagerClient.SetDefaultHeaders(gohttp.Header{ + "X-Original-User-Agent": {fmt.Sprintf("terraform-provider-ibm/%s", version.Version)}, + }) + } + + backupRecoveryManagerSreClientOptions := &backuprecoveryv1.HeliosSreApiV2Options{ + Authenticator: authenticator, + URL: EnvFallBack([]string{"IBMCLOUD_BACKUP_RECOVERY_MANAGER_SRE_ENDPOINT"}, backupRecoveryManagerSreURL), + } + if backupRecoveryManagerSreClientOptions.URL == "" { + session.backupRecoveryManagerSreClientErr = fmt.Errorf("IBMCLOUD_BACKUP_RECOVERY_MANAGER_SRE_ENDPOINT not set in env or endpoints file") + } + // Construct the service client. + session.backupRecoveryManagerSreClient, err = backuprecoveryv1.NewHeliosSreApiV2(backupRecoveryManagerSreClientOptions) + if err != nil { + session.backupRecoveryManagerSreClientErr = fmt.Errorf("Error occurred while configuring IBM Backup recovery API service: %q", err) + } + if session.backupRecoveryManagerSreClient != nil && session.backupRecoveryManagerSreClient.Service != nil { + // Enable retries for API calls + session.backupRecoveryManagerSreClient.Service.EnableRetries(c.RetryCount, c.RetryDelay) + // Add custom header for analytics + session.backupRecoveryManagerSreClient.SetDefaultHeaders(gohttp.Header{ + "X-Original-User-Agent": {fmt.Sprintf("terraform-provider-ibm/%s", version.Version)}, + }) + } + projectEndpoint := project.DefaultServiceURL // Construct an "options" struct for creating the service client. if fileMap != nil && c.Visibility != "public-and-private" { diff --git a/ibm/provider/provider.go b/ibm/provider/provider.go index 64c4317ee6..bdef618a2c 100644 --- a/ibm/provider/provider.go +++ b/ibm/provider/provider.go @@ -250,30 +250,39 @@ func Provider() *schema.Provider { "ibm_config_aggregator_resource_collection_status": configurationaggregator.AddConfigurationAggregatorInstanceFields(configurationaggregator.DataSourceIbmConfigAggregatorResourceCollectionStatus()), // // BackupAndRecovery - "ibm_backup_recovery_agent_upgrade_tasks": backuprecovery.DataSourceIbmBackupRecoveryAgentUpgradeTasks(), - "ibm_backup_recovery_download_agent": backuprecovery.DataSourceIbmBackupRecoveryDownloadAgent(), - "ibm_backup_recovery_search_indexed_object": backuprecovery.DataSourceIbmBackupRecoverySearchIndexedObject(), - "ibm_backup_recovery_object_snapshots": backuprecovery.DataSourceIbmBackupRecoveryObjectSnapshots(), - "ibm_backup_recovery_connectors_metadata": backuprecovery.DataSourceIbmBackupRecoveryConnectorsMetadata(), - "ibm_backup_recovery_connector_logs": backuprecovery.DataSourceIbmBackupRecoveryConnectorLogs(), - "ibm_backup_recovery_connector_status": backuprecovery.DataSourceIbmBackupRecoveryConnectorStatus(), - "ibm_backup_recovery_data_source_connections": backuprecovery.DataSourceIbmBackupRecoveryDataSourceConnections(), - "ibm_backup_recovery_data_source_connectors": backuprecovery.DataSourceIbmBackupRecoveryDataSourceConnectors(), - "ibm_backup_recovery_search_objects": backuprecovery.DataSourceIbmBackupRecoverySearchObjects(), - "ibm_backup_recovery_search_protected_objects": backuprecovery.DataSourceIbmBackupRecoverySearchProtectedObjects(), - "ibm_backup_recovery_protection_group": backuprecovery.DataSourceIbmBackupRecoveryProtectionGroup(), - "ibm_backup_recovery_protection_groups": backuprecovery.DataSourceIbmBackupRecoveryProtectionGroups(), - "ibm_backup_recovery_protection_group_runs": backuprecovery.DataSourceIbmBackupRecoveryProtectionGroupRuns(), - "ibm_backup_recovery_protection_policies": backuprecovery.DataSourceIbmBackupRecoveryProtectionPolicies(), - "ibm_backup_recovery_protection_policy": backuprecovery.DataSourceIbmBackupRecoveryProtectionPolicy(), - "ibm_backup_recovery": backuprecovery.DataSourceIbmBackupRecovery(), - "ibm_backup_recoveries": backuprecovery.DataSourceIbmBackupRecoveries(), - "ibm_backup_recovery_download_files": backuprecovery.DataSourceIbmBackupRecoveryDownloadFiles(), - "ibm_backup_recovery_source_registrations": backuprecovery.DataSourceIbmBackupRecoverySourceRegistrations(), - "ibm_backup_recovery_source_registration": backuprecovery.DataSourceIbmBackupRecoverySourceRegistration(), - "ibm_backup_recovery_download_indexed_files": backuprecovery.DataSourceIbmBackupRecoveryDownloadIndexedFiles(), - "ibm_backup_recovery_protection_sources": backuprecovery.DataSourceIbmBackupRecoveryProtectionSources(), - "ibm_backup_recovery_connector_get_users": backuprecovery.DataSourceIbmBackupRecoveryConnectorGetUsers(), + "ibm_backup_recovery_agent_upgrade_tasks": backuprecovery.DataSourceIbmBackupRecoveryAgentUpgradeTasks(), + "ibm_backup_recovery_download_agent": backuprecovery.DataSourceIbmBackupRecoveryDownloadAgent(), + "ibm_backup_recovery_search_indexed_object": backuprecovery.DataSourceIbmBackupRecoverySearchIndexedObject(), + "ibm_backup_recovery_object_snapshots": backuprecovery.DataSourceIbmBackupRecoveryObjectSnapshots(), + "ibm_backup_recovery_connectors_metadata": backuprecovery.DataSourceIbmBackupRecoveryConnectorsMetadata(), + "ibm_backup_recovery_connector_logs": backuprecovery.DataSourceIbmBackupRecoveryConnectorLogs(), + "ibm_backup_recovery_connector_status": backuprecovery.DataSourceIbmBackupRecoveryConnectorStatus(), + "ibm_backup_recovery_data_source_connections": backuprecovery.DataSourceIbmBackupRecoveryDataSourceConnections(), + "ibm_backup_recovery_data_source_connectors": backuprecovery.DataSourceIbmBackupRecoveryDataSourceConnectors(), + "ibm_backup_recovery_search_objects": backuprecovery.DataSourceIbmBackupRecoverySearchObjects(), + "ibm_backup_recovery_search_protected_objects": backuprecovery.DataSourceIbmBackupRecoverySearchProtectedObjects(), + "ibm_backup_recovery_protection_group": backuprecovery.DataSourceIbmBackupRecoveryProtectionGroup(), + "ibm_backup_recovery_protection_groups": backuprecovery.DataSourceIbmBackupRecoveryProtectionGroups(), + "ibm_backup_recovery_protection_group_runs": backuprecovery.DataSourceIbmBackupRecoveryProtectionGroupRuns(), + "ibm_backup_recovery_protection_policies": backuprecovery.DataSourceIbmBackupRecoveryProtectionPolicies(), + "ibm_backup_recovery_protection_policy": backuprecovery.DataSourceIbmBackupRecoveryProtectionPolicy(), + "ibm_backup_recovery": backuprecovery.DataSourceIbmBackupRecovery(), + "ibm_backup_recoveries": backuprecovery.DataSourceIbmBackupRecoveries(), + "ibm_backup_recovery_download_files": backuprecovery.DataSourceIbmBackupRecoveryDownloadFiles(), + "ibm_backup_recovery_source_registrations": backuprecovery.DataSourceIbmBackupRecoverySourceRegistrations(), + "ibm_backup_recovery_source_registration": backuprecovery.DataSourceIbmBackupRecoverySourceRegistration(), + "ibm_backup_recovery_download_indexed_files": backuprecovery.DataSourceIbmBackupRecoveryDownloadIndexedFiles(), + "ibm_backup_recovery_protection_sources": backuprecovery.DataSourceIbmBackupRecoveryProtectionSources(), + "ibm_backup_recovery_connector_get_users": backuprecovery.DataSourceIbmBackupRecoveryConnectorGetUsers(), + "ibm_backup_recovery_manager_get_component": backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponent(), + "ibm_backup_recovery_manager_get_components": backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponents(), + "ibm_backup_recovery_manager_export_report": backuprecovery.DataSourceIbmBackupRecoveryManagerExportReport(), + "ibm_backup_recovery_manager_get_report": backuprecovery.DataSourceIbmBackupRecoveryManagerGetReport(), + "ibm_backup_recovery_manager_get_report_preview": backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportPreview(), + "ibm_backup_recovery_manager_get_report_type": backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportType(), + "ibm_backup_recovery_manager_get_reports": backuprecovery.DataSourceIbmBackupRecoveryManagerGetReports(), + "ibm_backup_recovery_manager_get_resources": backuprecovery.DataSourceIbmBackupRecoveryManagerGetResources(), + "ibm_backup_recovery_manager_sre_get_cluster_info": backuprecovery.DataSourceIbmBackupRecoveryManagerSreGetClusterInfo(), // // AppID "ibm_appid_action_url": appid.DataSourceIBMAppIDActionURL(), diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_export_report.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_export_report.go new file mode 100644 index 0000000000..975169aa59 --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_export_report.go @@ -0,0 +1,502 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.105.1-067d600b-20250616-154447 + */ + +package backuprecovery + +import ( + "context" + "fmt" + "io" + "log" + "os" + "time" + + "github.com/IBM/go-sdk-core/v5/core" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" +) + +func DataSourceIbmBackupRecoveryManagerExportReport() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceIbmBackupRecoveryManagerExportReportRead, + + Schema: map[string]*schema.Schema{ + "report_id": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Description: "Specifies the id of the report.", + }, + "file_path": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Description: "Specifies the absolute path for download", + }, + "async": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Description: "Specifies if the report should be generated asynchronously.", + }, + "filters": &schema.Schema{ + Type: schema.TypeList, + Optional: true, + Description: "Specifies list of global filters that are applicable to given components in the report.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the attribute.", + }, + "filter_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the type of the filter that needs to be applied.", + }, + "in_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the in filter that are applied on attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_data_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the data type of the attribute.", + }, + "attribute_labels": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the optional label values for the attribute.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "bool_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of boolean values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeBool, + }, + }, + "int32_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of int32 values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "int64_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of int64 values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "string_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of string values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "range_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the filters that are applied on attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "lower_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the lower bound value. If specified, all the results which are greater than this value will be returned.", + }, + "upper_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the upper bound value. If specified, all the results which are lesser than this value will be returned.", + }, + }, + }, + }, + "systems_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the systems filter. Specifying this will pre filter all the results provided list of system identifier before applying aggregations.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "system_ids": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies an array of system identifiers. System identifiers may be of format clusterid:clusterincarnationid or a regionid (applicable only in case of DMaaS).", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "system_names": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the optional system names labels.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "time_range_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the time range filter. Specifying this will pre filter all the results on necessary resources like Protection Runs etc before applying aggregations. Currently, maximum allowed time range is 60 days.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "date_range": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Enum value for specifying the date range for a time filter. Considered only if lowerBound and upperBound are empty.", + }, + "duration_hours": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the duration preceding the current time for which the data must be fetch i.e fetch data between currentTime and currentTime - durationHours. This filter is only considered if neither upperBound, lowerBound or dateRange is specified.", + }, + "lower_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the lower bound value. If specified, all the results which are greater than this value will be returned.", + }, + "upper_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the upper bound value. If specified, all the results which are lesser than this value will be returned.", + }, + }, + }, + }, + }, + }, + }, + "layout": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Description: "The layout of the report which needs to be exported.", + }, + "report_format": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Description: "The format in which the report needs to be exported.", + }, + "timezone": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Description: "Specifies timezone of the user. If nil, defaults to UTC. The time specified should be a location name in the IANA Time Zone database, for example, 'America/Los_Angeles'.", + }, + }, + } +} + +func dataSourceIbmBackupRecoveryManagerExportReportRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + backupRecoveryClient, err := meta.(conns.ClientSession).BackupRecoveryManagerV1() + if err != nil { + tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_export_report", "read", "initialize-client") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + exportReportOptions := &backuprecoveryv1.ExportReportOptions{} + + exportReportOptions.SetID(d.Get("report_id").(string)) + if _, ok := d.GetOk("async"); ok { + exportReportOptions.SetAsync(d.Get("async").(bool)) + } + if _, ok := d.GetOk("filters"); ok { + var newFilters []backuprecoveryv1.AttributeFilter + for _, v := range d.Get("filters").([]interface{}) { + value := v.(map[string]interface{}) + newFiltersItem, err := ResourceIbmBackupRecoveryManagerExportReportMapToAttributeFilter(value) + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "ibm_backup_recovery_manager_export_report", "read", "parse-filters").GetDiag() + } + newFilters = append(newFilters, *newFiltersItem) + } + exportReportOptions.SetFilters(newFilters) + } + if _, ok := d.GetOk("layout"); ok { + exportReportOptions.SetLayout(d.Get("layout").(string)) + } + if _, ok := d.GetOk("report_format"); ok { + exportReportOptions.SetReportFormat(d.Get("report_format").(string)) + } + if _, ok := d.GetOk("timezone"); ok { + exportReportOptions.SetTimezone(d.Get("timezone").(string)) + } + + typeString, _, err := backupRecoveryClient.ExportReportWithContext(context, exportReportOptions) + if err != nil { + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("ExportReportWithContext failed: %s", err.Error()), "(Data) ibm_backup_recovery_manager_export_report", "read") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + d.SetId(dataSourceIbmBackupRecoveryManagerExportReportID(d)) + + err = saveReportExportToFile(typeString, d.Get("file_path").(string)) + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "ibm_backup_recovery_download_agent", "read", "parse-linux_params").GetDiag() + } + + return nil +} + +func saveReportExportToFile(response io.ReadCloser, filePath string) error { + outputFile, err := os.Create(filePath) + if err != nil { + return err + } + defer outputFile.Close() + + _, err = io.Copy(outputFile, response) + if err != nil { + return err + } + + err = response.Close() + if err != nil { + return err + } + + return nil +} + +// dataSourceIbmBackupRecoveryManagerExportReportID returns a reasonable ID for the list. +func dataSourceIbmBackupRecoveryManagerExportReportID(d *schema.ResourceData) string { + return time.Now().UTC().String() +} + +func ResourceIbmBackupRecoveryManagerExportReportMapToAttributeFilter(modelMap map[string]interface{}) (*backuprecoveryv1.AttributeFilter, error) { + model := &backuprecoveryv1.AttributeFilter{} + model.Attribute = core.StringPtr(modelMap["attribute"].(string)) + model.FilterType = core.StringPtr(modelMap["filter_type"].(string)) + if modelMap["in_filter_params"] != nil && len(modelMap["in_filter_params"].([]interface{})) > 0 { + InFilterParamsModel, err := ResourceIbmBackupRecoveryManagerExportReportMapToInFilterParams(modelMap["in_filter_params"].([]interface{})[0].(map[string]interface{})) + if err != nil { + return model, err + } + model.InFilterParams = InFilterParamsModel + } + if modelMap["range_filter_params"] != nil && len(modelMap["range_filter_params"].([]interface{})) > 0 { + RangeFilterParamsModel, err := ResourceIbmBackupRecoveryManagerExportReportMapToRangeFilterParams(modelMap["range_filter_params"].([]interface{})[0].(map[string]interface{})) + if err != nil { + return model, err + } + model.RangeFilterParams = RangeFilterParamsModel + } + if modelMap["systems_filter_params"] != nil && len(modelMap["systems_filter_params"].([]interface{})) > 0 { + SystemsFilterParamsModel, err := ResourceIbmBackupRecoveryManagerExportReportMapToSystemsFilterParams(modelMap["systems_filter_params"].([]interface{})[0].(map[string]interface{})) + if err != nil { + return model, err + } + model.SystemsFilterParams = SystemsFilterParamsModel + } + if modelMap["time_range_filter_params"] != nil && len(modelMap["time_range_filter_params"].([]interface{})) > 0 { + TimeRangeFilterParamsModel, err := ResourceIbmBackupRecoveryManagerExportReportMapToTimeRangeFilterParams(modelMap["time_range_filter_params"].([]interface{})[0].(map[string]interface{})) + if err != nil { + return model, err + } + model.TimeRangeFilterParams = TimeRangeFilterParamsModel + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerExportReportMapToInFilterParams(modelMap map[string]interface{}) (*backuprecoveryv1.InFilterParams, error) { + model := &backuprecoveryv1.InFilterParams{} + model.AttributeDataType = core.StringPtr(modelMap["attribute_data_type"].(string)) + if modelMap["attribute_labels"] != nil { + attributeLabels := []string{} + for _, attributeLabelsItem := range modelMap["attribute_labels"].([]interface{}) { + attributeLabels = append(attributeLabels, attributeLabelsItem.(string)) + } + model.AttributeLabels = attributeLabels + } + if modelMap["bool_filter_values"] != nil { + boolFilterValues := []bool{} + for _, boolFilterValuesItem := range modelMap["bool_filter_values"].([]interface{}) { + boolFilterValues = append(boolFilterValues, boolFilterValuesItem.(bool)) + } + model.BoolFilterValues = boolFilterValues + } + if modelMap["int32_filter_values"] != nil { + int32FilterValues := []int64{} + for _, int32FilterValuesItem := range modelMap["int32_filter_values"].([]interface{}) { + int32FilterValues = append(int32FilterValues, int64(int32FilterValuesItem.(int))) + } + model.Int32FilterValues = int32FilterValues + } + if modelMap["int64_filter_values"] != nil { + int64FilterValues := []int64{} + for _, int64FilterValuesItem := range modelMap["int64_filter_values"].([]interface{}) { + int64FilterValues = append(int64FilterValues, int64(int64FilterValuesItem.(int))) + } + model.Int64FilterValues = int64FilterValues + } + if modelMap["string_filter_values"] != nil { + stringFilterValues := []string{} + for _, stringFilterValuesItem := range modelMap["string_filter_values"].([]interface{}) { + stringFilterValues = append(stringFilterValues, stringFilterValuesItem.(string)) + } + model.StringFilterValues = stringFilterValues + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerExportReportMapToRangeFilterParams(modelMap map[string]interface{}) (*backuprecoveryv1.RangeFilterParams, error) { + model := &backuprecoveryv1.RangeFilterParams{} + if modelMap["lower_bound"] != nil { + model.LowerBound = core.Int64Ptr(int64(modelMap["lower_bound"].(int))) + } + if modelMap["upper_bound"] != nil { + model.UpperBound = core.Int64Ptr(int64(modelMap["upper_bound"].(int))) + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerExportReportMapToSystemsFilterParams(modelMap map[string]interface{}) (*backuprecoveryv1.SystemsFilterParams, error) { + model := &backuprecoveryv1.SystemsFilterParams{} + systemIds := []string{} + for _, systemIdsItem := range modelMap["system_ids"].([]interface{}) { + systemIds = append(systemIds, systemIdsItem.(string)) + } + model.SystemIds = systemIds + if modelMap["system_names"] != nil { + systemNames := []string{} + for _, systemNamesItem := range modelMap["system_names"].([]interface{}) { + systemNames = append(systemNames, systemNamesItem.(string)) + } + model.SystemNames = systemNames + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerExportReportMapToTimeRangeFilterParams(modelMap map[string]interface{}) (*backuprecoveryv1.TimeRangeFilterParams, error) { + model := &backuprecoveryv1.TimeRangeFilterParams{} + if modelMap["date_range"] != nil && modelMap["date_range"].(string) != "" { + model.DateRange = core.StringPtr(modelMap["date_range"].(string)) + } + if modelMap["duration_hours"] != nil { + model.DurationHours = core.Int64Ptr(int64(modelMap["duration_hours"].(int))) + } + if modelMap["lower_bound"] != nil { + model.LowerBound = core.Int64Ptr(int64(modelMap["lower_bound"].(int))) + } + if modelMap["upper_bound"] != nil { + model.UpperBound = core.Int64Ptr(int64(modelMap["upper_bound"].(int))) + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerExportReportAttributeFilterToMap(model *backuprecoveryv1.AttributeFilter) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute"] = *model.Attribute + modelMap["filter_type"] = *model.FilterType + if model.InFilterParams != nil { + inFilterParamsMap, err := ResourceIbmBackupRecoveryManagerExportReportInFilterParamsToMap(model.InFilterParams) + if err != nil { + return modelMap, err + } + modelMap["in_filter_params"] = []map[string]interface{}{inFilterParamsMap} + } + if model.RangeFilterParams != nil { + rangeFilterParamsMap, err := ResourceIbmBackupRecoveryManagerExportReportRangeFilterParamsToMap(model.RangeFilterParams) + if err != nil { + return modelMap, err + } + modelMap["range_filter_params"] = []map[string]interface{}{rangeFilterParamsMap} + } + if model.SystemsFilterParams != nil { + systemsFilterParamsMap, err := ResourceIbmBackupRecoveryManagerExportReportSystemsFilterParamsToMap(model.SystemsFilterParams) + if err != nil { + return modelMap, err + } + modelMap["systems_filter_params"] = []map[string]interface{}{systemsFilterParamsMap} + } + if model.TimeRangeFilterParams != nil { + timeRangeFilterParamsMap, err := ResourceIbmBackupRecoveryManagerExportReportTimeRangeFilterParamsToMap(model.TimeRangeFilterParams) + if err != nil { + return modelMap, err + } + modelMap["time_range_filter_params"] = []map[string]interface{}{timeRangeFilterParamsMap} + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerExportReportInFilterParamsToMap(model *backuprecoveryv1.InFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute_data_type"] = *model.AttributeDataType + if model.AttributeLabels != nil { + modelMap["attribute_labels"] = model.AttributeLabels + } + if model.BoolFilterValues != nil { + modelMap["bool_filter_values"] = model.BoolFilterValues + } + if model.Int32FilterValues != nil { + modelMap["int32_filter_values"] = model.Int32FilterValues + } + if model.Int64FilterValues != nil { + modelMap["int64_filter_values"] = model.Int64FilterValues + } + if model.StringFilterValues != nil { + modelMap["string_filter_values"] = model.StringFilterValues + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerExportReportRangeFilterParamsToMap(model *backuprecoveryv1.RangeFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.LowerBound != nil { + modelMap["lower_bound"] = flex.IntValue(model.LowerBound) + } + if model.UpperBound != nil { + modelMap["upper_bound"] = flex.IntValue(model.UpperBound) + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerExportReportSystemsFilterParamsToMap(model *backuprecoveryv1.SystemsFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["system_ids"] = model.SystemIds + if model.SystemNames != nil { + modelMap["system_names"] = model.SystemNames + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerExportReportTimeRangeFilterParamsToMap(model *backuprecoveryv1.TimeRangeFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.DateRange != nil { + modelMap["date_range"] = *model.DateRange + } + if model.DurationHours != nil { + modelMap["duration_hours"] = flex.IntValue(model.DurationHours) + } + if model.LowerBound != nil { + modelMap["lower_bound"] = flex.IntValue(model.LowerBound) + } + if model.UpperBound != nil { + modelMap["upper_bound"] = flex.IntValue(model.UpperBound) + } + return modelMap, nil +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_export_report_test.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_export_report_test.go new file mode 100644 index 0000000000..85e4cc3d7d --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_export_report_test.go @@ -0,0 +1,46 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.105.1-067d600b-20250616-154447 + */ + +package backuprecovery_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" +) + +func TestAccIbmBackupRecoveryManagerExportReportDataSourceBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckIbmBackupRecoveryManagerExportReportDataSourceConfigBasic(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_export_report.backup_recovery_manager_export_report_instance", "id"), + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_export_report.backup_recovery_manager_export_report_instance", "backup_recovery_manager_export_report_id"), + ), + }, + }, + }) +} + +func testAccCheckIbmBackupRecoveryManagerExportReportDataSourceConfigBasic() string { + return fmt.Sprintf(` + data "ibm_backup_recovery_manager_export_report" "backup_recovery_manager_export_report_instance" { + id = "id" + async = true + filters = [ { attribute="attribute", filter_type="In", in_filter_params={ attribute_data_type="Bool", attribute_labels=[ "attributeLabels" ], bool_filter_values=[ true ], int32_filter_values=[ 1 ], int64_filter_values=[ 1 ], string_filter_values=[ "stringFilterValues" ] }, range_filter_params={ lower_bound=1, upper_bound=1 }, systems_filter_params={ system_ids=[ "systemIds" ], system_names=[ "systemNames" ] }, time_range_filter_params={ date_range="Last1Hour", duration_hours=1, lower_bound=1, upper_bound=1 } } ] + layout = "layout" + reportFormat = "XLS" + timezone = "timezone" + } + `) +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_component.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_component.go new file mode 100644 index 0000000000..69b016235c --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_component.go @@ -0,0 +1,622 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.96.1-5136e54a-20241108-203028 + */ + +package backuprecovery + +import ( + "context" + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" +) + +func DataSourceIbmBackupRecoveryManagerGetComponent() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceIbmBackupRecoveryManagerGetComponentRead, + + Schema: map[string]*schema.Schema{ + "component_id": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Description: "Specifies the id of the report component.", + }, + "aggs": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the aggregation related information that needs to be applied on the attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "aggregated_attributes": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of aggregation properties to be applied on the attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "aggregation_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the aggregation type.", + }, + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the attribute name.", + }, + "label": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the label to be generated for this aggregated attribute. If not specified, then by default label of the column in the output will be combination of aggregation type and attribute.", + }, + }, + }, + }, + "grouped_attributes": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of attributes to be grouped in the aggregation.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "config": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the configuration parameters to customize and format the columns in the report artifacts like excel, pdf etc.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "xlsx_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the configuration parameters to customize a component in excel report.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_config": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies customized configuration for the attributes in the report. If not specified, all the attributes will be sent as-is to the report without any formatting.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the attribute.", + }, + "custom_label": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies a custom label for attribute to appear in the xlsx report. If not specified, default attribute name will be used.", + }, + "format": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies a custom format for attribute to appear in the xlsx report. If not specified, the attribute value is sent as-is.", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + "data": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the data returned after evaluating the component.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "description": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies description of the Component.", + }, + "filters": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the filters that are applied on specific report type attributes in order to compose this component.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the attribute.", + }, + "filter_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the type of the filter that needs to be applied.", + }, + "in_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the in filter that are applied on attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_data_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the data type of the attribute.", + }, + "attribute_labels": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the optional label values for the attribute.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "bool_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of boolean values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeBool, + }, + }, + "int32_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of int32 values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "int64_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of int64 values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "string_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of string values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "range_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the filters that are applied on attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "lower_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the lower bound value. If specified, all the results which are greater than this value will be returned.", + }, + "upper_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the upper bound value. If specified, all the results which are lesser than this value will be returned.", + }, + }, + }, + }, + "systems_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the systems filter. Specifying this will pre filter all the results provided list of system identifier before applying aggregations.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "system_ids": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies an array of system identifiers. System identifiers may be of format clusterid:clusterincarnationid or a regionid (applicable only in case of DMaaS).", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "system_names": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the optional system names labels.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "time_range_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the time range filter. Specifying this will pre filter all the results on necessary resources like Protection Runs etc before applying aggregations. Currently, maximum allowed time range is 60 days.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "date_range": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Enum value for specifying the date range for a time filter. Considered only if lowerBound and upperBound are empty.", + }, + "duration_hours": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the duration preceding the current time for which the data must be fetch i.e fetch data between currentTime and currentTime - durationHours. This filter is only considered if neither upperBound, lowerBound or dateRange is specified.", + }, + "lower_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the lower bound value. If specified, all the results which are greater than this value will be returned.", + }, + "upper_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the upper bound value. If specified, all the results which are lesser than this value will be returned.", + }, + }, + }, + }, + }, + }, + }, + "limit": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the parameters to limit the resulting dataset.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "from": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the offset to which resulting data will be skipped before applying the size parameter. For example if dataset size is 10 objects, from=2 and size=5, then from 10 objects only 5 objects are returned starting from offset 2 i.e., 2 to 7. If not specified, then none of the objects are skipped.", + }, + "size": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the number of objects to be returned from the offset specified.", + }, + }, + }, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the Component.", + }, + "report_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the report type on top of which this Component is created from.", + }, + "sort": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the sorting (ordering) parameters to be applied to the resulting data.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the attribute.", + }, + "desc": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + Description: "Specifies whether the sorting order should be descending. Default value is false.", + }, + }, + }, + }, + }, + } +} + +func dataSourceIbmBackupRecoveryManagerGetComponentRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + heliosReportingServiceApIsClient, err := meta.(conns.ClientSession).BackupRecoveryManagerV1() + if err != nil { + tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_component", "read", "initialize-client") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + getComponentByIdOptions := &backuprecoveryv1.GetComponentByIdOptions{} + + getComponentByIdOptions.SetID(d.Get("component_id").(string)) + + component, _, err := heliosReportingServiceApIsClient.GetComponentByIDWithContext(context, getComponentByIdOptions) + if err != nil { + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetComponentByIDWithContext failed: %s", err.Error()), "(Data) ibm_backup_recovery_manager_get_component", "read") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + d.SetId(*component.ID) + + if !core.IsNil(component.Aggs) { + aggs := []map[string]interface{}{} + aggsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentAttributeAggregationsToMap(component.Aggs) + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_component", "read", "aggs-to-map").GetDiag() + } + aggs = append(aggs, aggsMap) + if err = d.Set("aggs", aggs); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting aggs: %s", err), "(Data) ibm_backup_recovery_manager_get_component", "read", "set-aggs").GetDiag() + } + } + + if !core.IsNil(component.Config) { + config := []map[string]interface{}{} + configMap, err := DataSourceIbmBackupRecoveryManagerGetComponentCustomConfigParamsToMap(component.Config) + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_component", "read", "config-to-map").GetDiag() + } + config = append(config, configMap) + if err = d.Set("config", config); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting config: %s", err), "(Data) ibm_backup_recovery_manager_get_component", "read", "set-config").GetDiag() + } + } + + if !core.IsNil(component.Data) { + data := []map[string]interface{}{} + for _, dataItem := range component.Data { + data = append(data, dataItem) + } + if err = d.Set("data", data); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting data: %s", err), "(Data) ibm_backup_recovery_manager_get_component", "read", "set-data").GetDiag() + } + } + + if !core.IsNil(component.Description) { + if err = d.Set("description", component.Description); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting description: %s", err), "(Data) ibm_backup_recovery_manager_get_component", "read", "set-description").GetDiag() + } + } + + if !core.IsNil(component.Filters) { + filters := []map[string]interface{}{} + for _, filtersItem := range component.Filters { + filtersItemMap, err := DataSourceIbmBackupRecoveryManagerGetComponentAttributeFilterToMap(&filtersItem) // #nosec G601 + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_component", "read", "filters-to-map").GetDiag() + } + filters = append(filters, filtersItemMap) + } + if err = d.Set("filters", filters); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting filters: %s", err), "(Data) ibm_backup_recovery_manager_get_component", "read", "set-filters").GetDiag() + } + } + + if !core.IsNil(component.Limit) { + limit := []map[string]interface{}{} + limitMap, err := DataSourceIbmBackupRecoveryManagerGetComponentLimitParamsToMap(component.Limit) + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_component", "read", "limit-to-map").GetDiag() + } + limit = append(limit, limitMap) + if err = d.Set("limit", limit); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting limit: %s", err), "(Data) ibm_backup_recovery_manager_get_component", "read", "set-limit").GetDiag() + } + } + + if !core.IsNil(component.Name) { + if err = d.Set("name", component.Name); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting name: %s", err), "(Data) ibm_backup_recovery_manager_get_component", "read", "set-name").GetDiag() + } + } + + if !core.IsNil(component.ReportType) { + if err = d.Set("report_type", component.ReportType); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting report_type: %s", err), "(Data) ibm_backup_recovery_manager_get_component", "read", "set-report_type").GetDiag() + } + } + + if !core.IsNil(component.Sort) { + sort := []map[string]interface{}{} + for _, sortItem := range component.Sort { + sortItemMap, err := DataSourceIbmBackupRecoveryManagerGetComponentAttributeSortToMap(&sortItem) // #nosec G601 + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_component", "read", "sort-to-map").GetDiag() + } + sort = append(sort, sortItemMap) + } + if err = d.Set("sort", sort); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting sort: %s", err), "(Data) ibm_backup_recovery_manager_get_component", "read", "set-sort").GetDiag() + } + } + + return nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentAttributeAggregationsToMap(model *backuprecoveryv1.AttributeAggregations) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.AggregatedAttributes != nil { + aggregatedAttributes := []map[string]interface{}{} + for _, aggregatedAttributesItem := range model.AggregatedAttributes { + aggregatedAttributesItemMap, err := DataSourceIbmBackupRecoveryManagerGetComponentAggregatedAttributesParamsToMap(&aggregatedAttributesItem) // #nosec G601 + if err != nil { + return modelMap, err + } + aggregatedAttributes = append(aggregatedAttributes, aggregatedAttributesItemMap) + } + modelMap["aggregated_attributes"] = aggregatedAttributes + } + if model.GroupedAttributes != nil { + modelMap["grouped_attributes"] = model.GroupedAttributes + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentAggregatedAttributesParamsToMap(model *backuprecoveryv1.AggregatedAttributesParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["aggregation_type"] = *model.AggregationType + modelMap["attribute"] = *model.Attribute + if model.Label != nil { + modelMap["label"] = *model.Label + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentCustomConfigParamsToMap(model *backuprecoveryv1.CustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.XlsxParams != nil { + xlsxParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentXlsxCustomConfigParamsToMap(model.XlsxParams) + if err != nil { + return modelMap, err + } + modelMap["xlsx_params"] = []map[string]interface{}{xlsxParamsMap} + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentXlsxCustomConfigParamsToMap(model *backuprecoveryv1.XlsxCustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.AttributeConfig != nil { + attributeConfig := []map[string]interface{}{} + for _, attributeConfigItem := range model.AttributeConfig { + attributeConfigItemMap, err := DataSourceIbmBackupRecoveryManagerGetComponentXlsxAttributeCustomConfigParamsToMap(&attributeConfigItem) // #nosec G601 + if err != nil { + return modelMap, err + } + attributeConfig = append(attributeConfig, attributeConfigItemMap) + } + modelMap["attribute_config"] = attributeConfig + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentXlsxAttributeCustomConfigParamsToMap(model *backuprecoveryv1.XlsxAttributeCustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute_name"] = *model.AttributeName + if model.CustomLabel != nil { + modelMap["custom_label"] = *model.CustomLabel + } + if model.Format != nil { + modelMap["format"] = *model.Format + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentAttributeFilterToMap(model *backuprecoveryv1.AttributeFilter) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute"] = *model.Attribute + modelMap["filter_type"] = *model.FilterType + if model.InFilterParams != nil { + inFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentInFilterParamsToMap(model.InFilterParams) + if err != nil { + return modelMap, err + } + modelMap["in_filter_params"] = []map[string]interface{}{inFilterParamsMap} + } + if model.RangeFilterParams != nil { + rangeFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentRangeFilterParamsToMap(model.RangeFilterParams) + if err != nil { + return modelMap, err + } + modelMap["range_filter_params"] = []map[string]interface{}{rangeFilterParamsMap} + } + if model.SystemsFilterParams != nil { + systemsFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentSystemsFilterParamsToMap(model.SystemsFilterParams) + if err != nil { + return modelMap, err + } + modelMap["systems_filter_params"] = []map[string]interface{}{systemsFilterParamsMap} + } + if model.TimeRangeFilterParams != nil { + timeRangeFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentTimeRangeFilterParamsToMap(model.TimeRangeFilterParams) + if err != nil { + return modelMap, err + } + modelMap["time_range_filter_params"] = []map[string]interface{}{timeRangeFilterParamsMap} + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentInFilterParamsToMap(model *backuprecoveryv1.InFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute_data_type"] = *model.AttributeDataType + if model.AttributeLabels != nil { + modelMap["attribute_labels"] = model.AttributeLabels + } + if model.BoolFilterValues != nil { + modelMap["bool_filter_values"] = model.BoolFilterValues + } + if model.Int32FilterValues != nil { + modelMap["int32_filter_values"] = model.Int32FilterValues + } + if model.Int64FilterValues != nil { + modelMap["int64_filter_values"] = model.Int64FilterValues + } + if model.StringFilterValues != nil { + modelMap["string_filter_values"] = model.StringFilterValues + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentRangeFilterParamsToMap(model *backuprecoveryv1.RangeFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.LowerBound != nil { + modelMap["lower_bound"] = flex.IntValue(model.LowerBound) + } + if model.UpperBound != nil { + modelMap["upper_bound"] = flex.IntValue(model.UpperBound) + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentSystemsFilterParamsToMap(model *backuprecoveryv1.SystemsFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["system_ids"] = model.SystemIds + if model.SystemNames != nil { + modelMap["system_names"] = model.SystemNames + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentTimeRangeFilterParamsToMap(model *backuprecoveryv1.TimeRangeFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.DateRange != nil { + modelMap["date_range"] = *model.DateRange + } + if model.DurationHours != nil { + modelMap["duration_hours"] = flex.IntValue(model.DurationHours) + } + if model.LowerBound != nil { + modelMap["lower_bound"] = flex.IntValue(model.LowerBound) + } + if model.UpperBound != nil { + modelMap["upper_bound"] = flex.IntValue(model.UpperBound) + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentLimitParamsToMap(model *backuprecoveryv1.LimitParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.From != nil { + modelMap["from"] = flex.IntValue(model.From) + } + modelMap["size"] = flex.IntValue(model.Size) + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentAttributeSortToMap(model *backuprecoveryv1.AttributeSort) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute"] = *model.Attribute + if model.Desc != nil { + modelMap["desc"] = *model.Desc + } + return modelMap, nil +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_component_preview.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_component_preview.go new file mode 100644 index 0000000000..6e58f77a3a --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_component_preview.go @@ -0,0 +1,1234 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.105.1-067d600b-20250616-154447 + */ + +package backuprecovery + +import ( + "context" + "fmt" + "log" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" +) + +func DataSourceIbmBackupRecoveryManagerGetComponentPreview() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceIbmBackupRecoveryManagerGetComponentPreviewRead, + + Schema: map[string]*schema.Schema{ + "component_id": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Description: "Specifies the id of the component.", + }, + "filters": &schema.Schema{ + Type: schema.TypeList, + Optional: true, + Description: "Specifies list of global filters that are applicable to given components in the report.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the attribute.", + }, + "filter_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the type of the filter that needs to be applied.", + }, + "in_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the in filter that are applied on attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_data_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the data type of the attribute.", + }, + "attribute_labels": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the optional label values for the attribute.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "bool_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of boolean values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeBool, + }, + }, + "int32_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of int32 values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "int64_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of int64 values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "string_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of string values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "range_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the filters that are applied on attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "lower_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the lower bound value. If specified, all the results which are greater than this value will be returned.", + }, + "upper_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the upper bound value. If specified, all the results which are lesser than this value will be returned.", + }, + }, + }, + }, + "systems_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the systems filter. Specifying this will pre filter all the results provided list of system identifier before applying aggregations.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "system_ids": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies an array of system identifiers. System identifiers may be of format clusterid:clusterincarnationid or a regionid (applicable only in case of DMaaS).", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "system_names": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the optional system names labels.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "time_range_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the time range filter. Specifying this will pre filter all the results on necessary resources like Protection Runs etc before applying aggregations. Currently, maximum allowed time range is 60 days.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "date_range": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Enum value for specifying the date range for a time filter. Considered only if lowerBound and upperBound are empty.", + }, + "duration_hours": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the duration preceding the current time for which the data must be fetch i.e fetch data between currentTime and currentTime - durationHours. This filter is only considered if neither upperBound, lowerBound or dateRange is specified.", + }, + "lower_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the lower bound value. If specified, all the results which are greater than this value will be returned.", + }, + "upper_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the upper bound value. If specified, all the results which are lesser than this value will be returned.", + }, + }, + }, + }, + }, + }, + }, + "sort": &schema.Schema{ + Type: schema.TypeList, + Optional: true, + Description: "Specifies the sorting (ordering) parameters to be applied to the resulting data.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the attribute.", + }, + "desc": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + Description: "Specifies whether the sorting order should be descending. Default value is false.", + }, + }, + }, + }, + "timezone": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Description: "Specifies timezone of the user. If nil, defaults to UTC. The time specified should be a location name in the IANA Time Zone database, for example, 'America/Los_Angeles'.", + }, + "component": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies a Report Component.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "aggs": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the aggregation related information that needs to be applied on the attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "aggregated_attributes": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of aggregation properties to be applied on the attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "aggregation_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the aggregation type.", + }, + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the attribute name.", + }, + "label": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the label to be generated for this aggregated attribute. If not specified, then by default label of the column in the output will be combination of aggregation type and attribute.", + }, + }, + }, + }, + "grouped_attributes": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of attributes to be grouped in the aggregation.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "config": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the configuration parameters to customize and format the columns in the report artifacts like excel, pdf etc.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "xlsx_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the configuration parameters to customize a component in excel report.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_config": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies customized configuration for the attributes in the report. If not specified, all the attributes will be sent as-is to the report without any formatting.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the attribute.", + }, + "custom_label": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies a custom label for attribute to appear in the xlsx report. If not specified, default attribute name will be used.", + }, + "format": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies a custom format for attribute to appear in the xlsx report. If not specified, the attribute value is sent as-is.", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + "data": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the data returned after evaluating the component.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "description": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies description of the Component.", + }, + "filters": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the filters that are applied on specific report type attributes in order to compose this component.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the attribute.", + }, + "filter_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the type of the filter that needs to be applied.", + }, + "in_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the in filter that are applied on attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_data_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the data type of the attribute.", + }, + "attribute_labels": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the optional label values for the attribute.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "bool_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of boolean values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeBool, + }, + }, + "int32_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of int32 values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "int64_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of int64 values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "string_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of string values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "range_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the filters that are applied on attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "lower_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the lower bound value. If specified, all the results which are greater than this value will be returned.", + }, + "upper_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the upper bound value. If specified, all the results which are lesser than this value will be returned.", + }, + }, + }, + }, + "systems_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the systems filter. Specifying this will pre filter all the results provided list of system identifier before applying aggregations.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "system_ids": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies an array of system identifiers. System identifiers may be of format clusterid:clusterincarnationid or a regionid (applicable only in case of DMaaS).", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "system_names": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the optional system names labels.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "time_range_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the time range filter. Specifying this will pre filter all the results on necessary resources like Protection Runs etc before applying aggregations. Currently, maximum allowed time range is 60 days.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "date_range": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Enum value for specifying the date range for a time filter. Considered only if lowerBound and upperBound are empty.", + }, + "duration_hours": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the duration preceding the current time for which the data must be fetch i.e fetch data between currentTime and currentTime - durationHours. This filter is only considered if neither upperBound, lowerBound or dateRange is specified.", + }, + "lower_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the lower bound value. If specified, all the results which are greater than this value will be returned.", + }, + "upper_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the upper bound value. If specified, all the results which are lesser than this value will be returned.", + }, + }, + }, + }, + }, + }, + }, + "id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the id of the Component.", + }, + "limit": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the parameters to limit the resulting dataset.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "from": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the offset to which resulting data will be skipped before applying the size parameter. For example if dataset size is 10 objects, from=2 and size=5, then from 10 objects only 5 objects are returned starting from offset 2 i.e., 2 to 7. If not specified, then none of the objects are skipped.", + }, + "size": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the number of objects to be returned from the offset specified.", + }, + }, + }, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the Component.", + }, + "report_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the report type on top of which this Component is created from.", + }, + "sort": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the sorting (ordering) parameters to be applied to the resulting data.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the attribute.", + }, + "desc": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + Description: "Specifies whether the sorting order should be descending. Default value is false.", + }, + }, + }, + }, + }, + }, + }, + "generated_timestamp_usecs": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the epoch timestamp in UTC in microseconds.", + }, + "last_refresh_timestamp_usecs": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the last refresh timestamp of data used to evaluate the component. If this parameter is not returned, then 'generatedTimestampUsecs' can be used for last refreshed timestamp of the data. This is epoch timestamp in UTC in microseconds.", + }, + }, + } +} + +func dataSourceIbmBackupRecoveryManagerGetComponentPreviewRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + backupRecoveryClient, err := meta.(conns.ClientSession).BackupRecoveryManagerV1() + if err != nil { + tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_component_preview", "read", "initialize-client") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + getComponentPreviewOptions := &backuprecoveryv1.GetComponentPreviewOptions{} + + getComponentPreviewOptions.SetID(d.Get("component_id").(string)) + + if _, ok := d.GetOk("filters"); ok { + var filters []backuprecoveryv1.AttributeFilter + for _, v := range d.Get("filters").([]interface{}) { + value := v.(map[string]interface{}) + filtersItem, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewMapToAttributeFilter(value) + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_component_preview", "read", "parse-filters").GetDiag() + } + filters = append(filters, *filtersItem) + } + getComponentPreviewOptions.SetFilters(filters) + } + if _, ok := d.GetOk("limit"); ok { + limitModel, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewMapToLimitParams(d.Get("limit.0").(map[string]interface{})) + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "ibm_backup_recovery_manager_get_component_preview", "read", "parse-limit").GetDiag() + } + getComponentPreviewOptions.SetLimit(limitModel) + } + if _, ok := d.GetOk("sort"); ok { + var sort []backuprecoveryv1.AttributeSort + for _, v := range d.Get("sort").([]interface{}) { + value := v.(map[string]interface{}) + sortItem, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewMapToAttributeSort(value) + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_component_preview", "read", "parse-sort").GetDiag() + } + sort = append(sort, *sortItem) + } + getComponentPreviewOptions.SetSort(sort) + } + if _, ok := d.GetOk("timezone"); ok { + getComponentPreviewOptions.SetTimezone(d.Get("timezone").(string)) + } + + componentPreview, _, err := backupRecoveryClient.GetComponentPreviewWithContext(context, getComponentPreviewOptions) + if err != nil { + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetComponentPreviewWithContext failed: %s", err.Error()), "(Data) ibm_backup_recovery_manager_get_component_preview", "read") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + d.SetId(dataSourceIbmBackupRecoveryManagerGetComponentPreviewID(d)) + + if !core.IsNil(componentPreview.Component) { + component := []map[string]interface{}{} + componentMap, err := DataSourceIbmBackupRecoveryManagerGetComponentPreviewComponentToMap(componentPreview.Component) + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_component_preview", "read", "component-to-map").GetDiag() + } + component = append(component, componentMap) + if err = d.Set("component", component); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting component: %s", err), "(Data) ibm_backup_recovery_manager_get_component_preview", "read", "set-component").GetDiag() + } + } + + if !core.IsNil(componentPreview.GeneratedTimestampUsecs) { + if err = d.Set("generated_timestamp_usecs", flex.IntValue(componentPreview.GeneratedTimestampUsecs)); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting generated_timestamp_usecs: %s", err), "(Data) ibm_backup_recovery_manager_get_component_preview", "read", "set-generated_timestamp_usecs").GetDiag() + } + } + + if !core.IsNil(componentPreview.LastRefreshTimestampUsecs) { + if err = d.Set("last_refresh_timestamp_usecs", flex.IntValue(componentPreview.LastRefreshTimestampUsecs)); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting last_refresh_timestamp_usecs: %s", err), "(Data) ibm_backup_recovery_manager_get_component_preview", "read", "set-last_refresh_timestamp_usecs").GetDiag() + } + } + + return nil +} + +// dataSourceIbmBackupRecoveryManagerGetComponentPreviewID returns a reasonable ID for the list. +func dataSourceIbmBackupRecoveryManagerGetComponentPreviewID(d *schema.ResourceData) string { + return time.Now().UTC().String() +} + +func DataSourceIbmBackupRecoveryManagerGetComponentPreviewComponentToMap(model *backuprecoveryv1.Component) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.Aggs != nil { + aggsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentPreviewAttributeAggregationsToMap(model.Aggs) + if err != nil { + return modelMap, err + } + modelMap["aggs"] = []map[string]interface{}{aggsMap} + } + if model.Config != nil { + configMap, err := DataSourceIbmBackupRecoveryManagerGetComponentPreviewCustomConfigParamsToMap(model.Config) + if err != nil { + return modelMap, err + } + modelMap["config"] = []map[string]interface{}{configMap} + } + if model.Data != nil { + modelMap["data"] = model.Data + } + if model.Description != nil { + modelMap["description"] = *model.Description + } + if model.Filters != nil { + filters := []map[string]interface{}{} + for _, filtersItem := range model.Filters { + filtersItemMap, err := DataSourceIbmBackupRecoveryManagerGetComponentPreviewAttributeFilterToMap(&filtersItem) // #nosec G601 + if err != nil { + return modelMap, err + } + filters = append(filters, filtersItemMap) + } + modelMap["filters"] = filters + } + if model.ID != nil { + modelMap["id"] = *model.ID + } + if model.Limit != nil { + limitMap, err := DataSourceIbmBackupRecoveryManagerGetComponentPreviewLimitParamsToMap(model.Limit) + if err != nil { + return modelMap, err + } + modelMap["limit"] = []map[string]interface{}{limitMap} + } + if model.Name != nil { + modelMap["name"] = *model.Name + } + if model.ReportType != nil { + modelMap["report_type"] = *model.ReportType + } + if model.Sort != nil { + sort := []map[string]interface{}{} + for _, sortItem := range model.Sort { + sortItemMap, err := DataSourceIbmBackupRecoveryManagerGetComponentPreviewAttributeSortToMap(&sortItem) // #nosec G601 + if err != nil { + return modelMap, err + } + sort = append(sort, sortItemMap) + } + modelMap["sort"] = sort + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentPreviewAttributeAggregationsToMap(model *backuprecoveryv1.AttributeAggregations) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.AggregatedAttributes != nil { + aggregatedAttributes := []map[string]interface{}{} + for _, aggregatedAttributesItem := range model.AggregatedAttributes { + aggregatedAttributesItemMap, err := DataSourceIbmBackupRecoveryManagerGetComponentPreviewAggregatedAttributesParamsToMap(&aggregatedAttributesItem) // #nosec G601 + if err != nil { + return modelMap, err + } + aggregatedAttributes = append(aggregatedAttributes, aggregatedAttributesItemMap) + } + modelMap["aggregated_attributes"] = aggregatedAttributes + } + if model.GroupedAttributes != nil { + modelMap["grouped_attributes"] = model.GroupedAttributes + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentPreviewAggregatedAttributesParamsToMap(model *backuprecoveryv1.AggregatedAttributesParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["aggregation_type"] = *model.AggregationType + modelMap["attribute"] = *model.Attribute + if model.Label != nil { + modelMap["label"] = *model.Label + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentPreviewCustomConfigParamsToMap(model *backuprecoveryv1.CustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.XlsxParams != nil { + xlsxParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentPreviewXlsxCustomConfigParamsToMap(model.XlsxParams) + if err != nil { + return modelMap, err + } + modelMap["xlsx_params"] = []map[string]interface{}{xlsxParamsMap} + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentPreviewXlsxCustomConfigParamsToMap(model *backuprecoveryv1.XlsxCustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.AttributeConfig != nil { + attributeConfig := []map[string]interface{}{} + for _, attributeConfigItem := range model.AttributeConfig { + attributeConfigItemMap, err := DataSourceIbmBackupRecoveryManagerGetComponentPreviewXlsxAttributeCustomConfigParamsToMap(&attributeConfigItem) // #nosec G601 + if err != nil { + return modelMap, err + } + attributeConfig = append(attributeConfig, attributeConfigItemMap) + } + modelMap["attribute_config"] = attributeConfig + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentPreviewXlsxAttributeCustomConfigParamsToMap(model *backuprecoveryv1.XlsxAttributeCustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute_name"] = *model.AttributeName + if model.CustomLabel != nil { + modelMap["custom_label"] = *model.CustomLabel + } + if model.Format != nil { + modelMap["format"] = *model.Format + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentPreviewAttributeFilterToMap(model *backuprecoveryv1.AttributeFilter) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute"] = *model.Attribute + modelMap["filter_type"] = *model.FilterType + if model.InFilterParams != nil { + inFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentPreviewInFilterParamsToMap(model.InFilterParams) + if err != nil { + return modelMap, err + } + modelMap["in_filter_params"] = []map[string]interface{}{inFilterParamsMap} + } + if model.RangeFilterParams != nil { + rangeFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentPreviewRangeFilterParamsToMap(model.RangeFilterParams) + if err != nil { + return modelMap, err + } + modelMap["range_filter_params"] = []map[string]interface{}{rangeFilterParamsMap} + } + if model.SystemsFilterParams != nil { + systemsFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentPreviewSystemsFilterParamsToMap(model.SystemsFilterParams) + if err != nil { + return modelMap, err + } + modelMap["systems_filter_params"] = []map[string]interface{}{systemsFilterParamsMap} + } + if model.TimeRangeFilterParams != nil { + timeRangeFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentPreviewTimeRangeFilterParamsToMap(model.TimeRangeFilterParams) + if err != nil { + return modelMap, err + } + modelMap["time_range_filter_params"] = []map[string]interface{}{timeRangeFilterParamsMap} + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentPreviewInFilterParamsToMap(model *backuprecoveryv1.InFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute_data_type"] = *model.AttributeDataType + if model.AttributeLabels != nil { + modelMap["attribute_labels"] = model.AttributeLabels + } + if model.BoolFilterValues != nil { + modelMap["bool_filter_values"] = model.BoolFilterValues + } + if model.Int32FilterValues != nil { + modelMap["int32_filter_values"] = model.Int32FilterValues + } + if model.Int64FilterValues != nil { + modelMap["int64_filter_values"] = model.Int64FilterValues + } + if model.StringFilterValues != nil { + modelMap["string_filter_values"] = model.StringFilterValues + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentPreviewRangeFilterParamsToMap(model *backuprecoveryv1.RangeFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.LowerBound != nil { + modelMap["lower_bound"] = flex.IntValue(model.LowerBound) + } + if model.UpperBound != nil { + modelMap["upper_bound"] = flex.IntValue(model.UpperBound) + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentPreviewSystemsFilterParamsToMap(model *backuprecoveryv1.SystemsFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["system_ids"] = model.SystemIds + if model.SystemNames != nil { + modelMap["system_names"] = model.SystemNames + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentPreviewTimeRangeFilterParamsToMap(model *backuprecoveryv1.TimeRangeFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.DateRange != nil { + modelMap["date_range"] = *model.DateRange + } + if model.DurationHours != nil { + modelMap["duration_hours"] = flex.IntValue(model.DurationHours) + } + if model.LowerBound != nil { + modelMap["lower_bound"] = flex.IntValue(model.LowerBound) + } + if model.UpperBound != nil { + modelMap["upper_bound"] = flex.IntValue(model.UpperBound) + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentPreviewLimitParamsToMap(model *backuprecoveryv1.LimitParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.From != nil { + modelMap["from"] = flex.IntValue(model.From) + } + modelMap["size"] = flex.IntValue(model.Size) + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentPreviewAttributeSortToMap(model *backuprecoveryv1.AttributeSort) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute"] = *model.Attribute + if model.Desc != nil { + modelMap["desc"] = *model.Desc + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewMapToAttributeFilter(modelMap map[string]interface{}) (*backuprecoveryv1.AttributeFilter, error) { + model := &backuprecoveryv1.AttributeFilter{} + model.Attribute = core.StringPtr(modelMap["attribute"].(string)) + model.FilterType = core.StringPtr(modelMap["filter_type"].(string)) + if modelMap["in_filter_params"] != nil && len(modelMap["in_filter_params"].([]interface{})) > 0 { + InFilterParamsModel, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewMapToInFilterParams(modelMap["in_filter_params"].([]interface{})[0].(map[string]interface{})) + if err != nil { + return model, err + } + model.InFilterParams = InFilterParamsModel + } + if modelMap["range_filter_params"] != nil && len(modelMap["range_filter_params"].([]interface{})) > 0 { + RangeFilterParamsModel, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewMapToRangeFilterParams(modelMap["range_filter_params"].([]interface{})[0].(map[string]interface{})) + if err != nil { + return model, err + } + model.RangeFilterParams = RangeFilterParamsModel + } + if modelMap["systems_filter_params"] != nil && len(modelMap["systems_filter_params"].([]interface{})) > 0 { + SystemsFilterParamsModel, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewMapToSystemsFilterParams(modelMap["systems_filter_params"].([]interface{})[0].(map[string]interface{})) + if err != nil { + return model, err + } + model.SystemsFilterParams = SystemsFilterParamsModel + } + if modelMap["time_range_filter_params"] != nil && len(modelMap["time_range_filter_params"].([]interface{})) > 0 { + TimeRangeFilterParamsModel, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewMapToTimeRangeFilterParams(modelMap["time_range_filter_params"].([]interface{})[0].(map[string]interface{})) + if err != nil { + return model, err + } + model.TimeRangeFilterParams = TimeRangeFilterParamsModel + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewMapToInFilterParams(modelMap map[string]interface{}) (*backuprecoveryv1.InFilterParams, error) { + model := &backuprecoveryv1.InFilterParams{} + model.AttributeDataType = core.StringPtr(modelMap["attribute_data_type"].(string)) + if modelMap["attribute_labels"] != nil { + attributeLabels := []string{} + for _, attributeLabelsItem := range modelMap["attribute_labels"].([]interface{}) { + attributeLabels = append(attributeLabels, attributeLabelsItem.(string)) + } + model.AttributeLabels = attributeLabels + } + if modelMap["bool_filter_values"] != nil { + boolFilterValues := []bool{} + for _, boolFilterValuesItem := range modelMap["bool_filter_values"].([]interface{}) { + boolFilterValues = append(boolFilterValues, boolFilterValuesItem.(bool)) + } + model.BoolFilterValues = boolFilterValues + } + if modelMap["int32_filter_values"] != nil { + int32FilterValues := []int64{} + for _, int32FilterValuesItem := range modelMap["int32_filter_values"].([]interface{}) { + int32FilterValues = append(int32FilterValues, int64(int32FilterValuesItem.(int))) + } + model.Int32FilterValues = int32FilterValues + } + if modelMap["int64_filter_values"] != nil { + int64FilterValues := []int64{} + for _, int64FilterValuesItem := range modelMap["int64_filter_values"].([]interface{}) { + int64FilterValues = append(int64FilterValues, int64(int64FilterValuesItem.(int))) + } + model.Int64FilterValues = int64FilterValues + } + if modelMap["string_filter_values"] != nil { + stringFilterValues := []string{} + for _, stringFilterValuesItem := range modelMap["string_filter_values"].([]interface{}) { + stringFilterValues = append(stringFilterValues, stringFilterValuesItem.(string)) + } + model.StringFilterValues = stringFilterValues + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewMapToRangeFilterParams(modelMap map[string]interface{}) (*backuprecoveryv1.RangeFilterParams, error) { + model := &backuprecoveryv1.RangeFilterParams{} + if modelMap["lower_bound"] != nil { + model.LowerBound = core.Int64Ptr(int64(modelMap["lower_bound"].(int))) + } + if modelMap["upper_bound"] != nil { + model.UpperBound = core.Int64Ptr(int64(modelMap["upper_bound"].(int))) + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewMapToSystemsFilterParams(modelMap map[string]interface{}) (*backuprecoveryv1.SystemsFilterParams, error) { + model := &backuprecoveryv1.SystemsFilterParams{} + systemIds := []string{} + for _, systemIdsItem := range modelMap["system_ids"].([]interface{}) { + systemIds = append(systemIds, systemIdsItem.(string)) + } + model.SystemIds = systemIds + if modelMap["system_names"] != nil { + systemNames := []string{} + for _, systemNamesItem := range modelMap["system_names"].([]interface{}) { + systemNames = append(systemNames, systemNamesItem.(string)) + } + model.SystemNames = systemNames + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewMapToTimeRangeFilterParams(modelMap map[string]interface{}) (*backuprecoveryv1.TimeRangeFilterParams, error) { + model := &backuprecoveryv1.TimeRangeFilterParams{} + if modelMap["date_range"] != nil && modelMap["date_range"].(string) != "" { + model.DateRange = core.StringPtr(modelMap["date_range"].(string)) + } + if modelMap["duration_hours"] != nil { + model.DurationHours = core.Int64Ptr(int64(modelMap["duration_hours"].(int))) + } + if modelMap["lower_bound"] != nil { + model.LowerBound = core.Int64Ptr(int64(modelMap["lower_bound"].(int))) + } + if modelMap["upper_bound"] != nil { + model.UpperBound = core.Int64Ptr(int64(modelMap["upper_bound"].(int))) + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewMapToLimitParams(modelMap map[string]interface{}) (*backuprecoveryv1.LimitParams, error) { + model := &backuprecoveryv1.LimitParams{} + if modelMap["from"] != nil { + model.From = core.Int64Ptr(int64(modelMap["from"].(int))) + } + model.Size = core.Int64Ptr(int64(modelMap["size"].(int))) + return model, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewMapToAttributeSort(modelMap map[string]interface{}) (*backuprecoveryv1.AttributeSort, error) { + model := &backuprecoveryv1.AttributeSort{} + model.Attribute = core.StringPtr(modelMap["attribute"].(string)) + if modelMap["desc"] != nil { + model.Desc = core.BoolPtr(modelMap["desc"].(bool)) + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewAttributeFilterToMap(model *backuprecoveryv1.AttributeFilter) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute"] = *model.Attribute + modelMap["filter_type"] = *model.FilterType + if model.InFilterParams != nil { + inFilterParamsMap, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewInFilterParamsToMap(model.InFilterParams) + if err != nil { + return modelMap, err + } + modelMap["in_filter_params"] = []map[string]interface{}{inFilterParamsMap} + } + if model.RangeFilterParams != nil { + rangeFilterParamsMap, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewRangeFilterParamsToMap(model.RangeFilterParams) + if err != nil { + return modelMap, err + } + modelMap["range_filter_params"] = []map[string]interface{}{rangeFilterParamsMap} + } + if model.SystemsFilterParams != nil { + systemsFilterParamsMap, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewSystemsFilterParamsToMap(model.SystemsFilterParams) + if err != nil { + return modelMap, err + } + modelMap["systems_filter_params"] = []map[string]interface{}{systemsFilterParamsMap} + } + if model.TimeRangeFilterParams != nil { + timeRangeFilterParamsMap, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewTimeRangeFilterParamsToMap(model.TimeRangeFilterParams) + if err != nil { + return modelMap, err + } + modelMap["time_range_filter_params"] = []map[string]interface{}{timeRangeFilterParamsMap} + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewInFilterParamsToMap(model *backuprecoveryv1.InFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute_data_type"] = *model.AttributeDataType + if model.AttributeLabels != nil { + modelMap["attribute_labels"] = model.AttributeLabels + } + if model.BoolFilterValues != nil { + modelMap["bool_filter_values"] = model.BoolFilterValues + } + if model.Int32FilterValues != nil { + modelMap["int32_filter_values"] = model.Int32FilterValues + } + if model.Int64FilterValues != nil { + modelMap["int64_filter_values"] = model.Int64FilterValues + } + if model.StringFilterValues != nil { + modelMap["string_filter_values"] = model.StringFilterValues + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewRangeFilterParamsToMap(model *backuprecoveryv1.RangeFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.LowerBound != nil { + modelMap["lower_bound"] = flex.IntValue(model.LowerBound) + } + if model.UpperBound != nil { + modelMap["upper_bound"] = flex.IntValue(model.UpperBound) + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewSystemsFilterParamsToMap(model *backuprecoveryv1.SystemsFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["system_ids"] = model.SystemIds + if model.SystemNames != nil { + modelMap["system_names"] = model.SystemNames + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewTimeRangeFilterParamsToMap(model *backuprecoveryv1.TimeRangeFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.DateRange != nil { + modelMap["date_range"] = *model.DateRange + } + if model.DurationHours != nil { + modelMap["duration_hours"] = flex.IntValue(model.DurationHours) + } + if model.LowerBound != nil { + modelMap["lower_bound"] = flex.IntValue(model.LowerBound) + } + if model.UpperBound != nil { + modelMap["upper_bound"] = flex.IntValue(model.UpperBound) + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewComponentToMap(model *backuprecoveryv1.Component) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.Aggs != nil { + aggsMap, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewAttributeAggregationsToMap(model.Aggs) + if err != nil { + return modelMap, err + } + modelMap["aggs"] = []map[string]interface{}{aggsMap} + } + if model.Config != nil { + configMap, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewCustomConfigParamsToMap(model.Config) + if err != nil { + return modelMap, err + } + modelMap["config"] = []map[string]interface{}{configMap} + } + if model.Data != nil { + modelMap["data"] = model.Data + } + if model.Description != nil { + modelMap["description"] = *model.Description + } + if model.Filters != nil { + filters := []map[string]interface{}{} + for _, filtersItem := range model.Filters { + filtersItemMap, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewAttributeFilterToMap(&filtersItem) // #nosec G601 + if err != nil { + return modelMap, err + } + filters = append(filters, filtersItemMap) + } + modelMap["filters"] = filters + } + if model.ID != nil { + modelMap["id"] = *model.ID + } + if model.Limit != nil { + limitMap, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewLimitParamsToMap(model.Limit) + if err != nil { + return modelMap, err + } + modelMap["limit"] = []map[string]interface{}{limitMap} + } + if model.Name != nil { + modelMap["name"] = *model.Name + } + if model.ReportType != nil { + modelMap["report_type"] = *model.ReportType + } + if model.Sort != nil { + sort := []map[string]interface{}{} + for _, sortItem := range model.Sort { + sortItemMap, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewAttributeSortToMap(&sortItem) // #nosec G601 + if err != nil { + return modelMap, err + } + sort = append(sort, sortItemMap) + } + modelMap["sort"] = sort + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewAttributeAggregationsToMap(model *backuprecoveryv1.AttributeAggregations) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.AggregatedAttributes != nil { + aggregatedAttributes := []map[string]interface{}{} + for _, aggregatedAttributesItem := range model.AggregatedAttributes { + aggregatedAttributesItemMap, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewAggregatedAttributesParamsToMap(&aggregatedAttributesItem) // #nosec G601 + if err != nil { + return modelMap, err + } + aggregatedAttributes = append(aggregatedAttributes, aggregatedAttributesItemMap) + } + modelMap["aggregated_attributes"] = aggregatedAttributes + } + if model.GroupedAttributes != nil { + modelMap["grouped_attributes"] = model.GroupedAttributes + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewAggregatedAttributesParamsToMap(model *backuprecoveryv1.AggregatedAttributesParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["aggregation_type"] = *model.AggregationType + modelMap["attribute"] = *model.Attribute + if model.Label != nil { + modelMap["label"] = *model.Label + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewCustomConfigParamsToMap(model *backuprecoveryv1.CustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.XlsxParams != nil { + xlsxParamsMap, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewXlsxCustomConfigParamsToMap(model.XlsxParams) + if err != nil { + return modelMap, err + } + modelMap["xlsx_params"] = []map[string]interface{}{xlsxParamsMap} + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewXlsxCustomConfigParamsToMap(model *backuprecoveryv1.XlsxCustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.AttributeConfig != nil { + attributeConfig := []map[string]interface{}{} + for _, attributeConfigItem := range model.AttributeConfig { + attributeConfigItemMap, err := ResourceIbmBackupRecoveryManagerGetComponentPreviewXlsxAttributeCustomConfigParamsToMap(&attributeConfigItem) // #nosec G601 + if err != nil { + return modelMap, err + } + attributeConfig = append(attributeConfig, attributeConfigItemMap) + } + modelMap["attribute_config"] = attributeConfig + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewXlsxAttributeCustomConfigParamsToMap(model *backuprecoveryv1.XlsxAttributeCustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute_name"] = *model.AttributeName + if model.CustomLabel != nil { + modelMap["custom_label"] = *model.CustomLabel + } + if model.Format != nil { + modelMap["format"] = *model.Format + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewLimitParamsToMap(model *backuprecoveryv1.LimitParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.From != nil { + modelMap["from"] = flex.IntValue(model.From) + } + modelMap["size"] = flex.IntValue(model.Size) + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetComponentPreviewAttributeSortToMap(model *backuprecoveryv1.AttributeSort) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute"] = *model.Attribute + if model.Desc != nil { + modelMap["desc"] = *model.Desc + } + return modelMap, nil +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_component_preview_test.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_component_preview_test.go new file mode 100644 index 0000000000..90068be409 --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_component_preview_test.go @@ -0,0 +1,514 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.105.1-067d600b-20250616-154447 + */ + +package backuprecovery_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/service/backuprecovery" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" + "github.com/stretchr/testify/assert" +) + +func TestAccIbmBackupRecoveryManagerGetComponentPreviewDataSourceBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckIbmBackupRecoveryManagerGetComponentPreviewDataSourceConfigBasic(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_get_component_preview.backup_recovery_manager_get_component_preview_instance", "id"), + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_get_component_preview.backup_recovery_manager_get_component_preview_instance", "backup_recovery_manager_get_component_preview_id"), + ), + }, + }, + }) +} + +func testAccCheckIbmBackupRecoveryManagerGetComponentPreviewDataSourceConfigBasic() string { + return fmt.Sprintf(` + data "ibm_backup_recovery_manager_get_component_preview" "backup_recovery_manager_get_component_preview_instance" { + id = "id" + filters = [ { attribute="attribute", filter_type="In", in_filter_params={ attribute_data_type="Bool", attribute_labels=[ "attributeLabels" ], bool_filter_values=[ true ], int32_filter_values=[ 1 ], int64_filter_values=[ 1 ], string_filter_values=[ "stringFilterValues" ] }, range_filter_params={ lower_bound=1, upper_bound=1 }, systems_filter_params={ system_ids=[ "systemIds" ], system_names=[ "systemNames" ] }, time_range_filter_params={ date_range="Last1Hour", duration_hours=1, lower_bound=1, upper_bound=1 } } ] + sort = [ { attribute="attribute", desc=true } ] + timezone = "timezone" + } + `) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentPreviewComponentToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + aggregatedAttributesParamsModel := make(map[string]interface{}) + aggregatedAttributesParamsModel["aggregation_type"] = "sum" + aggregatedAttributesParamsModel["attribute"] = "testString" + aggregatedAttributesParamsModel["label"] = "testString" + + attributeAggregationsModel := make(map[string]interface{}) + attributeAggregationsModel["aggregated_attributes"] = []map[string]interface{}{aggregatedAttributesParamsModel} + attributeAggregationsModel["grouped_attributes"] = []string{"testString"} + + xlsxAttributeCustomConfigParamsModel := make(map[string]interface{}) + xlsxAttributeCustomConfigParamsModel["attribute_name"] = "testString" + xlsxAttributeCustomConfigParamsModel["custom_label"] = "testString" + xlsxAttributeCustomConfigParamsModel["format"] = "Timestamp" + + xlsxCustomConfigParamsModel := make(map[string]interface{}) + xlsxCustomConfigParamsModel["attribute_config"] = []map[string]interface{}{xlsxAttributeCustomConfigParamsModel} + + customConfigParamsModel := make(map[string]interface{}) + customConfigParamsModel["xlsx_params"] = []map[string]interface{}{xlsxCustomConfigParamsModel} + + inFilterParamsModel := make(map[string]interface{}) + inFilterParamsModel["attribute_data_type"] = "Bool" + inFilterParamsModel["attribute_labels"] = []string{"testString"} + inFilterParamsModel["bool_filter_values"] = []bool{true} + inFilterParamsModel["int32_filter_values"] = []int64{int64(38)} + inFilterParamsModel["int64_filter_values"] = []int64{int64(26)} + inFilterParamsModel["string_filter_values"] = []string{"testString"} + + rangeFilterParamsModel := make(map[string]interface{}) + rangeFilterParamsModel["lower_bound"] = int(26) + rangeFilterParamsModel["upper_bound"] = int(26) + + systemsFilterParamsModel := make(map[string]interface{}) + systemsFilterParamsModel["system_ids"] = []string{"testString"} + systemsFilterParamsModel["system_names"] = []string{"testString"} + + timeRangeFilterParamsModel := make(map[string]interface{}) + timeRangeFilterParamsModel["date_range"] = "Last1Hour" + timeRangeFilterParamsModel["duration_hours"] = int(26) + timeRangeFilterParamsModel["lower_bound"] = int(26) + timeRangeFilterParamsModel["upper_bound"] = int(26) + + attributeFilterModel := make(map[string]interface{}) + attributeFilterModel["attribute"] = "testString" + attributeFilterModel["filter_type"] = "In" + attributeFilterModel["in_filter_params"] = []map[string]interface{}{inFilterParamsModel} + attributeFilterModel["range_filter_params"] = []map[string]interface{}{rangeFilterParamsModel} + attributeFilterModel["systems_filter_params"] = []map[string]interface{}{systemsFilterParamsModel} + attributeFilterModel["time_range_filter_params"] = []map[string]interface{}{timeRangeFilterParamsModel} + + limitParamsModel := make(map[string]interface{}) + limitParamsModel["from"] = int(38) + limitParamsModel["size"] = int(1) + + attributeSortModel := make(map[string]interface{}) + attributeSortModel["attribute"] = "testString" + attributeSortModel["desc"] = true + + model := make(map[string]interface{}) + model["aggs"] = []map[string]interface{}{attributeAggregationsModel} + model["config"] = []map[string]interface{}{customConfigParamsModel} + model["data"] = []map[string]interface{}{map[string]interface{}{"anyKey": "anyValue"}} + model["description"] = "testString" + model["filters"] = []map[string]interface{}{attributeFilterModel} + model["id"] = "testString" + model["limit"] = []map[string]interface{}{limitParamsModel} + model["name"] = "testString" + model["report_type"] = "Failures" + model["sort"] = []map[string]interface{}{attributeSortModel} + + assert.Equal(t, result, model) + } + + aggregatedAttributesParamsModel := new(backuprecoveryv1.AggregatedAttributesParams) + aggregatedAttributesParamsModel.AggregationType = core.StringPtr("sum") + aggregatedAttributesParamsModel.Attribute = core.StringPtr("testString") + aggregatedAttributesParamsModel.Label = core.StringPtr("testString") + + attributeAggregationsModel := new(backuprecoveryv1.AttributeAggregations) + attributeAggregationsModel.AggregatedAttributes = []backuprecoveryv1.AggregatedAttributesParams{*aggregatedAttributesParamsModel} + attributeAggregationsModel.GroupedAttributes = []string{"testString"} + + xlsxAttributeCustomConfigParamsModel := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + xlsxAttributeCustomConfigParamsModel.AttributeName = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.CustomLabel = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.Format = core.StringPtr("Timestamp") + + xlsxCustomConfigParamsModel := new(backuprecoveryv1.XlsxCustomConfigParams) + xlsxCustomConfigParamsModel.AttributeConfig = []backuprecoveryv1.XlsxAttributeCustomConfigParams{*xlsxAttributeCustomConfigParamsModel} + + customConfigParamsModel := new(backuprecoveryv1.CustomConfigParams) + customConfigParamsModel.XlsxParams = xlsxCustomConfigParamsModel + + inFilterParamsModel := new(backuprecoveryv1.InFilterParams) + inFilterParamsModel.AttributeDataType = core.StringPtr("Bool") + inFilterParamsModel.AttributeLabels = []string{"testString"} + inFilterParamsModel.BoolFilterValues = []bool{true} + inFilterParamsModel.Int32FilterValues = []int64{int64(38)} + inFilterParamsModel.Int64FilterValues = []int64{int64(26)} + inFilterParamsModel.StringFilterValues = []string{"testString"} + + rangeFilterParamsModel := new(backuprecoveryv1.RangeFilterParams) + rangeFilterParamsModel.LowerBound = core.Int64Ptr(int64(26)) + rangeFilterParamsModel.UpperBound = core.Int64Ptr(int64(26)) + + systemsFilterParamsModel := new(backuprecoveryv1.SystemsFilterParams) + systemsFilterParamsModel.SystemIds = []string{"testString"} + systemsFilterParamsModel.SystemNames = []string{"testString"} + + timeRangeFilterParamsModel := new(backuprecoveryv1.TimeRangeFilterParams) + timeRangeFilterParamsModel.DateRange = core.StringPtr("Last1Hour") + timeRangeFilterParamsModel.DurationHours = core.Int64Ptr(int64(26)) + timeRangeFilterParamsModel.LowerBound = core.Int64Ptr(int64(26)) + timeRangeFilterParamsModel.UpperBound = core.Int64Ptr(int64(26)) + + attributeFilterModel := new(backuprecoveryv1.AttributeFilter) + attributeFilterModel.Attribute = core.StringPtr("testString") + attributeFilterModel.FilterType = core.StringPtr("In") + attributeFilterModel.InFilterParams = inFilterParamsModel + attributeFilterModel.RangeFilterParams = rangeFilterParamsModel + attributeFilterModel.SystemsFilterParams = systemsFilterParamsModel + attributeFilterModel.TimeRangeFilterParams = timeRangeFilterParamsModel + + limitParamsModel := new(backuprecoveryv1.LimitParams) + limitParamsModel.From = core.Int64Ptr(int64(38)) + limitParamsModel.Size = core.Int64Ptr(int64(1)) + + attributeSortModel := new(backuprecoveryv1.AttributeSort) + attributeSortModel.Attribute = core.StringPtr("testString") + attributeSortModel.Desc = core.BoolPtr(true) + + model := new(backuprecoveryv1.Component) + model.Aggs = attributeAggregationsModel + model.Config = customConfigParamsModel + model.Data = []map[string]interface{}{map[string]interface{}{"anyKey": "anyValue"}} + model.Description = core.StringPtr("testString") + model.Filters = []backuprecoveryv1.AttributeFilter{*attributeFilterModel} + model.ID = core.StringPtr("testString") + model.Limit = limitParamsModel + model.Name = core.StringPtr("testString") + model.ReportType = core.StringPtr("Failures") + model.Sort = []backuprecoveryv1.AttributeSort{*attributeSortModel} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentPreviewComponentToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentPreviewAttributeAggregationsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + aggregatedAttributesParamsModel := make(map[string]interface{}) + aggregatedAttributesParamsModel["aggregation_type"] = "sum" + aggregatedAttributesParamsModel["attribute"] = "testString" + aggregatedAttributesParamsModel["label"] = "testString" + + model := make(map[string]interface{}) + model["aggregated_attributes"] = []map[string]interface{}{aggregatedAttributesParamsModel} + model["grouped_attributes"] = []string{"testString"} + + assert.Equal(t, result, model) + } + + aggregatedAttributesParamsModel := new(backuprecoveryv1.AggregatedAttributesParams) + aggregatedAttributesParamsModel.AggregationType = core.StringPtr("sum") + aggregatedAttributesParamsModel.Attribute = core.StringPtr("testString") + aggregatedAttributesParamsModel.Label = core.StringPtr("testString") + + model := new(backuprecoveryv1.AttributeAggregations) + model.AggregatedAttributes = []backuprecoveryv1.AggregatedAttributesParams{*aggregatedAttributesParamsModel} + model.GroupedAttributes = []string{"testString"} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentPreviewAttributeAggregationsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentPreviewAggregatedAttributesParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["aggregation_type"] = "sum" + model["attribute"] = "testString" + model["label"] = "testString" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.AggregatedAttributesParams) + model.AggregationType = core.StringPtr("sum") + model.Attribute = core.StringPtr("testString") + model.Label = core.StringPtr("testString") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentPreviewAggregatedAttributesParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentPreviewCustomConfigParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + xlsxAttributeCustomConfigParamsModel := make(map[string]interface{}) + xlsxAttributeCustomConfigParamsModel["attribute_name"] = "testString" + xlsxAttributeCustomConfigParamsModel["custom_label"] = "testString" + xlsxAttributeCustomConfigParamsModel["format"] = "Timestamp" + + xlsxCustomConfigParamsModel := make(map[string]interface{}) + xlsxCustomConfigParamsModel["attribute_config"] = []map[string]interface{}{xlsxAttributeCustomConfigParamsModel} + + model := make(map[string]interface{}) + model["xlsx_params"] = []map[string]interface{}{xlsxCustomConfigParamsModel} + + assert.Equal(t, result, model) + } + + xlsxAttributeCustomConfigParamsModel := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + xlsxAttributeCustomConfigParamsModel.AttributeName = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.CustomLabel = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.Format = core.StringPtr("Timestamp") + + xlsxCustomConfigParamsModel := new(backuprecoveryv1.XlsxCustomConfigParams) + xlsxCustomConfigParamsModel.AttributeConfig = []backuprecoveryv1.XlsxAttributeCustomConfigParams{*xlsxAttributeCustomConfigParamsModel} + + model := new(backuprecoveryv1.CustomConfigParams) + model.XlsxParams = xlsxCustomConfigParamsModel + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentPreviewCustomConfigParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentPreviewXlsxCustomConfigParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + xlsxAttributeCustomConfigParamsModel := make(map[string]interface{}) + xlsxAttributeCustomConfigParamsModel["attribute_name"] = "testString" + xlsxAttributeCustomConfigParamsModel["custom_label"] = "testString" + xlsxAttributeCustomConfigParamsModel["format"] = "Timestamp" + + model := make(map[string]interface{}) + model["attribute_config"] = []map[string]interface{}{xlsxAttributeCustomConfigParamsModel} + + assert.Equal(t, result, model) + } + + xlsxAttributeCustomConfigParamsModel := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + xlsxAttributeCustomConfigParamsModel.AttributeName = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.CustomLabel = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.Format = core.StringPtr("Timestamp") + + model := new(backuprecoveryv1.XlsxCustomConfigParams) + model.AttributeConfig = []backuprecoveryv1.XlsxAttributeCustomConfigParams{*xlsxAttributeCustomConfigParamsModel} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentPreviewXlsxCustomConfigParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentPreviewXlsxAttributeCustomConfigParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["attribute_name"] = "testString" + model["custom_label"] = "testString" + model["format"] = "Timestamp" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + model.AttributeName = core.StringPtr("testString") + model.CustomLabel = core.StringPtr("testString") + model.Format = core.StringPtr("Timestamp") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentPreviewXlsxAttributeCustomConfigParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentPreviewAttributeFilterToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + inFilterParamsModel := make(map[string]interface{}) + inFilterParamsModel["attribute_data_type"] = "Bool" + inFilterParamsModel["attribute_labels"] = []string{"testString"} + inFilterParamsModel["bool_filter_values"] = []bool{true} + inFilterParamsModel["int32_filter_values"] = []int64{int64(38)} + inFilterParamsModel["int64_filter_values"] = []int64{int64(26)} + inFilterParamsModel["string_filter_values"] = []string{"testString"} + + rangeFilterParamsModel := make(map[string]interface{}) + rangeFilterParamsModel["lower_bound"] = int(26) + rangeFilterParamsModel["upper_bound"] = int(26) + + systemsFilterParamsModel := make(map[string]interface{}) + systemsFilterParamsModel["system_ids"] = []string{"testString"} + systemsFilterParamsModel["system_names"] = []string{"testString"} + + timeRangeFilterParamsModel := make(map[string]interface{}) + timeRangeFilterParamsModel["date_range"] = "Last1Hour" + timeRangeFilterParamsModel["duration_hours"] = int(26) + timeRangeFilterParamsModel["lower_bound"] = int(26) + timeRangeFilterParamsModel["upper_bound"] = int(26) + + model := make(map[string]interface{}) + model["attribute"] = "testString" + model["filter_type"] = "In" + model["in_filter_params"] = []map[string]interface{}{inFilterParamsModel} + model["range_filter_params"] = []map[string]interface{}{rangeFilterParamsModel} + model["systems_filter_params"] = []map[string]interface{}{systemsFilterParamsModel} + model["time_range_filter_params"] = []map[string]interface{}{timeRangeFilterParamsModel} + + assert.Equal(t, result, model) + } + + inFilterParamsModel := new(backuprecoveryv1.InFilterParams) + inFilterParamsModel.AttributeDataType = core.StringPtr("Bool") + inFilterParamsModel.AttributeLabels = []string{"testString"} + inFilterParamsModel.BoolFilterValues = []bool{true} + inFilterParamsModel.Int32FilterValues = []int64{int64(38)} + inFilterParamsModel.Int64FilterValues = []int64{int64(26)} + inFilterParamsModel.StringFilterValues = []string{"testString"} + + rangeFilterParamsModel := new(backuprecoveryv1.RangeFilterParams) + rangeFilterParamsModel.LowerBound = core.Int64Ptr(int64(26)) + rangeFilterParamsModel.UpperBound = core.Int64Ptr(int64(26)) + + systemsFilterParamsModel := new(backuprecoveryv1.SystemsFilterParams) + systemsFilterParamsModel.SystemIds = []string{"testString"} + systemsFilterParamsModel.SystemNames = []string{"testString"} + + timeRangeFilterParamsModel := new(backuprecoveryv1.TimeRangeFilterParams) + timeRangeFilterParamsModel.DateRange = core.StringPtr("Last1Hour") + timeRangeFilterParamsModel.DurationHours = core.Int64Ptr(int64(26)) + timeRangeFilterParamsModel.LowerBound = core.Int64Ptr(int64(26)) + timeRangeFilterParamsModel.UpperBound = core.Int64Ptr(int64(26)) + + model := new(backuprecoveryv1.AttributeFilter) + model.Attribute = core.StringPtr("testString") + model.FilterType = core.StringPtr("In") + model.InFilterParams = inFilterParamsModel + model.RangeFilterParams = rangeFilterParamsModel + model.SystemsFilterParams = systemsFilterParamsModel + model.TimeRangeFilterParams = timeRangeFilterParamsModel + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentPreviewAttributeFilterToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentPreviewInFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["attribute_data_type"] = "Bool" + model["attribute_labels"] = []string{"testString"} + model["bool_filter_values"] = []bool{true} + model["int32_filter_values"] = []int64{int64(38)} + model["int64_filter_values"] = []int64{int64(26)} + model["string_filter_values"] = []string{"testString"} + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.InFilterParams) + model.AttributeDataType = core.StringPtr("Bool") + model.AttributeLabels = []string{"testString"} + model.BoolFilterValues = []bool{true} + model.Int32FilterValues = []int64{int64(38)} + model.Int64FilterValues = []int64{int64(26)} + model.StringFilterValues = []string{"testString"} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentPreviewInFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentPreviewRangeFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["lower_bound"] = int(26) + model["upper_bound"] = int(26) + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.RangeFilterParams) + model.LowerBound = core.Int64Ptr(int64(26)) + model.UpperBound = core.Int64Ptr(int64(26)) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentPreviewRangeFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentPreviewSystemsFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["system_ids"] = []string{"testString"} + model["system_names"] = []string{"testString"} + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.SystemsFilterParams) + model.SystemIds = []string{"testString"} + model.SystemNames = []string{"testString"} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentPreviewSystemsFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentPreviewTimeRangeFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["date_range"] = "Last1Hour" + model["duration_hours"] = int(26) + model["lower_bound"] = int(26) + model["upper_bound"] = int(26) + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.TimeRangeFilterParams) + model.DateRange = core.StringPtr("Last1Hour") + model.DurationHours = core.Int64Ptr(int64(26)) + model.LowerBound = core.Int64Ptr(int64(26)) + model.UpperBound = core.Int64Ptr(int64(26)) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentPreviewTimeRangeFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentPreviewLimitParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["from"] = int(38) + model["size"] = int(1) + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.LimitParams) + model.From = core.Int64Ptr(int64(38)) + model.Size = core.Int64Ptr(int64(1)) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentPreviewLimitParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentPreviewAttributeSortToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["attribute"] = "testString" + model["desc"] = true + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.AttributeSort) + model.Attribute = core.StringPtr("testString") + model.Desc = core.BoolPtr(true) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentPreviewAttributeSortToMap(model) + assert.Nil(t, err) + checkResult(result) +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_component_test.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_component_test.go new file mode 100644 index 0000000000..6437f043be --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_component_test.go @@ -0,0 +1,361 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.96.1-5136e54a-20241108-203028 + */ + +package backuprecovery_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/service/backuprecovery" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" + "github.com/stretchr/testify/assert" +) + +func TestAccIbmBackupRecoveryManagerGetComponentDataSourceBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckIbmBackupRecoveryManagerGetComponentDataSourceConfigBasic(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_get_component.backup_recovery_manager_get_component_instance", "id"), + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_get_component.backup_recovery_manager_get_component_instance", "backup_recovery_manager_get_component_id"), + ), + }, + }, + }) +} + +func testAccCheckIbmBackupRecoveryManagerGetComponentDataSourceConfigBasic() string { + return fmt.Sprintf(` + data "ibm_backup_recovery_manager_get_component" "backup_recovery_manager_get_component_instance" { + id = "id" + } + `) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentAttributeAggregationsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + aggregatedAttributesParamsModel := make(map[string]interface{}) + aggregatedAttributesParamsModel["aggregation_type"] = "sum" + aggregatedAttributesParamsModel["attribute"] = "testString" + aggregatedAttributesParamsModel["label"] = "testString" + + model := make(map[string]interface{}) + model["aggregated_attributes"] = []map[string]interface{}{aggregatedAttributesParamsModel} + model["grouped_attributes"] = []string{"testString"} + + assert.Equal(t, result, model) + } + + aggregatedAttributesParamsModel := new(backuprecoveryv1.AggregatedAttributesParams) + aggregatedAttributesParamsModel.AggregationType = core.StringPtr("sum") + aggregatedAttributesParamsModel.Attribute = core.StringPtr("testString") + aggregatedAttributesParamsModel.Label = core.StringPtr("testString") + + model := new(backuprecoveryv1.AttributeAggregations) + model.AggregatedAttributes = []backuprecoveryv1.AggregatedAttributesParams{*aggregatedAttributesParamsModel} + model.GroupedAttributes = []string{"testString"} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentAttributeAggregationsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentAggregatedAttributesParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["aggregation_type"] = "sum" + model["attribute"] = "testString" + model["label"] = "testString" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.AggregatedAttributesParams) + model.AggregationType = core.StringPtr("sum") + model.Attribute = core.StringPtr("testString") + model.Label = core.StringPtr("testString") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentAggregatedAttributesParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentCustomConfigParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + xlsxAttributeCustomConfigParamsModel := make(map[string]interface{}) + xlsxAttributeCustomConfigParamsModel["attribute_name"] = "testString" + xlsxAttributeCustomConfigParamsModel["custom_label"] = "testString" + xlsxAttributeCustomConfigParamsModel["format"] = "Timestamp" + + xlsxCustomConfigParamsModel := make(map[string]interface{}) + xlsxCustomConfigParamsModel["attribute_config"] = []map[string]interface{}{xlsxAttributeCustomConfigParamsModel} + + model := make(map[string]interface{}) + model["xlsx_params"] = []map[string]interface{}{xlsxCustomConfigParamsModel} + + assert.Equal(t, result, model) + } + + xlsxAttributeCustomConfigParamsModel := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + xlsxAttributeCustomConfigParamsModel.AttributeName = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.CustomLabel = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.Format = core.StringPtr("Timestamp") + + xlsxCustomConfigParamsModel := new(backuprecoveryv1.XlsxCustomConfigParams) + xlsxCustomConfigParamsModel.AttributeConfig = []backuprecoveryv1.XlsxAttributeCustomConfigParams{*xlsxAttributeCustomConfigParamsModel} + + model := new(backuprecoveryv1.CustomConfigParams) + model.XlsxParams = xlsxCustomConfigParamsModel + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentCustomConfigParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentXlsxCustomConfigParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + xlsxAttributeCustomConfigParamsModel := make(map[string]interface{}) + xlsxAttributeCustomConfigParamsModel["attribute_name"] = "testString" + xlsxAttributeCustomConfigParamsModel["custom_label"] = "testString" + xlsxAttributeCustomConfigParamsModel["format"] = "Timestamp" + + model := make(map[string]interface{}) + model["attribute_config"] = []map[string]interface{}{xlsxAttributeCustomConfigParamsModel} + + assert.Equal(t, result, model) + } + + xlsxAttributeCustomConfigParamsModel := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + xlsxAttributeCustomConfigParamsModel.AttributeName = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.CustomLabel = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.Format = core.StringPtr("Timestamp") + + model := new(backuprecoveryv1.XlsxCustomConfigParams) + model.AttributeConfig = []backuprecoveryv1.XlsxAttributeCustomConfigParams{*xlsxAttributeCustomConfigParamsModel} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentXlsxCustomConfigParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentXlsxAttributeCustomConfigParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["attribute_name"] = "testString" + model["custom_label"] = "testString" + model["format"] = "Timestamp" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + model.AttributeName = core.StringPtr("testString") + model.CustomLabel = core.StringPtr("testString") + model.Format = core.StringPtr("Timestamp") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentXlsxAttributeCustomConfigParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentAttributeFilterToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + inFilterParamsModel := make(map[string]interface{}) + inFilterParamsModel["attribute_data_type"] = "Bool" + inFilterParamsModel["attribute_labels"] = []string{"testString"} + inFilterParamsModel["bool_filter_values"] = []bool{true} + inFilterParamsModel["int32_filter_values"] = []int64{int64(38)} + inFilterParamsModel["int64_filter_values"] = []int64{int64(26)} + inFilterParamsModel["string_filter_values"] = []string{"testString"} + + rangeFilterParamsModel := make(map[string]interface{}) + rangeFilterParamsModel["lower_bound"] = int(26) + rangeFilterParamsModel["upper_bound"] = int(26) + + systemsFilterParamsModel := make(map[string]interface{}) + systemsFilterParamsModel["system_ids"] = []string{"testString"} + systemsFilterParamsModel["system_names"] = []string{"testString"} + + timeRangeFilterParamsModel := make(map[string]interface{}) + timeRangeFilterParamsModel["date_range"] = "Last1Hour" + timeRangeFilterParamsModel["duration_hours"] = int(26) + timeRangeFilterParamsModel["lower_bound"] = int(26) + timeRangeFilterParamsModel["upper_bound"] = int(26) + + model := make(map[string]interface{}) + model["attribute"] = "testString" + model["filter_type"] = "In" + model["in_filter_params"] = []map[string]interface{}{inFilterParamsModel} + model["range_filter_params"] = []map[string]interface{}{rangeFilterParamsModel} + model["systems_filter_params"] = []map[string]interface{}{systemsFilterParamsModel} + model["time_range_filter_params"] = []map[string]interface{}{timeRangeFilterParamsModel} + + assert.Equal(t, result, model) + } + + inFilterParamsModel := new(backuprecoveryv1.InFilterParams) + inFilterParamsModel.AttributeDataType = core.StringPtr("Bool") + inFilterParamsModel.AttributeLabels = []string{"testString"} + inFilterParamsModel.BoolFilterValues = []bool{true} + inFilterParamsModel.Int32FilterValues = []int64{int64(38)} + inFilterParamsModel.Int64FilterValues = []int64{int64(26)} + inFilterParamsModel.StringFilterValues = []string{"testString"} + + rangeFilterParamsModel := new(backuprecoveryv1.RangeFilterParams) + rangeFilterParamsModel.LowerBound = core.Int64Ptr(int64(26)) + rangeFilterParamsModel.UpperBound = core.Int64Ptr(int64(26)) + + systemsFilterParamsModel := new(backuprecoveryv1.SystemsFilterParams) + systemsFilterParamsModel.SystemIds = []string{"testString"} + systemsFilterParamsModel.SystemNames = []string{"testString"} + + timeRangeFilterParamsModel := new(backuprecoveryv1.TimeRangeFilterParams) + timeRangeFilterParamsModel.DateRange = core.StringPtr("Last1Hour") + timeRangeFilterParamsModel.DurationHours = core.Int64Ptr(int64(26)) + timeRangeFilterParamsModel.LowerBound = core.Int64Ptr(int64(26)) + timeRangeFilterParamsModel.UpperBound = core.Int64Ptr(int64(26)) + + model := new(backuprecoveryv1.AttributeFilter) + model.Attribute = core.StringPtr("testString") + model.FilterType = core.StringPtr("In") + model.InFilterParams = inFilterParamsModel + model.RangeFilterParams = rangeFilterParamsModel + model.SystemsFilterParams = systemsFilterParamsModel + model.TimeRangeFilterParams = timeRangeFilterParamsModel + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentAttributeFilterToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentInFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["attribute_data_type"] = "Bool" + model["attribute_labels"] = []string{"testString"} + model["bool_filter_values"] = []bool{true} + model["int32_filter_values"] = []int64{int64(38)} + model["int64_filter_values"] = []int64{int64(26)} + model["string_filter_values"] = []string{"testString"} + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.InFilterParams) + model.AttributeDataType = core.StringPtr("Bool") + model.AttributeLabels = []string{"testString"} + model.BoolFilterValues = []bool{true} + model.Int32FilterValues = []int64{int64(38)} + model.Int64FilterValues = []int64{int64(26)} + model.StringFilterValues = []string{"testString"} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentInFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentRangeFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["lower_bound"] = int(26) + model["upper_bound"] = int(26) + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.RangeFilterParams) + model.LowerBound = core.Int64Ptr(int64(26)) + model.UpperBound = core.Int64Ptr(int64(26)) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentRangeFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentSystemsFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["system_ids"] = []string{"testString"} + model["system_names"] = []string{"testString"} + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.SystemsFilterParams) + model.SystemIds = []string{"testString"} + model.SystemNames = []string{"testString"} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentSystemsFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentTimeRangeFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["date_range"] = "Last1Hour" + model["duration_hours"] = int(26) + model["lower_bound"] = int(26) + model["upper_bound"] = int(26) + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.TimeRangeFilterParams) + model.DateRange = core.StringPtr("Last1Hour") + model.DurationHours = core.Int64Ptr(int64(26)) + model.LowerBound = core.Int64Ptr(int64(26)) + model.UpperBound = core.Int64Ptr(int64(26)) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentTimeRangeFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentLimitParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["from"] = int(38) + model["size"] = int(1) + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.LimitParams) + model.From = core.Int64Ptr(int64(38)) + model.Size = core.Int64Ptr(int64(1)) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentLimitParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentAttributeSortToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["attribute"] = "testString" + model["desc"] = true + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.AttributeSort) + model.Attribute = core.StringPtr("testString") + model.Desc = core.BoolPtr(true) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentAttributeSortToMap(model) + assert.Nil(t, err) + checkResult(result) +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_components.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_components.go new file mode 100644 index 0000000000..fa613cf514 --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_components.go @@ -0,0 +1,637 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.96.1-5136e54a-20241108-203028 + */ + +package backuprecovery + +import ( + "context" + "fmt" + "log" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" +) + +func DataSourceIbmBackupRecoveryManagerGetComponents() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceIbmBackupRecoveryManagerGetComponentsRead, + + Schema: map[string]*schema.Schema{ + "ids": &schema.Schema{ + Type: schema.TypeList, + Optional: true, + Description: "Specifies the ids of the report component to fetch.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "components": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of components.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "aggs": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the aggregation related information that needs to be applied on the attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "aggregated_attributes": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of aggregation properties to be applied on the attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "aggregation_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the aggregation type.", + }, + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the attribute name.", + }, + "label": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the label to be generated for this aggregated attribute. If not specified, then by default label of the column in the output will be combination of aggregation type and attribute.", + }, + }, + }, + }, + "grouped_attributes": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of attributes to be grouped in the aggregation.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "config": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the configuration parameters to customize and format the columns in the report artifacts like excel, pdf etc.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "xlsx_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the configuration parameters to customize a component in excel report.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_config": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies customized configuration for the attributes in the report. If not specified, all the attributes will be sent as-is to the report without any formatting.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the attribute.", + }, + "custom_label": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies a custom label for attribute to appear in the xlsx report. If not specified, default attribute name will be used.", + }, + "format": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies a custom format for attribute to appear in the xlsx report. If not specified, the attribute value is sent as-is.", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + "data": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the data returned after evaluating the component.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "description": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies description of the Component.", + }, + "filters": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the filters that are applied on specific report type attributes in order to compose this component.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the attribute.", + }, + "filter_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the type of the filter that needs to be applied.", + }, + "in_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the in filter that are applied on attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_data_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the data type of the attribute.", + }, + "attribute_labels": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the optional label values for the attribute.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "bool_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of boolean values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeBool, + }, + }, + "int32_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of int32 values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "int64_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of int64 values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "string_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of string values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "range_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the filters that are applied on attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "lower_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the lower bound value. If specified, all the results which are greater than this value will be returned.", + }, + "upper_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the upper bound value. If specified, all the results which are lesser than this value will be returned.", + }, + }, + }, + }, + "systems_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the systems filter. Specifying this will pre filter all the results provided list of system identifier before applying aggregations.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "system_ids": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies an array of system identifiers. System identifiers may be of format clusterid:clusterincarnationid or a regionid (applicable only in case of DMaaS).", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "system_names": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the optional system names labels.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "time_range_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the time range filter. Specifying this will pre filter all the results on necessary resources like Protection Runs etc before applying aggregations. Currently, maximum allowed time range is 60 days.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "date_range": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Enum value for specifying the date range for a time filter. Considered only if lowerBound and upperBound are empty.", + }, + "duration_hours": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the duration preceding the current time for which the data must be fetch i.e fetch data between currentTime and currentTime - durationHours. This filter is only considered if neither upperBound, lowerBound or dateRange is specified.", + }, + "lower_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the lower bound value. If specified, all the results which are greater than this value will be returned.", + }, + "upper_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the upper bound value. If specified, all the results which are lesser than this value will be returned.", + }, + }, + }, + }, + }, + }, + }, + "id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the id of the Component.", + }, + "limit": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the parameters to limit the resulting dataset.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "from": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the offset to which resulting data will be skipped before applying the size parameter. For example if dataset size is 10 objects, from=2 and size=5, then from 10 objects only 5 objects are returned starting from offset 2 i.e., 2 to 7. If not specified, then none of the objects are skipped.", + }, + "size": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the number of objects to be returned from the offset specified.", + }, + }, + }, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the Component.", + }, + "report_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the report type on top of which this Component is created from.", + }, + "sort": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the sorting (ordering) parameters to be applied to the resulting data.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the attribute.", + }, + "desc": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + Description: "Specifies whether the sorting order should be descending. Default value is false.", + }, + }, + }, + }, + }, + }, + }, + }, + } +} + +func dataSourceIbmBackupRecoveryManagerGetComponentsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + heliosReportingServiceApIsClient, err := meta.(conns.ClientSession).BackupRecoveryManagerV1() + if err != nil { + tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_components", "read", "initialize-client") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + getComponentsOptions := &backuprecoveryv1.GetComponentsOptions{} + + if _, ok := d.GetOk("ids"); ok { + var ids []string + for _, v := range d.Get("ids").([]interface{}) { + idsItem := v.(string) + ids = append(ids, idsItem) + } + getComponentsOptions.SetIds(ids) + } + + components, _, err := heliosReportingServiceApIsClient.GetComponentsWithContext(context, getComponentsOptions) + if err != nil { + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetComponentsWithContext failed: %s", err.Error()), "(Data) ibm_backup_recovery_manager_get_components", "read") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + d.SetId(dataSourceIbmBackupRecoveryManagerGetComponentsID(d)) + + if !core.IsNil(components.Components) { + componentItems := []map[string]interface{}{} + for _, componentsItem := range components.Components { + componentsItemMap, err := DataSourceIbmBackupRecoveryManagerGetComponentsComponentToMap(&componentsItem) // #nosec G601 + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_components", "read", "components-to-map").GetDiag() + } + componentItems = append(componentItems, componentsItemMap) + } + if err = d.Set("components", componentItems); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting components: %s", err), "(Data) ibm_backup_recovery_manager_get_components", "read", "set-components").GetDiag() + } + } + + return nil +} + +// dataSourceIbmBackupRecoveryManagerGetComponentsID returns a reasonable ID for the list. +func dataSourceIbmBackupRecoveryManagerGetComponentsID(d *schema.ResourceData) string { + return time.Now().UTC().String() +} + +func DataSourceIbmBackupRecoveryManagerGetComponentsComponentToMap(model *backuprecoveryv1.Component) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.Aggs != nil { + aggsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentsAttributeAggregationsToMap(model.Aggs) + if err != nil { + return modelMap, err + } + modelMap["aggs"] = []map[string]interface{}{aggsMap} + } + if model.Config != nil { + configMap, err := DataSourceIbmBackupRecoveryManagerGetComponentsCustomConfigParamsToMap(model.Config) + if err != nil { + return modelMap, err + } + modelMap["config"] = []map[string]interface{}{configMap} + } + if model.Data != nil { + modelMap["data"] = model.Data + } + if model.Description != nil { + modelMap["description"] = *model.Description + } + if model.Filters != nil { + filters := []map[string]interface{}{} + for _, filtersItem := range model.Filters { + filtersItemMap, err := DataSourceIbmBackupRecoveryManagerGetComponentsAttributeFilterToMap(&filtersItem) // #nosec G601 + if err != nil { + return modelMap, err + } + filters = append(filters, filtersItemMap) + } + modelMap["filters"] = filters + } + if model.ID != nil { + modelMap["id"] = *model.ID + } + if model.Limit != nil { + limitMap, err := DataSourceIbmBackupRecoveryManagerGetComponentsLimitParamsToMap(model.Limit) + if err != nil { + return modelMap, err + } + modelMap["limit"] = []map[string]interface{}{limitMap} + } + if model.Name != nil { + modelMap["name"] = *model.Name + } + if model.ReportType != nil { + modelMap["report_type"] = *model.ReportType + } + if model.Sort != nil { + sort := []map[string]interface{}{} + for _, sortItem := range model.Sort { + sortItemMap, err := DataSourceIbmBackupRecoveryManagerGetComponentsAttributeSortToMap(&sortItem) // #nosec G601 + if err != nil { + return modelMap, err + } + sort = append(sort, sortItemMap) + } + modelMap["sort"] = sort + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentsAttributeAggregationsToMap(model *backuprecoveryv1.AttributeAggregations) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.AggregatedAttributes != nil { + aggregatedAttributes := []map[string]interface{}{} + for _, aggregatedAttributesItem := range model.AggregatedAttributes { + aggregatedAttributesItemMap, err := DataSourceIbmBackupRecoveryManagerGetComponentsAggregatedAttributesParamsToMap(&aggregatedAttributesItem) // #nosec G601 + if err != nil { + return modelMap, err + } + aggregatedAttributes = append(aggregatedAttributes, aggregatedAttributesItemMap) + } + modelMap["aggregated_attributes"] = aggregatedAttributes + } + if model.GroupedAttributes != nil { + modelMap["grouped_attributes"] = model.GroupedAttributes + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentsAggregatedAttributesParamsToMap(model *backuprecoveryv1.AggregatedAttributesParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["aggregation_type"] = *model.AggregationType + modelMap["attribute"] = *model.Attribute + if model.Label != nil { + modelMap["label"] = *model.Label + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentsCustomConfigParamsToMap(model *backuprecoveryv1.CustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.XlsxParams != nil { + xlsxParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentsXlsxCustomConfigParamsToMap(model.XlsxParams) + if err != nil { + return modelMap, err + } + modelMap["xlsx_params"] = []map[string]interface{}{xlsxParamsMap} + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentsXlsxCustomConfigParamsToMap(model *backuprecoveryv1.XlsxCustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.AttributeConfig != nil { + attributeConfig := []map[string]interface{}{} + for _, attributeConfigItem := range model.AttributeConfig { + attributeConfigItemMap, err := DataSourceIbmBackupRecoveryManagerGetComponentsXlsxAttributeCustomConfigParamsToMap(&attributeConfigItem) // #nosec G601 + if err != nil { + return modelMap, err + } + attributeConfig = append(attributeConfig, attributeConfigItemMap) + } + modelMap["attribute_config"] = attributeConfig + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentsXlsxAttributeCustomConfigParamsToMap(model *backuprecoveryv1.XlsxAttributeCustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute_name"] = *model.AttributeName + if model.CustomLabel != nil { + modelMap["custom_label"] = *model.CustomLabel + } + if model.Format != nil { + modelMap["format"] = *model.Format + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentsAttributeFilterToMap(model *backuprecoveryv1.AttributeFilter) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute"] = *model.Attribute + modelMap["filter_type"] = *model.FilterType + if model.InFilterParams != nil { + inFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentsInFilterParamsToMap(model.InFilterParams) + if err != nil { + return modelMap, err + } + modelMap["in_filter_params"] = []map[string]interface{}{inFilterParamsMap} + } + if model.RangeFilterParams != nil { + rangeFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentsRangeFilterParamsToMap(model.RangeFilterParams) + if err != nil { + return modelMap, err + } + modelMap["range_filter_params"] = []map[string]interface{}{rangeFilterParamsMap} + } + if model.SystemsFilterParams != nil { + systemsFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentsSystemsFilterParamsToMap(model.SystemsFilterParams) + if err != nil { + return modelMap, err + } + modelMap["systems_filter_params"] = []map[string]interface{}{systemsFilterParamsMap} + } + if model.TimeRangeFilterParams != nil { + timeRangeFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetComponentsTimeRangeFilterParamsToMap(model.TimeRangeFilterParams) + if err != nil { + return modelMap, err + } + modelMap["time_range_filter_params"] = []map[string]interface{}{timeRangeFilterParamsMap} + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentsInFilterParamsToMap(model *backuprecoveryv1.InFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute_data_type"] = *model.AttributeDataType + if model.AttributeLabels != nil { + modelMap["attribute_labels"] = model.AttributeLabels + } + if model.BoolFilterValues != nil { + modelMap["bool_filter_values"] = model.BoolFilterValues + } + if model.Int32FilterValues != nil { + modelMap["int32_filter_values"] = model.Int32FilterValues + } + if model.Int64FilterValues != nil { + modelMap["int64_filter_values"] = model.Int64FilterValues + } + if model.StringFilterValues != nil { + modelMap["string_filter_values"] = model.StringFilterValues + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentsRangeFilterParamsToMap(model *backuprecoveryv1.RangeFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.LowerBound != nil { + modelMap["lower_bound"] = flex.IntValue(model.LowerBound) + } + if model.UpperBound != nil { + modelMap["upper_bound"] = flex.IntValue(model.UpperBound) + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentsSystemsFilterParamsToMap(model *backuprecoveryv1.SystemsFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["system_ids"] = model.SystemIds + if model.SystemNames != nil { + modelMap["system_names"] = model.SystemNames + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentsTimeRangeFilterParamsToMap(model *backuprecoveryv1.TimeRangeFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.DateRange != nil { + modelMap["date_range"] = *model.DateRange + } + if model.DurationHours != nil { + modelMap["duration_hours"] = flex.IntValue(model.DurationHours) + } + if model.LowerBound != nil { + modelMap["lower_bound"] = flex.IntValue(model.LowerBound) + } + if model.UpperBound != nil { + modelMap["upper_bound"] = flex.IntValue(model.UpperBound) + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentsLimitParamsToMap(model *backuprecoveryv1.LimitParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.From != nil { + modelMap["from"] = flex.IntValue(model.From) + } + modelMap["size"] = flex.IntValue(model.Size) + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetComponentsAttributeSortToMap(model *backuprecoveryv1.AttributeSort) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute"] = *model.Attribute + if model.Desc != nil { + modelMap["desc"] = *model.Desc + } + return modelMap, nil +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_components_test.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_components_test.go new file mode 100644 index 0000000000..772fa9ff43 --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_components_test.go @@ -0,0 +1,510 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.96.1-5136e54a-20241108-203028 + */ + +package backuprecovery_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/service/backuprecovery" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" + "github.com/stretchr/testify/assert" +) + +func TestAccIbmBackupRecoveryManagerGetComponentsDataSourceBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckIbmBackupRecoveryManagerGetComponentsDataSourceConfigBasic(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_get_components.backup_recovery_manager_get_components_instance", "id"), + ), + }, + }, + }) +} + +func testAccCheckIbmBackupRecoveryManagerGetComponentsDataSourceConfigBasic() string { + return fmt.Sprintf(` + data "ibm_backup_recovery_manager_get_components" "backup_recovery_manager_get_components_instance" { + ids = [ "ids" ] + } + `) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentsComponentToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + aggregatedAttributesParamsModel := make(map[string]interface{}) + aggregatedAttributesParamsModel["aggregation_type"] = "sum" + aggregatedAttributesParamsModel["attribute"] = "testString" + aggregatedAttributesParamsModel["label"] = "testString" + + attributeAggregationsModel := make(map[string]interface{}) + attributeAggregationsModel["aggregated_attributes"] = []map[string]interface{}{aggregatedAttributesParamsModel} + attributeAggregationsModel["grouped_attributes"] = []string{"testString"} + + xlsxAttributeCustomConfigParamsModel := make(map[string]interface{}) + xlsxAttributeCustomConfigParamsModel["attribute_name"] = "testString" + xlsxAttributeCustomConfigParamsModel["custom_label"] = "testString" + xlsxAttributeCustomConfigParamsModel["format"] = "Timestamp" + + xlsxCustomConfigParamsModel := make(map[string]interface{}) + xlsxCustomConfigParamsModel["attribute_config"] = []map[string]interface{}{xlsxAttributeCustomConfigParamsModel} + + customConfigParamsModel := make(map[string]interface{}) + customConfigParamsModel["xlsx_params"] = []map[string]interface{}{xlsxCustomConfigParamsModel} + + inFilterParamsModel := make(map[string]interface{}) + inFilterParamsModel["attribute_data_type"] = "Bool" + inFilterParamsModel["attribute_labels"] = []string{"testString"} + inFilterParamsModel["bool_filter_values"] = []bool{true} + inFilterParamsModel["int32_filter_values"] = []int64{int64(38)} + inFilterParamsModel["int64_filter_values"] = []int64{int64(26)} + inFilterParamsModel["string_filter_values"] = []string{"testString"} + + rangeFilterParamsModel := make(map[string]interface{}) + rangeFilterParamsModel["lower_bound"] = int(26) + rangeFilterParamsModel["upper_bound"] = int(26) + + systemsFilterParamsModel := make(map[string]interface{}) + systemsFilterParamsModel["system_ids"] = []string{"testString"} + systemsFilterParamsModel["system_names"] = []string{"testString"} + + timeRangeFilterParamsModel := make(map[string]interface{}) + timeRangeFilterParamsModel["date_range"] = "Last1Hour" + timeRangeFilterParamsModel["duration_hours"] = int(26) + timeRangeFilterParamsModel["lower_bound"] = int(26) + timeRangeFilterParamsModel["upper_bound"] = int(26) + + attributeFilterModel := make(map[string]interface{}) + attributeFilterModel["attribute"] = "testString" + attributeFilterModel["filter_type"] = "In" + attributeFilterModel["in_filter_params"] = []map[string]interface{}{inFilterParamsModel} + attributeFilterModel["range_filter_params"] = []map[string]interface{}{rangeFilterParamsModel} + attributeFilterModel["systems_filter_params"] = []map[string]interface{}{systemsFilterParamsModel} + attributeFilterModel["time_range_filter_params"] = []map[string]interface{}{timeRangeFilterParamsModel} + + limitParamsModel := make(map[string]interface{}) + limitParamsModel["from"] = int(38) + limitParamsModel["size"] = int(1) + + attributeSortModel := make(map[string]interface{}) + attributeSortModel["attribute"] = "testString" + attributeSortModel["desc"] = true + + model := make(map[string]interface{}) + model["aggs"] = []map[string]interface{}{attributeAggregationsModel} + model["config"] = []map[string]interface{}{customConfigParamsModel} + model["data"] = []map[string]interface{}{map[string]interface{}{"anyKey": "anyValue"}} + model["description"] = "testString" + model["filters"] = []map[string]interface{}{attributeFilterModel} + model["id"] = "testString" + model["limit"] = []map[string]interface{}{limitParamsModel} + model["name"] = "testString" + model["report_type"] = "Failures" + model["sort"] = []map[string]interface{}{attributeSortModel} + + assert.Equal(t, result, model) + } + + aggregatedAttributesParamsModel := new(backuprecoveryv1.AggregatedAttributesParams) + aggregatedAttributesParamsModel.AggregationType = core.StringPtr("sum") + aggregatedAttributesParamsModel.Attribute = core.StringPtr("testString") + aggregatedAttributesParamsModel.Label = core.StringPtr("testString") + + attributeAggregationsModel := new(backuprecoveryv1.AttributeAggregations) + attributeAggregationsModel.AggregatedAttributes = []backuprecoveryv1.AggregatedAttributesParams{*aggregatedAttributesParamsModel} + attributeAggregationsModel.GroupedAttributes = []string{"testString"} + + xlsxAttributeCustomConfigParamsModel := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + xlsxAttributeCustomConfigParamsModel.AttributeName = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.CustomLabel = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.Format = core.StringPtr("Timestamp") + + xlsxCustomConfigParamsModel := new(backuprecoveryv1.XlsxCustomConfigParams) + xlsxCustomConfigParamsModel.AttributeConfig = []backuprecoveryv1.XlsxAttributeCustomConfigParams{*xlsxAttributeCustomConfigParamsModel} + + customConfigParamsModel := new(backuprecoveryv1.CustomConfigParams) + customConfigParamsModel.XlsxParams = xlsxCustomConfigParamsModel + + inFilterParamsModel := new(backuprecoveryv1.InFilterParams) + inFilterParamsModel.AttributeDataType = core.StringPtr("Bool") + inFilterParamsModel.AttributeLabels = []string{"testString"} + inFilterParamsModel.BoolFilterValues = []bool{true} + inFilterParamsModel.Int32FilterValues = []int64{int64(38)} + inFilterParamsModel.Int64FilterValues = []int64{int64(26)} + inFilterParamsModel.StringFilterValues = []string{"testString"} + + rangeFilterParamsModel := new(backuprecoveryv1.RangeFilterParams) + rangeFilterParamsModel.LowerBound = core.Int64Ptr(int64(26)) + rangeFilterParamsModel.UpperBound = core.Int64Ptr(int64(26)) + + systemsFilterParamsModel := new(backuprecoveryv1.SystemsFilterParams) + systemsFilterParamsModel.SystemIds = []string{"testString"} + systemsFilterParamsModel.SystemNames = []string{"testString"} + + timeRangeFilterParamsModel := new(backuprecoveryv1.TimeRangeFilterParams) + timeRangeFilterParamsModel.DateRange = core.StringPtr("Last1Hour") + timeRangeFilterParamsModel.DurationHours = core.Int64Ptr(int64(26)) + timeRangeFilterParamsModel.LowerBound = core.Int64Ptr(int64(26)) + timeRangeFilterParamsModel.UpperBound = core.Int64Ptr(int64(26)) + + attributeFilterModel := new(backuprecoveryv1.AttributeFilter) + attributeFilterModel.Attribute = core.StringPtr("testString") + attributeFilterModel.FilterType = core.StringPtr("In") + attributeFilterModel.InFilterParams = inFilterParamsModel + attributeFilterModel.RangeFilterParams = rangeFilterParamsModel + attributeFilterModel.SystemsFilterParams = systemsFilterParamsModel + attributeFilterModel.TimeRangeFilterParams = timeRangeFilterParamsModel + + limitParamsModel := new(backuprecoveryv1.LimitParams) + limitParamsModel.From = core.Int64Ptr(int64(38)) + limitParamsModel.Size = core.Int64Ptr(int64(1)) + + attributeSortModel := new(backuprecoveryv1.AttributeSort) + attributeSortModel.Attribute = core.StringPtr("testString") + attributeSortModel.Desc = core.BoolPtr(true) + + model := new(backuprecoveryv1.Component) + model.Aggs = attributeAggregationsModel + model.Config = customConfigParamsModel + model.Data = []map[string]interface{}{map[string]interface{}{"anyKey": "anyValue"}} + model.Description = core.StringPtr("testString") + model.Filters = []backuprecoveryv1.AttributeFilter{*attributeFilterModel} + model.ID = core.StringPtr("testString") + model.Limit = limitParamsModel + model.Name = core.StringPtr("testString") + model.ReportType = core.StringPtr("Failures") + model.Sort = []backuprecoveryv1.AttributeSort{*attributeSortModel} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentsComponentToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentsAttributeAggregationsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + aggregatedAttributesParamsModel := make(map[string]interface{}) + aggregatedAttributesParamsModel["aggregation_type"] = "sum" + aggregatedAttributesParamsModel["attribute"] = "testString" + aggregatedAttributesParamsModel["label"] = "testString" + + model := make(map[string]interface{}) + model["aggregated_attributes"] = []map[string]interface{}{aggregatedAttributesParamsModel} + model["grouped_attributes"] = []string{"testString"} + + assert.Equal(t, result, model) + } + + aggregatedAttributesParamsModel := new(backuprecoveryv1.AggregatedAttributesParams) + aggregatedAttributesParamsModel.AggregationType = core.StringPtr("sum") + aggregatedAttributesParamsModel.Attribute = core.StringPtr("testString") + aggregatedAttributesParamsModel.Label = core.StringPtr("testString") + + model := new(backuprecoveryv1.AttributeAggregations) + model.AggregatedAttributes = []backuprecoveryv1.AggregatedAttributesParams{*aggregatedAttributesParamsModel} + model.GroupedAttributes = []string{"testString"} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentsAttributeAggregationsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentsAggregatedAttributesParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["aggregation_type"] = "sum" + model["attribute"] = "testString" + model["label"] = "testString" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.AggregatedAttributesParams) + model.AggregationType = core.StringPtr("sum") + model.Attribute = core.StringPtr("testString") + model.Label = core.StringPtr("testString") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentsAggregatedAttributesParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentsCustomConfigParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + xlsxAttributeCustomConfigParamsModel := make(map[string]interface{}) + xlsxAttributeCustomConfigParamsModel["attribute_name"] = "testString" + xlsxAttributeCustomConfigParamsModel["custom_label"] = "testString" + xlsxAttributeCustomConfigParamsModel["format"] = "Timestamp" + + xlsxCustomConfigParamsModel := make(map[string]interface{}) + xlsxCustomConfigParamsModel["attribute_config"] = []map[string]interface{}{xlsxAttributeCustomConfigParamsModel} + + model := make(map[string]interface{}) + model["xlsx_params"] = []map[string]interface{}{xlsxCustomConfigParamsModel} + + assert.Equal(t, result, model) + } + + xlsxAttributeCustomConfigParamsModel := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + xlsxAttributeCustomConfigParamsModel.AttributeName = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.CustomLabel = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.Format = core.StringPtr("Timestamp") + + xlsxCustomConfigParamsModel := new(backuprecoveryv1.XlsxCustomConfigParams) + xlsxCustomConfigParamsModel.AttributeConfig = []backuprecoveryv1.XlsxAttributeCustomConfigParams{*xlsxAttributeCustomConfigParamsModel} + + model := new(backuprecoveryv1.CustomConfigParams) + model.XlsxParams = xlsxCustomConfigParamsModel + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentsCustomConfigParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentsXlsxCustomConfigParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + xlsxAttributeCustomConfigParamsModel := make(map[string]interface{}) + xlsxAttributeCustomConfigParamsModel["attribute_name"] = "testString" + xlsxAttributeCustomConfigParamsModel["custom_label"] = "testString" + xlsxAttributeCustomConfigParamsModel["format"] = "Timestamp" + + model := make(map[string]interface{}) + model["attribute_config"] = []map[string]interface{}{xlsxAttributeCustomConfigParamsModel} + + assert.Equal(t, result, model) + } + + xlsxAttributeCustomConfigParamsModel := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + xlsxAttributeCustomConfigParamsModel.AttributeName = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.CustomLabel = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.Format = core.StringPtr("Timestamp") + + model := new(backuprecoveryv1.XlsxCustomConfigParams) + model.AttributeConfig = []backuprecoveryv1.XlsxAttributeCustomConfigParams{*xlsxAttributeCustomConfigParamsModel} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentsXlsxCustomConfigParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentsXlsxAttributeCustomConfigParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["attribute_name"] = "testString" + model["custom_label"] = "testString" + model["format"] = "Timestamp" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + model.AttributeName = core.StringPtr("testString") + model.CustomLabel = core.StringPtr("testString") + model.Format = core.StringPtr("Timestamp") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentsXlsxAttributeCustomConfigParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentsAttributeFilterToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + inFilterParamsModel := make(map[string]interface{}) + inFilterParamsModel["attribute_data_type"] = "Bool" + inFilterParamsModel["attribute_labels"] = []string{"testString"} + inFilterParamsModel["bool_filter_values"] = []bool{true} + inFilterParamsModel["int32_filter_values"] = []int64{int64(38)} + inFilterParamsModel["int64_filter_values"] = []int64{int64(26)} + inFilterParamsModel["string_filter_values"] = []string{"testString"} + + rangeFilterParamsModel := make(map[string]interface{}) + rangeFilterParamsModel["lower_bound"] = int(26) + rangeFilterParamsModel["upper_bound"] = int(26) + + systemsFilterParamsModel := make(map[string]interface{}) + systemsFilterParamsModel["system_ids"] = []string{"testString"} + systemsFilterParamsModel["system_names"] = []string{"testString"} + + timeRangeFilterParamsModel := make(map[string]interface{}) + timeRangeFilterParamsModel["date_range"] = "Last1Hour" + timeRangeFilterParamsModel["duration_hours"] = int(26) + timeRangeFilterParamsModel["lower_bound"] = int(26) + timeRangeFilterParamsModel["upper_bound"] = int(26) + + model := make(map[string]interface{}) + model["attribute"] = "testString" + model["filter_type"] = "In" + model["in_filter_params"] = []map[string]interface{}{inFilterParamsModel} + model["range_filter_params"] = []map[string]interface{}{rangeFilterParamsModel} + model["systems_filter_params"] = []map[string]interface{}{systemsFilterParamsModel} + model["time_range_filter_params"] = []map[string]interface{}{timeRangeFilterParamsModel} + + assert.Equal(t, result, model) + } + + inFilterParamsModel := new(backuprecoveryv1.InFilterParams) + inFilterParamsModel.AttributeDataType = core.StringPtr("Bool") + inFilterParamsModel.AttributeLabels = []string{"testString"} + inFilterParamsModel.BoolFilterValues = []bool{true} + inFilterParamsModel.Int32FilterValues = []int64{int64(38)} + inFilterParamsModel.Int64FilterValues = []int64{int64(26)} + inFilterParamsModel.StringFilterValues = []string{"testString"} + + rangeFilterParamsModel := new(backuprecoveryv1.RangeFilterParams) + rangeFilterParamsModel.LowerBound = core.Int64Ptr(int64(26)) + rangeFilterParamsModel.UpperBound = core.Int64Ptr(int64(26)) + + systemsFilterParamsModel := new(backuprecoveryv1.SystemsFilterParams) + systemsFilterParamsModel.SystemIds = []string{"testString"} + systemsFilterParamsModel.SystemNames = []string{"testString"} + + timeRangeFilterParamsModel := new(backuprecoveryv1.TimeRangeFilterParams) + timeRangeFilterParamsModel.DateRange = core.StringPtr("Last1Hour") + timeRangeFilterParamsModel.DurationHours = core.Int64Ptr(int64(26)) + timeRangeFilterParamsModel.LowerBound = core.Int64Ptr(int64(26)) + timeRangeFilterParamsModel.UpperBound = core.Int64Ptr(int64(26)) + + model := new(backuprecoveryv1.AttributeFilter) + model.Attribute = core.StringPtr("testString") + model.FilterType = core.StringPtr("In") + model.InFilterParams = inFilterParamsModel + model.RangeFilterParams = rangeFilterParamsModel + model.SystemsFilterParams = systemsFilterParamsModel + model.TimeRangeFilterParams = timeRangeFilterParamsModel + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentsAttributeFilterToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentsInFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["attribute_data_type"] = "Bool" + model["attribute_labels"] = []string{"testString"} + model["bool_filter_values"] = []bool{true} + model["int32_filter_values"] = []int64{int64(38)} + model["int64_filter_values"] = []int64{int64(26)} + model["string_filter_values"] = []string{"testString"} + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.InFilterParams) + model.AttributeDataType = core.StringPtr("Bool") + model.AttributeLabels = []string{"testString"} + model.BoolFilterValues = []bool{true} + model.Int32FilterValues = []int64{int64(38)} + model.Int64FilterValues = []int64{int64(26)} + model.StringFilterValues = []string{"testString"} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentsInFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentsRangeFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["lower_bound"] = int(26) + model["upper_bound"] = int(26) + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.RangeFilterParams) + model.LowerBound = core.Int64Ptr(int64(26)) + model.UpperBound = core.Int64Ptr(int64(26)) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentsRangeFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentsSystemsFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["system_ids"] = []string{"testString"} + model["system_names"] = []string{"testString"} + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.SystemsFilterParams) + model.SystemIds = []string{"testString"} + model.SystemNames = []string{"testString"} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentsSystemsFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentsTimeRangeFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["date_range"] = "Last1Hour" + model["duration_hours"] = int(26) + model["lower_bound"] = int(26) + model["upper_bound"] = int(26) + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.TimeRangeFilterParams) + model.DateRange = core.StringPtr("Last1Hour") + model.DurationHours = core.Int64Ptr(int64(26)) + model.LowerBound = core.Int64Ptr(int64(26)) + model.UpperBound = core.Int64Ptr(int64(26)) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentsTimeRangeFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentsLimitParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["from"] = int(38) + model["size"] = int(1) + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.LimitParams) + model.From = core.Int64Ptr(int64(38)) + model.Size = core.Int64Ptr(int64(1)) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentsLimitParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetComponentsAttributeSortToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["attribute"] = "testString" + model["desc"] = true + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.AttributeSort) + model.Attribute = core.StringPtr("testString") + model.Desc = core.BoolPtr(true) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetComponentsAttributeSortToMap(model) + assert.Nil(t, err) + checkResult(result) +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report.go new file mode 100644 index 0000000000..50cb725901 --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report.go @@ -0,0 +1,129 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.105.1-067d600b-20250616-154447 + */ + +package backuprecovery + +import ( + "context" + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" +) + +func DataSourceIbmBackupRecoveryManagerGetReport() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceIbmBackupRecoveryManagerGetReportRead, + + Schema: map[string]*schema.Schema{ + "report_id": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Description: "Specifies the id of the report.", + }, + "category": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies categoty of the Report.", + }, + "component_ids": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the list of component ids in the Report.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "description": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies description of the Report.", + }, + "supported_user_contexts": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies all the supported user contexts for this report.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "title": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the title of the report.", + }, + }, + } +} + +func dataSourceIbmBackupRecoveryManagerGetReportRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + backupRecoveryClient, err := meta.(conns.ClientSession).BackupRecoveryManagerV1() + if err != nil { + tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_report", "read", "initialize-client") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + getReportByIdOptions := &backuprecoveryv1.GetReportByIdOptions{} + + getReportByIdOptions.SetID(d.Get("report_id").(string)) + + report, _, err := backupRecoveryClient.GetReportByIDWithContext(context, getReportByIdOptions) + if err != nil { + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetReportByIDWithContext failed: %s", err.Error()), "(Data) ibm_backup_recovery_manager_get_report", "read") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + d.SetId(*report.ID) + + if !core.IsNil(report.Category) { + if err = d.Set("category", report.Category); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting category: %s", err), "(Data) ibm_backup_recovery_manager_get_report", "read", "set-category").GetDiag() + } + } + + if !core.IsNil(report.ComponentIds) { + componentIds := []interface{}{} + for _, componentIdsItem := range report.ComponentIds { + componentIds = append(componentIds, componentIdsItem) + } + if err = d.Set("component_ids", componentIds); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting component_ids: %s", err), "(Data) ibm_backup_recovery_manager_get_report", "read", "set-component_ids").GetDiag() + } + } + + if !core.IsNil(report.Description) { + if err = d.Set("description", report.Description); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting description: %s", err), "(Data) ibm_backup_recovery_manager_get_report", "read", "set-description").GetDiag() + } + } + + if !core.IsNil(report.SupportedUserContexts) { + supportedUserContexts := []interface{}{} + for _, supportedUserContextsItem := range report.SupportedUserContexts { + supportedUserContexts = append(supportedUserContexts, supportedUserContextsItem) + } + if err = d.Set("supported_user_contexts", supportedUserContexts); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting supported_user_contexts: %s", err), "(Data) ibm_backup_recovery_manager_get_report", "read", "set-supported_user_contexts").GetDiag() + } + } + + if !core.IsNil(report.Title) { + if err = d.Set("title", report.Title); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting title: %s", err), "(Data) ibm_backup_recovery_manager_get_report", "read", "set-title").GetDiag() + } + } + + return nil +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_preview.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_preview.go new file mode 100644 index 0000000000..ff55e38eae --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_preview.go @@ -0,0 +1,1050 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.96.1-5136e54a-20241108-203028 + */ + +package backuprecovery + +import ( + "context" + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" +) + +func DataSourceIbmBackupRecoveryManagerGetReportPreview() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceIbmBackupRecoveryManagerGetReportPreviewRead, + + Schema: map[string]*schema.Schema{ + "report_id": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Description: "Specifies the id of the report.", + }, + "component_ids": &schema.Schema{ + Type: schema.TypeList, + Optional: true, + Description: "Specifies list of components ids to be evaluated for the given report. If not specified, then all the components are evaluated.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "filters": &schema.Schema{ + Type: schema.TypeList, + Optional: true, + Description: "Specifies list of global filters that are applicable to given components in the report.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the attribute.", + }, + "filter_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the type of the filter that needs to be applied.", + }, + "in_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the in filter that are applied on attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_data_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the data type of the attribute.", + }, + "attribute_labels": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the optional label values for the attribute.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "bool_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of boolean values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeBool, + }, + }, + "int32_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of int32 values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "int64_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of int64 values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "string_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of string values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "range_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the filters that are applied on attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "lower_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the lower bound value. If specified, all the results which are greater than this value will be returned.", + }, + "upper_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the upper bound value. If specified, all the results which are lesser than this value will be returned.", + }, + }, + }, + }, + "systems_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the systems filter. Specifying this will pre filter all the results provided list of system identifier before applying aggregations.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "system_ids": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies an array of system identifiers. System identifiers may be of format clusterid:clusterincarnationid or a regionid (applicable only in case of DMaaS).", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "system_names": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the optional system names labels.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "time_range_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the time range filter. Specifying this will pre filter all the results on necessary resources like Protection Runs etc before applying aggregations. Currently, maximum allowed time range is 60 days.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "date_range": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Enum value for specifying the date range for a time filter. Considered only if lowerBound and upperBound are empty.", + }, + "duration_hours": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the duration preceding the current time for which the data must be fetch i.e fetch data between currentTime and currentTime - durationHours. This filter is only considered if neither upperBound, lowerBound or dateRange is specified.", + }, + "lower_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the lower bound value. If specified, all the results which are greater than this value will be returned.", + }, + "upper_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the upper bound value. If specified, all the results which are lesser than this value will be returned.", + }, + }, + }, + }, + }, + }, + }, + "timezone": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Description: "Specifies timezone of the user. If nil, defaults to UTC. The time specified should be a location name in the IANA Time Zone database, for example, 'America/Los_Angeles'.", + }, + "components": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the component params and data.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "aggs": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the aggregation related information that needs to be applied on the attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "aggregated_attributes": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of aggregation properties to be applied on the attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "aggregation_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the aggregation type.", + }, + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the attribute name.", + }, + "label": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the label to be generated for this aggregated attribute. If not specified, then by default label of the column in the output will be combination of aggregation type and attribute.", + }, + }, + }, + }, + "grouped_attributes": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of attributes to be grouped in the aggregation.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "config": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the configuration parameters to customize and format the columns in the report artifacts like excel, pdf etc.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "xlsx_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the configuration parameters to customize a component in excel report.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_config": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies customized configuration for the attributes in the report. If not specified, all the attributes will be sent as-is to the report without any formatting.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the attribute.", + }, + "custom_label": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies a custom label for attribute to appear in the xlsx report. If not specified, default attribute name will be used.", + }, + "format": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies a custom format for attribute to appear in the xlsx report. If not specified, the attribute value is sent as-is.", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + "data": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the data returned after evaluating the component.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "description": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies description of the Component.", + }, + "filters": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the filters that are applied on specific report type attributes in order to compose this component.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the attribute.", + }, + "filter_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the type of the filter that needs to be applied.", + }, + "in_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the in filter that are applied on attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute_data_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the data type of the attribute.", + }, + "attribute_labels": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the optional label values for the attribute.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "bool_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of boolean values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeBool, + }, + }, + "int32_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of int32 values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "int64_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of int64 values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "string_filter_values": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of string values to filter results on.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "range_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the filters that are applied on attributes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "lower_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the lower bound value. If specified, all the results which are greater than this value will be returned.", + }, + "upper_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the upper bound value. If specified, all the results which are lesser than this value will be returned.", + }, + }, + }, + }, + "systems_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the systems filter. Specifying this will pre filter all the results provided list of system identifier before applying aggregations.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "system_ids": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies an array of system identifiers. System identifiers may be of format clusterid:clusterincarnationid or a regionid (applicable only in case of DMaaS).", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "system_names": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the optional system names labels.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "time_range_filter_params": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the time range filter. Specifying this will pre filter all the results on necessary resources like Protection Runs etc before applying aggregations. Currently, maximum allowed time range is 60 days.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "date_range": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Enum value for specifying the date range for a time filter. Considered only if lowerBound and upperBound are empty.", + }, + "duration_hours": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the duration preceding the current time for which the data must be fetch i.e fetch data between currentTime and currentTime - durationHours. This filter is only considered if neither upperBound, lowerBound or dateRange is specified.", + }, + "lower_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the lower bound value. If specified, all the results which are greater than this value will be returned.", + }, + "upper_bound": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the upper bound value. If specified, all the results which are lesser than this value will be returned.", + }, + }, + }, + }, + }, + }, + }, + "id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the id of the Component.", + }, + "limit": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the parameters to limit the resulting dataset.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "from": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the offset to which resulting data will be skipped before applying the size parameter. For example if dataset size is 10 objects, from=2 and size=5, then from 10 objects only 5 objects are returned starting from offset 2 i.e., 2 to 7. If not specified, then none of the objects are skipped.", + }, + "size": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the number of objects to be returned from the offset specified.", + }, + }, + }, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the Component.", + }, + "report_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the report type on top of which this Component is created from.", + }, + "sort": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the sorting (ordering) parameters to be applied to the resulting data.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attribute": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the attribute.", + }, + "desc": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + Description: "Specifies whether the sorting order should be descending. Default value is false.", + }, + }, + }, + }, + }, + }, + }, + "generated_timestamp_usecs": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the epoch timestamp in UTC in microseconds.", + }, + "last_refresh_timestamp_usecs": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the last refresh timestamp of data used to evaluate the component. If this parameter is not returned, then 'generatedTimestampUsecs' can be used for last refreshed timestamp of the data. This is epoch timestamp in UTC in microseconds.", + }, + "title": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the title of the report.", + }, + }, + } +} + +func dataSourceIbmBackupRecoveryManagerGetReportPreviewRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + backupRecoveryClient, err := meta.(conns.ClientSession).BackupRecoveryManagerV1() + if err != nil { + tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_report_preview", "read", "initialize-client") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + getReportPreviewOptions := &backuprecoveryv1.GetReportPreviewOptions{} + + getReportPreviewOptions.SetID(d.Get("report_id").(string)) + if _, ok := d.GetOk("component_ids"); ok { + var componentIds []string + for _, v := range d.Get("component_ids").([]interface{}) { + componentIdsItem := v.(string) + componentIds = append(componentIds, componentIdsItem) + } + getReportPreviewOptions.SetComponentIds(componentIds) + } + if _, ok := d.GetOk("filters"); ok { + var filters []backuprecoveryv1.AttributeFilter + for _, v := range d.Get("filters").([]interface{}) { + value := v.(map[string]interface{}) + filtersItem, err := ResourceIbmBackupRecoveryManagerGetReportPreviewMapToAttributeFilter(value) + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "ibm_backup_recovery_manager_get_report_preview", "read", "parse-filters").GetDiag() + } + filters = append(filters, *filtersItem) + } + getReportPreviewOptions.SetFilters(filters) + } + if _, ok := d.GetOk("timezone"); ok { + getReportPreviewOptions.SetTimezone(d.Get("timezone").(string)) + } + + reportPreview, _, err := backupRecoveryClient.GetReportPreviewWithContext(context, getReportPreviewOptions) + if err != nil { + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetReportPreviewWithContext failed: %s", err.Error()), "(Data) ibm_backup_recovery_manager_get_report_preview", "read") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + d.SetId(*reportPreview.ID) + + if !core.IsNil(reportPreview.Components) { + components := []map[string]interface{}{} + for _, componentsItem := range reportPreview.Components { + componentsItemMap, err := DataSourceIbmBackupRecoveryManagerGetReportPreviewComponentToMap(&componentsItem) // #nosec G601 + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_report_preview", "read", "components-to-map").GetDiag() + } + components = append(components, componentsItemMap) + } + if err = d.Set("components", components); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting components: %s", err), "(Data) ibm_backup_recovery_manager_get_report_preview", "read", "set-components").GetDiag() + } + } + + if !core.IsNil(reportPreview.GeneratedTimestampUsecs) { + if err = d.Set("generated_timestamp_usecs", flex.IntValue(reportPreview.GeneratedTimestampUsecs)); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting generated_timestamp_usecs: %s", err), "(Data) ibm_backup_recovery_manager_get_report_preview", "read", "set-generated_timestamp_usecs").GetDiag() + } + } + + if !core.IsNil(reportPreview.LastRefreshTimestampUsecs) { + if err = d.Set("last_refresh_timestamp_usecs", flex.IntValue(reportPreview.LastRefreshTimestampUsecs)); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting last_refresh_timestamp_usecs: %s", err), "(Data) ibm_backup_recovery_manager_get_report_preview", "read", "set-last_refresh_timestamp_usecs").GetDiag() + } + } + + if !core.IsNil(reportPreview.Title) { + if err = d.Set("title", reportPreview.Title); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting title: %s", err), "(Data) ibm_backup_recovery_manager_get_report_preview", "read", "set-title").GetDiag() + } + } + + return nil +} + +func DataSourceIbmBackupRecoveryManagerGetReportPreviewComponentToMap(model *backuprecoveryv1.Component) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.Aggs != nil { + aggsMap, err := DataSourceIbmBackupRecoveryManagerGetReportPreviewAttributeAggregationsToMap(model.Aggs) + if err != nil { + return modelMap, err + } + modelMap["aggs"] = []map[string]interface{}{aggsMap} + } + if model.Config != nil { + configMap, err := DataSourceIbmBackupRecoveryManagerGetReportPreviewCustomConfigParamsToMap(model.Config) + if err != nil { + return modelMap, err + } + modelMap["config"] = []map[string]interface{}{configMap} + } + if model.Data != nil { + modelMap["data"] = model.Data + } + if model.Description != nil { + modelMap["description"] = *model.Description + } + if model.Filters != nil { + filters := []map[string]interface{}{} + for _, filtersItem := range model.Filters { + filtersItemMap, err := DataSourceIbmBackupRecoveryManagerGetReportPreviewAttributeFilterToMap(&filtersItem) // #nosec G601 + if err != nil { + return modelMap, err + } + filters = append(filters, filtersItemMap) + } + modelMap["filters"] = filters + } + if model.ID != nil { + modelMap["id"] = *model.ID + } + if model.Limit != nil { + limitMap, err := DataSourceIbmBackupRecoveryManagerGetReportPreviewLimitParamsToMap(model.Limit) + if err != nil { + return modelMap, err + } + modelMap["limit"] = []map[string]interface{}{limitMap} + } + if model.Name != nil { + modelMap["name"] = *model.Name + } + if model.ReportType != nil { + modelMap["report_type"] = *model.ReportType + } + if model.Sort != nil { + sort := []map[string]interface{}{} + for _, sortItem := range model.Sort { + sortItemMap, err := DataSourceIbmBackupRecoveryManagerGetReportPreviewAttributeSortToMap(&sortItem) // #nosec G601 + if err != nil { + return modelMap, err + } + sort = append(sort, sortItemMap) + } + modelMap["sort"] = sort + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetReportPreviewAttributeAggregationsToMap(model *backuprecoveryv1.AttributeAggregations) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.AggregatedAttributes != nil { + aggregatedAttributes := []map[string]interface{}{} + for _, aggregatedAttributesItem := range model.AggregatedAttributes { + aggregatedAttributesItemMap, err := DataSourceIbmBackupRecoveryManagerGetReportPreviewAggregatedAttributesParamsToMap(&aggregatedAttributesItem) // #nosec G601 + if err != nil { + return modelMap, err + } + aggregatedAttributes = append(aggregatedAttributes, aggregatedAttributesItemMap) + } + modelMap["aggregated_attributes"] = aggregatedAttributes + } + if model.GroupedAttributes != nil { + modelMap["grouped_attributes"] = model.GroupedAttributes + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetReportPreviewAggregatedAttributesParamsToMap(model *backuprecoveryv1.AggregatedAttributesParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["aggregation_type"] = *model.AggregationType + modelMap["attribute"] = *model.Attribute + if model.Label != nil { + modelMap["label"] = *model.Label + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetReportPreviewCustomConfigParamsToMap(model *backuprecoveryv1.CustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.XlsxParams != nil { + xlsxParamsMap, err := DataSourceIbmBackupRecoveryManagerGetReportPreviewXlsxCustomConfigParamsToMap(model.XlsxParams) + if err != nil { + return modelMap, err + } + modelMap["xlsx_params"] = []map[string]interface{}{xlsxParamsMap} + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetReportPreviewXlsxCustomConfigParamsToMap(model *backuprecoveryv1.XlsxCustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.AttributeConfig != nil { + attributeConfig := []map[string]interface{}{} + for _, attributeConfigItem := range model.AttributeConfig { + attributeConfigItemMap, err := DataSourceIbmBackupRecoveryManagerGetReportPreviewXlsxAttributeCustomConfigParamsToMap(&attributeConfigItem) // #nosec G601 + if err != nil { + return modelMap, err + } + attributeConfig = append(attributeConfig, attributeConfigItemMap) + } + modelMap["attribute_config"] = attributeConfig + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetReportPreviewXlsxAttributeCustomConfigParamsToMap(model *backuprecoveryv1.XlsxAttributeCustomConfigParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute_name"] = *model.AttributeName + if model.CustomLabel != nil { + modelMap["custom_label"] = *model.CustomLabel + } + if model.Format != nil { + modelMap["format"] = *model.Format + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetReportPreviewAttributeFilterToMap(model *backuprecoveryv1.AttributeFilter) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute"] = *model.Attribute + modelMap["filter_type"] = *model.FilterType + if model.InFilterParams != nil { + inFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetReportPreviewInFilterParamsToMap(model.InFilterParams) + if err != nil { + return modelMap, err + } + modelMap["in_filter_params"] = []map[string]interface{}{inFilterParamsMap} + } + if model.RangeFilterParams != nil { + rangeFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetReportPreviewRangeFilterParamsToMap(model.RangeFilterParams) + if err != nil { + return modelMap, err + } + modelMap["range_filter_params"] = []map[string]interface{}{rangeFilterParamsMap} + } + if model.SystemsFilterParams != nil { + systemsFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetReportPreviewSystemsFilterParamsToMap(model.SystemsFilterParams) + if err != nil { + return modelMap, err + } + modelMap["systems_filter_params"] = []map[string]interface{}{systemsFilterParamsMap} + } + if model.TimeRangeFilterParams != nil { + timeRangeFilterParamsMap, err := DataSourceIbmBackupRecoveryManagerGetReportPreviewTimeRangeFilterParamsToMap(model.TimeRangeFilterParams) + if err != nil { + return modelMap, err + } + modelMap["time_range_filter_params"] = []map[string]interface{}{timeRangeFilterParamsMap} + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetReportPreviewInFilterParamsToMap(model *backuprecoveryv1.InFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute_data_type"] = *model.AttributeDataType + if model.AttributeLabels != nil { + modelMap["attribute_labels"] = model.AttributeLabels + } + if model.BoolFilterValues != nil { + modelMap["bool_filter_values"] = model.BoolFilterValues + } + if model.Int32FilterValues != nil { + modelMap["int32_filter_values"] = model.Int32FilterValues + } + if model.Int64FilterValues != nil { + modelMap["int64_filter_values"] = model.Int64FilterValues + } + if model.StringFilterValues != nil { + modelMap["string_filter_values"] = model.StringFilterValues + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetReportPreviewRangeFilterParamsToMap(model *backuprecoveryv1.RangeFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.LowerBound != nil { + modelMap["lower_bound"] = flex.IntValue(model.LowerBound) + } + if model.UpperBound != nil { + modelMap["upper_bound"] = flex.IntValue(model.UpperBound) + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetReportPreviewSystemsFilterParamsToMap(model *backuprecoveryv1.SystemsFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["system_ids"] = model.SystemIds + if model.SystemNames != nil { + modelMap["system_names"] = model.SystemNames + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetReportPreviewTimeRangeFilterParamsToMap(model *backuprecoveryv1.TimeRangeFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.DateRange != nil { + modelMap["date_range"] = *model.DateRange + } + if model.DurationHours != nil { + modelMap["duration_hours"] = flex.IntValue(model.DurationHours) + } + if model.LowerBound != nil { + modelMap["lower_bound"] = flex.IntValue(model.LowerBound) + } + if model.UpperBound != nil { + modelMap["upper_bound"] = flex.IntValue(model.UpperBound) + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetReportPreviewLimitParamsToMap(model *backuprecoveryv1.LimitParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.From != nil { + modelMap["from"] = flex.IntValue(model.From) + } + modelMap["size"] = flex.IntValue(model.Size) + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetReportPreviewAttributeSortToMap(model *backuprecoveryv1.AttributeSort) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute"] = *model.Attribute + if model.Desc != nil { + modelMap["desc"] = *model.Desc + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetReportPreviewMapToAttributeFilter(modelMap map[string]interface{}) (*backuprecoveryv1.AttributeFilter, error) { + model := &backuprecoveryv1.AttributeFilter{} + model.Attribute = core.StringPtr(modelMap["attribute"].(string)) + model.FilterType = core.StringPtr(modelMap["filter_type"].(string)) + if modelMap["in_filter_params"] != nil && len(modelMap["in_filter_params"].([]interface{})) > 0 { + InFilterParamsModel, err := ResourceIbmBackupRecoveryManagerGetReportPreviewMapToInFilterParams(modelMap["in_filter_params"].([]interface{})[0].(map[string]interface{})) + if err != nil { + return model, err + } + model.InFilterParams = InFilterParamsModel + } + if modelMap["range_filter_params"] != nil && len(modelMap["range_filter_params"].([]interface{})) > 0 { + RangeFilterParamsModel, err := ResourceIbmBackupRecoveryManagerGetReportPreviewMapToRangeFilterParams(modelMap["range_filter_params"].([]interface{})[0].(map[string]interface{})) + if err != nil { + return model, err + } + model.RangeFilterParams = RangeFilterParamsModel + } + if modelMap["systems_filter_params"] != nil && len(modelMap["systems_filter_params"].([]interface{})) > 0 { + SystemsFilterParamsModel, err := ResourceIbmBackupRecoveryManagerGetReportPreviewMapToSystemsFilterParams(modelMap["systems_filter_params"].([]interface{})[0].(map[string]interface{})) + if err != nil { + return model, err + } + model.SystemsFilterParams = SystemsFilterParamsModel + } + if modelMap["time_range_filter_params"] != nil && len(modelMap["time_range_filter_params"].([]interface{})) > 0 { + TimeRangeFilterParamsModel, err := ResourceIbmBackupRecoveryManagerGetReportPreviewMapToTimeRangeFilterParams(modelMap["time_range_filter_params"].([]interface{})[0].(map[string]interface{})) + if err != nil { + return model, err + } + model.TimeRangeFilterParams = TimeRangeFilterParamsModel + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerGetReportPreviewMapToInFilterParams(modelMap map[string]interface{}) (*backuprecoveryv1.InFilterParams, error) { + model := &backuprecoveryv1.InFilterParams{} + model.AttributeDataType = core.StringPtr(modelMap["attribute_data_type"].(string)) + if modelMap["attribute_labels"] != nil { + attributeLabels := []string{} + for _, attributeLabelsItem := range modelMap["attribute_labels"].([]interface{}) { + attributeLabels = append(attributeLabels, attributeLabelsItem.(string)) + } + model.AttributeLabels = attributeLabels + } + if modelMap["bool_filter_values"] != nil { + boolFilterValues := []bool{} + for _, boolFilterValuesItem := range modelMap["bool_filter_values"].([]interface{}) { + boolFilterValues = append(boolFilterValues, boolFilterValuesItem.(bool)) + } + model.BoolFilterValues = boolFilterValues + } + if modelMap["int32_filter_values"] != nil { + int32FilterValues := []int64{} + for _, int32FilterValuesItem := range modelMap["int32_filter_values"].([]interface{}) { + int32FilterValues = append(int32FilterValues, int64(int32FilterValuesItem.(int))) + } + model.Int32FilterValues = int32FilterValues + } + if modelMap["int64_filter_values"] != nil { + int64FilterValues := []int64{} + for _, int64FilterValuesItem := range modelMap["int64_filter_values"].([]interface{}) { + int64FilterValues = append(int64FilterValues, int64(int64FilterValuesItem.(int))) + } + model.Int64FilterValues = int64FilterValues + } + if modelMap["string_filter_values"] != nil { + stringFilterValues := []string{} + for _, stringFilterValuesItem := range modelMap["string_filter_values"].([]interface{}) { + stringFilterValues = append(stringFilterValues, stringFilterValuesItem.(string)) + } + model.StringFilterValues = stringFilterValues + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerGetReportPreviewMapToRangeFilterParams(modelMap map[string]interface{}) (*backuprecoveryv1.RangeFilterParams, error) { + model := &backuprecoveryv1.RangeFilterParams{} + if modelMap["lower_bound"] != nil { + model.LowerBound = core.Int64Ptr(int64(modelMap["lower_bound"].(int))) + } + if modelMap["upper_bound"] != nil { + model.UpperBound = core.Int64Ptr(int64(modelMap["upper_bound"].(int))) + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerGetReportPreviewMapToSystemsFilterParams(modelMap map[string]interface{}) (*backuprecoveryv1.SystemsFilterParams, error) { + model := &backuprecoveryv1.SystemsFilterParams{} + systemIds := []string{} + for _, systemIdsItem := range modelMap["system_ids"].([]interface{}) { + systemIds = append(systemIds, systemIdsItem.(string)) + } + model.SystemIds = systemIds + if modelMap["system_names"] != nil { + systemNames := []string{} + for _, systemNamesItem := range modelMap["system_names"].([]interface{}) { + systemNames = append(systemNames, systemNamesItem.(string)) + } + model.SystemNames = systemNames + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerGetReportPreviewMapToTimeRangeFilterParams(modelMap map[string]interface{}) (*backuprecoveryv1.TimeRangeFilterParams, error) { + model := &backuprecoveryv1.TimeRangeFilterParams{} + if modelMap["date_range"] != nil && modelMap["date_range"].(string) != "" { + model.DateRange = core.StringPtr(modelMap["date_range"].(string)) + } + if modelMap["duration_hours"] != nil { + model.DurationHours = core.Int64Ptr(int64(modelMap["duration_hours"].(int))) + } + if modelMap["lower_bound"] != nil { + model.LowerBound = core.Int64Ptr(int64(modelMap["lower_bound"].(int))) + } + if modelMap["upper_bound"] != nil { + model.UpperBound = core.Int64Ptr(int64(modelMap["upper_bound"].(int))) + } + return model, nil +} + +func ResourceIbmBackupRecoveryManagerGetReportPreviewAttributeFilterToMap(model *backuprecoveryv1.AttributeFilter) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute"] = *model.Attribute + modelMap["filter_type"] = *model.FilterType + if model.InFilterParams != nil { + inFilterParamsMap, err := ResourceIbmBackupRecoveryManagerGetReportPreviewInFilterParamsToMap(model.InFilterParams) + if err != nil { + return modelMap, err + } + modelMap["in_filter_params"] = []map[string]interface{}{inFilterParamsMap} + } + if model.RangeFilterParams != nil { + rangeFilterParamsMap, err := ResourceIbmBackupRecoveryManagerGetReportPreviewRangeFilterParamsToMap(model.RangeFilterParams) + if err != nil { + return modelMap, err + } + modelMap["range_filter_params"] = []map[string]interface{}{rangeFilterParamsMap} + } + if model.SystemsFilterParams != nil { + systemsFilterParamsMap, err := ResourceIbmBackupRecoveryManagerGetReportPreviewSystemsFilterParamsToMap(model.SystemsFilterParams) + if err != nil { + return modelMap, err + } + modelMap["systems_filter_params"] = []map[string]interface{}{systemsFilterParamsMap} + } + if model.TimeRangeFilterParams != nil { + timeRangeFilterParamsMap, err := ResourceIbmBackupRecoveryManagerGetReportPreviewTimeRangeFilterParamsToMap(model.TimeRangeFilterParams) + if err != nil { + return modelMap, err + } + modelMap["time_range_filter_params"] = []map[string]interface{}{timeRangeFilterParamsMap} + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetReportPreviewInFilterParamsToMap(model *backuprecoveryv1.InFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["attribute_data_type"] = *model.AttributeDataType + if model.AttributeLabels != nil { + modelMap["attribute_labels"] = model.AttributeLabels + } + if model.BoolFilterValues != nil { + modelMap["bool_filter_values"] = model.BoolFilterValues + } + if model.Int32FilterValues != nil { + modelMap["int32_filter_values"] = model.Int32FilterValues + } + if model.Int64FilterValues != nil { + modelMap["int64_filter_values"] = model.Int64FilterValues + } + if model.StringFilterValues != nil { + modelMap["string_filter_values"] = model.StringFilterValues + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetReportPreviewRangeFilterParamsToMap(model *backuprecoveryv1.RangeFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.LowerBound != nil { + modelMap["lower_bound"] = flex.IntValue(model.LowerBound) + } + if model.UpperBound != nil { + modelMap["upper_bound"] = flex.IntValue(model.UpperBound) + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetReportPreviewSystemsFilterParamsToMap(model *backuprecoveryv1.SystemsFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + modelMap["system_ids"] = model.SystemIds + if model.SystemNames != nil { + modelMap["system_names"] = model.SystemNames + } + return modelMap, nil +} + +func ResourceIbmBackupRecoveryManagerGetReportPreviewTimeRangeFilterParamsToMap(model *backuprecoveryv1.TimeRangeFilterParams) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.DateRange != nil { + modelMap["date_range"] = *model.DateRange + } + if model.DurationHours != nil { + modelMap["duration_hours"] = flex.IntValue(model.DurationHours) + } + if model.LowerBound != nil { + modelMap["lower_bound"] = flex.IntValue(model.LowerBound) + } + if model.UpperBound != nil { + modelMap["upper_bound"] = flex.IntValue(model.UpperBound) + } + return modelMap, nil +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_preview_test.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_preview_test.go new file mode 100644 index 0000000000..ce6feacb8f --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_preview_test.go @@ -0,0 +1,514 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.96.1-5136e54a-20241108-203028 + */ + +package backuprecovery_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/service/backuprecovery" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" + "github.com/stretchr/testify/assert" +) + +func TestAccIbmBackupRecoveryManagerGetReportPreviewDataSourceBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckIbmBackupRecoveryManagerGetReportPreviewDataSourceConfigBasic(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_get_report_preview.backup_recovery_manager_get_report_preview_instance", "id"), + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_get_report_preview.backup_recovery_manager_get_report_preview_instance", "backup_recovery_manager_get_report_preview_id"), + ), + }, + }, + }) +} + +func testAccCheckIbmBackupRecoveryManagerGetReportPreviewDataSourceConfigBasic() string { + return fmt.Sprintf(` + data "ibm_backup_recovery_manager_get_report_preview" "backup_recovery_manager_get_report_preview_instance" { + id = "id" + componentIds = [ "componentIds" ] + filters = [ { attribute="attribute", filter_type="In", in_filter_params={ attribute_data_type="Bool", attribute_labels=[ "attributeLabels" ], bool_filter_values=[ true ], int32_filter_values=[ 1 ], int64_filter_values=[ 1 ], string_filter_values=[ "stringFilterValues" ] }, range_filter_params={ lower_bound=1, upper_bound=1 }, systems_filter_params={ system_ids=[ "systemIds" ], system_names=[ "systemNames" ] }, time_range_filter_params={ date_range="Last1Hour", duration_hours=1, lower_bound=1, upper_bound=1 } } ] + timezone = "timezone" + } + `) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportPreviewComponentToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + aggregatedAttributesParamsModel := make(map[string]interface{}) + aggregatedAttributesParamsModel["aggregation_type"] = "sum" + aggregatedAttributesParamsModel["attribute"] = "testString" + aggregatedAttributesParamsModel["label"] = "testString" + + attributeAggregationsModel := make(map[string]interface{}) + attributeAggregationsModel["aggregated_attributes"] = []map[string]interface{}{aggregatedAttributesParamsModel} + attributeAggregationsModel["grouped_attributes"] = []string{"testString"} + + xlsxAttributeCustomConfigParamsModel := make(map[string]interface{}) + xlsxAttributeCustomConfigParamsModel["attribute_name"] = "testString" + xlsxAttributeCustomConfigParamsModel["custom_label"] = "testString" + xlsxAttributeCustomConfigParamsModel["format"] = "Timestamp" + + xlsxCustomConfigParamsModel := make(map[string]interface{}) + xlsxCustomConfigParamsModel["attribute_config"] = []map[string]interface{}{xlsxAttributeCustomConfigParamsModel} + + customConfigParamsModel := make(map[string]interface{}) + customConfigParamsModel["xlsx_params"] = []map[string]interface{}{xlsxCustomConfigParamsModel} + + inFilterParamsModel := make(map[string]interface{}) + inFilterParamsModel["attribute_data_type"] = "Bool" + inFilterParamsModel["attribute_labels"] = []string{"testString"} + inFilterParamsModel["bool_filter_values"] = []bool{true} + inFilterParamsModel["int32_filter_values"] = []int64{int64(38)} + inFilterParamsModel["int64_filter_values"] = []int64{int64(26)} + inFilterParamsModel["string_filter_values"] = []string{"testString"} + + rangeFilterParamsModel := make(map[string]interface{}) + rangeFilterParamsModel["lower_bound"] = int(26) + rangeFilterParamsModel["upper_bound"] = int(26) + + systemsFilterParamsModel := make(map[string]interface{}) + systemsFilterParamsModel["system_ids"] = []string{"testString"} + systemsFilterParamsModel["system_names"] = []string{"testString"} + + timeRangeFilterParamsModel := make(map[string]interface{}) + timeRangeFilterParamsModel["date_range"] = "Last1Hour" + timeRangeFilterParamsModel["duration_hours"] = int(26) + timeRangeFilterParamsModel["lower_bound"] = int(26) + timeRangeFilterParamsModel["upper_bound"] = int(26) + + attributeFilterModel := make(map[string]interface{}) + attributeFilterModel["attribute"] = "testString" + attributeFilterModel["filter_type"] = "In" + attributeFilterModel["in_filter_params"] = []map[string]interface{}{inFilterParamsModel} + attributeFilterModel["range_filter_params"] = []map[string]interface{}{rangeFilterParamsModel} + attributeFilterModel["systems_filter_params"] = []map[string]interface{}{systemsFilterParamsModel} + attributeFilterModel["time_range_filter_params"] = []map[string]interface{}{timeRangeFilterParamsModel} + + limitParamsModel := make(map[string]interface{}) + limitParamsModel["from"] = int(38) + limitParamsModel["size"] = int(1) + + attributeSortModel := make(map[string]interface{}) + attributeSortModel["attribute"] = "testString" + attributeSortModel["desc"] = true + + model := make(map[string]interface{}) + model["aggs"] = []map[string]interface{}{attributeAggregationsModel} + model["config"] = []map[string]interface{}{customConfigParamsModel} + model["data"] = []map[string]interface{}{map[string]interface{}{"anyKey": "anyValue"}} + model["description"] = "testString" + model["filters"] = []map[string]interface{}{attributeFilterModel} + model["id"] = "testString" + model["limit"] = []map[string]interface{}{limitParamsModel} + model["name"] = "testString" + model["report_type"] = "Failures" + model["sort"] = []map[string]interface{}{attributeSortModel} + + assert.Equal(t, result, model) + } + + aggregatedAttributesParamsModel := new(backuprecoveryv1.AggregatedAttributesParams) + aggregatedAttributesParamsModel.AggregationType = core.StringPtr("sum") + aggregatedAttributesParamsModel.Attribute = core.StringPtr("testString") + aggregatedAttributesParamsModel.Label = core.StringPtr("testString") + + attributeAggregationsModel := new(backuprecoveryv1.AttributeAggregations) + attributeAggregationsModel.AggregatedAttributes = []backuprecoveryv1.AggregatedAttributesParams{*aggregatedAttributesParamsModel} + attributeAggregationsModel.GroupedAttributes = []string{"testString"} + + xlsxAttributeCustomConfigParamsModel := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + xlsxAttributeCustomConfigParamsModel.AttributeName = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.CustomLabel = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.Format = core.StringPtr("Timestamp") + + xlsxCustomConfigParamsModel := new(backuprecoveryv1.XlsxCustomConfigParams) + xlsxCustomConfigParamsModel.AttributeConfig = []backuprecoveryv1.XlsxAttributeCustomConfigParams{*xlsxAttributeCustomConfigParamsModel} + + customConfigParamsModel := new(backuprecoveryv1.CustomConfigParams) + customConfigParamsModel.XlsxParams = xlsxCustomConfigParamsModel + + inFilterParamsModel := new(backuprecoveryv1.InFilterParams) + inFilterParamsModel.AttributeDataType = core.StringPtr("Bool") + inFilterParamsModel.AttributeLabels = []string{"testString"} + inFilterParamsModel.BoolFilterValues = []bool{true} + inFilterParamsModel.Int32FilterValues = []int64{int64(38)} + inFilterParamsModel.Int64FilterValues = []int64{int64(26)} + inFilterParamsModel.StringFilterValues = []string{"testString"} + + rangeFilterParamsModel := new(backuprecoveryv1.RangeFilterParams) + rangeFilterParamsModel.LowerBound = core.Int64Ptr(int64(26)) + rangeFilterParamsModel.UpperBound = core.Int64Ptr(int64(26)) + + systemsFilterParamsModel := new(backuprecoveryv1.SystemsFilterParams) + systemsFilterParamsModel.SystemIds = []string{"testString"} + systemsFilterParamsModel.SystemNames = []string{"testString"} + + timeRangeFilterParamsModel := new(backuprecoveryv1.TimeRangeFilterParams) + timeRangeFilterParamsModel.DateRange = core.StringPtr("Last1Hour") + timeRangeFilterParamsModel.DurationHours = core.Int64Ptr(int64(26)) + timeRangeFilterParamsModel.LowerBound = core.Int64Ptr(int64(26)) + timeRangeFilterParamsModel.UpperBound = core.Int64Ptr(int64(26)) + + attributeFilterModel := new(backuprecoveryv1.AttributeFilter) + attributeFilterModel.Attribute = core.StringPtr("testString") + attributeFilterModel.FilterType = core.StringPtr("In") + attributeFilterModel.InFilterParams = inFilterParamsModel + attributeFilterModel.RangeFilterParams = rangeFilterParamsModel + attributeFilterModel.SystemsFilterParams = systemsFilterParamsModel + attributeFilterModel.TimeRangeFilterParams = timeRangeFilterParamsModel + + limitParamsModel := new(backuprecoveryv1.LimitParams) + limitParamsModel.From = core.Int64Ptr(int64(38)) + limitParamsModel.Size = core.Int64Ptr(int64(1)) + + attributeSortModel := new(backuprecoveryv1.AttributeSort) + attributeSortModel.Attribute = core.StringPtr("testString") + attributeSortModel.Desc = core.BoolPtr(true) + + model := new(backuprecoveryv1.Component) + model.Aggs = attributeAggregationsModel + model.Config = customConfigParamsModel + model.Data = []map[string]interface{}{map[string]interface{}{"anyKey": "anyValue"}} + model.Description = core.StringPtr("testString") + model.Filters = []backuprecoveryv1.AttributeFilter{*attributeFilterModel} + model.ID = core.StringPtr("testString") + model.Limit = limitParamsModel + model.Name = core.StringPtr("testString") + model.ReportType = core.StringPtr("Failures") + model.Sort = []backuprecoveryv1.AttributeSort{*attributeSortModel} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportPreviewComponentToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportPreviewAttributeAggregationsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + aggregatedAttributesParamsModel := make(map[string]interface{}) + aggregatedAttributesParamsModel["aggregation_type"] = "sum" + aggregatedAttributesParamsModel["attribute"] = "testString" + aggregatedAttributesParamsModel["label"] = "testString" + + model := make(map[string]interface{}) + model["aggregated_attributes"] = []map[string]interface{}{aggregatedAttributesParamsModel} + model["grouped_attributes"] = []string{"testString"} + + assert.Equal(t, result, model) + } + + aggregatedAttributesParamsModel := new(backuprecoveryv1.AggregatedAttributesParams) + aggregatedAttributesParamsModel.AggregationType = core.StringPtr("sum") + aggregatedAttributesParamsModel.Attribute = core.StringPtr("testString") + aggregatedAttributesParamsModel.Label = core.StringPtr("testString") + + model := new(backuprecoveryv1.AttributeAggregations) + model.AggregatedAttributes = []backuprecoveryv1.AggregatedAttributesParams{*aggregatedAttributesParamsModel} + model.GroupedAttributes = []string{"testString"} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportPreviewAttributeAggregationsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportPreviewAggregatedAttributesParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["aggregation_type"] = "sum" + model["attribute"] = "testString" + model["label"] = "testString" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.AggregatedAttributesParams) + model.AggregationType = core.StringPtr("sum") + model.Attribute = core.StringPtr("testString") + model.Label = core.StringPtr("testString") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportPreviewAggregatedAttributesParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportPreviewCustomConfigParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + xlsxAttributeCustomConfigParamsModel := make(map[string]interface{}) + xlsxAttributeCustomConfigParamsModel["attribute_name"] = "testString" + xlsxAttributeCustomConfigParamsModel["custom_label"] = "testString" + xlsxAttributeCustomConfigParamsModel["format"] = "Timestamp" + + xlsxCustomConfigParamsModel := make(map[string]interface{}) + xlsxCustomConfigParamsModel["attribute_config"] = []map[string]interface{}{xlsxAttributeCustomConfigParamsModel} + + model := make(map[string]interface{}) + model["xlsx_params"] = []map[string]interface{}{xlsxCustomConfigParamsModel} + + assert.Equal(t, result, model) + } + + xlsxAttributeCustomConfigParamsModel := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + xlsxAttributeCustomConfigParamsModel.AttributeName = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.CustomLabel = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.Format = core.StringPtr("Timestamp") + + xlsxCustomConfigParamsModel := new(backuprecoveryv1.XlsxCustomConfigParams) + xlsxCustomConfigParamsModel.AttributeConfig = []backuprecoveryv1.XlsxAttributeCustomConfigParams{*xlsxAttributeCustomConfigParamsModel} + + model := new(backuprecoveryv1.CustomConfigParams) + model.XlsxParams = xlsxCustomConfigParamsModel + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportPreviewCustomConfigParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportPreviewXlsxCustomConfigParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + xlsxAttributeCustomConfigParamsModel := make(map[string]interface{}) + xlsxAttributeCustomConfigParamsModel["attribute_name"] = "testString" + xlsxAttributeCustomConfigParamsModel["custom_label"] = "testString" + xlsxAttributeCustomConfigParamsModel["format"] = "Timestamp" + + model := make(map[string]interface{}) + model["attribute_config"] = []map[string]interface{}{xlsxAttributeCustomConfigParamsModel} + + assert.Equal(t, result, model) + } + + xlsxAttributeCustomConfigParamsModel := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + xlsxAttributeCustomConfigParamsModel.AttributeName = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.CustomLabel = core.StringPtr("testString") + xlsxAttributeCustomConfigParamsModel.Format = core.StringPtr("Timestamp") + + model := new(backuprecoveryv1.XlsxCustomConfigParams) + model.AttributeConfig = []backuprecoveryv1.XlsxAttributeCustomConfigParams{*xlsxAttributeCustomConfigParamsModel} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportPreviewXlsxCustomConfigParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportPreviewXlsxAttributeCustomConfigParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["attribute_name"] = "testString" + model["custom_label"] = "testString" + model["format"] = "Timestamp" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.XlsxAttributeCustomConfigParams) + model.AttributeName = core.StringPtr("testString") + model.CustomLabel = core.StringPtr("testString") + model.Format = core.StringPtr("Timestamp") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportPreviewXlsxAttributeCustomConfigParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportPreviewAttributeFilterToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + inFilterParamsModel := make(map[string]interface{}) + inFilterParamsModel["attribute_data_type"] = "Bool" + inFilterParamsModel["attribute_labels"] = []string{"testString"} + inFilterParamsModel["bool_filter_values"] = []bool{true} + inFilterParamsModel["int32_filter_values"] = []int64{int64(38)} + inFilterParamsModel["int64_filter_values"] = []int64{int64(26)} + inFilterParamsModel["string_filter_values"] = []string{"testString"} + + rangeFilterParamsModel := make(map[string]interface{}) + rangeFilterParamsModel["lower_bound"] = int(26) + rangeFilterParamsModel["upper_bound"] = int(26) + + systemsFilterParamsModel := make(map[string]interface{}) + systemsFilterParamsModel["system_ids"] = []string{"testString"} + systemsFilterParamsModel["system_names"] = []string{"testString"} + + timeRangeFilterParamsModel := make(map[string]interface{}) + timeRangeFilterParamsModel["date_range"] = "Last1Hour" + timeRangeFilterParamsModel["duration_hours"] = int(26) + timeRangeFilterParamsModel["lower_bound"] = int(26) + timeRangeFilterParamsModel["upper_bound"] = int(26) + + model := make(map[string]interface{}) + model["attribute"] = "testString" + model["filter_type"] = "In" + model["in_filter_params"] = []map[string]interface{}{inFilterParamsModel} + model["range_filter_params"] = []map[string]interface{}{rangeFilterParamsModel} + model["systems_filter_params"] = []map[string]interface{}{systemsFilterParamsModel} + model["time_range_filter_params"] = []map[string]interface{}{timeRangeFilterParamsModel} + + assert.Equal(t, result, model) + } + + inFilterParamsModel := new(backuprecoveryv1.InFilterParams) + inFilterParamsModel.AttributeDataType = core.StringPtr("Bool") + inFilterParamsModel.AttributeLabels = []string{"testString"} + inFilterParamsModel.BoolFilterValues = []bool{true} + inFilterParamsModel.Int32FilterValues = []int64{int64(38)} + inFilterParamsModel.Int64FilterValues = []int64{int64(26)} + inFilterParamsModel.StringFilterValues = []string{"testString"} + + rangeFilterParamsModel := new(backuprecoveryv1.RangeFilterParams) + rangeFilterParamsModel.LowerBound = core.Int64Ptr(int64(26)) + rangeFilterParamsModel.UpperBound = core.Int64Ptr(int64(26)) + + systemsFilterParamsModel := new(backuprecoveryv1.SystemsFilterParams) + systemsFilterParamsModel.SystemIds = []string{"testString"} + systemsFilterParamsModel.SystemNames = []string{"testString"} + + timeRangeFilterParamsModel := new(backuprecoveryv1.TimeRangeFilterParams) + timeRangeFilterParamsModel.DateRange = core.StringPtr("Last1Hour") + timeRangeFilterParamsModel.DurationHours = core.Int64Ptr(int64(26)) + timeRangeFilterParamsModel.LowerBound = core.Int64Ptr(int64(26)) + timeRangeFilterParamsModel.UpperBound = core.Int64Ptr(int64(26)) + + model := new(backuprecoveryv1.AttributeFilter) + model.Attribute = core.StringPtr("testString") + model.FilterType = core.StringPtr("In") + model.InFilterParams = inFilterParamsModel + model.RangeFilterParams = rangeFilterParamsModel + model.SystemsFilterParams = systemsFilterParamsModel + model.TimeRangeFilterParams = timeRangeFilterParamsModel + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportPreviewAttributeFilterToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportPreviewInFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["attribute_data_type"] = "Bool" + model["attribute_labels"] = []string{"testString"} + model["bool_filter_values"] = []bool{true} + model["int32_filter_values"] = []int64{int64(38)} + model["int64_filter_values"] = []int64{int64(26)} + model["string_filter_values"] = []string{"testString"} + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.InFilterParams) + model.AttributeDataType = core.StringPtr("Bool") + model.AttributeLabels = []string{"testString"} + model.BoolFilterValues = []bool{true} + model.Int32FilterValues = []int64{int64(38)} + model.Int64FilterValues = []int64{int64(26)} + model.StringFilterValues = []string{"testString"} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportPreviewInFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportPreviewRangeFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["lower_bound"] = int(26) + model["upper_bound"] = int(26) + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.RangeFilterParams) + model.LowerBound = core.Int64Ptr(int64(26)) + model.UpperBound = core.Int64Ptr(int64(26)) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportPreviewRangeFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportPreviewSystemsFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["system_ids"] = []string{"testString"} + model["system_names"] = []string{"testString"} + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.SystemsFilterParams) + model.SystemIds = []string{"testString"} + model.SystemNames = []string{"testString"} + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportPreviewSystemsFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportPreviewTimeRangeFilterParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["date_range"] = "Last1Hour" + model["duration_hours"] = int(26) + model["lower_bound"] = int(26) + model["upper_bound"] = int(26) + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.TimeRangeFilterParams) + model.DateRange = core.StringPtr("Last1Hour") + model.DurationHours = core.Int64Ptr(int64(26)) + model.LowerBound = core.Int64Ptr(int64(26)) + model.UpperBound = core.Int64Ptr(int64(26)) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportPreviewTimeRangeFilterParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportPreviewLimitParamsToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["from"] = int(38) + model["size"] = int(1) + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.LimitParams) + model.From = core.Int64Ptr(int64(38)) + model.Size = core.Int64Ptr(int64(1)) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportPreviewLimitParamsToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportPreviewAttributeSortToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["attribute"] = "testString" + model["desc"] = true + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.AttributeSort) + model.Attribute = core.StringPtr("testString") + model.Desc = core.BoolPtr(true) + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportPreviewAttributeSortToMap(model) + assert.Nil(t, err) + checkResult(result) +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_test.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_test.go new file mode 100644 index 0000000000..467762677d --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_test.go @@ -0,0 +1,41 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.105.1-067d600b-20250616-154447 + */ + +package backuprecovery_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" +) + +func TestAccIbmBackupRecoveryManagerGetReportDataSourceBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckIbmBackupRecoveryManagerGetReportDataSourceConfigBasic(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_get_report.backup_recovery_manager_get_report_instance", "id"), + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_get_report.backup_recovery_manager_get_report_instance", "backup_recovery_manager_get_report_id"), + ), + }, + }, + }) +} + +func testAccCheckIbmBackupRecoveryManagerGetReportDataSourceConfigBasic() string { + return fmt.Sprintf(` + data "ibm_backup_recovery_manager_get_report" "backup_recovery_manager_get_report_instance" { + id = "id" + } + `) +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_type.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_type.go new file mode 100644 index 0000000000..8eb7d681d0 --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_type.go @@ -0,0 +1,110 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.105.1-067d600b-20250616-154447 + */ + +package backuprecovery + +import ( + "context" + "fmt" + "log" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" +) + +func DataSourceIbmBackupRecoveryManagerGetReportType() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceIbmBackupRecoveryManagerGetReportTypeRead, + + Schema: map[string]*schema.Schema{ + "report_type": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Description: "Specifies the report type.", + }, + "attributes": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the attribute name.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "data_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the data type of the attribute.", + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the attribute name.", + }, + }, + }, + }, + }, + } +} + +func dataSourceIbmBackupRecoveryManagerGetReportTypeRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + backupRecoveryClient, err := meta.(conns.ClientSession).BackupRecoveryManagerV1() + if err != nil { + tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_report_type", "read", "initialize-client") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + getReportTypeOptions := &backuprecoveryv1.GetReportTypeOptions{} + + getReportTypeOptions.SetReportType(d.Get("report_type").(string)) + + reportTypeAttributes, _, err := backupRecoveryClient.GetReportTypeWithContext(context, getReportTypeOptions) + if err != nil { + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetReportTypeWithContext failed: %s", err.Error()), "(Data) ibm_backup_recovery_manager_get_report_type", "read") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + d.SetId(dataSourceIbmBackupRecoveryManagerGetReportTypeID(d)) + + if !core.IsNil(reportTypeAttributes.Attributes) { + attributes := []map[string]interface{}{} + for _, attributesItem := range reportTypeAttributes.Attributes { + attributesItemMap, err := DataSourceIbmBackupRecoveryManagerGetReportTypeReportTypeAttributeToMap(&attributesItem) // #nosec G601 + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_report_type", "read", "attributes-to-map").GetDiag() + } + attributes = append(attributes, attributesItemMap) + } + if err = d.Set("attributes", attributes); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting attributes: %s", err), "(Data) ibm_backup_recovery_manager_get_report_type", "read", "set-attributes").GetDiag() + } + } + + return nil +} + +// dataSourceIbmBackupRecoveryManagerGetReportTypeID returns a reasonable ID for the list. +func dataSourceIbmBackupRecoveryManagerGetReportTypeID(d *schema.ResourceData) string { + return time.Now().UTC().String() +} + +func DataSourceIbmBackupRecoveryManagerGetReportTypeReportTypeAttributeToMap(model *backuprecoveryv1.ReportTypeAttribute) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.DataType != nil { + modelMap["data_type"] = *model.DataType + } + if model.Name != nil { + modelMap["name"] = *model.Name + } + return modelMap, nil +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_type_test.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_type_test.go new file mode 100644 index 0000000000..47ce66a942 --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_report_type_test.go @@ -0,0 +1,63 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.105.1-067d600b-20250616-154447 + */ + +package backuprecovery_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/service/backuprecovery" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" + "github.com/stretchr/testify/assert" +) + +func TestAccIbmBackupRecoveryManagerGetReportTypeDataSourceBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckIbmBackupRecoveryManagerGetReportTypeDataSourceConfigBasic(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_get_report_type.backup_recovery_manager_get_report_type_instance", "id"), + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_get_report_type.backup_recovery_manager_get_report_type_instance", "report_type"), + ), + }, + }, + }) +} + +func testAccCheckIbmBackupRecoveryManagerGetReportTypeDataSourceConfigBasic() string { + return fmt.Sprintf(` + data "ibm_backup_recovery_manager_get_report_type" "backup_recovery_manager_get_report_type_instance" { + reportType = "Failures" + } + `) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportTypeReportTypeAttributeToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["data_type"] = "Bool" + model["name"] = "testString" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.ReportTypeAttribute) + model.DataType = core.StringPtr("Bool") + model.Name = core.StringPtr("testString") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportTypeReportTypeAttributeToMap(model) + assert.Nil(t, err) + checkResult(result) +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_reports.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_reports.go new file mode 100644 index 0000000000..cd444858d9 --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_reports.go @@ -0,0 +1,166 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.105.1-067d600b-20250616-154447 + */ + +package backuprecovery + +import ( + "context" + "fmt" + "log" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" +) + +func DataSourceIbmBackupRecoveryManagerGetReports() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceIbmBackupRecoveryManagerGetReportsRead, + + Schema: map[string]*schema.Schema{ + "ids": &schema.Schema{ + Type: schema.TypeList, + Optional: true, + Description: "Specifies the ids of reports to fetch.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "user_context": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Description: "Specifies the user context to filter reports.", + }, + "reports": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of reports.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "category": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies categoty of the Report.", + }, + "component_ids": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the list of component ids in the Report.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "description": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies description of the Report.", + }, + "id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the id of the report.", + }, + "supported_user_contexts": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies all the supported user contexts for this report.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "title": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the title of the report.", + }, + }, + }, + }, + }, + } +} + +func dataSourceIbmBackupRecoveryManagerGetReportsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + backupRecoveryClient, err := meta.(conns.ClientSession).BackupRecoveryManagerV1() + if err != nil { + tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_reports", "read", "initialize-client") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + getReportsOptions := &backuprecoveryv1.GetReportsOptions{} + + if _, ok := d.GetOk("ids"); ok { + var ids []string + for _, v := range d.Get("ids").([]interface{}) { + idsItem := v.(string) + ids = append(ids, idsItem) + } + getReportsOptions.SetIds(ids) + } + if _, ok := d.GetOk("user_context"); ok { + getReportsOptions.SetUserContext(d.Get("user_context").(string)) + } + + reports, _, err := backupRecoveryClient.GetReportsWithContext(context, getReportsOptions) + if err != nil { + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetReportsWithContext failed: %s", err.Error()), "(Data) ibm_backup_recovery_manager_get_reports", "read") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + d.SetId(dataSourceIbmBackupRecoveryManagerGetReportsID(d)) + + if !core.IsNil(reports.Reports) { + reportsMap := []map[string]interface{}{} + for _, reportsItem := range reports.Reports { + reportsItemMap, err := DataSourceIbmBackupRecoveryManagerGetReportsReportToMap(&reportsItem) // #nosec G601 + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_reports", "read", "reports-to-map").GetDiag() + } + reportsMap = append(reportsMap, reportsItemMap) + } + if err = d.Set("reports", reportsMap); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting reports: %s", err), "(Data) ibm_backup_recovery_manager_get_reports", "read", "set-reports").GetDiag() + } + } + + return nil +} + +// dataSourceIbmBackupRecoveryManagerGetReportsID returns a reasonable ID for the list. +func dataSourceIbmBackupRecoveryManagerGetReportsID(d *schema.ResourceData) string { + return time.Now().UTC().String() +} + +func DataSourceIbmBackupRecoveryManagerGetReportsReportToMap(model *backuprecoveryv1.Report) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.Category != nil { + modelMap["category"] = *model.Category + } + if model.ComponentIds != nil { + modelMap["component_ids"] = model.ComponentIds + } + if model.Description != nil { + modelMap["description"] = *model.Description + } + if model.ID != nil { + modelMap["id"] = *model.ID + } + if model.SupportedUserContexts != nil { + modelMap["supported_user_contexts"] = model.SupportedUserContexts + } + if model.Title != nil { + modelMap["title"] = *model.Title + } + return modelMap, nil +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_reports_test.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_reports_test.go new file mode 100644 index 0000000000..c448ca82fb --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_reports_test.go @@ -0,0 +1,71 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.105.1-067d600b-20250616-154447 + */ + +package backuprecovery_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/service/backuprecovery" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" + "github.com/stretchr/testify/assert" +) + +func TestAccIbmBackupRecoveryManagerGetReportsDataSourceBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckIbmBackupRecoveryManagerGetReportsDataSourceConfigBasic(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_get_reports.backup_recovery_manager_get_reports_instance", "id"), + ), + }, + }, + }) +} + +func testAccCheckIbmBackupRecoveryManagerGetReportsDataSourceConfigBasic() string { + return fmt.Sprintf(` + data "ibm_backup_recovery_manager_get_reports" "backup_recovery_manager_get_reports_instance" { + ids = [ "ids" ] + userContext = "IBMBaaS" + } + `) +} + +func TestDataSourceIbmBackupRecoveryManagerGetReportsReportToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["category"] = "Protection" + model["component_ids"] = []string{"testString"} + model["description"] = "testString" + model["id"] = "testString" + model["supported_user_contexts"] = []string{"IBMBaaS"} + model["title"] = "testString" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.Report) + model.Category = core.StringPtr("Protection") + model.ComponentIds = []string{"testString"} + model.Description = core.StringPtr("testString") + model.ID = core.StringPtr("testString") + model.SupportedUserContexts = []string{"IBMBaaS"} + model.Title = core.StringPtr("testString") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetReportsReportToMap(model) + assert.Nil(t, err) + checkResult(result) +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_resources.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_resources.go new file mode 100644 index 0000000000..42299a557b --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_resources.go @@ -0,0 +1,405 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.96.1-5136e54a-20241108-203028 + */ + +package backuprecovery + +import ( + "context" + "fmt" + "log" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" +) + +func DataSourceIbmBackupRecoveryManagerGetResources() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceIbmBackupRecoveryManagerGetResourcesRead, + + Schema: map[string]*schema.Schema{ + "resource_type": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Description: "Specifies the type of the resource.", + }, + "external_targets": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the list of External Targets.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the id of the External target.", + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the External target.", + }, + "system_id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the id of the System.", + }, + "system_name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the System.", + }, + "target_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the type of the External target.", + }, + }, + }, + }, + "message_code_mappings": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the list of Message codes.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "message_code": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the message code.", + }, + "message_guid": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the message GUID.", + }, + }, + }, + }, + "policies": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the list of Protection Groups.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the id of the Protection Policy.", + }, + "is_global_policy": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + Description: "Specifies whether this is a Global Policy.", + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the Protection Policy.", + }, + "system_id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the id of the System.", + }, + "system_name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the System.", + }, + }, + }, + }, + "protection_groups": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the list of Protection Groups.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the id of the Protection Group.", + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the Protection Group.", + }, + "system_id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the id of the System.", + }, + "system_name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the System.", + }, + }, + }, + }, + "sources": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the list of Registered sources.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "environments": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies list of all environments discovered as part of this source.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the registered source.", + }, + "uuid": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the unique identifier of registered source.", + }, + }, + }, + }, + "tenants": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the list of Tenants.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the id of the Tenant.", + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the name of the Tenant.", + }, + }, + }, + }, + }, + } +} + +func dataSourceIbmBackupRecoveryManagerGetResourcesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + backupRecoveryClient, err := meta.(conns.ClientSession).BackupRecoveryManagerV1() + if err != nil { + tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_resources", "read", "initialize-client") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + getResourcesOptions := &backuprecoveryv1.GetResourcesOptions{} + + getResourcesOptions.SetResourceType(d.Get("resource_type").(string)) + + resources, _, err := backupRecoveryClient.GetResourcesWithContext(context, getResourcesOptions) + if err != nil { + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetResourcesWithContext failed: %s", err.Error()), "(Data) ibm_backup_recovery_manager_get_resources", "read") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + d.SetId(dataSourceIbmBackupRecoveryManagerGetResourcesID(d)) + + if !core.IsNil(resources.ExternalTargets) { + externalTargets := []map[string]interface{}{} + for _, externalTargetsItem := range resources.ExternalTargets { + externalTargetsItemMap, err := DataSourceIbmBackupRecoveryManagerGetResourcesExternalTargetToMap(&externalTargetsItem) // #nosec G601 + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_resources", "read", "external_targets-to-map").GetDiag() + } + externalTargets = append(externalTargets, externalTargetsItemMap) + } + if err = d.Set("external_targets", externalTargets); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting external_targets: %s", err), "(Data) ibm_backup_recovery_manager_get_resources", "read", "set-external_targets").GetDiag() + } + } + + if !core.IsNil(resources.MessageCodeMappings) { + messageCodeMappings := []map[string]interface{}{} + for _, messageCodeMappingsItem := range resources.MessageCodeMappings { + messageCodeMappingsItemMap, err := DataSourceIbmBackupRecoveryManagerGetResourcesMessageCodeMappingToMap(&messageCodeMappingsItem) // #nosec G601 + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_resources", "read", "message_code_mappings-to-map").GetDiag() + } + messageCodeMappings = append(messageCodeMappings, messageCodeMappingsItemMap) + } + if err = d.Set("message_code_mappings", messageCodeMappings); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting message_code_mappings: %s", err), "(Data) ibm_backup_recovery_manager_get_resources", "read", "set-message_code_mappings").GetDiag() + } + } + + if !core.IsNil(resources.Policies) { + policies := []map[string]interface{}{} + for _, policiesItem := range resources.Policies { + policiesItemMap, err := DataSourceIbmBackupRecoveryManagerGetResourcesPolicyToMap(&policiesItem) // #nosec G601 + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_resources", "read", "policies-to-map").GetDiag() + } + policies = append(policies, policiesItemMap) + } + if err = d.Set("policies", policies); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting policies: %s", err), "(Data) ibm_backup_recovery_manager_get_resources", "read", "set-policies").GetDiag() + } + } + + if !core.IsNil(resources.ProtectionGroups) { + protectionGroups := []map[string]interface{}{} + for _, protectionGroupsItem := range resources.ProtectionGroups { + protectionGroupsItemMap, err := DataSourceIbmBackupRecoveryManagerGetResourcesProtectionGroupToMap(&protectionGroupsItem) // #nosec G601 + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_resources", "read", "protection_groups-to-map").GetDiag() + } + protectionGroups = append(protectionGroups, protectionGroupsItemMap) + } + if err = d.Set("protection_groups", protectionGroups); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting protection_groups: %s", err), "(Data) ibm_backup_recovery_manager_get_resources", "read", "set-protection_groups").GetDiag() + } + } + + if !core.IsNil(resources.Sources) { + sources := []map[string]interface{}{} + for _, sourcesItem := range resources.Sources { + sourcesItemMap, err := DataSourceIbmBackupRecoveryManagerGetResourcesRegisteredSourceToMap(&sourcesItem) // #nosec G601 + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_resources", "read", "sources-to-map").GetDiag() + } + sources = append(sources, sourcesItemMap) + } + if err = d.Set("sources", sources); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting sources: %s", err), "(Data) ibm_backup_recovery_manager_get_resources", "read", "set-sources").GetDiag() + } + } + + if !core.IsNil(resources.Tenants) { + tenants := []map[string]interface{}{} + for _, tenantsItem := range resources.Tenants { + tenantsItemMap, err := DataSourceIbmBackupRecoveryManagerGetResourcesTenantToMap(&tenantsItem) // #nosec G601 + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_get_resources", "read", "tenants-to-map").GetDiag() + } + tenants = append(tenants, tenantsItemMap) + } + if err = d.Set("tenants", tenants); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting tenants: %s", err), "(Data) ibm_backup_recovery_manager_get_resources", "read", "set-tenants").GetDiag() + } + } + + return nil +} + +// dataSourceIbmBackupRecoveryManagerGetResourcesID returns a reasonable ID for the list. +func dataSourceIbmBackupRecoveryManagerGetResourcesID(d *schema.ResourceData) string { + return time.Now().UTC().String() +} + +func DataSourceIbmBackupRecoveryManagerGetResourcesExternalTargetToMap(model *backuprecoveryv1.ExternalTarget) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.ID != nil { + modelMap["id"] = *model.ID + } + if model.Name != nil { + modelMap["name"] = *model.Name + } + if model.SystemID != nil { + modelMap["system_id"] = *model.SystemID + } + if model.SystemName != nil { + modelMap["system_name"] = *model.SystemName + } + if model.TargetType != nil { + modelMap["target_type"] = *model.TargetType + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetResourcesMessageCodeMappingToMap(model *backuprecoveryv1.MessageCodeMapping) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.MessageCode != nil { + modelMap["message_code"] = *model.MessageCode + } + if model.MessageGuid != nil { + modelMap["message_guid"] = *model.MessageGuid + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetResourcesPolicyToMap(model *backuprecoveryv1.Policy) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.ID != nil { + modelMap["id"] = *model.ID + } + if model.IsGlobalPolicy != nil { + modelMap["is_global_policy"] = *model.IsGlobalPolicy + } + if model.Name != nil { + modelMap["name"] = *model.Name + } + if model.SystemID != nil { + modelMap["system_id"] = *model.SystemID + } + if model.SystemName != nil { + modelMap["system_name"] = *model.SystemName + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetResourcesProtectionGroupToMap(model *backuprecoveryv1.ProtectionGroup) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.ID != nil { + modelMap["id"] = *model.ID + } + if model.Name != nil { + modelMap["name"] = *model.Name + } + if model.SystemID != nil { + modelMap["system_id"] = *model.SystemID + } + if model.SystemName != nil { + modelMap["system_name"] = *model.SystemName + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetResourcesRegisteredSourceToMap(model *backuprecoveryv1.RegisteredSource) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.Environments != nil { + modelMap["environments"] = model.Environments + } + if model.Name != nil { + modelMap["name"] = *model.Name + } + if model.UUID != nil { + modelMap["uuid"] = *model.UUID + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerGetResourcesTenantToMap(model *backuprecoveryv1.Tenant) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.ID != nil { + modelMap["id"] = *model.ID + } + if model.Name != nil { + modelMap["name"] = *model.Name + } + return modelMap, nil +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_resources_test.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_resources_test.go new file mode 100644 index 0000000000..b336b6e20e --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_get_resources_test.go @@ -0,0 +1,171 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.96.1-5136e54a-20241108-203028 + */ + +package backuprecovery_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/service/backuprecovery" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" + "github.com/stretchr/testify/assert" +) + +func TestAccIbmBackupRecoveryManagerGetResourcesDataSourceBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckIbmBackupRecoveryManagerGetResourcesDataSourceConfigBasic(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_get_resources.backup_recovery_manager_get_resources_instance", "id"), + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_get_resources.backup_recovery_manager_get_resources_instance", "resource_type"), + ), + }, + }, + }) +} + +func testAccCheckIbmBackupRecoveryManagerGetResourcesDataSourceConfigBasic() string { + return fmt.Sprintf(` + data "ibm_backup_recovery_manager_get_resources" "backup_recovery_manager_get_resources_instance" { + resourceType = "Policies" + } + `) +} + +func TestDataSourceIbmBackupRecoveryManagerGetResourcesExternalTargetToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["id"] = "testString" + model["name"] = "testString" + model["system_id"] = "testString" + model["system_name"] = "testString" + model["target_type"] = "testString" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.ExternalTarget) + model.ID = core.StringPtr("testString") + model.Name = core.StringPtr("testString") + model.SystemID = core.StringPtr("testString") + model.SystemName = core.StringPtr("testString") + model.TargetType = core.StringPtr("testString") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetResourcesExternalTargetToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetResourcesMessageCodeMappingToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["message_code"] = "testString" + model["message_guid"] = "testString" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.MessageCodeMapping) + model.MessageCode = core.StringPtr("testString") + model.MessageGuid = core.StringPtr("testString") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetResourcesMessageCodeMappingToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetResourcesPolicyToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["id"] = "testString" + model["is_global_policy"] = true + model["name"] = "testString" + model["system_id"] = "testString" + model["system_name"] = "testString" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.Policy) + model.ID = core.StringPtr("testString") + model.IsGlobalPolicy = core.BoolPtr(true) + model.Name = core.StringPtr("testString") + model.SystemID = core.StringPtr("testString") + model.SystemName = core.StringPtr("testString") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetResourcesPolicyToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetResourcesProtectionGroupToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["id"] = "testString" + model["name"] = "testString" + model["system_id"] = "testString" + model["system_name"] = "testString" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.ProtectionGroup) + model.ID = core.StringPtr("testString") + model.Name = core.StringPtr("testString") + model.SystemID = core.StringPtr("testString") + model.SystemName = core.StringPtr("testString") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetResourcesProtectionGroupToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetResourcesRegisteredSourceToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["environments"] = []string{"testString"} + model["name"] = "testString" + model["uuid"] = "testString" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.RegisteredSource) + model.Environments = []string{"testString"} + model.Name = core.StringPtr("testString") + model.UUID = core.StringPtr("testString") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetResourcesRegisteredSourceToMap(model) + assert.Nil(t, err) + checkResult(result) +} + +func TestDataSourceIbmBackupRecoveryManagerGetResourcesTenantToMap(t *testing.T) { + checkResult := func(result map[string]interface{}) { + model := make(map[string]interface{}) + model["id"] = "testString" + model["name"] = "testString" + + assert.Equal(t, result, model) + } + + model := new(backuprecoveryv1.Tenant) + model.ID = core.StringPtr("testString") + model.Name = core.StringPtr("testString") + + result, err := backuprecovery.DataSourceIbmBackupRecoveryManagerGetResourcesTenantToMap(model) + assert.Nil(t, err) + checkResult(result) +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_sre_get_cluster_info.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_sre_get_cluster_info.go new file mode 100644 index 0000000000..23e6eec245 --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_sre_get_cluster_info.go @@ -0,0 +1,511 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.96.1-5136e54a-20241108-203028 + */ + +package backuprecovery + +import ( + "context" + "fmt" + "log" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/ibm-backup-recovery-sdk-go/backuprecoveryv1" +) + +func DataSourceIbmBackupRecoveryManagerSreGetClusterInfo() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceIbmBackupRecoveryManagerSreGetClusterInfoRead, + + Schema: map[string]*schema.Schema{ + "cohesity_clusters": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the array of clusters upgrade details.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "auth_support_for_pkg_downloads": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + Description: "If cluster can support authHeader for upgrade or not.", + }, + "available_versions": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the release versions the cluster can upgrade to.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "notes": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies release's notes.", + }, + "patch_details": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the details of the available patch release.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "notes": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies patch release's notes.", + }, + "release_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Release's type.", + }, + "version": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies patch release's version.", + }, + }, + }, + }, + "release_stage": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the stage of a release.", + }, + "release_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Release's type e.g, LTS, Feature, Patch, MCM.", + }, + "type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the type of package or release.", + }, + "version": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies release's version.", + }, + }, + }, + }, + "cluster_id": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies cluster id.", + }, + "cluster_incarnation_id": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies cluster incarnation id.", + }, + "cluster_name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies cluster's name.", + }, + "current_patch_version": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies current patch version of the cluster.", + }, + "current_version": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies if the cluster is connected to helios.", + }, + "health": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the health of the cluster.", + }, + "is_connected_to_helios": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + Description: "Specifies if the cluster is connected to helios.", + }, + "location": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the location of the cluster.", + }, + "multi_tenancy_enabled": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + Description: "Specifies if multi tenancy is enabled in the cluster.", + }, + "node_ips": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies an array of node ips for the cluster.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "number_of_nodes": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the number of nodes in the cluster.", + }, + "patch_target_upgrade_url": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the patch package URL for the cluster. This is populated for patch update only.", + }, + "patch_target_version": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies target version to which clusters are upgrading. This is populated for patch update only.", + }, + "provider_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the type of the cluster provider.", + }, + "scheduled_timestamp": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Time at which an upgrade is scheduled.", + }, + "status": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the upgrade status of the cluster.", + }, + "target_upgrade_url": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the upgrade URL for the cluster. This is populated for upgrade only.", + }, + "target_version": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies target version to which clusters are to be upgraded. This is populated for upgrade only.", + }, + "total_capacity": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies how total memory capacity of the cluster.", + }, + "type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the type of the cluster.", + }, + "update_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the type of upgrade performed on a cluster. This is to be used with status field to know the status of the upgrade action performed on cluster.", + }, + "used_capacity": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies how much of the cluster capacity is consumed.", + }, + }, + }, + }, + "sp_clusters": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies the array of clusters claimed from IBM Storage Protect environment.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "cluster_id": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies cluster id.", + }, + "cluster_incarnation_id": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies cluster incarnation id.", + }, + "cluster_name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies cluster's name.", + }, + "current_version": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the currently running version on cluster.", + }, + "health": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the health of the cluster.", + }, + "is_connected_to_helios": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + Description: "Specifies if the cluster is connected to helios.", + }, + "node_ips": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Description: "Specifies an array of node ips for the cluster.", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "number_of_nodes": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies the number of nodes in the cluster.", + }, + "provider_type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the type of the cluster provider.", + }, + "total_capacity": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies total capacity of the cluster in bytes.", + }, + "type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Description: "Specifies the type of the SP cluster.", + }, + "used_capacity": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + Description: "Specifies how much of the cluster capacity is consumed in bytes.", + }, + }, + }, + }, + }, + } +} + +func dataSourceIbmBackupRecoveryManagerSreGetClusterInfoRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + heliosSreApiClient, err := meta.(conns.ClientSession).BackupRecoveryManagerSreV1() + if err != nil { + tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_sre_get_cluster_info", "read", "initialize-client") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + getClustersInfoOptions := &backuprecoveryv1.GetClustersInfoOptions{} + + clusterDetails, _, err := heliosSreApiClient.GetClustersInfoWithContext(context, getClustersInfoOptions) + if err != nil { + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetClustersInfoWithContext failed: %s", err.Error()), "(Data) ibm_backup_recovery_manager_sre_get_cluster_info", "read") + log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) + return tfErr.GetDiag() + } + + d.SetId(dataSourceIbmBackupRecoveryManagerSreGetClusterInfoID(d)) + + if !core.IsNil(clusterDetails.CohesityClusters) { + cohesityClusters := []map[string]interface{}{} + for _, cohesityClustersItem := range clusterDetails.CohesityClusters { + cohesityClustersItemMap, err := DataSourceIbmBackupRecoveryManagerSreGetClusterInfoClusterInfoToMap(&cohesityClustersItem) // #nosec G601 + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_sre_get_cluster_info", "read", "cohesity_clusters-to-map").GetDiag() + } + cohesityClusters = append(cohesityClusters, cohesityClustersItemMap) + } + if err = d.Set("cohesity_clusters", cohesityClusters); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting cohesity_clusters: %s", err), "(Data) ibm_backup_recovery_manager_sre_get_cluster_info", "read", "set-cohesity_clusters").GetDiag() + } + } + + if !core.IsNil(clusterDetails.SpClusters) { + spClusters := []map[string]interface{}{} + for _, spClustersItem := range clusterDetails.SpClusters { + spClustersItemMap, err := DataSourceIbmBackupRecoveryManagerSreGetClusterInfoSPClusterInfoToMap(&spClustersItem) // #nosec G601 + if err != nil { + return flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_backup_recovery_manager_sre_get_cluster_info", "read", "sp_clusters-to-map").GetDiag() + } + spClusters = append(spClusters, spClustersItemMap) + } + if err = d.Set("sp_clusters", spClusters); err != nil { + return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting sp_clusters: %s", err), "(Data) ibm_backup_recovery_manager_sre_get_cluster_info", "read", "set-sp_clusters").GetDiag() + } + } + + return nil +} + +// dataSourceIbmBackupRecoveryManagerSreGetClusterInfoID returns a reasonable ID for the list. +func dataSourceIbmBackupRecoveryManagerSreGetClusterInfoID(d *schema.ResourceData) string { + return time.Now().UTC().String() +} + +func DataSourceIbmBackupRecoveryManagerSreGetClusterInfoClusterInfoToMap(model *backuprecoveryv1.ClusterInfo) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.AuthSupportForPkgDownloads != nil { + modelMap["auth_support_for_pkg_downloads"] = *model.AuthSupportForPkgDownloads + } + if model.AvailableVersions != nil { + availableVersions := []map[string]interface{}{} + for _, availableVersionsItem := range model.AvailableVersions { + availableVersionsItemMap, err := DataSourceIbmBackupRecoveryManagerSreGetClusterInfoAvailableReleaseVersionToMap(&availableVersionsItem) // #nosec G601 + if err != nil { + return modelMap, err + } + availableVersions = append(availableVersions, availableVersionsItemMap) + } + modelMap["available_versions"] = availableVersions + } + if model.ClusterID != nil { + modelMap["cluster_id"] = flex.IntValue(model.ClusterID) + } + if model.ClusterIncarnationID != nil { + modelMap["cluster_incarnation_id"] = flex.IntValue(model.ClusterIncarnationID) + } + if model.ClusterName != nil { + modelMap["cluster_name"] = *model.ClusterName + } + if model.CurrentPatchVersion != nil { + modelMap["current_patch_version"] = *model.CurrentPatchVersion + } + if model.CurrentVersion != nil { + modelMap["current_version"] = *model.CurrentVersion + } + if model.Health != nil { + modelMap["health"] = *model.Health + } + if model.IsConnectedToHelios != nil { + modelMap["is_connected_to_helios"] = *model.IsConnectedToHelios + } + if model.Location != nil { + modelMap["location"] = *model.Location + } + if model.MultiTenancyEnabled != nil { + modelMap["multi_tenancy_enabled"] = *model.MultiTenancyEnabled + } + if model.NodeIps != nil { + modelMap["node_ips"] = model.NodeIps + } + if model.NumberOfNodes != nil { + modelMap["number_of_nodes"] = flex.IntValue(model.NumberOfNodes) + } + if model.PatchTargetUpgradeURL != nil { + modelMap["patch_target_upgrade_url"] = *model.PatchTargetUpgradeURL + } + if model.PatchTargetVersion != nil { + modelMap["patch_target_version"] = *model.PatchTargetVersion + } + if model.ProviderType != nil { + modelMap["provider_type"] = *model.ProviderType + } + if model.ScheduledTimestamp != nil { + modelMap["scheduled_timestamp"] = flex.IntValue(model.ScheduledTimestamp) + } + if model.Status != nil { + modelMap["status"] = *model.Status + } + if model.TargetUpgradeURL != nil { + modelMap["target_upgrade_url"] = *model.TargetUpgradeURL + } + if model.TargetVersion != nil { + modelMap["target_version"] = *model.TargetVersion + } + if model.TotalCapacity != nil { + modelMap["total_capacity"] = flex.IntValue(model.TotalCapacity) + } + if model.Type != nil { + modelMap["type"] = *model.Type + } + if model.UpdateType != nil { + modelMap["update_type"] = *model.UpdateType + } + if model.UsedCapacity != nil { + modelMap["used_capacity"] = flex.IntValue(model.UsedCapacity) + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerSreGetClusterInfoAvailableReleaseVersionToMap(model *backuprecoveryv1.AvailableReleaseVersion) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.Notes != nil { + modelMap["notes"] = *model.Notes + } + if model.PatchDetails != nil { + patchDetailsMap, err := DataSourceIbmBackupRecoveryManagerSreGetClusterInfoAvailablePatchReleaseToMap(model.PatchDetails) + if err != nil { + return modelMap, err + } + modelMap["patch_details"] = []map[string]interface{}{patchDetailsMap} + } + if model.ReleaseStage != nil { + modelMap["release_stage"] = *model.ReleaseStage + } + if model.ReleaseType != nil { + modelMap["release_type"] = *model.ReleaseType + } + if model.Type != nil { + modelMap["type"] = *model.Type + } + if model.Version != nil { + modelMap["version"] = *model.Version + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerSreGetClusterInfoAvailablePatchReleaseToMap(model *backuprecoveryv1.AvailablePatchRelease) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.Notes != nil { + modelMap["notes"] = *model.Notes + } + if model.ReleaseType != nil { + modelMap["release_type"] = *model.ReleaseType + } + if model.Version != nil { + modelMap["version"] = *model.Version + } + return modelMap, nil +} + +func DataSourceIbmBackupRecoveryManagerSreGetClusterInfoSPClusterInfoToMap(model *backuprecoveryv1.SPClusterInfo) (map[string]interface{}, error) { + modelMap := make(map[string]interface{}) + if model.ClusterID != nil { + modelMap["cluster_id"] = flex.IntValue(model.ClusterID) + } + if model.ClusterIncarnationID != nil { + modelMap["cluster_incarnation_id"] = flex.IntValue(model.ClusterIncarnationID) + } + if model.ClusterName != nil { + modelMap["cluster_name"] = *model.ClusterName + } + if model.CurrentVersion != nil { + modelMap["current_version"] = *model.CurrentVersion + } + if model.Health != nil { + modelMap["health"] = *model.Health + } + if model.IsConnectedToHelios != nil { + modelMap["is_connected_to_helios"] = *model.IsConnectedToHelios + } + if model.NodeIps != nil { + modelMap["node_ips"] = model.NodeIps + } + if model.NumberOfNodes != nil { + modelMap["number_of_nodes"] = flex.IntValue(model.NumberOfNodes) + } + if model.ProviderType != nil { + modelMap["provider_type"] = *model.ProviderType + } + if model.TotalCapacity != nil { + modelMap["total_capacity"] = flex.IntValue(model.TotalCapacity) + } + if model.Type != nil { + modelMap["type"] = *model.Type + } + if model.UsedCapacity != nil { + modelMap["used_capacity"] = flex.IntValue(model.UsedCapacity) + } + return modelMap, nil +} diff --git a/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_sre_get_cluster_info_test.go b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_sre_get_cluster_info_test.go new file mode 100644 index 0000000000..7563fb8801 --- /dev/null +++ b/ibm/service/backuprecovery/data_source_ibm_backup_recovery_manager_sre_get_cluster_info_test.go @@ -0,0 +1,39 @@ +// Copyright IBM Corp. 2025 All Rights Reserved. +// Licensed under the Mozilla Public License v2.0 + +/* + * IBM OpenAPI Terraform Generator Version: 3.96.1-5136e54a-20241108-203028 + */ + +package backuprecovery_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" +) + +func TestAccIbmBackupRecoveryManagerSreGetClusterInfoDataSourceBasic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckIbmBackupRecoveryManagerSreGetClusterInfoDataSourceConfigBasic(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.ibm_backup_recovery_manager_sre_get_cluster_info.backup_recovery_manager_sre_get_cluster_info_instance", "id"), + ), + }, + }, + }) +} + +func testAccCheckIbmBackupRecoveryManagerSreGetClusterInfoDataSourceConfigBasic() string { + return fmt.Sprintf(` + data "ibm_backup_recovery_manager_sre_get_cluster_info" "backup_recovery_manager_sre_get_cluster_info_instance" { + } + `) +}