|
| 1 | +package oracledatabase |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 8 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 9 | +) |
| 10 | + |
| 11 | +func DataSourceOracleDatabaseAutonomousDatabases() *schema.Resource { |
| 12 | + dsSchema := map[string]*schema.Schema{ |
| 13 | + "project": { |
| 14 | + Type: schema.TypeString, |
| 15 | + Optional: true, |
| 16 | + Description: "The ID of the project in which the dataset is located. If it is not provided, the provider project is used.", |
| 17 | + }, |
| 18 | + "location": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Required: true, |
| 21 | + Description: "location", |
| 22 | + }, |
| 23 | + "autonomous_databases": { |
| 24 | + Type: schema.TypeList, |
| 25 | + Computed: true, |
| 26 | + Elem: &schema.Resource{ |
| 27 | + Schema: tpgresource.DatasourceSchemaFromResourceSchema(ResourceOracleDatabaseAutonomousDatabase().Schema), |
| 28 | + }, |
| 29 | + }, |
| 30 | + } |
| 31 | + return &schema.Resource{ |
| 32 | + Read: dataSourceOracleDatabaseAutonomousDatabasesRead, |
| 33 | + Schema: dsSchema, |
| 34 | + } |
| 35 | + |
| 36 | +} |
| 37 | + |
| 38 | +func dataSourceOracleDatabaseAutonomousDatabasesRead(d *schema.ResourceData, meta interface{}) error { |
| 39 | + config := meta.(*transport_tpg.Config) |
| 40 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + url, err := tpgresource.ReplaceVars(d, config, "{{OracleDatabaseBasePath}}projects/{{project}}/locations/{{location}}/autonomousDatabases") |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("Error constructing id: %s", err) |
| 48 | + } |
| 49 | + |
| 50 | + billingProject := "" |
| 51 | + project, err := tpgresource.GetProject(d, config) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("Error fetching project for autonomousDatabases: %s", err) |
| 54 | + } |
| 55 | + billingProject = project |
| 56 | + // err == nil indicates that the billing_project value was found |
| 57 | + if bp, err := tpgresource.GetBillingProject(d, config); err == nil { |
| 58 | + billingProject = bp |
| 59 | + } |
| 60 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 61 | + Config: config, |
| 62 | + Method: "GET", |
| 63 | + Project: billingProject, |
| 64 | + RawURL: url, |
| 65 | + UserAgent: userAgent, |
| 66 | + }) |
| 67 | + |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("Error reading autonomousDatabases: %s", err) |
| 70 | + } |
| 71 | + |
| 72 | + if err := d.Set("project", project); err != nil { |
| 73 | + return fmt.Errorf("Error setting autonomousDatabases project: %s", err) |
| 74 | + } |
| 75 | + |
| 76 | + if err := d.Set("autonomous_databases", flattenOracleDatabaseautonomousDatabases(res["autonomousDatabases"], d, config)); err != nil { |
| 77 | + return fmt.Errorf("Error setting autonomousDatabases: %s", err) |
| 78 | + } |
| 79 | + |
| 80 | + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/autonomousDatabases") |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("Error constructing id: %s", err) |
| 83 | + } |
| 84 | + d.SetId(id) |
| 85 | + |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +func flattenOracleDatabaseautonomousDatabases(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) []map[string]interface{} { |
| 90 | + if v == nil { |
| 91 | + return nil |
| 92 | + } |
| 93 | + l := v.([]interface{}) |
| 94 | + transformed := make([]map[string]interface{}, 0) |
| 95 | + for _, raw := range l { |
| 96 | + original := raw.(map[string]interface{}) |
| 97 | + transformed = append(transformed, map[string]interface{}{ |
| 98 | + "name": flattenOracleDatabaseAutonomousDatabaseName(original["name"], d, config), |
| 99 | + "database": flattenOracleDatabaseAutonomousDatabaseDatabase(original["database"], d, config), |
| 100 | + "display_name": flattenOracleDatabaseAutonomousDatabaseDisplayName(original["displayName"], d, config), |
| 101 | + "entitlement_id": flattenOracleDatabaseAutonomousDatabaseEntitlementId(original["entitlementId"], d, config), |
| 102 | + "properties": flattenOracleDatabaseAutonomousDatabaseProperties(original["properties"], d, config), |
| 103 | + "labels": flattenOracleDatabaseAutonomousDatabaseLabels(original["labels"], d, config), |
| 104 | + "network": flattenOracleDatabaseAutonomousDatabaseNetwork(original["network"], d, config), |
| 105 | + "cidr": flattenOracleDatabaseAutonomousDatabaseCidr(original["cidr"], d, config), |
| 106 | + "create_time": flattenOracleDatabaseAutonomousDatabaseCreateTime(original["createTime"], d, config), |
| 107 | + "terraform_labels": flattenOracleDatabaseAutonomousDatabaseTerraformLabels(original["labels"], d, config), |
| 108 | + "effective_labels": flattenOracleDatabaseAutonomousDatabaseEffectiveLabels(original["labels"], d, config), |
| 109 | + }) |
| 110 | + } |
| 111 | + return transformed |
| 112 | +} |
0 commit comments