Skip to content

Add support for embedded fields#29

Merged
flyinghermit merged 7 commits into
mainfrom
marco/add_embed_fields
May 26, 2023
Merged

Add support for embedded fields#29
flyinghermit merged 7 commits into
mainfrom
marco/add_embed_fields

Conversation

@marcoandredinis

@marcoandredinis marcoandredinis commented Apr 5, 2023

Copy link
Copy Markdown
Contributor

When there's an embed proto field (extension (gogoproto.embed) = true) we need to embed the message's fields into the current message.

So this:

message DeviceV1 {
  ResourceHeader Header = 1 [(gogoproto.embed) = true];
  string other = 2;
}
message ResourceHeader {
  string Kind = 1;
}

Should become the following tfschema:

kind: string
other: string

However, we were creating the following schema:

header:
  kind: string
other: string

This PR changes this to support embedding those fields.
I did a manual test for the DeviceTrust resource which we are about to add support for in Teleport's Terraform provider.

TODO

  • Tests

- update field_build_context to return message path instead of full path when the field is embedded field
- unit tests for copyTo copyFrom functions
@flyinghermit flyinghermit marked this pull request as ready for review May 23, 2023 15:01
@flyinghermit

Copy link
Copy Markdown
Contributor

@nklaassen Tagging you as reviewer since you are also reviewing gravitational/teleport-plugins#801

@flyinghermit flyinghermit removed their request for review May 23, 2023 16:41
Comment thread test/test.proto Outdated
Comment thread test/fixtures.go Outdated

@marcoandredinis marcoandredinis left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Looks good (I might be a little biased 🙈)

Comment thread field.go
if len(fs) == 0 {
return trace.BadParameter("expected at least one field")
}
f.MapValueField = fs[0]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why is only the first field used?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good question, pinging @marcoandredinis for reply.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like for maps this can only ever be of size 0. When this is an embedded struct, it can return multiple fields. If it's not an embedded struct, then it just returns a single element array.

Comment thread test/test.proto Outdated
Comment thread test/test.proto Outdated
Comment thread test/test.proto Outdated
Comment thread test/test_terraform.go

@mdwn mdwn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is way simpler than my implementation!

@flyinghermit

Copy link
Copy Markdown
Contributor

Friendly ping for code owner review please @r0mant

Comment thread test/test.proto Outdated
Comment thread test/test.proto Outdated
flyinghermit and others added 2 commits May 26, 2023 21:51
Co-authored-by: Roman Tkachenko <roman@goteleport.com>
Co-authored-by: Roman Tkachenko <roman@goteleport.com>
@flyinghermit

Copy link
Copy Markdown
Contributor

Thanks everyone for review.

@flyinghermit flyinghermit merged commit 20a8bcd into main May 26, 2023
flyinghermit added a commit to gravitational/teleport-plugins that referenced this pull request May 29, 2023
Terraform support for device trust. 

### Device type definition
Currently, there's two Device types implemented for Device Trust: 
1. [Device](https://github.com/gravitational/teleport/blob/master/api/proto/teleport/devicetrust/v1/device.proto) proto, which is the main definition used by all Device Trust related services.
2. [DeviceV1](https://github.com/gravitational/teleport/blob/master/api/proto/teleport/legacy/types/device.proto) used by this provider.

### Provider implementation
Unlike other existing resources, [DeviceV1](https://github.com/gravitational/teleport/blob/master/api/proto/teleport/legacy/types/device.proto) does not fully utilize the metadata fields such as `label`, `description`, etc. Only `metadata.name` is supported, specifically to be compatible with schema generation for terraform provider. Additionally, the [DeviceV1](https://github.com/gravitational/teleport/blob/master/api/proto/teleport/legacy/types/device.proto) embeds the `ResourceHeader` field. [protoc-gen-terraform](gravitational/protoc-gen-terraform#29) is updated to support such embedded fields.
 
This PR adds a new fields to the template payload:
- `UUIDMetadataName` field: used to generate `Metadata.Name` as uuid value. The [`plural_resource.go.tpl`](https://github.com/gravitational/teleport-plugins/blob/a76be035e7498090958e05f356426d2a4084281d/terraform/gen/plural_resource.go.tpl) template is updated to accommodate `UUIDMetadataName` field. 

### Supported device trust CRUD fields
As for supported fields, only `asset_tag`, `os_type`, and `enroll_status` are supported by this provider. 
1. Create: Create device: `asset_tag`, `os_type`, `enroll_status` (optional as this value can be updated outside of Terraform and it won't be a good experience for our users to keep the `.tf` files in sync with the state )
4. Update: Update `enroll_status`: `enrolled -> not_enrolled`
5. Delete: Delete device.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants