chore: update api specification for vcf-5.2 #44
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In order to have a good experience with our community, we recommend that you read the contributing guidelines for making a pull request.
Summary of Pull Request
This change updates the OpenAPI document with the one released with VCF 5.2 and regenerates the bindings using a new tool.
Guide for Reviewers
This PR contains two commits to ease reviewing.
0138b31 - Adds a dependency to
oapi-codegen
, updates the main.go file, updates theREADME.md
, adds the new generated code.ed2eff1 - deletes all files related to go-swagger and its usage, and all bindings generated with that tool.
FAQ
[Q] Why are you replacing
go-swagger
with something else?[A]
go-swagger
supports "swagger" a.k.a OpenAPI 2.0. As of 5.2 VCF's API is released following the OpenAPI 3.0.1 specification. Thego-swagger
maintainers have stated that they will not attempt to support OpenAPI 3.X in future releases.[Q] How did you choose
oapi-codegen
as the new tool?[A] I can answer this question in great detail offline. I evaluated 3 of the most popular tools for generating (Go) code.
I took into consideration the level of support by maintainers, the ease of adoption, the cost of use. I believe that the tool I chose offers the best trade-off.
[Q] How is the code generated with the new tool different?
[A] There are fundamental differences that I will outline in the next section.
[Q] How did you test this change?
[A] I created a local override for the SDK and prepared the necessary code changes to the VCF Terraform provider in advance. I ran at least one acceptance test for each and every resource and data source. The changes are available in this branch.
Differences in the Generated Code
The code generated with
oapi-codegen
is very different than the one we've seen fromgo-swagger
.In this section I will highlight the most important changes to the bindings and the way the are used.
oapi-codegen
handles optional fields in models properlyIn my opinion there was a fundamental flaw in the way
go-swagger
treated optional and required properties. In fact I think it had the two concepts reversed.oapi-codegen
produces struct types for required fields. Optional fields are generated as pointers to structs and can be set to nil and effectively ignored. This should mean that we can finally put an end to bugs like thisoapi-codegen
generates all operations and models in a single fileoapi-codegen
does not generate *Params structsWe no longer need to instantiate "params" structs when calling APIs with parameters. Parameters are now... parameters to the respective methods.
oapi-codegen
serializes the response based on the status codeLets examine a fictional GET operation called "CustomOperation". It has a 3 possible status codes - 200, 400 and 500.
The tool produces a method to call that operation "CustomOperationWithResponse" that returns "OperationResult" and error. "OperationResult" is the generated response type and error is the built-it error type.
The error will be set only if there is a non-API-related problem such as a network failure. The actual API errors are part of the "OperationResult" type.
"OperationResult" will contain 3 (among others not of interest right now) fields - JSON200, JSON400 and JSON500.
JSON200 would be of type "OperationResult" while JSON400 and JSON500 would be of type "Error", a generated type, common for all VCF APIs. This allows us to easily read what we need based on the status code (available in a StatusCode field) but it means that we would have to handle multiple error cases every time.
There is an easy solution to this - since all errors are of the same type we can just check the status code and if it is not a "success code" we can just deserialize the body into a VCF error and voila! This is demonstrated in https://github.com/vmware/terraform-provider-vcf/compare/chore/openapi-3-alt and is a lot simpler than it sounds.
Type of Pull Request
Please describe:
Related to Existing Issues
Issue Number: N/A
Test and Documentation Coverage
For bug fixes or features:
Ran the acceptance tests for the VCF provider.
The corresponding changes are available in https://github.com/vmware/terraform-provider-vcf/compare/chore/openapi-3-alt
Breaking Changes?
The bindings for 5.1.1 are being deleted