forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_client_config.go
74 lines (62 loc) · 1.94 KB
/
data_source_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package azurerm
import (
"fmt"
"time"
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"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_application_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)
ctx := meta.(*ArmClient).StopContext
var servicePrincipal *graphrbac.ServicePrincipal
if client.usingServicePrincipal {
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(ctx, filter)
if listErr != nil {
return fmt.Errorf("Error listing Service Principals: %#v", listErr)
}
if listResult.Values() == nil || len(listResult.Values()) != 1 {
return fmt.Errorf("Unexpected Service Principal query result: %#v", listResult.Values())
}
servicePrincipal = &(listResult.Values())[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)
if principal := servicePrincipal; principal != nil {
d.Set("service_principal_application_id", principal.AppID)
d.Set("service_principal_object_id", principal.ObjectID)
}
return nil
}