-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
data_source_sql_database.go
115 lines (87 loc) · 2.8 KB
/
data_source_sql_database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package azurerm
import (
"fmt"
"time"
"github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2017-03-01-preview/sql"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func dataSourceSqlDatabase() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmSqlDatabaseRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"location": {
Type: schema.TypeString,
Computed: true,
},
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
"server_name": {
Type: schema.TypeString,
Required: true,
},
"edition": {
Type: schema.TypeString,
Computed: true,
},
"collation": {
Type: schema.TypeString,
Computed: true,
},
"elastic_pool_name": {
Type: schema.TypeString,
Computed: true,
},
"default_secondary_location": {
Type: schema.TypeString,
Computed: true,
},
"read_scale": {
Type: schema.TypeBool,
Computed: true,
},
"tags": tags.Schema(),
"failover_group_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceArmSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Sql.DatabasesClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()
name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
serverName := d.Get("server_name").(string)
resp, err := client.Get(ctx, resourceGroup, serverName, name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("SQL Database %q (server %q / resource group %q) was not found", name, serverName, resourceGroup)
}
return fmt.Errorf("Error retrieving SQL Database %q (server %q / resource group %q): %s", name, serverName, resourceGroup, err)
}
d.Set("location", azure.NormalizeLocation(*resp.Location))
if id := resp.ID; id != nil {
d.SetId(*id)
}
if props := resp.DatabaseProperties; props != nil {
d.Set("collation", props.Collation)
d.Set("default_secondary_location", props.DefaultSecondaryLocation)
d.Set("edition", string(props.Edition))
d.Set("elastic_pool_name", props.ElasticPoolName)
d.Set("failover_group_id", props.FailoverGroupID)
d.Set("read_scale", props.ReadScale == sql.ReadScaleEnabled)
}
return tags.FlattenAndSet(d, resp.Tags)
}