|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +// ---------------------------------------------------------------------------- |
| 4 | +// |
| 5 | +// *** AUTO GENERATED CODE *** Type: Handwritten *** |
| 6 | +// |
| 7 | +// ---------------------------------------------------------------------------- |
| 8 | +// |
| 9 | +// This code is generated by Magic Modules using the following: |
| 10 | +// |
| 11 | +// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/services/backupdr/data_source_backup_dr_data_source_reference.go |
| 12 | +// |
| 13 | +// DO NOT EDIT this file directly. Any changes made to this file will be |
| 14 | +// overwritten during the next generation cycle. |
| 15 | +// |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | +package backupdr |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "strconv" |
| 22 | + |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 24 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" |
| 25 | + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
| 26 | +) |
| 27 | + |
| 28 | +func DataSourceGoogleCloudBackupDRDataSourceReferences() *schema.Resource { |
| 29 | + return &schema.Resource{ |
| 30 | + Read: dataSourceGoogleCloudBackupDRDataSourceReferencesRead, |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "location": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Required: true, |
| 35 | + Description: "The location to list the data source references from.", |
| 36 | + }, |
| 37 | + "project": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Optional: true, |
| 40 | + Computed: true, |
| 41 | + Description: "The ID of the project in which the resource belongs.", |
| 42 | + }, |
| 43 | + "resource_type": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Required: true, |
| 46 | + Description: `The resource type to get the data source references for. Examples include, "compute.googleapis.com/Instance", "sqladmin.googleapis.com/Instance".`, |
| 47 | + }, |
| 48 | + |
| 49 | + // Output: a computed list of the data source references found |
| 50 | + "data_source_references": { |
| 51 | + Type: schema.TypeList, |
| 52 | + Computed: true, |
| 53 | + Description: "A list of the data source references found.", |
| 54 | + Elem: &schema.Resource{ |
| 55 | + Schema: map[string]*schema.Schema{ |
| 56 | + "name": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + "data_source": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + Description: "The underlying data source resource.", |
| 64 | + }, |
| 65 | + "gcp_resource_name": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + Description: "The GCP resource name for the data source.", |
| 69 | + }, |
| 70 | + "backup_config_state": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Description: "The state of the backup config for the data source.", |
| 74 | + }, |
| 75 | + "backup_count": { |
| 76 | + Type: schema.TypeInt, |
| 77 | + Computed: true, |
| 78 | + Description: "The number of backups for the data source.", |
| 79 | + }, |
| 80 | + "last_backup_state": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Computed: true, |
| 83 | + Description: "The state of the last backup.", |
| 84 | + }, |
| 85 | + "last_successful_backup_time": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Computed: true, |
| 88 | + Description: "The last time a successful backup was made.", |
| 89 | + }, |
| 90 | + "resource_type": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Computed: true, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func dataSourceGoogleCloudBackupDRDataSourceReferencesRead(d *schema.ResourceData, meta interface{}) error { |
| 102 | + config := meta.(*transport_tpg.Config) |
| 103 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + project, err := tpgresource.GetProject(d, config) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + location := d.Get("location").(string) |
| 114 | + resourceType := d.Get("resource_type").(string) |
| 115 | + |
| 116 | + url := fmt.Sprintf("%sprojects/%s/locations/%s/dataSourceReferences:fetchForResourceType?resourceType=%s", config.BackupDRBasePath, project, location, resourceType) |
| 117 | + |
| 118 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 119 | + Config: config, |
| 120 | + Method: "GET", |
| 121 | + Project: project, |
| 122 | + RawURL: url, |
| 123 | + UserAgent: userAgent, |
| 124 | + }) |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("Error reading DataSourceReferences: %s", err) |
| 127 | + } |
| 128 | + |
| 129 | + items, ok := res["dataSourceReferences"].([]interface{}) |
| 130 | + if !ok { |
| 131 | + items = make([]interface{}, 0) |
| 132 | + } |
| 133 | + |
| 134 | + flattenedDataSourceReferences, err := flattenDataSourceReferences(items) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + |
| 139 | + if err := d.Set("data_source_references", flattenedDataSourceReferences); err != nil { |
| 140 | + return fmt.Errorf("Error setting data_source_references: %s", err) |
| 141 | + } |
| 142 | + |
| 143 | + d.SetId(url) |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +func flattenDataSourceReferences(items []interface{}) ([]map[string]interface{}, error) { |
| 149 | + references := make([]map[string]interface{}, 0, len(items)) |
| 150 | + for _, item := range items { |
| 151 | + data, ok := item.(map[string]interface{}) |
| 152 | + if !ok { |
| 153 | + return nil, fmt.Errorf("cannot cast item to map[string]interface{}") |
| 154 | + } |
| 155 | + |
| 156 | + ref := map[string]interface{}{ |
| 157 | + "name": data["name"], |
| 158 | + "data_source": data["dataSource"], |
| 159 | + "backup_config_state": data["dataSourceBackupConfigState"], |
| 160 | + } |
| 161 | + |
| 162 | + // The API returns backup count as a string, so we parse it to an integer. |
| 163 | + if v, ok := data["dataSourceBackupCount"].(string); ok { |
| 164 | + if i, err := strconv.Atoi(v); err == nil { |
| 165 | + ref["backup_count"] = i |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // Flatten the nested dataSourceBackupConfigInfo object. |
| 170 | + if configInfo, ok := data["dataSourceBackupConfigInfo"].(map[string]interface{}); ok { |
| 171 | + ref["last_backup_state"] = configInfo["lastBackupState"] |
| 172 | + ref["last_successful_backup_time"] = configInfo["lastSuccessfulBackupConsistencyTime"] |
| 173 | + } |
| 174 | + |
| 175 | + if resourceInfo, ok := data["dataSourceGcpResourceInfo"].(map[string]interface{}); ok { |
| 176 | + ref["gcp_resource_name"] = resourceInfo["gcpResourcename"] |
| 177 | + ref["resource_type"] = resourceInfo["type"] |
| 178 | + } |
| 179 | + |
| 180 | + references = append(references, ref) |
| 181 | + } |
| 182 | + return references, nil |
| 183 | +} |
0 commit comments