Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #131 - Rename resource creators from camelCase to PascalCase #136

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions codegen/src/CodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ class CodeGen(implicit providerConfig: Config.ProviderConfig, typeMapper: TypeMa
val argsClassCoordinates = typeCoordinates.asResourceClass(asArgsType = true)
val baseClassName = Type.Name(baseClassCoordinates.className).syntax
val argsClassName = Type.Name(argsClassCoordinates.className).syntax
val factoryMethodName = Term.Name(decapitalize(baseClassCoordinates.className)).syntax

val baseFileImports = {
val conditionalIdentifiers =
Expand Down Expand Up @@ -396,33 +395,33 @@ class CodeGen(implicit providerConfig: Config.ProviderConfig, typeMapper: TypeMa
|${baseClassParams.map(param => s" ${param}").mkString(",\n")}
|) extends ${resourceBaseClass} derives ResourceDecoder""".stripMargin

val hasDefaultArgsConstructor = resourceDefinition.requiredInputs.forall { propertyName =>
val propertyDefinition = resourceDefinition.inputProperties(propertyName)
propertyDefinition.default.nonEmpty || propertyDefinition.const.nonEmpty
}

val argsDefault = if (hasDefaultArgsConstructor) s""" = ${argsClassName}()""" else ""

// the type has to match pulumi's resource type schema, ie kubernetes:core/v1:Pod
val typ = Lit.String(typeToken)

val baseCompanion =
if (hasOutputExtensions) {
s"""|object $baseClassName:
| def apply(using ctx: Context)(
| name: NonEmptyString,
| args: ${argsClassName}${argsDefault},
| opts: CustomResourceOptions = CustomResourceOptions()
| ): Output[$baseClassName] =
| ctx.registerResource[$baseClassName, $argsClassName](${typ}, name, args, opts)
|
| given outputOps: {} with
| extension(output: Output[$baseClassName])
|${baseOutputExtensionMethods.map(meth => s" ${meth}").mkString("\n")}""".stripMargin
} else {
s"""object $baseClassName"""
}

// the type has to match pulumi's resource type schema, ie kubernetes:core/v1:Pod
val typ = Lit.String(typeToken)

val hasDefaultArgsConstructor = resourceDefinition.requiredInputs.forall { propertyName =>
val propertyDefinition = resourceDefinition.inputProperties(propertyName)
propertyDefinition.default.nonEmpty || propertyDefinition.const.nonEmpty
}
val argsDefault = if (hasDefaultArgsConstructor) s""" = ${argsClassName}()""" else ""
val factoryMethod =
s"""|def $factoryMethodName(using ctx: Context)(
| name: NonEmptyString,
| args: ${argsClassName}${argsDefault},
| opts: CustomResourceOptions = CustomResourceOptions()
|): Output[$baseClassName] =
| ctx.registerResource[$baseClassName, $argsClassName](${typ}, name, args, opts)
|""".stripMargin

val argsClass = makeArgsClass(argsClassName = argsClassName, inputProperties = inputProperties, isResource = true, isProvider = isProvider)

val argsCompanion = makeArgsCompanion(argsClassName = argsClassName, inputProperties = inputProperties, isResource = true)
Expand All @@ -436,8 +435,6 @@ class CodeGen(implicit providerConfig: Config.ProviderConfig, typeMapper: TypeMa
|${baseClass}
|
|${baseCompanion}
|
|${factoryMethod}
|""".stripMargin

val argsClassFileContent =
Expand Down
29 changes: 14 additions & 15 deletions experimental/src/main/scala/besom/liftoff.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
//> using dep "org.virtuslab::besom-kubernetes:0.0.1-SNAPSHOT"
//> using dep "io.github.iltotore::iron:2.1.0"
//> using dep "io.github.iltotore::iron:2.2.1"

import besom.*
import besom.api.{kubernetes => k8s}

import k8s.core.v1.inputs.*
import k8s.apps.v1.inputs.*
import k8s.meta.v1.inputs.*
import k8s.apps.v1.{deployment, DeploymentArgs, statefulSet, StatefulSetArgs}
import k8s.apps.v1.{Deployment, DeploymentArgs, StatefulSet, StatefulSetArgs}
import k8s.core.v1.{
configMap,
ConfigMap,
ConfigMapArgs,
namespace,
service,
Namespace,
Service,
ServiceArgs,
persistentVolume,
PersistentVolume,
PersistentVolumeArgs
}
Expand All @@ -27,12 +26,12 @@ case class Redis(connectionString: Output[String])(using ComponentBase) extends

def redisCluster(name: NonEmptyString, nodes: Int :| Positive)(using Context): Output[Redis] =
component(name, "besom:liftoff:Redis") {
val redisNamespace = namespace(s"redis-cluster-namespace-$name")
val redisNamespace = Namespace(s"redis-cluster-namespace-$name")

val labels = Map("app" -> name)

def createHostPathVolume(num: Int): Output[PersistentVolume] =
persistentVolume(
PersistentVolume(
s"redis-cluster-pv-$num-$name",
PersistentVolumeArgs(
metadata = ObjectMetaArgs(
Expand All @@ -49,7 +48,7 @@ def redisCluster(name: NonEmptyString, nodes: Int :| Positive)(using Context): O
)
)

val redisConfigMap = configMap(
val redisConfigMap = ConfigMap(
s"redis-cluster-configmap-$name",
ConfigMapArgs(
metadata = ObjectMetaArgs(
Expand All @@ -69,7 +68,7 @@ def redisCluster(name: NonEmptyString, nodes: Int :| Positive)(using Context): O
)
)

val redisStatefulSet = statefulSet(
val redisStatefulSet = StatefulSet(
s"redis-cluster-statefulset-$name",
StatefulSetArgs(
metadata = ObjectMetaArgs(
Expand Down Expand Up @@ -156,7 +155,7 @@ def redisCluster(name: NonEmptyString, nodes: Int :| Positive)(using Context): O
)
)

val redisService = service(
val redisService = Service(
s"redis-cluster-service-$name",
ServiceArgs(
metadata = ObjectMetaArgs(
Expand Down Expand Up @@ -188,12 +187,12 @@ def redisCluster(name: NonEmptyString, nodes: Int :| Positive)(using Context): O

@main def main = Pulumi.run {
val labels = Map("app" -> "nginx")
val appNamespace: Output[k8s.core.v1.Namespace] = namespace("liftoff")
val appNamespace: Output[k8s.core.v1.Namespace] = Namespace("liftoff")

val html =
"<h1>Welcome to Besom: Functional Infrastructure in Scala 3</h1>"

val indexHtmlConfigMap: Output[k8s.core.v1.ConfigMap] = configMap(
val indexHtmlConfigMap: Output[k8s.core.v1.ConfigMap] = ConfigMap(
"index-html-configmap",
ConfigMapArgs(
metadata = ObjectMetaArgs(
Expand All @@ -207,7 +206,7 @@ def redisCluster(name: NonEmptyString, nodes: Int :| Positive)(using Context): O
)
)

val nginxDeployment = deployment(
val nginxDeployment = Deployment(
"nginx",
DeploymentArgs(
spec = DeploymentSpecArgs(
Expand Down Expand Up @@ -251,7 +250,7 @@ def redisCluster(name: NonEmptyString, nodes: Int :| Positive)(using Context): O
)
)

val nginxService = service(
val nginxService = Service(
"nginx",
ServiceArgs(
spec = ServiceSpecArgs(
Expand Down
4 changes: 2 additions & 2 deletions template/default/Main.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import besom.*
import besom.api.random.{randomPet}
import besom.api.random.{RandomPet}
@main def main = Pulumi.run {
for
randomPet <- randomPet("randomPetServer")
randomPet <- RandomPet("randomPetServer")
name <- randomPet.id
yield Pulumi.exports(
name = name
Expand Down
4 changes: 2 additions & 2 deletions template/kubernetes/Main.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import besom.*
import besom.api.kubernetes.apps.v1.{deployment, DeploymentArgs}
import besom.api.kubernetes.apps.v1.{Deployment, DeploymentArgs}
import besom.api.kubernetes.core.v1.inputs.{ContainerArgs, ContainerPortArgs, PodSpecArgs, PodTemplateSpecArgs}
import besom.api.kubernetes.meta.v1.inputs.{LabelSelectorArgs, ObjectMetaArgs}
import besom.api.kubernetes.apps.v1.inputs.DeploymentSpecArgs

@main def main = Pulumi.run {
val appLabels = Map("app" -> "nginx")
for nginxDeployment <- deployment(
for nginxDeployment <- Deployment(
"nginx",
DeploymentArgs(
spec = DeploymentSpecArgs(
Expand Down