@@ -10,23 +10,21 @@ import (
10
10
"log"
11
11
12
12
"github.com/cloudflare/cloudflare-go"
13
- "github.com/hashicorp/go-tfe"
14
13
"github.com/spf13/cobra"
15
14
"github.com/spf13/viper"
16
15
17
16
"github.com/silinternational/idp-cli/cmd/cli/flags"
18
17
)
19
18
20
19
type DnsCommand struct {
21
- cfClient * cloudflare.API
22
- cfZone * cloudflare.ResourceContainer
23
- domainName string
24
- failback bool
25
- tfcOrg string
26
- tfcOrgAlt string
27
- tfcToken string
28
- tfcTokenAlt string
29
- testMode bool
20
+ cfClient * cloudflare.API
21
+ cfZone * cloudflare.ResourceContainer
22
+ domainName string
23
+ env string
24
+ failback bool
25
+ region string
26
+ region2 string
27
+ testMode bool
30
28
}
31
29
32
30
type DnsValues struct {
@@ -60,8 +58,7 @@ func runDnsCommand(failback bool) {
60
58
61
59
d := newDnsCommand (pFlags , failback )
62
60
63
- values := d .getDnsValuesFromTfc (pFlags )
64
- d .setDnsRecordValues (pFlags .idp , values )
61
+ d .setDnsRecordValues (pFlags .idp )
65
62
}
66
63
67
64
func newDnsCommand (pFlags PersistentFlags , failback bool ) * DnsCommand {
@@ -92,57 +89,58 @@ func newDnsCommand(pFlags PersistentFlags, failback bool) *DnsCommand {
92
89
fmt .Printf ("Using domain name %s with ID %s\n " , d .domainName , zoneID )
93
90
d .cfZone = cloudflare .ZoneIdentifier (zoneID )
94
91
95
- d .tfcToken = pFlags .tfcToken
96
- d .tfcTokenAlt = pFlags .tfcTokenAlt
97
- d .tfcOrg = pFlags .org
98
- d . tfcOrgAlt = pFlags . orgAlt
92
+ d .env = pFlags .env
93
+ d .region = pFlags .region
94
+ d .region2 = pFlags .secondaryRegion
95
+
99
96
d .failback = failback
100
97
101
98
return & d
102
99
}
103
100
104
- func (d * DnsCommand ) setDnsRecordValues (idpKey string , dnsValues DnsValues ) {
101
+ func (d * DnsCommand ) setDnsRecordValues (idpKey string ) {
105
102
if d .failback {
106
103
fmt .Println ("Setting DNS records to primary region..." )
107
104
} else {
108
105
fmt .Println ("Setting DNS records to secondary region..." )
109
106
}
110
107
108
+ region := d .region2
109
+ if d .failback {
110
+ region = d .region
111
+ }
112
+
113
+ supportBotName := "sherlock"
114
+ if d .env != envProd {
115
+ supportBotName = "watson"
116
+ }
117
+
111
118
dnsRecords := []struct {
112
- name string
113
- valueFlag string
114
- tfcValue string
119
+ name string
120
+ value string
115
121
}{
116
122
// "mfa-api" is the TOTP API, also known as serverless-mfa-api
117
- {"mfa-api" , "mfa-api-value" , dnsValues . mfa },
123
+ {"mfa-api" , "mfa-api-" + region },
118
124
119
125
// "twosv-api" is the Webauthn API, also known as serverless-mfa-api-go
120
- {"twosv-api" , "twosv-api-value" , dnsValues . twosv },
126
+ {"twosv-api" , "twosv-api-" + region },
121
127
122
- // "support-bot" is the idp-support-bot API that is configured in the Slack API dashboard
123
- {"sherlock" , "support-bot-value" , dnsValues . bot },
128
+ // this is the idp-support-bot API that is configured in the Slack API dashboard
129
+ {supportBotName , supportBotName + "-" + region },
124
130
125
131
// ECS services
126
- {idpKey + "-email" , " email-service-value" , dnsValues . albInternal },
127
- {idpKey + "-broker" , "id -broker-value" , dnsValues . albInternal },
128
- {idpKey + "-pw-api" , " pw-api-value" , dnsValues . albExternal },
129
- {idpKey , "ssp-value" , dnsValues . albExternal },
130
- {idpKey + "-sync" , "id -sync-value" , dnsValues . albExternal },
132
+ {idpKey + "-email" , idpKey + "- email-" + region },
133
+ {idpKey + "-broker" , idpKey + " -broker-" + region },
134
+ {idpKey + "-pw-api" , idpKey + "- pw-api-" + region },
135
+ {idpKey , idpKey + "-" + region },
136
+ {idpKey + "-sync" , idpKey + " -sync-" + region },
131
137
}
132
138
133
139
for _ , record := range dnsRecords {
134
- value := getDnsValue (record .valueFlag , record .tfcValue )
135
- d .setCloudflareCname (record .name , value )
140
+ d .setCloudflareCname (record .name , record .value )
136
141
}
137
142
}
138
143
139
- func getDnsValue (valueFlag , tfcValue string ) string {
140
- if tfcValue != "" {
141
- return tfcValue
142
- }
143
- return viper .GetString (valueFlag )
144
- }
145
-
146
144
func (d * DnsCommand ) setCloudflareCname (name , value string ) {
147
145
if value == "" {
148
146
fmt .Printf (" skipping %s (no value provided)\n " , name )
@@ -186,142 +184,3 @@ func (d *DnsCommand) setCloudflareCname(name, value string) {
186
184
log .Fatalf ("error updating DNS record %s: %s" , name , err )
187
185
}
188
186
}
189
-
190
- func (d * DnsCommand ) getDnsValuesFromTfc (pFlags PersistentFlags ) (values DnsValues ) {
191
- ctx := context .Background ()
192
-
193
- var clusterWorkspaceName string
194
- if d .failback {
195
- clusterWorkspaceName = clusterWorkspace (pFlags )
196
- } else {
197
- clusterWorkspaceName = clusterSecondaryWorkspace (pFlags )
198
- }
199
-
200
- internal , external := d .getAlbValuesFromTfc (ctx , clusterWorkspaceName )
201
- values .albInternal = internal
202
- values .albExternal = external
203
-
204
- bot := "idp-support-bot-prod"
205
- if pFlags .env != envProd {
206
- bot = "idp-support-bot-dev" // TODO: consider renaming the workspace name so this can be simplified
207
- }
208
- values .bot = d .getLambdaDnsValueFromTfc (ctx , bot )
209
-
210
- twosv := "serverless-mfa-api-go-prod"
211
- if pFlags .env != envProd {
212
- twosv = "serverless-mfa-api-go-dev" // TODO: consider renaming the workspace name so this can be simplified
213
- }
214
- values .twosv = d .getLambdaDnsValueFromTfc (ctx , twosv )
215
-
216
- mfa := "serverless-mfa-api-prod"
217
- if pFlags .env != envProd {
218
- mfa = "serverless-mfa-api-dev" // TODO: consider renaming the workspace name so this can be simplified
219
- }
220
- values .mfa = d .getLambdaDnsValueFromTfc (ctx , mfa )
221
- return
222
- }
223
-
224
- func (d * DnsCommand ) getAlbValuesFromTfc (ctx context.Context , workspaceName string ) (internal , external string ) {
225
- workspaceID , client , err := d .findTfcWorkspace (ctx , workspaceName )
226
- if err != nil {
227
- fmt .Printf ("Failed to get ALB DNS values: %s\n Will use DNS config values if provided.\n " , err )
228
- return
229
- }
230
-
231
- outputs , err := client .StateVersionOutputs .ReadCurrent (ctx , workspaceID )
232
- if err != nil {
233
- fmt .Printf ("Error reading Terraform state outputs on workspace %s: %s" , workspaceName , err )
234
- return
235
- }
236
-
237
- for _ , item := range outputs .Items {
238
- itemValue , _ := item .Value .(string )
239
- switch item .Name {
240
- case "alb_dns_name" :
241
- external = itemValue
242
- case "internal_alb_dns_name" :
243
- internal = itemValue
244
- }
245
- }
246
- return
247
- }
248
-
249
- func (d * DnsCommand ) getLambdaDnsValueFromTfc (ctx context.Context , workspaceName string ) string {
250
- outputName := "secondary_region_domain_name"
251
- if d .failback {
252
- outputName = "primary_region_domain_name"
253
- }
254
- val , err := d .getTfcOutputFromWorkspace (ctx , workspaceName , outputName )
255
- if err != nil {
256
- fmt .Printf ("Error: %s\n Will use config value if provided.\n " , err )
257
- return ""
258
- }
259
- return val
260
- }
261
-
262
- func (d * DnsCommand ) getTfcOutputFromWorkspace (ctx context.Context , workspaceName , outputName string ) (string , error ) {
263
- workspaceID , client , err := d .findTfcWorkspace (ctx , workspaceName )
264
- if err != nil {
265
- return "" , fmt .Errorf ("failed to get DNS value from %s: %w" , workspaceName , err )
266
- }
267
-
268
- outputs , err := client .StateVersionOutputs .ReadCurrent (ctx , workspaceID )
269
- if err != nil {
270
- return "" , fmt .Errorf ("error reading Terraform state outputs on workspace %s: %w" , workspaceName , err )
271
- }
272
-
273
- for _ , item := range outputs .Items {
274
- if item .Name == outputName {
275
- if itemValue , ok := item .Value .(string ); ok {
276
- return itemValue , nil
277
- }
278
- break
279
- }
280
- }
281
-
282
- return "" , fmt .Errorf ("value for %s not found in %s\n " , outputName , workspaceName )
283
- }
284
-
285
- // findTfcWorkspace looks for a workspace by name in two different Terraform Cloud accounts and returns
286
- // the workspace ID and an API client for the account where the workspace was found
287
- func (d * DnsCommand ) findTfcWorkspace (ctx context.Context , workspaceName string ) (id string , client * tfe.Client , err error ) {
288
- config := & tfe.Config {
289
- Token : d .tfcToken ,
290
- RetryServerErrors : true ,
291
- }
292
-
293
- client , err = tfe .NewClient (config )
294
- if err != nil {
295
- err = fmt .Errorf ("error creating Terraform client: %s" , err )
296
- return
297
- }
298
-
299
- w , err := client .Workspaces .Read (ctx , d .tfcOrg , workspaceName )
300
- if err == nil {
301
- id = w .ID
302
- return
303
- }
304
-
305
- if d .tfcTokenAlt == "" {
306
- err = fmt .Errorf ("error reading Terraform workspace %s: %s" , workspaceName , err )
307
- return
308
- }
309
-
310
- fmt .Printf ("Workspace %s not found using %s, trying %s\n " , workspaceName , flags .TfcToken , flags .TfcTokenAlternate )
311
-
312
- config .Token = d .tfcTokenAlt
313
- client , err = tfe .NewClient (config )
314
- if err != nil {
315
- err = fmt .Errorf ("error creating alternate Terraform client: %s" , err )
316
- return
317
- }
318
-
319
- w , err = client .Workspaces .Read (ctx , d .tfcOrgAlt , workspaceName )
320
- if err != nil {
321
- err = fmt .Errorf ("error reading Terraform workspace %s using %s: %s" , workspaceName , flags .TfcTokenAlternate , err )
322
- return
323
- }
324
-
325
- id = w .ID
326
- return
327
- }
0 commit comments