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

Validate Name, Version and Enviroment #7896

Merged
merged 16 commits into from
Sep 17, 2021
Merged
3 changes: 3 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@

### Bug Fixes

- [codegen/schema] - Correct validation for Package
[#7896](https://github.com/pulumi/pulumi/pull/7896)

- [cli] Use json.Unmarshal instead of custom parser
[#7954](https://github.com/pulumi/pulumi/pull/7954)
4 changes: 4 additions & 0 deletions pkg/cmd/pulumi/schema_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"

"github.com/hashicorp/hcl/v2"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -71,6 +72,9 @@ func newSchemaCheckCommand() *cobra.Command {
diagWriter := hcl.NewDiagnosticTextWriter(os.Stderr, nil, 0, true)
wrErr := diagWriter.WriteDiagnostics(diags)
contract.IgnoreError(wrErr)
if err == nil && len(diags) != 0 {
return errors.New("schema validation failed")
Copy link
Member

@pgavlin pgavlin Sep 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if err == nil && len(diags) != 0 {
return errors.New("schema validation failed")
if err == nil && diags.HasErrors() {
return errors.New("schema validation failed")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this change didn't get applied?

}
return err
}),
}
Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/docs/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func initTestPackageSpec(t *testing.T) {
}
testPackageSpec = schema.PackageSpec{
Name: providerPackage,
Version: "0.0.1",
Description: "A fake provider package used for testing.",
Meta: &schema.MetadataSpec{
ModuleFormat: "(.*)(?:/[^/]*)",
Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/dotnet/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

var testPackageSpec = schema.PackageSpec{
Name: "aws",
Version: "0.0.1",
Description: "A fake provider package used for testing.",
Meta: &schema.MetadataSpec{
ModuleFormat: "(.*)(?:/[^/]*)",
Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/go/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

var testPackageSpec = schema.PackageSpec{
Name: "aws",
Version: "0.0.1",
Description: "A fake provider package used for testing.",
Meta: &schema.MetadataSpec{
ModuleFormat: "(.*)(?:/[^/]*)",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "py_tests",
"version": "0.0.1",
"functions": {
"madeup-package:codegentest:funcWithAllOptionalInputs": {
"description": "Check codegen of functions with all optional inputs.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "py_tests",
"version": "0.0.1",
"functions": {
"madeup-package:codegentest:funcWithConstInput": {
"description": "Codegen demo with const inputs",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "py_tests",
"version": "0.0.1",
"functions": {
"madeup-package:codegentest:funcWithDefaultValue": {
"description": "Check codegen of functions with default values.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "py_tests",
"version": "0.0.1",
"functions": {
"madeup-package:codegentest:funcWithDictParam": {
"description": "Check codegen of functions with a Dict<str,str> parameter.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "py_tests",
"version": "0.0.1",
"functions": {
"madeup-package:codegentest:funcWithListParam": {
"description": "Check codegen of functions with a List parameter.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "py_tests",
"version": "0.0.1",
"functions": {
"azure-native:codegentest:getClientConfig": {
"description": "Use this function to access the current configuration of the native Azure provider.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "py_tests",
"version": "0.0.1",
"functions": {
"azure-native:codegentest:getIntegrationRuntimeObjectMetadatum": {
"description": "A list of SSIS object metadata.\nAPI Version: 2018-06-01.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "py_tests",
"version": "0.0.1",
"functions": {
"azure-native:codegentest:listStorageAccountKeys": {
"description": "The response from the ListKeys operation.\nAPI Version: 2021-02-01.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ menu:
<dd><a href=""></a></dd>
<dt>License</dt>
<dd></dd>
<dt>Version</dt>
<dd>0.0.1</dd>
</dl>

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "xyz",
"version": "0.0.1",
"types": {
"example:index:Foo": {
"properties": {
Expand Down
2 changes: 2 additions & 0 deletions pkg/codegen/internal/test/testdata/schema/bad-enum-1.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "bad-enum-1",
"version": "0.0.1",
"types": {
"fake-provider:module1:BadEnum": {
"type": "string",
Expand Down
2 changes: 2 additions & 0 deletions pkg/codegen/internal/test/testdata/schema/bad-enum-2.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "bad-enum-2",
"version": "0.0.1",
"types": {
"fake-provider:module1:BadEnum": {
"type": "string",
Expand Down
2 changes: 2 additions & 0 deletions pkg/codegen/internal/test/testdata/schema/bad-enum-3.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "bad-enum-3",
"version": "0.0.1",
"types": {
"fake-provider:module1:BadEnum": {
"type": "integer",
Expand Down
2 changes: 2 additions & 0 deletions pkg/codegen/internal/test/testdata/schema/bad-enum-4.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "bad-enum-4",
"version": "0.0.1",
"types": {
"fake-provider:module1:BadEnum": {
"type": "boolean",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "bad-methods-1",
"version": "0.0.1",
"resources": {
"xyz:index:Foo": {
"methods": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "bad-methods-2",
"version": "0.0.1",
"resources": {
"xyz:index:Foo": {
"methods": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "bad-methods-3",
"version": "0.0.1",
"resources": {
"xyz:index:Foo": {
"methods": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "bad-methods-4",
"version": "0.0.1",
"resources": {
"xyz:index:Foo": {
"methods": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "bad-methods-5",
"version": "0.0.1",
"resources": {
"xyz:index:Foo": {
"methods": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "bad-methods-6",
"version": "0.0.1",
"resources": {
"xyz:index:Foo": {
"methods": {
Expand Down
2 changes: 2 additions & 0 deletions pkg/codegen/internal/test/testdata/schema/good-enum-1.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "good-enum-1",
"version": "0.0.1",
"types": {
"fake-provider:module1:Color": {
"type": "string",
Expand Down
2 changes: 2 additions & 0 deletions pkg/codegen/internal/test/testdata/schema/good-enum-2.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "good-enum-2",
"version": "0.0.1",
"types": {
"fake-provider:module1:Number": {
"type": "integer",
Expand Down
2 changes: 2 additions & 0 deletions pkg/codegen/internal/test/testdata/schema/good-enum-3.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "good-enum-3",
"version": "0.0.1",
"types": {
"fake-provider:module1:Boolean": {
"type": "boolean",
Expand Down
2 changes: 2 additions & 0 deletions pkg/codegen/internal/test/testdata/schema/good-enum-4.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "good-enum-4",
"version": "0.0.1",
"types": {
"fake-provider:module1:Number2": {
"type": "number",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "good-methods-1",
"version": "0.0.1",
"resources": {
"xyz:index:Foo": {
"methods": {
Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/nodejs/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

var testPackageSpec = schema.PackageSpec{
Name: "aws",
Version: "0.0.1",
Description: "A fake provider package used for testing.",
Meta: &schema.MetadataSpec{
ModuleFormat: "(.*)(?:/[^/]*)",
Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/python/gen_resource_mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestGenResourceMappingsIsDeterministic(t *testing.T) {
minimalSchema := `
{
"name": "aws",
"version": "0.0.1",
"meta": {
"moduleFormat": "(.*)(?:/[^/]*)"
},
Expand Down
11 changes: 11 additions & 0 deletions pkg/codegen/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,11 @@ func errorf(path, message string, args ...interface{}) *hcl.Diagnostic {
func bindSpec(spec PackageSpec, languages map[string]Language, loader Loader) (*Package, hcl.Diagnostics, error) {
var diags hcl.Diagnostics

// Validate that there is a name
if spec.Name == "" {
diags = diags.Append(errorf("#/name", "no name provided"))
}

// Parse the version, if any.
var version *semver.Version
if spec.Version != "" {
Expand Down Expand Up @@ -1990,6 +1995,9 @@ func (t *types) bindType(path string, spec TypeSpec, inputShape bool) (result Ty
var discriminator string
var mapping map[string]string
if spec.Discriminator != nil {
if spec.Discriminator.PropertyName == "" {
diags = diags.Append(errorf(path, "discriminator must provide a property name"))
}
discriminator = spec.Discriminator.PropertyName
mapping = spec.Discriminator.Mapping
}
Expand Down Expand Up @@ -2135,6 +2143,9 @@ func bindDefaultValue(path string, value interface{}, spec *DefaultSpec, typ Typ
for name, raw := range spec.Language {
language[name] = json.RawMessage(raw)
}
if len(spec.Environment) == 0 {
diags = diags.Append(errorf(path, "Default must specify an environment"))
}

dv.Environment, dv.Language = spec.Environment, language
}
Expand Down