-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
data_source_function_app.go
200 lines (167 loc) · 5.29 KB
/
data_source_function_app.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package web
import (
"fmt"
"strings"
"time"
"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/clients"
webValidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/web/validate"
"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 dataSourceArmFunctionApp() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmFunctionAppRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: webValidate.AppServiceName,
},
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
"location": azure.SchemaLocationForDataSource(),
"app_service_plan_id": {
Type: schema.TypeString,
Computed: true,
},
"app_settings": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"connection_string": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeString,
Sensitive: true,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"default_hostname": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"site_credential": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Computed: true,
},
"password": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
},
},
"os_type": {
Type: schema.TypeString,
Computed: true,
},
"outbound_ip_addresses": {
Type: schema.TypeString,
Computed: true,
},
"possible_outbound_ip_addresses": {
Type: schema.TypeString,
Computed: true,
},
"tags": tags.Schema(),
},
}
}
func dataSourceArmFunctionAppRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Web.AppServicesClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
resourceGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: AzureRM Function App %q (Resource Group %q) was not found", name, resourceGroup)
}
return fmt.Errorf("Error making Read request on AzureRM Function App %q: %+v", name, err)
}
appSettingsResp, err := client.ListApplicationSettings(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(appSettingsResp.Response) {
return fmt.Errorf("Error: AzureRM Function App AppSettings %q (Resource Group %q) was not found", name, resourceGroup)
}
return fmt.Errorf("Error making Read request on AzureRM Function App AppSettings %q: %+v", name, err)
}
connectionStringsResp, err := client.ListConnectionStrings(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error making Read request on AzureRM Function App ConnectionStrings %q: %+v", name, err)
}
siteCredFuture, err := client.ListPublishingCredentials(ctx, resourceGroup, name)
if err != nil {
return err
}
err = siteCredFuture.WaitForCompletionRef(ctx, client.Client)
if err != nil {
return err
}
siteCredResp, err := siteCredFuture.Result(*client)
if err != nil {
return fmt.Errorf("Error making Read request on AzureRM App Service Site Credential %q: %+v", name, err)
}
d.SetId(*resp.ID)
d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if props := resp.SiteProperties; props != nil {
d.Set("app_service_plan_id", props.ServerFarmID)
d.Set("enabled", props.Enabled)
d.Set("default_hostname", props.DefaultHostName)
d.Set("outbound_ip_addresses", props.OutboundIPAddresses)
d.Set("possible_outbound_ip_addresses", props.PossibleOutboundIPAddresses)
}
osType := ""
if v := resp.Kind; v != nil && strings.Contains(*v, "linux") {
osType = "linux"
}
d.Set("os_type", osType)
appSettings := flattenAppServiceAppSettings(appSettingsResp.Properties)
if err = d.Set("app_settings", appSettings); err != nil {
return err
}
if err = d.Set("connection_string", flattenFunctionAppConnectionStrings(connectionStringsResp.Properties)); err != nil {
return err
}
siteCred := flattenFunctionAppSiteCredential(siteCredResp.UserProperties)
if err = d.Set("site_credential", siteCred); err != nil {
return err
}
return tags.FlattenAndSet(d, resp.Tags)
}