Skip to content

Conversation

@bendbennett
Copy link
Contributor

This PR:

  • Adds validation to verify that the same name is not used for multiple data sources or multiple resources.
  • Adds validation to verify that attributes and blocks with the same name at the same level of nesting are not used.
  • Adds validation to verify that objects do not contain keys with the same name.

as the `all` command only generates schema for data sources, provider and resources at
present.

## Further Considerations
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
## Further Considerations

specValidator := validate.NewSpecValidator()

commands := map[string]cli.CommandFactory{
"schema": func() (cli.Command, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

In the future I'm hoping we can still support terraform-plugin-codegen-framework schema -data-source=examplecloud_thing (or something along those lines) so existing providers can inject //go:generate directives to replace manually written code.

Copy link
Contributor

@bflad bflad May 30, 2023

Choose a reason for hiding this comment

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

Although looking at this again, I'm guessing we'd be looking for a command structure like:

|- all
|- data-source TYPE (flags like -schema)
|- provider (flags like -schema)
 \ resource TYPE (flags like -schema)

But hopefully you get the idea 😄

Comment on lines +73 to +75
// validateSchemaNames determines whether any of the names used for data sources or
// resources are duplicated. SchemaNames also determines whether any of the names used
// for attributes or blocks at the same level of nesting within a schema are duplicated.
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this particular validation logic is undebatable (e.g. it is never valid for a provider to have duplicated resource names) and may be desired for any code implementing the spec on either side of it, I wonder if we should uplift this logic into the spec Go module? It's a bummer that it seems like this cannot be introduced into the JSON schema itself.

We could introduce something like the following there:

package resource

type Resources []Resource

func (rs Resources) Validate(ctx context.Context) error {
  // Resource name duplication detection
  // Delegate to new (Resource).Validate() method on each for Schema, etc. validation
}

package spec

type Specification struct {
  // ...
  Resources resource.Resources `json:"resources,omitempty"`
}

func (s Specification) Validate(ctx context.Context) error {
  // Delegate to (resource.Resources).Validate(), etc. methods
}

At this point we could debate the merits of the spec.Validate() function doing this automatically (with the unfortunate part of not returning the then-already JSON unmarshalled spec.Specification that would be needed to call the (spec.Specification).Validate() method), introducing a separate function that does the JSON Schema validate and JSON unmarshal with this additional validation logic, or whatever suits our fancy.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I'd be in favour of the latter, having something like the following available on the spec Go module:

func Generate(ctx context.Context, document []byte) (Specification, error) {
	if err := Validate(ctx, document); err != nil {
		return Specification{}, err
	}

	var s Specification
	if err = json.Unmarshal(src, &s); err != nil {
		return s, err
	} 

	if err = s.Validate(ctx); err != nil {
		return s, err
	}

	return s, nil
}

@bendbennett
Copy link
Contributor Author

Validation for ensuring that there are not duplicate names in use for data sources, resources, attributes, blocks and object attribute types has been added directly to the codegen.spec repo.

Other changes have been rolled into [WIP] Add custom type handling for elements and schema generation for provider and resources or in the case of the command structure an issue has been created.

@bendbennett bendbennett closed this Jun 8, 2023
@bendbennett bendbennett deleted the validation-and-restructure branch June 20, 2023 08:58
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants