|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +package backupdr |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" |
| 9 | + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
| 10 | +) |
| 11 | + |
| 12 | +func DataSourceGoogleCloudBackupDRBackup() *schema.Resource { |
| 13 | + dsSchema := map[string]*schema.Schema{ |
| 14 | + "name": { |
| 15 | + Type: schema.TypeString, |
| 16 | + Computed: true, |
| 17 | + Description: `Name of resource`, |
| 18 | + }, |
| 19 | + "backups": { |
| 20 | + Type: schema.TypeList, |
| 21 | + Computed: true, |
| 22 | + Description: `List of all backups under data source.`, |
| 23 | + Elem: &schema.Resource{ |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "name": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Computed: true, |
| 28 | + Description: `Name of the resource.`, |
| 29 | + }, |
| 30 | + "location": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Computed: true, |
| 33 | + Description: `Location of the resource.`, |
| 34 | + }, |
| 35 | + "backup_id": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + Description: `Id of the requesting object, Backup.`, |
| 39 | + }, |
| 40 | + "backup_vault_id": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + Description: `Name of the Backup Vault associated with Backup.`, |
| 44 | + }, |
| 45 | + "data_source_id": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Description: `Name of the Data Source associated with Backup.`, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + "location": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Required: true, |
| 56 | + }, |
| 57 | + "project": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Required: true, |
| 60 | + }, |
| 61 | + "data_source_id": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Required: true, |
| 64 | + }, |
| 65 | + "backup_vault_id": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Required: true, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + return &schema.Resource{ |
| 72 | + Read: dataSourceGoogleCloudBackupDrBackupRead, |
| 73 | + Schema: dsSchema, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func dataSourceGoogleCloudBackupDrBackupRead(d *schema.ResourceData, meta interface{}) error { |
| 78 | + config := meta.(*transport_tpg.Config) |
| 79 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + project, err := tpgresource.GetProject(d, config) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + location, err := tpgresource.GetLocation(d, config) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + if len(location) == 0 { |
| 94 | + return fmt.Errorf("Cannot determine location: set location in this data source or at provider-level") |
| 95 | + } |
| 96 | + |
| 97 | + billingProject := project |
| 98 | + url, err := tpgresource.ReplaceVars(d, config, "{{BackupDRBasePath}}projects/{{project}}/locations/{{location}}/backupVaults/{{backup_vault_id}}/dataSources/{{data_source_id}}/backups") |
| 99 | + |
| 100 | + fmt.Sprintf("url: %s", url) |
| 101 | + |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 107 | + billingProject = bp |
| 108 | + } |
| 109 | + |
| 110 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 111 | + Config: config, |
| 112 | + Method: "GET", |
| 113 | + Project: billingProject, |
| 114 | + RawURL: url, |
| 115 | + UserAgent: userAgent, |
| 116 | + }) |
| 117 | + |
| 118 | + if err != nil { |
| 119 | + return fmt.Errorf("Error reading BackupVault: %s", err) |
| 120 | + } |
| 121 | + |
| 122 | + if err := d.Set("backups", flattenDataSourceBackupDRBackups(res["backups"], d, config)); err != nil { |
| 123 | + return fmt.Errorf("Error reading Backup: %s", err) |
| 124 | + } |
| 125 | + |
| 126 | + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/backupVaults/{{backup_vault_id}}/dataSources/{{data_source_id}}/backups") |
| 127 | + d.SetId(id) |
| 128 | + d.Set("name", id) |
| 129 | + |
| 130 | + return nil |
| 131 | +} |
| 132 | + |
| 133 | +func flattenDataSourceBackupDRBackups(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 134 | + if v == nil { |
| 135 | + return v |
| 136 | + } |
| 137 | + l := v.([]interface{}) |
| 138 | + transformed := make([]interface{}, 0, len(l)) |
| 139 | + |
| 140 | + for _, raw := range l { |
| 141 | + original := raw.(map[string]interface{}) |
| 142 | + if len(original) < 1 { |
| 143 | + continue |
| 144 | + } |
| 145 | + transformed = append(transformed, map[string]interface{}{ |
| 146 | + "name": flattenDataSourceBackupDRBackupsName(original["name"], d, config), |
| 147 | + "location": flattenDataSourceBackupDRBackupsLocation(original["location"], d, config), |
| 148 | + "backup_id": flattenDataSourceBackupDRBackupsBackupId(original["backupId"], d, config), |
| 149 | + "backup_vault_id": flattenDataSourceBackupDRBackupsBackupVaultId(original["backupVaultId"], d, config), |
| 150 | + "data_source_id": flattenDataSourceBackupDRBackupsDataSourceId(original["dataSourceId"], d, config), |
| 151 | + }) |
| 152 | + } |
| 153 | + return transformed |
| 154 | +} |
| 155 | + |
| 156 | +func flattenDataSourceBackupDRBackupsName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 157 | + return v |
| 158 | +} |
| 159 | + |
| 160 | +func flattenDataSourceBackupDRBackupsLocation(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 161 | + return v |
| 162 | +} |
| 163 | + |
| 164 | +func flattenDataSourceBackupDRBackupsBackupId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 165 | + return v |
| 166 | +} |
| 167 | + |
| 168 | +func flattenDataSourceBackupDRBackupsBackupVaultId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 169 | + return v |
| 170 | +} |
| 171 | + |
| 172 | +func flattenDataSourceBackupDRBackupsDataSourceId(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 173 | + return v |
| 174 | +} |
0 commit comments