Skip to content

Commit bc93dd1

Browse files
authored
cmd/atlas/internal/cmdext: ignore cloud block if token is not set (#3104)
but keep the atlas block
1 parent 32d8985 commit bc93dd1

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

cmd/atlas/internal/cmdext/cmdext.go

+26-26
Original file line numberDiff line numberDiff line change
@@ -493,41 +493,41 @@ type AtlasConfig struct {
493493
// }
494494
func (c *AtlasConfig) InitBlock() schemahcl.Option {
495495
return schemahcl.WithInitBlock("atlas", func(_ context.Context, ectx *hcl.EvalContext, block *hclsyntax.Block) (cty.Value, error) {
496+
atlasVal, diags := (&hclsyntax.ScopeTraversalExpr{
497+
Traversal: hcl.Traversal{hcl.TraverseRoot{Name: "atlas", SrcRange: block.Range()}},
498+
}).Value(ectx)
499+
if diags.HasErrors() {
500+
if len(diags) > 1 || diags[0].Summary != "Unknown variable" {
501+
return cty.NilVal, fmt.Errorf("atlas.cloud: getting config: %v", diags)
502+
}
503+
// Create an empty object if the atlas object is not set.
504+
atlasVal = cty.ObjectVal(map[string]cty.Value{})
505+
}
496506
var args struct {
497507
Cloud struct {
498508
Token string `hcl:"token"`
499509
URL string `hcl:"url,optional"`
500510
Project string `hcl:"project,optional"`
501511
} `hcl:"cloud,block"`
502512
}
503-
if diags := gohcl.DecodeBody(block.Body, ectx, &args); diags.HasErrors() {
513+
switch diags := gohcl.DecodeBody(block.Body, ectx, &args); {
514+
case diags.HasErrors():
504515
return cty.NilVal, fmt.Errorf("atlas.cloud: decoding body: %v", diags)
505-
}
506-
if args.Cloud.Token == "" {
507-
return cty.NilVal, nil // If no token was set, the cloud is not initialized.
508-
}
509-
if args.Cloud.Project == "" {
510-
args.Cloud.Project = cloudapi.DefaultProjectName
511-
}
512-
c.Token = args.Cloud.Token
513-
c.Project = args.Cloud.Project
514-
c.Client = cloudapi.New(args.Cloud.URL, args.Cloud.Token)
515-
cloud := cty.ObjectVal(map[string]cty.Value{
516-
"client": cty.CapsuleVal(clientType, c.Client),
517-
"project": cty.StringVal(args.Cloud.Project),
518-
})
519-
av, diags := (&hclsyntax.ScopeTraversalExpr{
520-
Traversal: hcl.Traversal{hcl.TraverseRoot{Name: "atlas", SrcRange: block.Range()}},
521-
}).Value(ectx)
522-
switch {
523-
case !diags.HasErrors():
524-
m := av.AsValueMap()
525-
m["cloud"] = cloud
526-
return cty.ObjectVal(m), nil
527-
case len(diags) == 1 && diags[0].Summary == "Unknown variable":
528-
return cty.ObjectVal(map[string]cty.Value{"cloud": cloud}), nil
516+
case args.Cloud.Token == "":
517+
return atlasVal, nil // If no token was set, the cloud is not initialized.
529518
default:
530-
return cty.NilVal, fmt.Errorf("atlas.cloud: getting config: %v", diags)
519+
if args.Cloud.Project == "" {
520+
args.Cloud.Project = cloudapi.DefaultProjectName
521+
}
522+
c.Token = args.Cloud.Token
523+
c.Project = args.Cloud.Project
524+
c.Client = cloudapi.New(args.Cloud.URL, args.Cloud.Token)
525+
m := atlasVal.AsValueMap()
526+
m["cloud"] = cty.ObjectVal(map[string]cty.Value{
527+
"client": cty.CapsuleVal(clientType, c.Client),
528+
"project": cty.StringVal(args.Cloud.Project),
529+
})
530+
return cty.ObjectVal(m), nil
531531
}
532532
})
533533
}

cmd/atlas/internal/cmdext/cmdext_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,22 @@ atlas {
533533
token = ""
534534
}
535535
}
536+
`), &v, map[string]cty.Value{})
537+
require.NoError(t, err)
538+
require.Nil(t, cfg.Client)
539+
require.Empty(t, cfg.Project)
540+
541+
cfg = &cmdext.AtlasConfig{}
542+
err = state.EvalBytes([]byte(`
543+
atlas {
544+
cloud {
545+
url = "url"
546+
token = ""
547+
}
548+
}
549+
env {
550+
name = atlas.env
551+
}
536552
`), &v, map[string]cty.Value{})
537553
require.NoError(t, err)
538554
require.Nil(t, cfg.Client)

0 commit comments

Comments
 (0)