forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_arm_client_config.go
60 lines (50 loc) · 1.51 KB
/
data_source_arm_client_config.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
package azurerm
import (
"fmt"
"time"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceArmClientConfig() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmClientConfigRead,
Schema: map[string]*schema.Schema{
"client_id": {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
"subscription_id": {
Type: schema.TypeString,
Computed: true,
},
"service_principal_object_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceArmClientConfigRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
spClient := client.servicePrincipalsClient
// Application & Service Principal is 1:1 per tenant. Since we know the appId (client_id)
// here, we can query for the Service Principal whose appId matches.
filter := fmt.Sprintf("appId eq '%s'", client.clientId)
listResult, listErr := spClient.List(filter)
if listErr != nil {
return fmt.Errorf("Error listing Service Principals: %#v", listErr)
}
if listResult.Value == nil || len(*listResult.Value) != 1 {
return fmt.Errorf("Unexpected Service Principal query result: %#v", listResult.Value)
}
servicePrincipal := (*listResult.Value)[0]
d.SetId(time.Now().UTC().String())
d.Set("client_id", client.clientId)
d.Set("tenant_id", client.tenantId)
d.Set("subscription_id", client.subscriptionId)
d.Set("service_principal_object_id", *servicePrincipal.ObjectID)
return nil
}