Skip to content

Commit

Permalink
Generate datasources based on OpenAPI specification
Browse files Browse the repository at this point in the history
This is an experiment to see whether generating the provider based on
the OpenAPI specification at https://github.com/hashicorp/boundary/blob/main/internal/gen/controller.swagger.json
could work.

The schema is converted from the definitions given in the document to
to map[string]*schema.Schema, with two special cases:
  - when there is an object in an object, I convert it to a one element
  list as terraform-plugin-sdk v2 does not know how to express this,
  - when there is an opaque attribute (`map[string]interface{}`), I
  skip it completely as terraform-plugin-sdk does not expose
  `DynamicPseudoType` that would make it possible to express this
  attribute in native Terraform, the workaround I use in the Consul
  provider is to use `schema.TypeString` and `jsonencode()` but it is not
  ideal. Since I only worked on the datasources here I chose to skip
  those attributes for now.

Once the schema is converted, we create the `ReadContext` function that
is needed for the datasource. As it can be a bit tricky to use the Go
client for each service, I chose to use directly the global *api.Client
and to manually add the query params and get the raw response. While it
would not be recommended for an external project to use the client this
way, it fits nicely here and keep the code simple. Finally the result is
written to the state, looking at the schema we generated previously to
convert it.

The tests are written manually so the developper can make sure that
everything is working as expected even thought the code was generated and
not written manually.

While the conversion of the schema could be made at runtime and only
one `ReadContext` function is actually needed, I find generating the code
make it quite easy to review and should make it easier for contributors
already accustomed to writing Terraform providers to look for errors or
fork the provider for their needs.

While I only worked on datasources returning lists of elements for now,
I think the same approach could be used to generate datasources returning
a single element and ultimately resources. This would make it very easy
to keep the Terraform provider in sync with new Boundary versions,
especially as the OpenAPI spec is created from the Protobuf files and
the CLI is already generated on a similar principle.

The code in generate_datasource.go is not very nice, but it does get the
job done. I may spin it off in its own project in the future to add more
feature to it.

Closes hashicorp#99
  • Loading branch information
remilapeyre authored and moduli committed May 6, 2024
1 parent e847f5f commit dad33e4
Show file tree
Hide file tree
Showing 50 changed files with 5,257 additions and 0 deletions.
56 changes: 56 additions & 0 deletions docs/data-sources/accounts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "boundary_accounts Data Source - terraform-provider-boundary"
subcategory: ""
description: |-
Lists all Accounts in a specific Auth Method.
---

# boundary_accounts (Data Source)

Lists all Accounts in a specific Auth Method.



<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- **auth_method_id** (String)
- **filter** (String)
- **id** (String) The ID of this resource.

### Read-Only

- **items** (List of Object) (see [below for nested schema](#nestedatt--items))

<a id="nestedatt--items"></a>
### Nested Schema for `items`

Read-Only:

- **auth_method_id** (String)
- **authorized_actions** (List of String)
- **created_time** (String)
- **description** (String)
- **id** (String)
- **managed_group_ids** (List of String)
- **name** (String)
- **scope** (List of Object) (see [below for nested schema](#nestedobjatt--items--scope))
- **type** (String)
- **updated_time** (String)
- **version** (Number)

<a id="nestedobjatt--items--scope"></a>
### Nested Schema for `items.scope`

Read-Only:

- **description** (String)
- **id** (String)
- **name** (String)
- **parent_scope_id** (String)
- **type** (String)


57 changes: 57 additions & 0 deletions docs/data-sources/auth_methods.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "boundary_auth_methods Data Source - terraform-provider-boundary"
subcategory: ""
description: |-
Lists all Auth Methods.
---

# boundary_auth_methods (Data Source)

Lists all Auth Methods.



<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- **filter** (String)
- **id** (String) The ID of this resource.
- **recursive** (Boolean)
- **scope_id** (String)

### Read-Only

- **items** (List of Object) (see [below for nested schema](#nestedatt--items))

<a id="nestedatt--items"></a>
### Nested Schema for `items`

Read-Only:

- **authorized_actions** (List of String)
- **created_time** (String)
- **description** (String)
- **id** (String)
- **is_primary** (Boolean)
- **name** (String)
- **scope** (List of Object) (see [below for nested schema](#nestedobjatt--items--scope))
- **scope_id** (String)
- **type** (String)
- **updated_time** (String)
- **version** (Number)

<a id="nestedobjatt--items--scope"></a>
### Nested Schema for `items.scope`

Read-Only:

- **description** (String)
- **id** (String)
- **name** (String)
- **parent_scope_id** (String)
- **type** (String)


58 changes: 58 additions & 0 deletions docs/data-sources/auth_tokens.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "boundary_auth_tokens Data Source - terraform-provider-boundary"
subcategory: ""
description: |-
Lists all Auth Tokens.
---

# boundary_auth_tokens (Data Source)

Lists all Auth Tokens.



<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- **filter** (String)
- **id** (String) The ID of this resource.
- **recursive** (Boolean)
- **scope_id** (String)

### Read-Only

- **items** (List of Object) (see [below for nested schema](#nestedatt--items))

<a id="nestedatt--items"></a>
### Nested Schema for `items`

Read-Only:

- **account_id** (String)
- **approximate_last_used_time** (String)
- **auth_method_id** (String)
- **authorized_actions** (List of String)
- **created_time** (String)
- **expiration_time** (String)
- **id** (String)
- **scope** (List of Object) (see [below for nested schema](#nestedobjatt--items--scope))
- **scope_id** (String)
- **token** (String)
- **updated_time** (String)
- **user_id** (String)

<a id="nestedobjatt--items--scope"></a>
### Nested Schema for `items.scope`

Read-Only:

- **description** (String)
- **id** (String)
- **name** (String)
- **parent_scope_id** (String)
- **type** (String)


55 changes: 55 additions & 0 deletions docs/data-sources/credential_libraries.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "boundary_credential_libraries Data Source - terraform-provider-boundary"
subcategory: ""
description: |-
Lists all Credential Library.
---

# boundary_credential_libraries (Data Source)

Lists all Credential Library.



<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- **credential_store_id** (String)
- **filter** (String)
- **id** (String) The ID of this resource.

### Read-Only

- **items** (List of Object) (see [below for nested schema](#nestedatt--items))

<a id="nestedatt--items"></a>
### Nested Schema for `items`

Read-Only:

- **authorized_actions** (List of String)
- **created_time** (String)
- **credential_store_id** (String)
- **description** (String)
- **id** (String)
- **name** (String)
- **scope** (List of Object) (see [below for nested schema](#nestedobjatt--items--scope))
- **type** (String)
- **updated_time** (String)
- **version** (Number)

<a id="nestedobjatt--items--scope"></a>
### Nested Schema for `items.scope`

Read-Only:

- **description** (String)
- **id** (String)
- **name** (String)
- **parent_scope_id** (String)
- **type** (String)


56 changes: 56 additions & 0 deletions docs/data-sources/credential_stores.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "boundary_credential_stores Data Source - terraform-provider-boundary"
subcategory: ""
description: |-
Lists all Credential Stores.
---

# boundary_credential_stores (Data Source)

Lists all Credential Stores.



<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- **filter** (String)
- **id** (String) The ID of this resource.
- **recursive** (Boolean)
- **scope_id** (String)

### Read-Only

- **items** (List of Object) (see [below for nested schema](#nestedatt--items))

<a id="nestedatt--items"></a>
### Nested Schema for `items`

Read-Only:

- **authorized_actions** (List of String)
- **created_time** (String)
- **description** (String)
- **id** (String)
- **name** (String)
- **scope** (List of Object) (see [below for nested schema](#nestedobjatt--items--scope))
- **scope_id** (String)
- **type** (String)
- **updated_time** (String)
- **version** (Number)

<a id="nestedobjatt--items--scope"></a>
### Nested Schema for `items.scope`

Read-Only:

- **description** (String)
- **id** (String)
- **name** (String)
- **parent_scope_id** (String)
- **type** (String)


66 changes: 66 additions & 0 deletions docs/data-sources/groups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "boundary_groups Data Source - terraform-provider-boundary"
subcategory: ""
description: |-
Lists all Groups.
---

# boundary_groups (Data Source)

Lists all Groups.



<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- **filter** (String)
- **id** (String) The ID of this resource.
- **recursive** (Boolean)
- **scope_id** (String)

### Read-Only

- **items** (List of Object) (see [below for nested schema](#nestedatt--items))

<a id="nestedatt--items"></a>
### Nested Schema for `items`

Read-Only:

- **authorized_actions** (List of String)
- **created_time** (String)
- **description** (String)
- **id** (String)
- **member_ids** (List of String)
- **members** (List of Object) (see [below for nested schema](#nestedobjatt--items--members))
- **name** (String)
- **scope** (List of Object) (see [below for nested schema](#nestedobjatt--items--scope))
- **scope_id** (String)
- **updated_time** (String)
- **version** (Number)

<a id="nestedobjatt--items--members"></a>
### Nested Schema for `items.members`

Read-Only:

- **id** (String)
- **scope_id** (String)


<a id="nestedobjatt--items--scope"></a>
### Nested Schema for `items.scope`

Read-Only:

- **description** (String)
- **id** (String)
- **name** (String)
- **parent_scope_id** (String)
- **type** (String)


Loading

0 comments on commit dad33e4

Please sign in to comment.