forked from mirrors/terraform-provider-binarylane
1f24f45af7
* WIP commit - Adding VPC resource * Tests passing for VPC resource * Eject vpc_route_entries into its own resource so it can be applied later
4.2 KiB
4.2 KiB
Contributing
Local development
First time setup
Based on this example from the terraform docs,
go mod tidy- Run
go generateto fetch/transform the OpenAPI spec ininternal/binarylane/openapi.json - Create or modify
~/.terraformrcin your home directory
provider_installation {
dev_overrides {
# Example GOBIN path, will need to be replaced with your own GOBIN path. Default is $GOPATH/bin
"oscarhermoso/binarylane" = "/home/oscarhermoso/Git/terraform-provider-binarylane/bin"
}
direct {}
}
- Build and test the provider
go build -o bin/terraform-provider-binarylane
go install
cd examples/basic
terraform plan
terraform apply
Testing
TF_ACC=1 go test -v ./...
Adding resources/data sources to the provider
- Make any changes to
provider_gen_config.yml(see https://developer.hashicorp.com/terraform/plugin/code-generation/openapi-generator#generator-config) - Update generated files
go generate
- Scaffold any new resources and data sources
tfplugingen-framework scaffold resource \
--output-dir ./internal/provider \
--name REPLACE_ME
tfplugingen-framework scaffold data-source \
--output-dir ./internal/provider \
--name REPLACE_ME
- Populate the template scaffolding:
Resource
Define the interfaces that the resource should implement:
- var _ resource.Resource = (*exampleResource)(nil)
+ var (
+ _ resource.Resource = &exampleResource{}
+ _ resource.ResourceWithConfigure = &exampleResource{}
+ // _ resource.ResourceWithImportState = &exampleResource{}
+ )
Add the API client to the resource:
- type exampleResource struct{}
+ type exampleResource struct {
+ bc *BinarylaneClient
+ }
Extend the resource model from the generated resources.*Model.
type exampleResourceModel struct {
- Id types.String `tfsdk:"id"`
+ resources.ExampleModel
+ // Add any additional fields here
}
Add a Configure method to the resource:
+ func (d *exampleResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
+ if req.ProviderData == nil {
+ return
+ }
+ bc, ok := req.ProviderData.(BinarylaneClient)
+ if !ok {
+ resp.Diagnostics.AddError(
+ "Unexpected Data Source Configure Type",
+ fmt.Sprintf("Expected *BinarylaneClient, got: %T.", req.ProviderData),
+ )
+ return
+ }
+ d.bc = &bc
+ }
Add the new data source/resource to provider.go:
return []func() resource.Resource{
# ...
+ NewExampleResource,
}
Data Source
Define the interfaces that the data source should implement:
- var _ resource.Resource = (*exampleDataSource)(nil)
+ var (
+ _ datasource.DataSource = &exampleDataSource{}
+ _ datasource.DataSourceWithConfigure = &exampleDataSource{}
+ )
Add the API client to the data source:
```diff
- type exampleResource struct{}
+ type exampleResource struct {
+ bc *BinarylaneClient
+ }
Derive the data source model from the generated resources.*Model.
type exampleDataSourceModel struct {
- Id types.String `tfsdk:"id"`
+ resources.ExampleModel
}
Add a Configure method to the data source.
+ func (d *vpcDataSource) Configure(
+ _ context.Context,
+ req datasource.ConfigureRequest,
+ resp *datasource.ConfigureResponse,
+ ) {
+ if req.ProviderData == nil {
+ return
+ }
+
+ bc, ok := req.ProviderData.(BinarylaneClient)
+ if !ok {
+ resp.Diagnostics.AddError(
+ "Unexpected Data Source Configure Type",
+ fmt.Sprintf("Expected *BinarylaneClient, got: %T.", req.ProviderData))
+ return
+ }
+
+ d.bc = &bc
+ }
Use the generated schema to define the data source schema.
func (d *vpcDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
- resp.Schema = schema.Schema{
- Attributes: map[string]schema.Attribute{
- "id": schema.StringAttribute{
- Computed: true,
- },
- },
- }
+ resp.Schema = *convertResourceSchemaToDataSourceSchema(ctx, resources.ExampleResourceSchema(ctx))
+ resp.Schema.Description = "TODO"
}