-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
starlark/types: Terraform global object
- Loading branch information
Showing
11 changed files
with
248 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mcuadros/ascode/terraform" | ||
"go.starlark.net/starlark" | ||
) | ||
|
||
type Terraform struct { | ||
b *Backend | ||
p *ProviderCollection | ||
} | ||
|
||
func MakeTerraform(pm *terraform.PluginManager) *Terraform { | ||
return &Terraform{ | ||
p: NewProviderCollection(pm), | ||
} | ||
} | ||
|
||
// Attr honors the starlark.HasAttrs interface. | ||
func (t *Terraform) Attr(name string) (starlark.Value, error) { | ||
switch name { | ||
case "provider": | ||
return t.p, nil | ||
case "backend": | ||
if t.b == nil { | ||
return starlark.None, nil | ||
} | ||
|
||
return t.b, nil | ||
} | ||
|
||
return starlark.None, nil | ||
} | ||
|
||
// SetField honors the starlark.HasSetField interface. | ||
func (t *Terraform) SetField(name string, val starlark.Value) error { | ||
if name != "backend" { | ||
errmsg := fmt.Sprintf("terraform has no .%s field or method", name) | ||
return starlark.NoSuchAttrError(errmsg) | ||
} | ||
|
||
if b, ok := val.(*Backend); ok { | ||
t.b = b | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("unexpected value %s at %s", val.Type(), name) | ||
} | ||
|
||
// AttrNames honors the starlark.HasAttrs interface. | ||
func (t *Terraform) AttrNames() []string { | ||
return []string{"provider", "backend"} | ||
} | ||
|
||
// Freeze honors the starlark.Value interface. | ||
func (t *Terraform) Freeze() {} // immutable | ||
|
||
// Hash honors the starlark.Value interface. | ||
func (t *Terraform) Hash() (uint32, error) { | ||
return 0, fmt.Errorf("unhashable type: Terraform") | ||
} | ||
|
||
// String honors the starlark.Value interface. | ||
func (t *Terraform) String() string { | ||
return "terraform" | ||
} | ||
|
||
// Truth honors the starlark.Value interface. | ||
func (t *Terraform) Truth() starlark.Bool { | ||
return t.p.Len() != 0 | ||
} | ||
|
||
// Type honors the starlark.Value interface. | ||
func (t *Terraform) Type() string { | ||
return "Terraform" | ||
} | ||
|
||
var _ starlark.Value = &Terraform{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestTerraform(t *testing.T) { | ||
doTest(t, "testdata/terraform.star") | ||
} |
Oops, something went wrong.