Skip to content

Commit 50b9cdd

Browse files
jasondamourclaude
andcommitted
[new] Add Terraform provider petstore sample and acceptance tests
Add sample generation config and generated Terraform provider for the petstore OpenAPI spec, along with Go acceptance tests that exercise the provider lifecycle (create, read, update, delete, import). Generated output includes: - Provider with auth configuration (API key, bearer, basic) - Pet, User, and Store resources and data sources - Client package with HTTP client and model structs - Go acceptance tests using terraform-plugin-testing - Example Terraform configurations for each resource/data source Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ad46e19 commit 50b9cdd

35 files changed

Lines changed: 1777 additions & 0 deletions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
generatorName: terraform-provider
2+
outputDir: samples/client/petstore/terraform
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/terraform-provider
5+
gitHost: github.com
6+
gitUserId: example
7+
gitRepoId: terraform-provider-petstore
8+
additionalProperties:
9+
providerName: "petstore"
10+
providerAddress: "registry.terraform.io/example/petstore"
11+
hideGenerationTimestamp: "true"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*.dll
2+
*.exe
3+
*.exe~
4+
*.dylib
5+
*.so
6+
*.test
7+
*.out
8+
*.tfstate
9+
*.tfstate.*
10+
.terraform/
11+
terraform.tfvars
12+
terraform-provider-petstore
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.gitignore
2+
GNUmakefile
3+
README.md
4+
examples/provider/provider.tf
5+
go.mod
6+
internal/client/client.go
7+
internal/client/model_api_response.go
8+
internal/client/model_category.go
9+
internal/client/model_order.go
10+
internal/client/model_pet.go
11+
internal/client/model_tag.go
12+
internal/client/model_user.go
13+
internal/provider/pet_data_source.go
14+
internal/provider/pet_model.go
15+
internal/provider/pet_resource.go
16+
internal/provider/provider.go
17+
internal/provider/store_data_source.go
18+
internal/provider/store_model.go
19+
internal/provider/store_resource.go
20+
internal/provider/user_data_source.go
21+
internal/provider/user_model.go
22+
internal/provider/user_resource.go
23+
main.go
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.20.0-SNAPSHOT
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
default: testacc
2+
3+
# Run acceptance tests
4+
.PHONY: testacc
5+
testacc:
6+
TF_ACC=1 go test ./... -v $(TESTARGS) -timeout 120m
7+
8+
# Build provider
9+
.PHONY: build
10+
build:
11+
go build -o terraform-provider-petstore
12+
13+
# Install provider locally
14+
.PHONY: install
15+
install: build
16+
mkdir -p ~/.terraform.d/plugins/registry.terraform.io/example/petstore/0.1.0/$(shell go env GOOS)_$(shell go env GOARCH)
17+
mv terraform-provider-petstore ~/.terraform.d/plugins/registry.terraform.io/example/petstore/0.1.0/$(shell go env GOOS)_$(shell go env GOARCH)/
18+
19+
# Generate documentation
20+
.PHONY: docs
21+
docs:
22+
go generate ./...
23+
24+
# Run linter
25+
.PHONY: lint
26+
lint:
27+
golangci-lint run ./...
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Terraform Provider petstore
2+
3+
This Terraform provider was generated using [OpenAPI Generator](https://openapi-generator.tech).
4+
5+
## Requirements
6+
7+
- [Terraform](https://www.terraform.io/downloads.html) >= 1.0
8+
- [Go](https://golang.org/doc/install) >= 1.21
9+
10+
## Building The Provider
11+
12+
1. Clone the repository
13+
2. Enter the repository directory
14+
3. Build the provider using the Go `install` command:
15+
16+
```shell
17+
go install
18+
```
19+
20+
## Using the provider
21+
22+
```hcl
23+
terraform {
24+
required_providers {
25+
petstore = {
26+
source = "registry.terraform.io/example/petstore"
27+
}
28+
}
29+
}
30+
31+
provider "petstore" {
32+
endpoint = "http://petstore.swagger.io/v2"
33+
}
34+
```
35+
36+
## Developing the Provider
37+
38+
If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (see [Requirements](#requirements) above).
39+
40+
To compile the provider, run `go install`. This will build the provider and put the provider binary in the `$GOPATH/bin` directory.
41+
42+
To generate or update documentation, run `go generate`.
43+
44+
In order to run the full suite of Acceptance tests, run `make testacc`.
45+
46+
```shell
47+
make testacc
48+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
data "petstore_pet" "example" {
2+
name = "fido"
3+
photo_urls = "https://example.com/fido.jpg"
4+
}
5+
6+
output "pet_status" {
7+
value = data.petstore_pet.example.status
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
data "petstore_user" "example" {
2+
username = "johndoe"
3+
}
4+
5+
output "user_email" {
6+
value = data.petstore_user.example.email
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
required_providers {
3+
petstore = {
4+
source = "registry.terraform.io/example/petstore"
5+
}
6+
}
7+
}
8+
9+
provider "petstore" {
10+
endpoint = "http://petstore.swagger.io/v2"
11+
# api_key = "your-api-key"
12+
# token = "your-bearer-token"
13+
}

0 commit comments

Comments
 (0)