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

Add package aliasing in codegen #342

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
38 changes: 17 additions & 21 deletions codegen/src/CodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class CodeGen(implicit
): Seq[SourceFile] = {
val (configFiles, configDependencies) = sourceFilesForConfig(pulumiPackage, packageInfo)
configFiles ++
sourceFilesForProviderResource(pulumiPackage) ++
sourceFilesForNonResourceTypes(pulumiPackage, configDependencies) ++
sourceFilesForResources(pulumiPackage) ++
sourceFilesForFunctions(pulumiPackage)
sourceFilesForProviderResource(pulumiPackage, packageInfo) ++
sourceFilesForNonResourceTypes(pulumiPackage, packageInfo, configDependencies) ++
sourceFilesForResources(pulumiPackage, packageInfo) ++
sourceFilesForFunctions(pulumiPackage, packageInfo)
}

def projectConfigFile(schemaName: String, packageVersion: PackageVersion): SourceFile = {
Expand Down Expand Up @@ -108,23 +108,19 @@ class CodeGen(implicit
SourceFile(filePath = filePath, sourceCode = fileContent)
}

def sourceFilesForProviderResource(pulumiPackage: PulumiPackage): Seq[SourceFile] = {
val typeToken = pulumiPackage.providerTypeToken
val moduleToPackageParts = pulumiPackage.moduleToPackageParts
val providerToPackageParts = pulumiPackage.providerToPackageParts
val typeCoordinates =
PulumiDefinitionCoordinates.fromRawToken(typeToken, moduleToPackageParts, providerToPackageParts)
def sourceFilesForProviderResource(pulumiPackage: PulumiPackage, packageInfo: PulumiPackageInfo): Seq[SourceFile] = {
val typeCoordinates = PulumiToken(pulumiPackage.providerTypeToken).toCoordinates(packageInfo)

sourceFilesForResource(
typeCoordinates = typeCoordinates,
resourceDefinition = pulumiPackage.provider,
methods = pulumiPackage.parsedMethods(pulumiPackage.provider),
methods = pulumiPackage.parsedMethods(packageInfo)(pulumiPackage.provider),
isProvider = true
)
}

def sourceFilesForNonResourceTypes(pulumiPackage: PulumiPackage, configDependencies: Seq[ConfigDependency]): Seq[SourceFile] = {
pulumiPackage.parsedTypes.flatMap { case (coordinates, typeDefinition) =>
def sourceFilesForNonResourceTypes(pulumiPackage: PulumiPackage, packageInfo: PulumiPackageInfo, configDependencies: Seq[ConfigDependency]): Seq[SourceFile] = {
pulumiPackage.parsedTypes(packageInfo).flatMap { case (coordinates, typeDefinition) =>
typeDefinition match {
case enumDef: EnumTypeDefinition =>
sourceFilesForEnum(
Expand Down Expand Up @@ -255,13 +251,13 @@ class CodeGen(implicit
Seq(baseClassSourceFile, argsClassSourceFile)
}

def sourceFilesForResources(pulumiPackage: PulumiPackage): Seq[SourceFile] = {
pulumiPackage.parsedResources
def sourceFilesForResources(pulumiPackage: PulumiPackage, packageInfo: PulumiPackageInfo): Seq[SourceFile] = {
pulumiPackage.parsedResources(packageInfo)
.map { case (coordinates, resourceDefinition) =>
sourceFilesForResource(
typeCoordinates = coordinates,
resourceDefinition = resourceDefinition,
methods = pulumiPackage.parsedMethods(resourceDefinition),
methods = pulumiPackage.parsedMethods(packageInfo)(resourceDefinition),
isProvider = false
)
}
Expand Down Expand Up @@ -494,8 +490,8 @@ class CodeGen(implicit
Seq(baseClassSourceFile, argsClassSourceFile) ++ methodFiles.flatten
}

def sourceFilesForFunctions(pulumiPackage: PulumiPackage)(implicit logger: Logger): Seq[SourceFile] = {
pulumiPackage.parsedFunctions
def sourceFilesForFunctions(pulumiPackage: PulumiPackage, packageInfo: PulumiPackageInfo)(implicit logger: Logger): Seq[SourceFile] = {
pulumiPackage.parsedFunctions(packageInfo)
.filterNot { case (_, f) => isMethod(f) }
.map { case (coordinates, functionDefinition) =>
sourceFilesForFunction(
Expand Down Expand Up @@ -614,7 +610,7 @@ class CodeGen(implicit
): (Seq[SourceFile], Seq[ConfigDependency]) = {
val PulumiToken(_, _, providerName) = PulumiToken(packageInfo.providerTypeToken)
val coordinates = PulumiToken(providerName, Utils.configModuleName, Utils.configTypeName)
.toCoordinates(pulumiPackage)
.toCoordinates(packageInfo)
.asConfigClass

val configsWithDefault = pulumiPackage.config.defaults
Expand Down Expand Up @@ -682,8 +678,8 @@ class CodeGen(implicit
val sources = Seq(SourceFile(file, code))
val dependencies: Seq[ConfigDependency] = configVariables.flatMap { case (_, configDefinition) =>
configDefinition.typeReference.asTokenAndDependency.flatMap {
case (Some(token), None) => ConfigSourceDependency(token.toCoordinates(pulumiPackage)) :: Nil
case (Some(token), Some(metadata)) => ConfigRuntimeDependency(token.toCoordinates(pulumiPackage), metadata) :: Nil
case (Some(token), None) => ConfigSourceDependency(token.toCoordinates(packageInfo)) :: Nil
case (Some(token), Some(metadata)) => ConfigRuntimeDependency(token.toCoordinates(packageInfo), metadata) :: Nil
case _ => Nil
}
}
Expand Down
11 changes: 7 additions & 4 deletions codegen/src/CodeGen.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,9 @@ class CodeGenTest extends munit.FunSuite {
name = "Type with dots in package segment",
json = """|{
| "name": "embedded-crd",
| "allowedPackageNames": [
| "kubernetes"
| ],
| "types": {
| "kubernetes:crd.k8s.amazonaws.com/v1alpha1:ENIConfig": {
| "properties": {
Expand Down Expand Up @@ -805,10 +808,10 @@ class CodeGenTest extends munit.FunSuite {
implicit val logger: Logger = new Logger(config.logLevel)

implicit val schemaProvider: SchemaProvider = new DownloadingSchemaProvider(schemaCacheDirPath = Config.DefaultSchemasDir)
val (pulumiPackage, packageInfo) = schemaProvider.packageInfo(
PackageMetadata(defaultTestSchemaName, "0.0.0"),
PulumiPackage.fromString(data.json)
)
val (pulumiPackage, packageInfo) = {
val p = PulumiPackage.fromString(data.json)
schemaProvider.packageInfo(PackageMetadata(p.name), p)
}
implicit val providerConfig: ProviderConfig = Config.providersConfigs(packageInfo.name)
implicit val tm: TypeMapper = new TypeMapper(packageInfo, schemaProvider)

Expand Down
6 changes: 4 additions & 2 deletions codegen/src/DefinitionCoordinates.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ class DefinitionCoordinatesTest extends munit.FunSuite {

tests.foreach(data => {
test(s"Type: ${data.token.asString}".withTags(data.tags.toSet)) {
val pulumiPackage = PulumiPackage("test")
val coords = data.token.toCoordinates(pulumiPackage)
val schemaProvider: SchemaProvider = new DownloadingSchemaProvider(schemaCacheDirPath = Config.DefaultSchemasDir)
val (_, packageInfo) = schemaProvider.packageInfo(PackageMetadata(data.token.provider), PulumiPackage(data.token.provider))

val coords = data.token.toCoordinates(packageInfo)

data.expected.foreach {
case ResourceClassExpectations(fullPackageName, fullyQualifiedTypeRef, filePath, asArgsType) =>
Expand Down
17 changes: 9 additions & 8 deletions codegen/src/PropertyInfo.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import scala.meta.dialects.Scala33

//noinspection ScalaFileName,TypeAnnotation
class PropertyInfoTest extends munit.FunSuite {
object TestPackageMetadata extends PackageMetadata("test-as-scala-type", Some(PackageVersion.default))

case class Data(
name: String,
Expand All @@ -17,7 +16,7 @@ class PropertyInfoTest extends munit.FunSuite {
expectedType: String,
expectedArgsType: Option[String] = None,
expectedInputArgsType: Option[String] = None,
metadata: PackageMetadata = TestPackageMetadata,
metadataOrName: PackageMetadata | SchemaName = "",
tags: Set[munit.Tag] = Set()
)

Expand All @@ -43,7 +42,8 @@ class PropertyInfoTest extends munit.FunSuite {
|}
|""".stripMargin,
expectedName = "routingRules",
expectedType = "String | scala.collection.immutable.List[String]"
expectedType = "String | scala.collection.immutable.List[String]",
metadataOrName = "aws"
),
Data(
name = "property with underlying object",
Expand All @@ -55,7 +55,8 @@ class PropertyInfoTest extends munit.FunSuite {
|}
|""".stripMargin,
expectedName = "spec",
expectedType = "scala.Predef.Map[String, String]"
expectedType = "scala.Predef.Map[String, String]",
metadataOrName = "kubernetes"
),
Data(
name = "property with a union with a resource",
Expand All @@ -77,7 +78,7 @@ class PropertyInfoTest extends munit.FunSuite {
|""".stripMargin,
expectedName = "restApi",
expectedType = "String | besom.api.aws.apigateway.RestApi",
metadata = PackageMetadata("aws", "6.7.0"),
metadataOrName = PackageMetadata("aws", "6.7.0"),
tags = Set(munit.Ignore) // FIXME: https://github.com/VirtusLab/besom/issues/144
),
Data(
Expand Down Expand Up @@ -116,9 +117,9 @@ class PropertyInfoTest extends munit.FunSuite {

implicit val schemaProvider: SchemaProvider =
new DownloadingSchemaProvider(schemaCacheDirPath = Config.DefaultSchemasDir)
val (_, packageInfo) = data.metadata match {
case m @ TestPackageMetadata => schemaProvider.packageInfo(m, PulumiPackage(name = m.name))
case _ => schemaProvider.packageInfo(data.metadata)
val (_, packageInfo) = data.metadataOrName match {
case m: PackageMetadata => schemaProvider.packageInfo(m) // download schema
case n: SchemaName => schemaProvider.packageInfo(PackageMetadata(n), PulumiPackage(name = n))
}
implicit val tm: TypeMapper = new TypeMapper(packageInfo, schemaProvider)

Expand Down
34 changes: 0 additions & 34 deletions codegen/src/PulumiDefinitionCoordinates.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,40 +83,6 @@ case class PulumiDefinitionCoordinates private (
}

object PulumiDefinitionCoordinates {
def fromRawToken(
typeToken: String,
moduleToPackageParts: String => Seq[String],
providerToPackageParts: String => Seq[String]
): PulumiDefinitionCoordinates = {
fromToken(
typeToken = PulumiToken(typeToken),
moduleToPackageParts = moduleToPackageParts,
providerToPackageParts = providerToPackageParts
)
}

def fromToken(
typeToken: PulumiToken,
moduleToPackageParts: String => Seq[String],
providerToPackageParts: String => Seq[String]
): PulumiDefinitionCoordinates = {
typeToken match {
case PulumiToken("pulumi", "providers", providerName) =>
PulumiDefinitionCoordinates(
token = typeToken,
providerPackageParts = providerName :: Nil,
modulePackageParts = Utils.indexModuleName :: Nil,
definitionName = Utils.providerTypeName
)
case PulumiToken(providerName, moduleName, definitionName) =>
PulumiDefinitionCoordinates(
token = typeToken,
providerPackageParts = providerToPackageParts(providerName),
modulePackageParts = moduleToPackageParts(moduleName),
definitionName = definitionName
)
}
}

private def inputsPackage = "inputs"
private def outputsPackage = "outputs"
Expand Down
8 changes: 6 additions & 2 deletions codegen/src/PulumiDefinitionCoordinates.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ class PulumiDefinitionCoordinatesTest extends munit.FunSuite {
tests.foreach { data =>
test(s"Type: ${data.typeToken}".withTags(data.tags)) {
implicit val providerConfig: ProviderConfig = Config.providersConfigs(data.schemaName)
val coords = PulumiToken(data.typeToken).toCoordinates(data.pulumiPackage)

val schemaProvider: SchemaProvider = new DownloadingSchemaProvider(schemaCacheDirPath = Config.DefaultSchemasDir)
val (_, packageInfo) = schemaProvider.packageInfo(PackageMetadata(data.schemaName), data.pulumiPackage)

val coords = PulumiToken(data.typeToken).toCoordinates(packageInfo)

data.expected.foreach {
case ResourceClassExpectations(fullPackageName, fullyQualifiedTypeRef, filePath, asArgsType) =>
Expand Down Expand Up @@ -259,7 +263,7 @@ class PulumiDefinitionCoordinatesTest extends munit.FunSuite {
"PascalCase" -> "PascalCase",
"lowercase" -> "Lowercase",
"UPPERCASE" -> "Uppercase",
"ListenerXForwardedForConfig" -> "ListenerxForwardedForConfig",
"ListenerXForwardedForConfig" -> "ListenerxForwardedForConfig"
)

for ((input, expected) <- testCases) {
Expand Down
4 changes: 3 additions & 1 deletion codegen/src/PulumiPackage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import upickle.implicits.key as fieldKey
* Name is the unqualified name of the package
* @param version
* Version is the version of the package
* @param allowedPackageNames
* A list of allowed package name in addition to the Name property. This is used to support legacy package names.
* @param meta
* Format metadata about this package
* @param pluginDownloadURL
Expand All @@ -39,7 +41,7 @@ import upickle.implicits.key as fieldKey
case class PulumiPackage(
name: String,
version: Option[String] = None,
allowedPackageNames: List[String] = List.empty, // TODO: Handle allowedPackageNames
allowedPackageNames: List[String] = List.empty,
meta: Meta = Meta(),
pluginDownloadURL: Option[String] = None,
types: Map[String, TypeDefinition] = Map.empty,
Expand Down
7 changes: 4 additions & 3 deletions codegen/src/PulumiPackageInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ type EnumInstanceName = String
case class PulumiPackageInfo(
name: SchemaName,
version: PackageVersion,
enumTypeTokens: Set[String],
objectTypeTokens: Set[String],
allowedPackageNames: Set[SchemaName],
providerTypeToken: String,
resourceTypeTokens: Set[String],
moduleToPackageParts: String => Seq[String],
providerToPackageParts: String => Seq[String],
enumTypeTokens: Set[String],
objectTypeTokens: Set[String],
resourceTypeTokens: Set[String],
enumValueToInstances: Map[PulumiToken, Map[ConstValue, EnumInstanceName]]
) {
def asPackageMetadata: PackageMetadata = PackageMetadata(name, Some(version))
Expand Down
47 changes: 33 additions & 14 deletions codegen/src/PulumiToken.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package besom.codegen

import besom.codegen.metaschema.PulumiPackage

import scala.util.matching.Regex

/** The parsed Pulumi type token used in Pulumi schema in a clean, canonical form, that enforces all three parts present
Expand All @@ -14,25 +12,46 @@ import scala.util.matching.Regex
* @param raw
* the raw Pulumi type token
*/
case class PulumiToken private (provider: String, module: String, name: String, raw: String) extends PulumiToken.RawToken {
case class PulumiToken private (provider: String, module: String, name: String, raw: String) extends PulumiToken.RawToken:
def asString: String = PulumiToken.concat(provider, module, name)

/** Transform the Pulumi type token to a Pulumi definition coordinates
* @param pulumiPackage
* the Pulumi package containing the schema information
* @param pulumiPackageInfo
* the Pulumi package schema information
* @return
* the Pulumi definition coordinates for the given Pulumi type token
* the Pulumi definition coordinates for the given Pulumi type token and Pulumi package schema information
*/
def toCoordinates(pulumiPackage: PulumiPackage): PulumiDefinitionCoordinates = {
import besom.codegen.Utils.PulumiPackageOps
@throws[TypeMapperError]("if the package name is not allowed in the context of the given Pulumi package")
def toCoordinates(pulumiPackageInfo: PulumiPackageInfo): PulumiDefinitionCoordinates =
this match
case t @ PulumiToken("pulumi", "providers", providerName) =>
validatePackageName(pulumiPackageInfo.name, pulumiPackageInfo.allowedPackageNames)(providerName)
PulumiDefinitionCoordinates(
token = t,
providerPackageParts = providerName :: Nil,
modulePackageParts = Utils.indexModuleName :: Nil,
definitionName = Utils.providerTypeName
)
case t @ PulumiToken(providerName, moduleName, definitionName) =>
validatePackageName(pulumiPackageInfo.name, pulumiPackageInfo.allowedPackageNames)(providerName)
PulumiDefinitionCoordinates(
token = t,
providerPackageParts = pulumiPackageInfo.providerToPackageParts(providerName),
modulePackageParts = pulumiPackageInfo.moduleToPackageParts(moduleName),
definitionName = definitionName
)
end toCoordinates

PulumiDefinitionCoordinates.fromToken(
typeToken = this,
moduleToPackageParts = pulumiPackage.moduleToPackageParts,
providerToPackageParts = pulumiPackage.providerToPackageParts
)
private def validatePackageName(packageName: String, allowedPackageNames: Set[String])(name: String): Unit = {
val allNames = allowedPackageNames + packageName
if (!allNames.contains(name)) {
throw TypeMapperError(
s"$this has invalid package name '$name', in context of package '${packageName}', " +
s"allowed names for the current package: ${allNames.mkString("'", "', '", "'")}"
)
}
}
}
end PulumiToken

object PulumiToken {
private val tokenFmtShort: String = "([^:]*):([^:]*)" // provider:name
Expand Down
Loading
Loading