@@ -9,33 +9,52 @@ import (
9
9
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources"
10
10
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-04-01/storage"
11
11
12
+ "github.com/Azure/go-autorest/autorest"
12
13
"github.com/Azure/go-autorest/autorest/azure/auth"
13
14
14
15
"terraform-provider-iterative/task/common"
15
16
"terraform-provider-iterative/task/common/ssh"
16
17
)
17
18
18
19
func New (ctx context.Context , cloud common.Cloud , tags map [string ]string ) (* Client , error ) {
19
- settings , err := auth .GetSettingsFromEnvironment ()
20
- if err != nil {
21
- return nil , err
22
- }
23
-
24
- subscription := settings .GetSubscriptionID ()
25
- if subscription == "" {
26
- return nil , errors .New ("subscription environment variable not found" )
27
- }
28
-
29
- authorizer , err := settings .GetAuthorizer ()
30
- if err != nil {
31
- return nil , err
20
+ var authorizer autorest.Authorizer
21
+
22
+ if azCredentials := cloud .Credentials .AZCredentials ; azCredentials != nil {
23
+ au , err := auth .NewClientCredentialsConfig (
24
+ azCredentials .ClientID ,
25
+ azCredentials .ClientSecret ,
26
+ azCredentials .TenantID ,
27
+ ).Authorizer ()
28
+ if err != nil {
29
+ return nil , err
30
+ }
31
+ authorizer = au
32
+ } else {
33
+ settings , err := auth .GetSettingsFromEnvironment ()
34
+ if err != nil {
35
+ return nil , err
36
+ }
37
+ credentials , err := settings .GetClientCredentials ()
38
+ if err != nil {
39
+ return nil , err
40
+ }
41
+ authorizer , err = settings .GetAuthorizer ()
42
+ if err != nil {
43
+ return nil , err
44
+ }
45
+
46
+ cloud .Credentials .AZCredentials = & common.AZCredentials {
47
+ SubscriptionID : settings .GetSubscriptionID (),
48
+ ClientID : credentials .ClientID ,
49
+ ClientSecret : credentials .ClientSecret ,
50
+ TenantID : credentials .TenantID ,
51
+ }
32
52
}
33
53
34
54
agent := "tpi"
35
55
36
56
c := & Client {
37
- Cloud : cloud ,
38
- Settings : settings ,
57
+ Cloud : cloud ,
39
58
}
40
59
41
60
for key , value := range tags {
@@ -54,75 +73,79 @@ func New(ctx context.Context, cloud common.Cloud, tags map[string]string) (*Clie
54
73
region = val
55
74
}
56
75
76
+ if cloud .Credentials .AZCredentials .SubscriptionID == "" {
77
+ return nil , errors .New ("subscription environment variable not found" )
78
+ }
79
+
57
80
c .Region = region
58
81
59
- c .Services .Groups = resources .NewGroupsClient (subscription )
82
+ c .Services .Groups = resources .NewGroupsClient (cloud . Credentials . AZCredentials . SubscriptionID )
60
83
c .Services .Groups .Authorizer = authorizer
61
84
if err := c .Services .Groups .AddToUserAgent (agent ); err != nil {
62
85
return nil , err
63
86
}
64
87
65
- c .Services .SecurityGroups = network .NewSecurityGroupsClient (subscription )
88
+ c .Services .SecurityGroups = network .NewSecurityGroupsClient (cloud . Credentials . AZCredentials . SubscriptionID )
66
89
c .Services .SecurityGroups .Authorizer = authorizer
67
90
if err := c .Services .SecurityGroups .AddToUserAgent (agent ); err != nil {
68
91
return nil , err
69
92
}
70
93
71
- c .Services .PublicIPPrefixes = network .NewPublicIPPrefixesClient (subscription )
94
+ c .Services .PublicIPPrefixes = network .NewPublicIPPrefixesClient (cloud . Credentials . AZCredentials . SubscriptionID )
72
95
c .Services .PublicIPPrefixes .Authorizer = authorizer
73
96
if err := c .Services .PublicIPPrefixes .AddToUserAgent (agent ); err != nil {
74
97
return nil , err
75
98
}
76
99
77
- c .Services .PublicIPAddresses = network .NewPublicIPAddressesClient (subscription )
100
+ c .Services .PublicIPAddresses = network .NewPublicIPAddressesClient (cloud . Credentials . AZCredentials . SubscriptionID )
78
101
c .Services .PublicIPAddresses .Authorizer = authorizer
79
102
if err := c .Services .PublicIPAddresses .AddToUserAgent (agent ); err != nil {
80
103
return nil , err
81
104
}
82
105
83
- c .Services .VirtualNetworks = network .NewVirtualNetworksClient (subscription )
106
+ c .Services .VirtualNetworks = network .NewVirtualNetworksClient (cloud . Credentials . AZCredentials . SubscriptionID )
84
107
c .Services .VirtualNetworks .Authorizer = authorizer
85
108
if err := c .Services .VirtualNetworks .AddToUserAgent (agent ); err != nil {
86
109
return nil , err
87
110
}
88
111
89
- c .Services .Subnets = network .NewSubnetsClient (subscription )
112
+ c .Services .Subnets = network .NewSubnetsClient (cloud . Credentials . AZCredentials . SubscriptionID )
90
113
c .Services .Subnets .Authorizer = authorizer
91
114
if err := c .Services .Subnets .AddToUserAgent (agent ); err != nil {
92
115
return nil , err
93
116
}
94
117
95
- c .Services .Interfaces = network .NewInterfacesClient (subscription )
118
+ c .Services .Interfaces = network .NewInterfacesClient (cloud . Credentials . AZCredentials . SubscriptionID )
96
119
c .Services .Interfaces .Authorizer = authorizer
97
120
if err := c .Services .Interfaces .AddToUserAgent (agent ); err != nil {
98
121
return nil , err
99
122
}
100
123
101
- c .Services .VirtualMachines = compute .NewVirtualMachinesClient (subscription )
124
+ c .Services .VirtualMachines = compute .NewVirtualMachinesClient (cloud . Credentials . AZCredentials . SubscriptionID )
102
125
c .Services .VirtualMachines .Authorizer = authorizer
103
126
if err := c .Services .VirtualMachines .AddToUserAgent (agent ); err != nil {
104
127
return nil , err
105
128
}
106
129
107
- c .Services .VirtualMachineScaleSets = compute .NewVirtualMachineScaleSetsClient (subscription )
130
+ c .Services .VirtualMachineScaleSets = compute .NewVirtualMachineScaleSetsClient (cloud . Credentials . AZCredentials . SubscriptionID )
108
131
c .Services .VirtualMachineScaleSets .Authorizer = authorizer
109
132
if err := c .Services .VirtualMachineScaleSets .AddToUserAgent (agent ); err != nil {
110
133
return nil , err
111
134
}
112
135
113
- c .Services .VirtualMachineScaleSetVMs = compute .NewVirtualMachineScaleSetVMsClient (subscription )
136
+ c .Services .VirtualMachineScaleSetVMs = compute .NewVirtualMachineScaleSetVMsClient (cloud . Credentials . AZCredentials . SubscriptionID )
114
137
c .Services .VirtualMachineScaleSetVMs .Authorizer = authorizer
115
138
if err := c .Services .VirtualMachineScaleSetVMs .AddToUserAgent (agent ); err != nil {
116
139
return nil , err
117
140
}
118
141
119
- c .Services .StorageAccounts = storage .NewAccountsClient (subscription )
142
+ c .Services .StorageAccounts = storage .NewAccountsClient (cloud . Credentials . AZCredentials . SubscriptionID )
120
143
c .Services .StorageAccounts .Authorizer = authorizer
121
144
if err := c .Services .StorageAccounts .AddToUserAgent (agent ); err != nil {
122
145
return nil , err
123
146
}
124
147
125
- c .Services .BlobContainers = storage .NewBlobContainersClient (subscription )
148
+ c .Services .BlobContainers = storage .NewBlobContainersClient (cloud . Credentials . AZCredentials . SubscriptionID )
126
149
c .Services .BlobContainers .Authorizer = authorizer
127
150
if err := c .Services .BlobContainers .AddToUserAgent (agent ); err != nil {
128
151
return nil , err
@@ -153,10 +176,7 @@ type Client struct {
153
176
}
154
177
155
178
func (c * Client ) GetKeyPair (ctx context.Context ) (* ssh.DeterministicSSHKeyPair , error ) {
156
- credentials , err := c .Settings .GetClientCredentials ()
157
- if err != nil {
158
- return nil , err
159
- }
179
+ credentials := c .Cloud .Credentials .AZCredentials
160
180
161
181
if len (credentials .ClientSecret ) == 0 {
162
182
return nil , errors .New ("unable to find client secret" )
0 commit comments