1
1
package azuredevops
2
2
3
3
import (
4
+ "fmt"
5
+ "strings"
6
+
4
7
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5
8
"github.com/microsoft/azure-devops-go-api/azuredevops/serviceendpoint"
6
9
crud "github.com/microsoft/terraform-provider-azuredevops/azuredevops/crud/serviceendpoint"
@@ -10,48 +13,116 @@ import (
10
13
11
14
func resourceServiceEndpointAzureRM () * schema.Resource {
12
15
r := crud .GenBaseServiceEndpointResource (flattenServiceEndpointAzureRM , expandServiceEndpointAzureRM , parseImportedProjectIDAndServiceEndpointID )
13
- crud .MakeUnprotectedSchema (r , "azurerm_spn_clientid" , "ARM_CLIENT_ID" , "The service principal id which should be used." )
14
- crud .MakeProtectedSchema (r , "azurerm_spn_clientsecret" , "ARM_CLIENT_SECRET" , "The service principal secret which should be used." )
15
16
crud .MakeUnprotectedSchema (r , "azurerm_spn_tenantid" , "ARM_TENANT_ID" , "The service principal tenant id which should be used." )
16
17
crud .MakeUnprotectedSchema (r , "azurerm_subscription_id" , "ARM_SUBSCRIPTION_ID" , "The Azure subscription Id which should be used." )
17
18
crud .MakeUnprotectedSchema (r , "azurerm_subscription_name" , "ARM_SUBSCRIPTION_NAME" , "The Azure subscription name which should be used." )
18
- crud .MakeUnprotectedSchema (r , "azurerm_scope" , "ARM_SCOPE" , "The Azure scope which should be used by the spn." )
19
+
20
+ r .Schema ["resource_group" ] = & schema.Schema {
21
+ Type : schema .TypeString ,
22
+ Optional : true ,
23
+ ForceNew : true ,
24
+ Description : "Scope Resource Group" ,
25
+ ConflictsWith : []string {"credentials" },
26
+ }
27
+
28
+ secretHashKey , secretHashSchema := tfhelper .GenerateSecreteMemoSchema ("serviceprincipalkey" )
29
+ r .Schema ["credentials" ] = & schema.Schema {
30
+ Type : schema .TypeList ,
31
+ Optional : true ,
32
+ MaxItems : 1 ,
33
+ ForceNew : true ,
34
+ ConflictsWith : []string {"resource_group" },
35
+ Elem : & schema.Resource {
36
+ Schema : map [string ]* schema.Schema {
37
+ "serviceprincipalid" : {
38
+ Type : schema .TypeString ,
39
+ Required : true ,
40
+ Description : "The service principal id which should be used." ,
41
+ },
42
+ "serviceprincipalkey" : {
43
+ Type : schema .TypeString ,
44
+ Required : true ,
45
+ Description : "The service principal secret which should be used." ,
46
+ Sensitive : true ,
47
+ DiffSuppressFunc : tfhelper .DiffFuncSuppressSecretChanged ,
48
+ },
49
+ secretHashKey : secretHashSchema ,
50
+ },
51
+ },
52
+ }
53
+
19
54
return r
20
55
}
21
56
22
57
// Convert internal Terraform data structure to an AzDO data structure
23
58
func expandServiceEndpointAzureRM (d * schema.ResourceData ) (* serviceendpoint.ServiceEndpoint , * string ) {
24
59
serviceEndpoint , projectID := crud .DoBaseExpansion (d )
60
+
61
+ scope := fmt .Sprintf ("/subscriptions/%s" , d .Get ("azurerm_subscription_id" ))
62
+ scopeLevel := "Subscription"
63
+ if _ , ok := d .GetOk ("resource_group" ); ok {
64
+ scope += fmt .Sprintf ("/resourcegroups/%s" , d .Get ("resource_group" ))
65
+ scopeLevel = "ResourceGroup"
66
+ }
67
+
25
68
serviceEndpoint .Authorization = & serviceendpoint.EndpointAuthorization {
26
69
Parameters : & map [string ]string {
27
70
"authenticationType" : "spnKey" ,
28
- "scope" : d .Get ("azurerm_scope" ).(string ),
29
- "serviceprincipalid" : d .Get ("azurerm_spn_clientid" ).(string ),
30
- "serviceprincipalkey" : d .Get ("azurerm_spn_clientsecret" ).(string ),
71
+ "serviceprincipalid" : "" ,
72
+ "serviceprincipalkey" : "" ,
31
73
"tenantid" : d .Get ("azurerm_spn_tenantid" ).(string ),
32
74
},
33
75
Scheme : converter .String ("ServicePrincipal" ),
34
76
}
35
77
serviceEndpoint .Data = & map [string ]string {
36
- "creationMode" : "Manual " ,
78
+ "creationMode" : "Automatic " ,
37
79
"environment" : "AzureCloud" ,
38
80
"scopeLevel" : "Subscription" ,
39
- "SubscriptionId" : d .Get ("azurerm_subscription_id" ).(string ),
40
- "SubscriptionName" : d .Get ("azurerm_subscription_name" ).(string ),
81
+ "subscriptionId" : d .Get ("azurerm_subscription_id" ).(string ),
82
+ "subscriptionName" : d .Get ("azurerm_subscription_name" ).(string ),
83
+ }
84
+
85
+ if scopeLevel == "ResourceGroup" {
86
+ (* serviceEndpoint .Authorization .Parameters )["scope" ] = scope
87
+ }
88
+
89
+ if _ , ok := d .GetOk ("credentials" ); ok {
90
+ credentials := d .Get ("credentials" ).([]interface {})[0 ].(map [string ]interface {})
91
+ (* serviceEndpoint .Authorization .Parameters )["serviceprincipalid" ] = credentials ["serviceprincipalid" ].(string )
92
+ (* serviceEndpoint .Authorization .Parameters )["serviceprincipalkey" ] = credentials ["serviceprincipalkey" ].(string )
93
+ (* serviceEndpoint .Data )["creationMode" ] = "Manual"
41
94
}
95
+
42
96
serviceEndpoint .Type = converter .String ("azurerm" )
43
97
serviceEndpoint .Url = converter .String ("https://management.azure.com/" )
44
98
return serviceEndpoint , projectID
45
99
}
46
100
101
+ func flattenCredentials (serviceEndpoint * serviceendpoint.ServiceEndpoint , hashKey string , hashValue string ) interface {} {
102
+ return []map [string ]interface {}{{
103
+ "serviceprincipalid" : (* serviceEndpoint .Authorization .Parameters )["serviceprincipalid" ],
104
+ "serviceprincipalkey" : (* serviceEndpoint .Authorization .Parameters )["serviceprincipalkey" ],
105
+ hashKey : hashValue ,
106
+ }}
107
+ }
108
+
47
109
// Convert AzDO data structure to internal Terraform data structure
48
110
func flattenServiceEndpointAzureRM (d * schema.ResourceData , serviceEndpoint * serviceendpoint.ServiceEndpoint , projectID * string ) {
49
111
crud .DoBaseFlattening (d , serviceEndpoint , projectID )
50
- d .Set ("azurerm_scope" , (* serviceEndpoint .Authorization .Parameters )["scope" ])
51
- d .Set ("azurerm_spn_clientid" , (* serviceEndpoint .Authorization .Parameters )["serviceprincipalid" ])
52
- tfhelper .HelpFlattenSecret (d , "azurerm_spn_clientsecret" )
112
+ scope := (* serviceEndpoint .Authorization .Parameters )["scope" ]
113
+
114
+ if (* serviceEndpoint .Data )["creationMode" ] == "Manual" {
115
+ newHash , hashKey := tfhelper .HelpFlattenSecretNested (d , "credentials" , d .Get ("credentials.0" ).(map [string ]interface {}), "serviceprincipalkey" )
116
+ credentials := flattenCredentials (serviceEndpoint , hashKey , newHash )
117
+ d .Set ("credentials" , credentials )
118
+ }
119
+
120
+ s := strings .SplitN (scope , "/" , - 1 )
121
+ if len (s ) == 5 {
122
+ d .Set ("resource_group" , s [4 ])
123
+ }
124
+
53
125
d .Set ("azurerm_spn_tenantid" , (* serviceEndpoint .Authorization .Parameters )["tenantid" ])
54
- d .Set ("azurerm_spn_clientsecret" , (* serviceEndpoint .Authorization .Parameters )["serviceprincipalkey" ])
55
- d .Set ("azurerm_subscription_id" , (* serviceEndpoint .Data )["SubscriptionId" ])
56
- d .Set ("azurerm_subscription_name" , (* serviceEndpoint .Data )["SubscriptionName" ])
126
+ d .Set ("azurerm_subscription_id" , (* serviceEndpoint .Data )["subscriptionId" ])
127
+ d .Set ("azurerm_subscription_name" , (* serviceEndpoint .Data )["subscriptionName" ])
57
128
}
0 commit comments