/
data_source_azuread_application.go
156 lines (125 loc) · 3.59 KB
/
data_source_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
package azurerm
import (
"fmt"
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func dataSourceArmAzureADApplication() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmAzureADApplicationRead,
Schema: map[string]*schema.Schema{
"object_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"name"},
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"object_id"},
},
"homepage": {
Type: schema.TypeString,
Computed: true,
},
"identifier_uris": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"reply_urls": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"available_to_other_tenants": {
Type: schema.TypeBool,
Computed: true,
},
"oauth2_allow_implicit_flow": {
Type: schema.TypeBool,
Computed: true,
},
"application_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceArmAzureADApplicationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).applicationsClient
ctx := meta.(*ArmClient).StopContext
var application graphrbac.Application
if oId, ok := d.GetOk("object_id"); ok {
objectId := oId.(string)
resp, err := client.Get(ctx, objectId)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: AzureAD Application with ID %q was not found", objectId)
}
return fmt.Errorf("Error making Read request on AzureAD Application with ID %q: %+v", objectId, err)
}
application = resp
} else {
resp, err := client.ListComplete(ctx, "")
if err != nil {
return fmt.Errorf("Error listing Azure AD Applications: %+v", err)
}
name := d.Get("name").(string)
var app *graphrbac.Application
for _, v := range *resp.Response().Value {
if v.DisplayName != nil {
if *v.DisplayName == name {
app = &v
break
}
}
}
if app == nil {
return fmt.Errorf("Couldn't locate an Azure AD Application with a name of %q", name)
}
application = *app
}
d.SetId(*application.ObjectID)
d.Set("object_id", application.ObjectID)
d.Set("name", application.DisplayName)
d.Set("application_id", application.AppID)
d.Set("homepage", application.Homepage)
d.Set("available_to_other_tenants", application.AvailableToOtherTenants)
d.Set("oauth2_allow_implicit_flow", application.Oauth2AllowImplicitFlow)
identifierUris := flattenAzureADDataSourceApplicationIdentifierUris(application.IdentifierUris)
if err := d.Set("identifier_uris", identifierUris); err != nil {
return fmt.Errorf("Error setting `identifier_uris`: %+v", err)
}
replyUrls := flattenAzureADDataSourceApplicationReplyUrls(application.ReplyUrls)
if err := d.Set("reply_urls", replyUrls); err != nil {
return fmt.Errorf("Error setting `reply_urls`: %+v", err)
}
return nil
}
func flattenAzureADDataSourceApplicationIdentifierUris(input *[]string) []string {
output := make([]string, 0)
if input != nil {
for _, v := range *input {
output = append(output, v)
}
}
return output
}
func flattenAzureADDataSourceApplicationReplyUrls(input *[]string) []string {
output := make([]string, 0)
if input != nil {
for _, v := range *input {
output = append(output, v)
}
}
return output
}