Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Terraform Provider: organize imports of generated code
Browse files Browse the repository at this point in the history
Updates code generation to sort and group imports in the same
manner as dictated by the gci rules in the golanci-lint config.
This also removes the redundant `tfschema` import alias for packages
which do not have custom schema.
  • Loading branch information
rosstimothy committed Dec 5, 2023
1 parent 0037058 commit c9c593a
Show file tree
Hide file tree
Showing 39 changed files with 247 additions and 221 deletions.
20 changes: 19 additions & 1 deletion terraform/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ var (
Kind: "device",
ID: "trustedDevice.Metadata.Name",
HasStaticID: true,
SchemaPackage: "schemav1",
SchemaPackagePath: "github.com/gravitational/teleport-plugins/terraform/tfschema/devicetrust/v1",
IsPlainStruct: true,
UUIDMetadataName: true,
Expand Down Expand Up @@ -477,7 +478,24 @@ func generate(p payload, tpl, outFile string) {
log.Fatal(err)
}

t, err := template.ParseFiles(path.Join("gen", tpl))
funcs := template.FuncMap{
"schemaImport": func(p payload) string {
if p.SchemaPackage == "tfschema" {
return `"` + p.SchemaPackagePath + `"`
}

return p.SchemaPackage + ` "` + p.SchemaPackagePath + `"`
},
"protoImport": func(p payload) string {
if p.ConvertPackagePath != "" {
return "convert" + ` "` + p.ConvertPackagePath + `"`
}

return p.ProtoPackage + ` "` + p.ProtoPackagePath + `"`
},
}

t, err := template.New(p.Name).Funcs(funcs).ParseFiles(path.Join("gen", tpl))
if err != nil {
log.Fatal(err)
}
Expand Down
16 changes: 6 additions & 10 deletions terraform/gen/plural_data_source.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,16 @@ package provider
import (
"context"

{{ if not .IsPlainStruct }}
{{- protoImport . }}
{{ end }}
"github.com/gravitational/trace"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/path"

{{.SchemaPackage}} "{{.SchemaPackagePath}}"
{{if not .IsPlainStruct -}}
{{- if .ConvertPackagePath}}
convert "{{.ConvertPackagePath}}"
{{ else }}
{{.ProtoPackage}} "{{.ProtoPackagePath}}"
{{- end}}
{{end -}}
"github.com/gravitational/trace"
{{ schemaImport . }}
)

// dataSourceTeleport{{.Name}}Type is the data source metadata type
Expand Down
37 changes: 18 additions & 19 deletions terraform/gen/plural_resource.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,30 @@ package provider

import (
"context"
{{- if .RandomMetadataName }}
"crypto/rand"
"encoding/hex"
{{- end }}
"fmt"
{{- if .RandomMetadataName}}
"crypto/rand"
"encoding/hex"
{{- end}}
{{- if .UUIDMetadataName}}
"github.com/google/uuid"
{{- end}}
{{- range $i, $a := .ExtraImports}}
{{- range $i, $a := .ExtraImports }}
"{{$a}}"
{{- end}}

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/path"

{{- end }}
{{ if .UUIDMetadataName }}
"github.com/google/uuid"
{{- end }}
{{.ProtoPackage}} "{{.ProtoPackagePath}}"
{{.SchemaPackage}} "{{.SchemaPackagePath}}"
{{- if .ConvertPackagePath}}
convert "{{.ConvertPackagePath}}"
{{- end}}
{{ if .ConvertPackagePath -}}
convert "{{.ConvertPackagePath}}"
{{- end}}
"github.com/gravitational/teleport/integrations/lib/backoff"
"github.com/gravitational/trace"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/jonboulle/clockwork"

{{ schemaImport . }}
)

// resourceTeleport{{.Name}}Type is the resource metadata type
Expand Down
10 changes: 5 additions & 5 deletions terraform/gen/singular_data_source.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ package provider
import (
"context"

{{ if not .IsPlainStruct }}
{{- protoImport . }}
{{- end }}
"github.com/gravitational/trace"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"

{{.SchemaPackage}} "{{.SchemaPackagePath}}"
{{if not .IsPlainStruct -}}
{{.ProtoPackage}} "{{.ProtoPackagePath}}"
{{end -}}
"github.com/gravitational/trace"
{{ schemaImport . }}
)

// dataSourceTeleport{{.Name}}Type is the data source metadata type
Expand Down
10 changes: 5 additions & 5 deletions terraform/gen/singular_resource.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import (
"math"
{{- end}}

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"

{{.ProtoPackage}} "{{.ProtoPackagePath}}"
{{.SchemaPackage}} "{{.SchemaPackagePath}}"
"github.com/gravitational/teleport/integrations/lib/backoff"
"github.com/gravitational/trace"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/jonboulle/clockwork"

{{ schemaImport . }}
)

// resourceTeleport{{.Name}}Type is the resource metadata type
Expand Down
9 changes: 4 additions & 5 deletions terraform/provider/data_source_teleport_access_list.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions terraform/provider/data_source_teleport_app.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions terraform/provider/data_source_teleport_auth_preference.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions terraform/provider/data_source_teleport_database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions terraform/provider/data_source_teleport_device_trust.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions terraform/provider/data_source_teleport_github_connector.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions terraform/provider/data_source_teleport_login_rule.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions terraform/provider/data_source_teleport_oidc_connector.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions terraform/provider/data_source_teleport_okta_import_rule.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c9c593a

Please sign in to comment.