Skip to content

Commit

Permalink
remove type from resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Singer committed Jan 31, 2023
1 parent 06c723f commit ec393b8
Show file tree
Hide file tree
Showing 17 changed files with 30 additions and 52 deletions.
15 changes: 6 additions & 9 deletions pkg/cli/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"strings"

"github.com/klothoplatform/klotho/pkg/config"
"github.com/klothoplatform/klotho/pkg/core"
execunit "github.com/klothoplatform/klotho/pkg/exec_unit"
"github.com/klothoplatform/klotho/pkg/infra/kubernetes"
Expand Down Expand Up @@ -167,24 +168,20 @@ func OutputResources(result *core.CompilationResult, outDir string) (resourceCou
return
}

func GetLanguagesUsed(result *core.CompilationResult) map[core.ExecutableType]bool {
executableLangs := make(map[core.ExecutableType]bool)
func GetLanguagesUsed(result *core.CompilationResult) []core.ExecutableType {
executableLangs := []core.ExecutableType{}
for _, res := range result.Resources() {
switch r := res.(type) {
case *core.ExecutionUnit:
executableLangs[r.Executable.Type] = true
executableLangs = append(executableLangs, r.Executable.Type)
}
}
return executableLangs
}

func GetResourceTypeCount(result *core.CompilationResult) (resourceCounts map[string]map[string]int) {
resourceCounts = make(map[string]map[string]int)
func GetResourceTypeCount(result *core.CompilationResult, cfg *config.Application) (resourceCounts []string) {
for _, res := range result.Resources() {
if _, ok := resourceCounts[res.Key().Kind]; !ok {
resourceCounts[res.Key().Kind] = make(map[string]int)
}
resourceCounts[res.Key().Kind][res.Type()]++
resourceCounts = append(resourceCounts, cfg.GetResourceType(res))
}
return
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/cli/klothomain.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
}
klothoName := "klotho"
if km.VersionQualifier != "" {
klothoName += " " + km.VersionQualifier
analyticsClient.Properties[km.VersionQualifier] = true
}

// if update is specified do the update in place
Expand Down Expand Up @@ -290,7 +290,7 @@ func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
"app": appCfg.AppName,
})

analyticsClient.Info(klothoName + "pre-compile")
analyticsClient.Info(klothoName + " pre-compile")

input, err := input.ReadOSDir(appCfg, cfg.config)
if err != nil {
Expand Down Expand Up @@ -321,7 +321,7 @@ func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
Plugins: plugins.Plugins(),
}

analyticsClient.Info(klothoName + "compiling")
analyticsClient.Info(klothoName + " compiling")

result, err := compiler.Compile(input)
if err != nil || hadErrors.Load() {
Expand All @@ -330,7 +330,7 @@ func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
} else {
err = errors.New("Failed run of klotho invocation")
}
analyticsClient.Error(klothoName + "compiling failed")
analyticsClient.Error(klothoName + " compiling failed")

cmd.SilenceErrors = true
cmd.SilenceUsage = true
Expand All @@ -347,10 +347,10 @@ func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
}

CloseTreeSitter(result)
analyticsClient.AppendProperties(map[string]interface{}{"resource_types": GetResourceTypeCount(result)})
analyticsClient.AppendProperties(map[string]interface{}{"resource_types": GetResourceTypeCount(result, &appCfg)})
analyticsClient.AppendProperties(map[string]interface{}{"languages": GetLanguagesUsed(result)})
analyticsClient.AppendProperties(map[string]interface{}{"resources": resourceCounts})
analyticsClient.Info(klothoName + "compile complete")
analyticsClient.Info(klothoName + " compile complete")

return nil
}
2 changes: 0 additions & 2 deletions pkg/core/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ type (
}

CloudResource interface {
// Type returns the provider-specific type (implementation) of the resource's kind (`Key().Kind`)
Type() string
// Key returns the unique identifier for the resource.
Key() ResourceKey
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/core/exec_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ var (
ExecutableTypeGolang = ExecutableType("Golang")
)

func (unit *ExecutionUnit) Type() string { return unit.ExecType }

func (unit *ExecutionUnit) Key() ResourceKey {
return ResourceKey{
Name: unit.Name,
Expand Down
2 changes: 0 additions & 2 deletions pkg/core/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package core
type (
Gateway struct {
Name string
GWType string
Routes []Route
// Map of gateway targets with the exec unit name as the key
DefinedIn string
Expand Down Expand Up @@ -41,7 +40,6 @@ var (
}
)

func (gw *Gateway) Type() string { return gw.GWType }
func NewGateway(name string) *Gateway {
return &Gateway{
Name: name,
Expand Down
2 changes: 0 additions & 2 deletions pkg/core/infra_as_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ type InfraFiles struct {

var InfraAsCodeKind = "infra_as_code"

func (*InfraFiles) Type() string { return "" }

func (iac *InfraFiles) Key() ResourceKey {
return ResourceKey{
Name: iac.Name,
Expand Down
2 changes: 0 additions & 2 deletions pkg/core/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ type (

var InputFilesKind = "input_files"

func (*InputFiles) Type() string { return "" }

func (*InputFiles) Key() ResourceKey {
return ResourceKey{
Kind: InputFilesKind,
Expand Down
7 changes: 2 additions & 5 deletions pkg/core/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (

type (
Persist struct {
Name string
PersistType string
Kind PersistKind
Name string
Kind PersistKind
}
Secrets struct {
Persist
Expand All @@ -28,8 +27,6 @@ const (
PersistRedisClusterKind PersistKind = "persist_redis_cluster"
)

func (p *Persist) Type() string { return p.PersistType }

func (p *Persist) Key() ResourceKey {
return ResourceKey{
Name: p.Name,
Expand Down
3 changes: 0 additions & 3 deletions pkg/core/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ type (
PubSub struct {
Path string
Name string
PSType string
Events map[string]*Event
}

Expand All @@ -22,8 +21,6 @@ type (

const PubSubKind = "pubsub"

func (p PubSub) Type() string { return p.PSType }

func (p PubSub) Key() ResourceKey {
return ResourceKey{
Name: p.Name,
Expand Down
3 changes: 0 additions & 3 deletions pkg/core/static_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

type (
StaticUnit struct {
StaticType string
Name string
IndexDocument string
StaticFiles ConcurrentMap[string, File]
Expand All @@ -17,8 +16,6 @@ type (

var StaticUnitKind = "static_unit"

func (unit *StaticUnit) Type() string { return unit.StaticType }

func (unit *StaticUnit) Key() ResourceKey {
return ResourceKey{
Name: unit.Name,
Expand Down
5 changes: 3 additions & 2 deletions pkg/lang/golang/aws_runtime/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ var dockerfileLambda []byte

func (r *AwsRuntime) AddExecRuntimeFiles(unit *core.ExecutionUnit, result *core.CompilationResult, deps *core.Dependencies) error {
var DockerFile []byte
switch unit.Type() {
unitType := r.Cfg.GetResourceType(unit)
switch unitType {
case "lambda":
DockerFile = dockerfileLambda
default:
return errors.Errorf("unsupported execution unit type: '%s'", unit.Type())
return errors.Errorf("unsupported execution unit type: '%s'", unitType)
}

templateData := TemplateData{
Expand Down
8 changes: 5 additions & 3 deletions pkg/lang/javascript/aws_runtime/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func (r *AwsRuntime) AddPubsubRuntimeFiles(unit *core.ExecutionUnit) error {

func (r *AwsRuntime) AddProxyRuntimeFiles(unit *core.ExecutionUnit, proxyType string) error {
var proxyFile []byte
unitType := r.Config.GetResourceType(unit)
switch proxyType {
case "eks":
proxyFile = proxyEks
Expand All @@ -184,7 +185,7 @@ func (r *AwsRuntime) AddProxyRuntimeFiles(unit *core.ExecutionUnit, proxyType st
case "lambda":
proxyFile = proxyLambda
default:
return errors.Errorf("unsupported exceution unit type: '%s'", unit.Type())
return errors.Errorf("unsupported exceution unit type: '%s'", unitType)
}

err := r.AddRuntimeFile(unit, proxyType+"_proxy.js.tmpl", proxyFile)
Expand All @@ -201,15 +202,16 @@ func (r *AwsRuntime) AddProxyRuntimeFiles(unit *core.ExecutionUnit, proxyType st

func (r *AwsRuntime) AddExecRuntimeFiles(unit *core.ExecutionUnit, result *core.CompilationResult, deps *core.Dependencies) error {
var DockerFile, Dispatcher []byte
switch unit.Type() {
unitType := r.Config.GetResourceType(unit)
switch unitType {
case "ecs", "eks", "apprunner":
DockerFile = dockerfileFargate
Dispatcher = dispatcherFargate
case "lambda":
DockerFile = dockerfileLambda
Dispatcher = dispatcherLambda
default:
return errors.Errorf("unsupported execution unit type: '%s'", unit.Type())
return errors.Errorf("unsupported execution unit type: '%s'", unitType)
}

templateData := TemplateData{
Expand Down
7 changes: 4 additions & 3 deletions pkg/lang/python/aws_runtime/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ func (r *AwsRuntime) GetAppName() string {
func (r *AwsRuntime) AddExecRuntimeFiles(unit *core.ExecutionUnit, result *core.CompilationResult, deps *core.Dependencies) error {
var dockerFile, dispatcher []byte
var requirements string
switch unit.Type() {
unitType := r.Cfg.GetResourceType(unit)
switch unitType {
case "lambda":
dockerFile = dockerfileLambda
dispatcher = dispatcherLambda
Expand All @@ -104,7 +105,7 @@ func (r *AwsRuntime) AddExecRuntimeFiles(unit *core.ExecutionUnit, result *core.
dispatcher = dispatcherFargate
requirements = execRequirementsFargate
default:
return errors.Errorf("unsupported execution unit type: '%s'", unit.Type())
return errors.Errorf("unsupported execution unit type: '%s'", unitType)
}

templateData := TemplateData{
Expand Down Expand Up @@ -242,7 +243,7 @@ func (r *AwsRuntime) AddProxyRuntimeFiles(unit *core.ExecutionUnit, proxyType st
case "lambda":
fileContents = proxyLambdaContents
default:
return errors.Errorf("unsupported exceution unit type: '%s'", unit.Type())
return errors.Errorf("unsupported exceution unit type: '%s'", r.Cfg.GetResourceType(unit))
}
err := r.AddRuntimeFile(unit, proxyType+"_proxy.py", []byte(fileContents))
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/provider/aws/infra_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (a *AWS) Transform(result *core.CompilationResult, deps *core.Dependencies)
cfg := a.Config.GetExecutionUnit(key.Name)
unit := provider.ExecUnit{
Name: res.Name,
Type: res.Type(),
Type: cfg.Type,
EnvironmentVariables: res.EnvironmentVariables,
NetworkPlacement: cfg.NetworkPlacement,
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func (a *AWS) Transform(result *core.CompilationResult, deps *core.Dependencies)
cfg := a.Config.GetStaticUnit(key.Name)
unit := provider.StaticUnit{
Name: res.Name,
Type: res.Type(),
Type: cfg.Type,
IndexDocument: res.IndexDocument,
ContentDeliveryNetwork: cfg.ContentDeliveryNetwork,
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/provider/aws/infra_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func TestInfraTemplateModification(t *testing.T) {
name: "simple test",
results: []core.CloudResource{&core.Gateway{
Name: "gw",
GWType: core.GatewayKind,
Routes: []core.Route{{Path: "/"}},
},
&core.ExecutionUnit{
Expand Down Expand Up @@ -145,7 +144,6 @@ func Test_GenerateCloudfrontDistributions(t *testing.T) {
results: []core.CloudResource{
&core.Gateway{
Name: "gw",
GWType: core.GatewayKind,
Routes: []core.Route{{Path: "/"}},
},
},
Expand Down Expand Up @@ -251,7 +249,6 @@ func Test_GenerateCloudfrontDistributions(t *testing.T) {
},
&core.Gateway{
Name: "gw",
GWType: core.GatewayKind,
Routes: []core.Route{{Path: "/"}},
},
},
Expand Down
3 changes: 1 addition & 2 deletions pkg/static_unit/plugin_static_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func (p StaticUnitSplit) Transform(result *core.CompilationResult, deps *core.De
errs.Append(cause)
}
newUnit := &core.StaticUnit{
StaticType: p.Config.GetStaticUnit(cap.ID).Type,
Name: cap.ID,
Name: cap.ID,
}

indexDocument, ok := cap.Directives.String("index_document")
Expand Down
2 changes: 1 addition & 1 deletion pkg/topology/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func generateIconID(resource core.ResourceKey) string {
return fmt.Sprintf("%s_%s", resource.Name, resource.Kind)
}
func (p Plugin) getImagePath(resource core.CloudResource) string {
imgPath, _ := core.DiagramEntityToImgPath.Get(resource.Key().Kind, resource.Type(), p.Config.Provider)
imgPath, _ := core.DiagramEntityToImgPath.Get(resource.Key().Kind, p.Config.GetResourceType(resource), p.Config.Provider)
return baseIconUrl + imgPath
}

Expand Down

0 comments on commit ec393b8

Please sign in to comment.