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

Update examples #346

Merged
merged 6 commits into from
Jan 24, 2024
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
11 changes: 7 additions & 4 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ on:
env:
PULUMI_CONFIG_PASSPHRASE: 'passphrase'
PULUMI_SKIP_UPDATE_CHECK: 'true'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
preview:
if: "! github.event.pull_request.head.repo.fork"
runs-on: ubuntu-latest
timeout-minutes: 20
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: extractions/setup-just@v1
Expand All @@ -32,11 +33,13 @@ jobs:
with:
jvm: 11
power: true
- name: Prepare dependencies
run: |-
just publish-local-json publish-local-codegen publish-local-sdk install-language-plugin publish-local-compiler-plugin
just cli packages generate aws docker gcp kubernetes random tls
just cli packages publish-local aws docker gcp kubernetes random tls
- name: Login in to pulumi
run: pulumi login --local
- name: Install language plugin
run: |-
pulumi plugin install language scala --server github://api.github.com/VirtusLab/besom
- name: Test examples
env:
AWS_EC2_METADATA_DISABLED: true
Expand Down
10 changes: 7 additions & 3 deletions .github/workflows/templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ on:
env:
PULUMI_CONFIG_PASSPHRASE: 'passphrase'
PULUMI_SKIP_UPDATE_CHECK: 'true'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
preview:
if: "! github.event.pull_request.head.repo.fork"
runs-on: ubuntu-latest
timeout-minutes: 20
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: extractions/setup-just@v1
Expand All @@ -32,9 +33,12 @@ jobs:
with:
jvm: 11
power: true
- name: Prepare dependencies
run: |-
just publish-local-json publish-local-codegen publish-local-sdk install-language-plugin publish-local-compiler-plugin
just cli packages generate aws docker gcp kubernetes random tls
just cli packages publish-local aws docker gcp kubernetes random tls
- name: Login in to pulumi
run: pulumi login --local
- name: Install language plugin
run: pulumi plugin install language scala --server github://api.github.com/VirtusLab/besom
- name: Test templates
run: just test-templates
17 changes: 7 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,30 +120,27 @@ therefore might need to manipulate `PATH` to prefer the local version.

Publish locally and install necessary Besom packages:
```bash
just publish-local-json
just publish-local-core
just publish-local-codegen
just publish-local-compiler-plugin
just install-language-plugin
```

#### Publish additional SDKs

You have to generate an SDK for a provider of your choice, to do that run:
```bash
just generate-provider ${provider_name} ${provider_version}
just publish-local-provider ${provider_name} ${provider_version}
```

e.g.:
You have to generate an SDK for a provider of your choice, use `just cli`, e.g.:

```bash
just generate-provider kubernetes 4.2.0
just publish-local-provider kubernetes 4.2.0
export GITHUB_TOKEN=$(gh auth token)
just cli packages generate kubernetes
just cli packages publish-local kubernetes
```

### Working with published dependencies

Release builds of the Besom SDK are published to Maven Central
as `org.virtuslab::besom-core:x.x.x`.
as `org.virtuslab::besom-core:x.y.z` and `org.virtuslab::besom-[package]:a.b.c-core.x.y.z`.

### Adding examples and testing them locally

Expand Down
22 changes: 12 additions & 10 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ before-commit: compile-all test-all

# Compiles the protobufs for the language SDK
compile-pulumi-protobufs:
scala-cli run --suppress-experimental-feature-warning --suppress-directives-in-multiple-files-warning scripts/Proto.scala -- all
scala-cli run --suppress-experimental-feature-warning --suppress-directives-in-multiple-files-warning scripts -- proto all

# Compiles core besom SDK
compile-core: publish-local-json
Expand Down Expand Up @@ -263,19 +263,20 @@ test-codegen:
publish-local-codegen: test-codegen
scala-cli --power publish local codegen --project-version {{besom-version}} --suppress-experimental-feature-warning

export-github-token:
export GITHUB_TOKEN=$(gh auth token)

# Dowloads Pulumi Packages metadata for all providers in the registry
get-metadata-all:
export GITHUB_TOKEN=$(gh auth token); \
scala-cli run --suppress-experimental-feature-warning --suppress-directives-in-multiple-files-warning scripts/Packages.scala -- metadata-all
scala-cli run --suppress-experimental-feature-warning --suppress-directives-in-multiple-files-warning scripts -- packages metadata-all

# Generates Scala SDKs for all providers in the registry
generate-provider-all:
export GITHUB_TOKEN=$(gh auth token); \
scala-cli run --suppress-experimental-feature-warning --suppress-directives-in-multiple-files-warning scripts/Packages.scala -- generate-all
scala-cli run --suppress-experimental-feature-warning --suppress-directives-in-multiple-files-warning scripts -- packages generate-all

# Publishes locally Scala SDKs for all providers in the registry
publish-local-provider-all:
scala-cli run --suppress-experimental-feature-warning --suppress-directives-in-multiple-files-warning scripts/Packages.scala -- publish-local-all
scala-cli run --suppress-experimental-feature-warning --suppress-directives-in-multiple-files-warning scripts -- packages publish-local-all

# Download the schema for a specific provider, e.g. `just get-schema kubernetes 4.0.0`
get-schema schema-name schema-version:
Expand Down Expand Up @@ -341,7 +342,7 @@ clean-test-integration-codegen:

# Copies test schemas from pulumi repo to the testdata directory
copy-test-schemas:
scala-cli run --suppress-experimental-feature-warning --suppress-directives-in-multiple-files-warning scripts/Schemas.scala -- all
scala-cli run --suppress-experimental-feature-warning --suppress-directives-in-multiple-files-warning scripts -- schemas all

####################
# Templates and examples
Expand Down Expand Up @@ -417,9 +418,9 @@ compile-scripts: publish-local-codegen
clean-scripts:
scala-cli --power clean scripts

# Update Besom version
bump-version new-version:
scala-cli run scripts/Version.scala -- bump {{new-version}}
# Use Besom scripts directly
cli *ARGS:
scala-cli run scripts --suppress-experimental-feature-warning --suppress-directives-in-multiple-files-warning scripts -- {{ARGS}}

####################
# Troubleshooting
Expand Down Expand Up @@ -479,3 +480,4 @@ clean-slate-liftoff: clean-sdk
setup-intellij:
for file in `ls */project.scala | cut -f1 -d'/'`; do scala-cli setup-ide $file --suppress-experimental-feature-warning --suppress-directives-in-multiple-files-warning; done
for file in `ls */*/project.scala | cut -f1,2 -d'/'`; do scala-cli setup-ide $file --suppress-experimental-feature-warning --suppress-directives-in-multiple-files-warning; done
for file in `ls */*/*/project.scala | cut -f1,2,3 -d'/'`; do scala-cli setup-ide $file --suppress-experimental-feature-warning --suppress-directives-in-multiple-files-warning; done
39 changes: 27 additions & 12 deletions codegen/src/PulumiDefinitionCoordinates.scala
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,33 @@ object PulumiDefinitionCoordinates {
decapitalized
}

private[codegen] def normalize(input: String): String =
val head = input.head.toUpper
val it = input.tail.iterator.buffered
val tail =
for c <- it
yield
if it.headOption.map(_.isUpper).getOrElse(true)
then c.toLower
else c

(head +: tail.toArray).mkString
end normalize
private[codegen] def words(input: String): List[String] =
import scala.collection.mutable
val words = mutable.ArrayBuffer.empty[mutable.ArrayBuffer[Char]]
for (c, i) <- input.zipWithIndex do
def isWithinBounds(j: Int) = j >= 0 && j < input.length
val prev = if isWithinBounds(i - 1) then Some(input(i - 1)) else None
val next = if isWithinBounds(i + 1) then Some(input(i + 1)) else None

(prev.map(_.isUpper), c.isUpper, next.map(_.isUpper)) match
// word boundary - exclusive
case _ if c.isWhitespace || !c.isLetterOrDigit /* special character */ =>
words += mutable.ArrayBuffer.empty // new word
// word boundary - inclusive
case (None, _, _) /* first letter */
| (Some(false), true, Some(false)) /* down-up-down */
| (Some(true), true, Some(false)) /* up-up-down */
| (Some(false), true, Some(true)) /* down-up-up */
| (Some(false), true, None) /* down-up-nil */ =>
words += mutable.ArrayBuffer.empty // new word
words(words.length - 1) += c // append to the new word
// word continuation
case _ => words(words.length - 1) += c // append to the current word
end for
words.filter(_.nonEmpty).map(_.mkString).toList
end words

private[codegen] def normalize(input: String): String = words(input).map(_.toLowerCase.capitalize).mkString

@throws[PulumiDefinitionCoordinatesError]("if 'definitionName' is empty")
def apply(
Expand Down
41 changes: 39 additions & 2 deletions codegen/src/PulumiDefinitionCoordinates.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import scala.meta.dialects.Scala33

//noinspection ScalaFileName,TypeAnnotation
class PulumiDefinitionCoordinatesTest extends munit.FunSuite {

import besom.codegen.metaschema.{Java, Language, Meta, PulumiPackage}

implicit val logger: Logger = new Logger
Expand Down Expand Up @@ -57,12 +58,14 @@ class PulumiDefinitionCoordinatesTest extends munit.FunSuite {
args: FunctionClassExpectations.FunctionClassExpectationsArgs,
result: FunctionClassExpectations.FunctionClassExpectationsResult
) extends Expectations

object FunctionClassExpectations {
case class FunctionClassExpectationsArgs(
fullPackageName: String,
fullyQualifiedTypeRef: String,
filePath: String
)

case class FunctionClassExpectationsResult(
fullPackageName: String,
fullyQualifiedTypeRef: String,
Expand Down Expand Up @@ -214,7 +217,7 @@ 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 coords = PulumiToken(data.typeToken).toCoordinates(data.pulumiPackage)

data.expected.foreach {
case ResourceClassExpectations(fullPackageName, fullyQualifiedTypeRef, filePath, asArgsType) =>
Expand Down Expand Up @@ -248,18 +251,52 @@ class PulumiDefinitionCoordinatesTest extends munit.FunSuite {
}
}
}
}

class PulumiDefinitionCoordinatesPrivateTest extends munit.FunSuite {

test("split string to words") {
val testCases = Map(
"" -> List(),
" " -> List(),
"A" -> List("A"),
"AbC" -> List("Ab", "C"),
"ABc" -> List("A", "Bc"),
"ID" -> List("ID"),
"URL" -> List("URL"),
"JSON" -> List("JSON"),
"JSONPatch" -> List("JSON", "Patch"),
"URLParser" -> List("URL", "Parser"),
"camelCase" -> List("camel", "Case"),
"PascalCase" -> List("Pascal", "Case"),
"lowercase" -> List("lowercase"),
"UPPERCASE" -> List("UPPERCASE"),
"ListenerXForwardedForConfig" -> List("Listener", "X", "Forwarded", "For", "Config"),
"BucketIAMMember" -> List("Bucket", "IAM", "Member"),
"Json2Parser" -> List("Json2", "Parser"),
"My-Friend.Bob" -> List("My", "Friend", "Bob"),
"a-glad-dayTime" -> List("a", "glad", "day", "Time")
)

for ((input, expected) <- testCases) {
assertEquals(PulumiDefinitionCoordinates.words(input), expected)
}
}

test("normalize strings correctly") {
val testCases = Map(
"A" -> "A",
"ID" -> "Id",
"URL" -> "Url",
"JSON" -> "Json",
"JSONPatch" -> "JsonPatch",
"URLParser" -> "UrlParser",
"camelCase" -> "CamelCase",
"PascalCase" -> "PascalCase",
"lowercase" -> "Lowercase",
"UPPERCASE" -> "Uppercase",
"ListenerXForwardedForConfig" -> "ListenerxForwardedForConfig",
"ListenerXForwardedForConfig" -> "ListenerXForwardedForConfig",
"BucketIAMMember" -> "BucketIamMember"
)

for ((input, expected) <- testCases) {
Expand Down
6 changes: 3 additions & 3 deletions examples/aws-s3-folder/project.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//> using scala "3.3.1"
//> using plugin "org.virtuslab::besom-compiler-plugin:0.1.0"
//> using dep "org.virtuslab::besom-core:0.1.0"
//> using dep "org.virtuslab::besom-aws:6.2.1-core.0.1.0"
//> using plugin "org.virtuslab::besom-compiler-plugin:0.1.1-SNAPSHOT"
//> using dep "org.virtuslab::besom-core:0.1.1-SNAPSHOT"
//> using dep "org.virtuslab::besom-aws:6.18.1-core.0.1.1-SNAPSHOT"
//> using options -Werror -Wunused:all -Wvalue-discard -Wnonunit-statement
2 changes: 1 addition & 1 deletion examples/aws-secrets-manager/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import besom.api.aws.secretsmanager.SecretVersionArgs

@main def main = Pulumi.run {
// Get the Pulumi secret value
val mySecret = config.getSecret("aws-secrets-manager:mySecret")
val mySecret = config.requireString("aws-secrets-manager:mySecret")

// Create an AWS secret
val secret = aws.secretsmanager.Secret("mySecret")
Expand Down
6 changes: 3 additions & 3 deletions examples/aws-secrets-manager/project.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//> using scala "3.3.1"
//> using plugin "org.virtuslab::besom-compiler-plugin:0.1.0"
//> using dep "org.virtuslab::besom-core:0.1.0"
//> using dep "org.virtuslab::besom-aws:6.2.1-core.0.1.0"
//> using plugin "org.virtuslab::besom-compiler-plugin:0.1.1-SNAPSHOT"
//> using dep "org.virtuslab::besom-core:0.1.1-SNAPSHOT"
//> using dep "org.virtuslab::besom-aws:6.18.1-core.0.1.1-SNAPSHOT"
//> using options -Werror -Wunused:all -Wvalue-discard -Wnonunit-statement
7 changes: 2 additions & 5 deletions examples/aws-webserver/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ import besom.api.tls
"web-server-www",
ec2.InstanceArgs(
ami = ami.id,
instanceType = ec2.enums.InstanceType.T2_Micro.value, // t2.micro is available in the AWS free tier
instanceType = ec2.enums.InstanceType.T2_Micro, // t2.micro is available in the AWS free tier
vpcSecurityGroupIds = List(securityGroup.id), // reference the group object above
keyName = keyPair.keyName,
userData = userData,
Expand Down Expand Up @@ -126,8 +126,5 @@ def getExternalIp(using Context): Output[String] = {
val source = Source.fromURL("https://checkip.amazonaws.com")
Using(source) { response => response.mkString.trim } match
case scala.util.Success(ip) => Output(ip)
case scala.util.Failure(e) => OutputThrow(new Exception("Failed to get external IP address", e))
case scala.util.Failure(e) => Output.fail(Exception("Failed to get external IP address", e))
}

def OutputThrow[A](ex: Exception)(using Context): Output[A] =
Output(null).map(_ => throw ex)
4 changes: 4 additions & 0 deletions examples/aws-webserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ These values are indicated with `***`.
```
Hello, World!
```

```bash
open http://$(pulumi stack output publicHostName)
```

6. From there, feel free to experiment. Simply making edits and running pulumi up will incrementally update your infrastructure.

Expand Down
6 changes: 3 additions & 3 deletions examples/aws-webserver/project.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//> using scala 3.3.1
//> using options -Werror -Wunused:all -Wvalue-discard -Wnonunit-statement
//> using plugin org.virtuslab::besom-compiler-plugin:0.1.0
//> using plugin org.virtuslab::besom-compiler-plugin:0.1.1-SNAPSHOT
//> using dep org.virtuslab::besom-core:0.1.1-SNAPSHOT
//> using dep org.virtuslab::besom-aws:6.12.0-core.0.1.1-SNAPSHOT
//> using dep org.virtuslab::besom-tls:4.11.0-core.0.1.1-SNAPSHOT
//> using dep org.virtuslab::besom-aws:6.18.1-core.0.1.1-SNAPSHOT
//> using dep org.virtuslab::besom-tls:5.0.0-core.0.1.1-SNAPSHOT
2 changes: 1 addition & 1 deletion examples/docker-multi-container-app/app/project.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//> using scala 3.3.1
//> using options -java-output-version:17 -Werror -Wunused:all -Wvalue-discard -Wnonunit-statement

//> using dep com.softwaremill.sttp.tapir::tapir-jdkhttp-server:1.9.3
//> using dep com.softwaremill.sttp.tapir::tapir-jdkhttp-server:1.9.6
//> using dep redis.clients:jedis:5.1.0

//> using publish.organization org.virtuslab.besom.example
Expand Down
14 changes: 1 addition & 13 deletions examples/docker-multi-container-app/infra/Main.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import besom.*
import besom.api.docker
import docker.inputs._
import docker.inputs.*

@main def main = Pulumi.run {
// Set defaults for redis
Expand Down Expand Up @@ -44,18 +44,6 @@ import docker.inputs._
)
)

// Build image from local app directory
/* val appImage = docker.Image(
"appImage",
docker.ImageArgs(
build = DockerBuildArgs(
context = "../app"
),
imageName = "app",
skipPush = true
)
)*/

// Set external port for app url
val appPort = 3000

Expand Down
Loading
Loading