-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
resource_arm_azuread_application.go
261 lines (210 loc) · 7.87 KB
/
resource_arm_azuread_application.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package azurerm
import (
"fmt"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func resourceArmActiveDirectoryApplication() *schema.Resource {
return &schema.Resource{
DeprecationMessage: `The Azure Active Directory resources have been split out into their own Provider.
Information on migrating to the new AzureAD Provider can be found here: https://terraform.io/docs/providers/azurerm/guides/migrating-to-azuread.html
As such the Azure Active Directory resources within the AzureRM Provider are now deprecated and will be removed in v2.0 of the AzureRM Provider.
`,
Create: resourceArmActiveDirectoryApplicationCreate,
Read: resourceArmActiveDirectoryApplicationRead,
Update: resourceArmActiveDirectoryApplicationUpdate,
Delete: resourceArmActiveDirectoryApplicationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},
"homepage": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.URLIsHTTPS,
},
"identifier_uris": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validate.URLIsHTTPOrHTTPS,
},
},
"reply_urls": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validate.URLIsHTTPOrHTTPS,
},
},
"available_to_other_tenants": {
Type: schema.TypeBool,
Optional: true,
},
"oauth2_allow_implicit_flow": {
Type: schema.TypeBool,
Optional: true,
},
"application_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceArmActiveDirectoryApplicationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Graph.ApplicationsClient
ctx, cancel := timeouts.ForCreate(meta.(*ArmClient).StopContext, d)
defer cancel()
// NOTE: name isn't the Resource ID here, so we don't check it exists
name := d.Get("name").(string)
availableToOtherTenants := d.Get("available_to_other_tenants").(bool)
properties := graphrbac.ApplicationCreateParameters{
DisplayName: &name,
Homepage: expandAzureRmActiveDirectoryApplicationHomepage(d, name),
IdentifierUris: expandAzureRmActiveDirectoryApplicationIdentifierUris(d),
ReplyUrls: expandAzureRmActiveDirectoryApplicationReplyUrls(d),
AvailableToOtherTenants: utils.Bool(availableToOtherTenants),
}
if v, ok := d.GetOk("oauth2_allow_implicit_flow"); ok {
properties.Oauth2AllowImplicitFlow = utils.Bool(v.(bool))
}
app, err := client.Create(ctx, properties)
if err != nil {
return err
}
d.SetId(*app.ObjectID)
return resourceArmActiveDirectoryApplicationRead(d, meta)
}
func resourceArmActiveDirectoryApplicationUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Graph.ApplicationsClient
ctx, cancel := timeouts.ForUpdate(meta.(*ArmClient).StopContext, d)
defer cancel()
name := d.Get("name").(string)
var properties graphrbac.ApplicationUpdateParameters
if d.HasChange("name") {
properties.DisplayName = &name
}
if d.HasChange("homepage") {
properties.Homepage = expandAzureRmActiveDirectoryApplicationHomepage(d, name)
}
if d.HasChange("identifier_uris") {
properties.IdentifierUris = expandAzureRmActiveDirectoryApplicationIdentifierUris(d)
}
if d.HasChange("reply_urls") {
properties.ReplyUrls = expandAzureRmActiveDirectoryApplicationReplyUrls(d)
}
if d.HasChange("available_to_other_tenants") {
availableToOtherTenants := d.Get("available_to_other_tenants").(bool)
properties.AvailableToOtherTenants = utils.Bool(availableToOtherTenants)
}
if d.HasChange("oauth2_allow_implicit_flow") {
oauth := d.Get("oauth2_allow_implicit_flow").(bool)
properties.Oauth2AllowImplicitFlow = utils.Bool(oauth)
}
if _, err := client.Patch(ctx, d.Id(), properties); err != nil {
return fmt.Errorf("Error patching Azure AD Application with ID %q: %+v", d.Id(), err)
}
return resourceArmActiveDirectoryApplicationRead(d, meta)
}
func resourceArmActiveDirectoryApplicationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Graph.ApplicationsClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()
resp, err := client.Get(ctx, d.Id())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Azure AD Application with ID %q was not found - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving Azure AD Application with ID %q: %+v", d.Id(), err)
}
d.Set("name", resp.DisplayName)
d.Set("application_id", resp.AppID)
d.Set("homepage", resp.Homepage)
d.Set("available_to_other_tenants", resp.AvailableToOtherTenants)
d.Set("oauth2_allow_implicit_flow", resp.Oauth2AllowImplicitFlow)
identifierUris := make([]string, 0)
if s := resp.IdentifierUris; s != nil {
identifierUris = *s
}
if err := d.Set("identifier_uris", identifierUris); err != nil {
return fmt.Errorf("Error setting `identifier_uris`: %+v", err)
}
replyUrls := make([]string, 0)
if s := resp.ReplyUrls; s != nil {
replyUrls = *s
}
if err := d.Set("reply_urls", replyUrls); err != nil {
return fmt.Errorf("Error setting `reply_urls`: %+v", err)
}
return nil
}
func resourceArmActiveDirectoryApplicationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Graph.ApplicationsClient
ctx, cancel := timeouts.ForDelete(meta.(*ArmClient).StopContext, d)
defer cancel()
// in order to delete an application which is available to other tenants, we first have to disable this setting
availableToOtherTenants := d.Get("available_to_other_tenants").(bool)
if availableToOtherTenants {
log.Printf("[DEBUG] Azure AD Application is available to other tenants - disabling that feature before deleting.")
properties := graphrbac.ApplicationUpdateParameters{
AvailableToOtherTenants: utils.Bool(false),
}
if _, err := client.Patch(ctx, d.Id(), properties); err != nil {
return fmt.Errorf("Error patching Azure AD Application with ID %q: %+v", d.Id(), err)
}
}
resp, err := client.Delete(ctx, d.Id())
if err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error Deleting Azure AD Application with ID %q: %+v", d.Id(), err)
}
}
return nil
}
func expandAzureRmActiveDirectoryApplicationHomepage(d *schema.ResourceData, name string) *string {
if v, ok := d.GetOk("homepage"); ok {
return utils.String(v.(string))
}
return utils.String(fmt.Sprintf("https://%s", name))
}
func expandAzureRmActiveDirectoryApplicationIdentifierUris(d *schema.ResourceData) *[]string {
identifierUris := d.Get("identifier_uris").([]interface{})
identifiers := make([]string, 0)
for _, id := range identifierUris {
identifiers = append(identifiers, id.(string))
}
return &identifiers
}
func expandAzureRmActiveDirectoryApplicationReplyUrls(d *schema.ResourceData) *[]string {
replyUrls := d.Get("reply_urls").([]interface{})
urls := make([]string, 0)
for _, url := range replyUrls {
urls = append(urls, url.(string))
}
return &urls
}