Skip to content

Commit

Permalink
Handle empty config YAML and PCL conditions explicitly and show tests…
Browse files Browse the repository at this point in the history
… for all possible combinations
  • Loading branch information
guineveresaenger committed Jan 10, 2025
1 parent c1ee08d commit 5fb65af
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 7 deletions.
21 changes: 15 additions & 6 deletions pkg/tfgen/installation_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,6 @@ func translateCodeBlocks(contentStr string, g *Generator) (string, error) {

// This function renders the Pulumi.yaml config file for a given language if configuration is included in the example.
func processConfigYaml(pulumiYAML, lang string) string {
if pulumiYAML == "" {
return pulumiYAML
}
// Replace the project name from the default `/` to a more descriptive name
nameRegex := regexp.MustCompile(`name: /*`)
pulumiYAMLFile := nameRegex.ReplaceAllString(pulumiYAML, "name: configuration-example")
Expand Down Expand Up @@ -259,10 +256,19 @@ func convertExample(g *Generator, code string, exampleNumber int) (string, error
return "", err
}

if pclExample.PCL == "" {
// If both PCL and PulumiYAML fields are empty, we can return.
if pclExample.PulumiYAML == "" && pclExample.PCL == "" {
return "", nil
}

// If we have a valid provider config but no additional code, we only render a YAML configuration block
// with no choosers and an empty language runtime field
if pclExample.PulumiYAML != "" && pclExample.PCL == "" {
if pclExample.PCL == "" {
return processConfigYaml(pclExample.PulumiYAML, ""), nil
}
}

langs := genLanguageToSlice(g.language)
const (
chooserStart = `{{< chooser language "typescript,python,go,csharp,java,yaml" >}}` + "\n"
Expand All @@ -277,8 +283,11 @@ func convertExample(g *Generator, code string, exampleNumber int) (string, error
choosableStart := fmt.Sprintf("{{%% choosable language %s %%}}\n", lang)

// Generate the Pulumi.yaml config file for each language
configFile := pclExample.PulumiYAML
pulumiYAML := processConfigYaml(configFile, lang)
var pulumiYAML string
if pclExample.PulumiYAML != "" {
pulumiYAML = processConfigYaml(pclExample.PulumiYAML, lang)
}

// Generate language example
convertedLang, err := converter.singleExampleFromPCLToLanguage(pclExample, lang)
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions pkg/tfgen/installation_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,32 @@ func TestTranslateCodeBlocks(t *testing.T) {
language: RegistryDocs,
},
},
{
name: "Translates standalone provider config into Pulumi config YAML",
contentStr: readfile(t, "test_data/installation-docs/provider-config-only.md"),
expected: readfile(t, "test_data/installation-docs/provider-config-only-expected.md"),
g: &Generator{
sink: mockSink{},
cliConverterState: &cliConverter{
info: p,
pcls: pclsMap,
},
language: RegistryDocs,
},
},
{
name: "Translates standalone example into languages",
contentStr: readfile(t, "test_data/installation-docs/example-only.md"),
expected: readfile(t, "test_data/installation-docs/example-only-expected.md"),
g: &Generator{
sink: mockSink{},
cliConverterState: &cliConverter{
info: p,
pcls: pclsMap,
},
language: RegistryDocs,
},
},
}

for _, tt := range testCases {
Expand Down
132 changes: 132 additions & 0 deletions pkg/tfgen/test_data/installation-docs/example-only-expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
This example is invalid and should not be translated for this test to pass

## Example Usage

{{< chooser language "typescript,python,go,csharp,java,yaml" >}}
{{% choosable language typescript %}}
```typescript
import * as pulumi from "@pulumi/pulumi";
import * as simple from "@pulumi/simple";

//# Define a resource
const aResource = new simple.index.Resource("a_resource", {
renamedInput1: "hello",
inputTwo: true,
});
export const someOutput = aResource.result;
```
{{% /choosable %}}
{{% choosable language python %}}
```python
import pulumi
import pulumi_simple as simple

## Define a resource
a_resource = simple.index.Resource("a_resource",
renamed_input1=hello,
input_two=True)
pulumi.export("someOutput", a_resource["result"])
```
{{% /choosable %}}
{{% choosable language csharp %}}
```csharp
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Simple = Pulumi.Simple;

return await Deployment.RunAsync(() =>
{
//# Define a resource
var aResource = new Simple.Index.Resource("a_resource", new()
{
RenamedInput1 = "hello",
InputTwo = true,
});

return new Dictionary<string, object?>
{
["someOutput"] = aResource.Result,
};
});

```
{{% /choosable %}}
{{% choosable language go %}}
```go
package main

import (
"github.com/pulumi/pulumi-simple/sdk/go/simple"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
// # Define a resource
aResource, err := simple.NewResource(ctx, "a_resource", &simple.ResourceArgs{
RenamedInput1: "hello",
InputTwo: true,
})
if err != nil {
return err
}
ctx.Export("someOutput", aResource.Result)
return nil
})
}
```
{{% /choosable %}}
{{% choosable language yaml %}}
```yaml
resources:
## Define a resource
aResource:
type: simple:resource
name: a_resource
properties:
renamedInput1: hello
inputTwo: true
outputs:
someOutput: ${aResource.result}
```
{{% /choosable %}}
{{% choosable language java %}}
```java
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.simple.resource;
import com.pulumi.simple.ResourceArgs;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}

public static void stack(Context ctx) {
//# Define a resource
var aResource = new Resource("aResource", ResourceArgs.builder()
.renamedInput1("hello")
.inputTwo(true)
.build());

ctx.export("someOutput", aResource.result());
}
}
```
{{% /choosable %}}
{{< /chooser >}}


## Configuration Reference

The following configuration inputs are supported:
19 changes: 19 additions & 0 deletions pkg/tfgen/test_data/installation-docs/example-only.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
This example is invalid and should not be translated for this test to pass

## Example Usage

```hcl
## Define a resource
resource "simple_resource" "a_resource" {
input_one = "hello"
input_two = true
}
output "some_output" {
value = simple_resource.a_resource.result
}
```

## Configuration Reference

The following configuration inputs are supported:
2 changes: 1 addition & 1 deletion pkg/tfgen/test_data/installation-docs/invalid-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This example is invalid and should not be translated for this test to pass

```hcl
# Configure the OpenStack Provider
proider "simple-provider" {
misspelledprovider "simple-provider" {
user_name = "admin"
tenant_name = "admin"
password = "pwd"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
This example should translate at least the Pulumi config

## Example Usage

```yaml
# Pulumi.yaml provider configuration file
name: configuration-example
runtime:
config:
simple-provider:authUrl:
value: http://myauthurl:5000/v3
simple-provider:password:
value: pwd
simple-provider:region:
value: RegionOne
simple-provider:tenantName:
value: admin
simple-provider:userName:
value: admin

```


## Configuration Reference

The following configuration inputs are supported:
18 changes: 18 additions & 0 deletions pkg/tfgen/test_data/installation-docs/provider-config-only.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
This example should translate at least the Pulumi config

## Example Usage

```hcl
# Configure the OpenStack Provider
provider "simple-provider" {
user_name = "admin"
tenant_name = "admin"
password = "pwd"
auth_url = "http://myauthurl:5000/v3"
region = "RegionOne"
}
```

## Configuration Reference

The following configuration inputs are supported:

0 comments on commit 5fb65af

Please sign in to comment.