9
9
"os"
10
10
"os/exec"
11
11
"path/filepath"
12
- "reflect"
13
12
"strings"
14
13
15
14
"github.com/hashicorp/terraform-exec/tfexec"
@@ -102,6 +101,8 @@ func (t *terraform) Provision(stream proto.DRPCProvisioner_ProvisionStream) erro
102
101
env = append (env ,
103
102
"CODER_URL=" + start .Metadata .CoderUrl ,
104
103
"CODER_WORKSPACE_TRANSITION=" + strings .ToLower (start .Metadata .WorkspaceTransition .String ()),
104
+ "CODER_WORKSPACE_NAME=" + start .Metadata .WorkspaceName ,
105
+ "CODER_WORKSPACE_OWNER=" + start .Metadata .WorkspaceOwner ,
105
106
)
106
107
for key , value := range provisionersdk .AgentScriptEnv () {
107
108
env = append (env , key + "=" + value )
@@ -285,38 +286,24 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
285
286
if envRaw , has := resource .Expressions ["env" ]; has {
286
287
env , ok := envRaw .ConstantValue .(map [string ]string )
287
288
if ! ok {
288
- return nil , xerrors .Errorf ("unexpected type %q for env map" , reflect . TypeOf ( envRaw .ConstantValue ). String () )
289
+ return nil , xerrors .Errorf ("unexpected type %T for env map" , envRaw .ConstantValue )
289
290
}
290
291
agent .Env = env
291
292
}
292
293
if startupScriptRaw , has := resource .Expressions ["startup_script" ]; has {
293
294
startupScript , ok := startupScriptRaw .ConstantValue .(string )
294
295
if ! ok {
295
- return nil , xerrors .Errorf ("unexpected type %q for startup script" , reflect . TypeOf ( startupScriptRaw .ConstantValue ). String () )
296
+ return nil , xerrors .Errorf ("unexpected type %T for startup script" , startupScriptRaw .ConstantValue )
296
297
}
297
298
agent .StartupScript = startupScript
298
299
}
299
- if auth , has := resource .Expressions ["auth" ]; has {
300
- if len (auth .ExpressionData .NestedBlocks ) > 0 {
301
- block := auth .ExpressionData .NestedBlocks [0 ]
302
- authType , has := block ["type" ]
303
- if has {
304
- authTypeValue , valid := authType .ConstantValue .(string )
305
- if ! valid {
306
- return nil , xerrors .Errorf ("unexpected type %q for auth type" , reflect .TypeOf (authType .ConstantValue ))
307
- }
308
- switch authTypeValue {
309
- case "google-instance-identity" :
310
- instanceID , _ := block ["instance_id" ].ConstantValue .(string )
311
- agent .Auth = & proto.Agent_GoogleInstanceIdentity {
312
- GoogleInstanceIdentity : & proto.GoogleInstanceIdentityAuth {
313
- InstanceId : instanceID ,
314
- },
315
- }
316
- default :
317
- return nil , xerrors .Errorf ("unknown auth type: %q" , authTypeValue )
318
- }
319
- }
300
+ if instanceIDRaw , has := resource .Expressions ["instance_id" ]; has {
301
+ instanceID , ok := instanceIDRaw .ConstantValue .(string )
302
+ if ! ok {
303
+ return nil , xerrors .Errorf ("unexpected type %T for instance_id" , instanceIDRaw .ConstantValue )
304
+ }
305
+ agent .Auth = & proto.Agent_InstanceId {
306
+ InstanceId : instanceID ,
320
307
}
321
308
}
322
309
@@ -379,12 +366,9 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
379
366
resources := make ([]* proto.Resource , 0 )
380
367
if state .Values != nil {
381
368
type agentAttributes struct {
382
- ID string `mapstructure:"id"`
383
- Token string `mapstructure:"token"`
384
- Auth []struct {
385
- Type string `mapstructure:"type"`
386
- InstanceID string `mapstructure:"instance_id"`
387
- } `mapstructure:"auth"`
369
+ ID string `mapstructure:"id"`
370
+ Token string `mapstructure:"token"`
371
+ InstanceID string `mapstructure:"instance_id"`
388
372
Env map [string ]string `mapstructure:"env"`
389
373
StartupScript string `mapstructure:"startup_script"`
390
374
}
@@ -409,17 +393,9 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
409
393
Token : attrs .Token ,
410
394
},
411
395
}
412
- if len (attrs .Auth ) > 0 {
413
- auth := attrs .Auth [0 ]
414
- switch auth .Type {
415
- case "google-instance-identity" :
416
- agent .Auth = & proto.Agent_GoogleInstanceIdentity {
417
- GoogleInstanceIdentity : & proto.GoogleInstanceIdentityAuth {
418
- InstanceId : auth .InstanceID ,
419
- },
420
- }
421
- default :
422
- return nil , xerrors .Errorf ("unknown auth type: %q" , auth .Type )
396
+ if attrs .InstanceID != "" {
397
+ agent .Auth = & proto.Agent_InstanceId {
398
+ InstanceId : attrs .InstanceID ,
423
399
}
424
400
}
425
401
resourceKey := strings .Join ([]string {resource .Type , resource .Name }, "." )
@@ -453,14 +429,12 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
453
429
}
454
430
}
455
431
456
- if agent != nil {
457
- if agent .GetGoogleInstanceIdentity () != nil {
458
- // Make sure the instance has an instance ID!
459
- _ , exists := resource .AttributeValues ["instance_id" ]
460
- if ! exists {
461
- // This was a mistake!
462
- agent = nil
463
- }
432
+ if agent != nil && agent .GetInstanceId () != "" {
433
+ // Make sure the instance has an instance ID!
434
+ _ , exists := resource .AttributeValues ["instance_id" ]
435
+ if ! exists {
436
+ // This was a mistake!
437
+ agent = nil
464
438
}
465
439
}
466
440
0 commit comments