Skip to content

Commit

Permalink
Merge pull request #48 from pluralsh/marcin/prod-2468-use-embedded-st…
Browse files Browse the repository at this point in the history
…ruct-types-in-terraform-provider-once-it-will

enhancement: Use embedded struct types
  • Loading branch information
maciaszczykm authored Aug 12, 2024
2 parents fc3ed8c + 9f5b854 commit 064ae34
Show file tree
Hide file tree
Showing 16 changed files with 596 additions and 1,194 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Visit https://golangci-lint.run/ for usage documentation
# and information on other useful linters
args: --timeout=10m
issues:
max-per-linter: 0
max-same-issues: 0

linters:
disable-all: true
enable:
Expand All @@ -24,4 +24,4 @@ linters:
- unconvert
- unparam
- unused
- vet
- govet
325 changes: 164 additions & 161 deletions go.mod

Large diffs are not rendered by default.

1,285 changes: 366 additions & 919 deletions go.sum

Large diffs are not rendered by default.

28 changes: 0 additions & 28 deletions internal/client/wait.go

This file was deleted.

3 changes: 2 additions & 1 deletion internal/datasource/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"terraform-provider-plural/internal/client"
"terraform-provider-plural/internal/common"
"terraform-provider-plural/internal/model"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand Down Expand Up @@ -81,7 +82,7 @@ func (p *providerDataSource) Configure(_ context.Context, req datasource.Configu
}

func (p *providerDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data provider
var data model.Provider
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
Expand Down
22 changes: 0 additions & 22 deletions internal/datasource/provider_model.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource
package model

import (
"context"
Expand All @@ -13,7 +13,7 @@ import (
"github.com/pluralsh/polly/algorithms"
)

type customStackRun struct {
type CustomStackRun struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Documentation types.String `tfsdk:"documentation"`
Expand All @@ -22,7 +22,7 @@ type customStackRun struct {
Configuration types.Set `tfsdk:"configuration"`
}

func (csr *customStackRun) Attributes(ctx context.Context, d diag.Diagnostics, client *client.Client) (*gqlclient.CustomStackRunAttributes, error) {
func (csr *CustomStackRun) Attributes(ctx context.Context, d diag.Diagnostics, client *client.Client) (*gqlclient.CustomStackRunAttributes, error) {
return &gqlclient.CustomStackRunAttributes{
Name: csr.Name.ValueString(),
Documentation: csr.Documentation.ValueStringPointer(),
Expand All @@ -32,7 +32,7 @@ func (csr *customStackRun) Attributes(ctx context.Context, d diag.Diagnostics, c
}, nil
}

func (csr *customStackRun) commandsAttribute(ctx context.Context, d diag.Diagnostics) []*gqlclient.CommandAttributes {
func (csr *CustomStackRun) commandsAttribute(ctx context.Context, d diag.Diagnostics) []*gqlclient.CommandAttributes {
if csr.Commands.IsNull() {
return nil
}
Expand All @@ -55,7 +55,7 @@ func (csr *customStackRun) commandsAttribute(ctx context.Context, d diag.Diagnos
return result
}

func (csr *customStackRun) configurationAttribute(ctx context.Context, d diag.Diagnostics) []*gqlclient.PrConfigurationAttributes {
func (csr *CustomStackRun) configurationAttribute(ctx context.Context, d diag.Diagnostics) []*gqlclient.PrConfigurationAttributes {
if csr.Configuration.IsNull() {
return nil
}
Expand All @@ -80,7 +80,7 @@ func (csr *customStackRun) configurationAttribute(ctx context.Context, d diag.Di
return result
}

func (csr *customStackRun) From(customStackRun *gqlclient.CustomStackRunFragment, ctx context.Context, d diag.Diagnostics) {
func (csr *CustomStackRun) From(customStackRun *gqlclient.CustomStackRunFragment, ctx context.Context, d diag.Diagnostics) {
csr.Id = types.StringValue(customStackRun.ID)
csr.Name = types.StringValue(customStackRun.Name)
csr.Documentation = types.StringPointerValue(customStackRun.Documentation)
Expand Down
10 changes: 1 addition & 9 deletions internal/model/git_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
gqlclient "github.com/pluralsh/console/go/client"
)

// TODO: Embed structs like these once it will be supported: https://github.com/hashicorp/terraform-plugin-framework/pull/1021

type GitRepository struct {
Id types.String `tfsdk:"id"`
Url types.String `tfsdk:"url"`
Expand All @@ -18,8 +16,7 @@ func (gr *GitRepository) From(response *gqlclient.GitRepositoryFragment) {
}

type GitRepositoryExtended struct {
Id types.String `tfsdk:"id"`
Url types.String `tfsdk:"url"`
GitRepository
PrivateKey types.String `tfsdk:"private_key"`
Passphrase types.String `tfsdk:"passphrase"`
Username types.String `tfsdk:"username"`
Expand All @@ -29,11 +26,6 @@ type GitRepositoryExtended struct {
Decrypt types.Bool `tfsdk:"decrypt"`
}

func (gre *GitRepositoryExtended) From(response *gqlclient.GitRepositoryFragment) {
gre.Id = types.StringValue(response.ID)
gre.Url = types.StringValue(response.URL)
}

func (gre *GitRepositoryExtended) Attributes() gqlclient.GitAttributes {
return gqlclient.GitAttributes{
URL: gre.Url.ValueString(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource
package model

import (
"context"
Expand All @@ -14,7 +14,7 @@ import (
"github.com/pluralsh/polly/algorithms"
)

type infrastructureStack struct {
type InfrastructureStack struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Type types.String `tfsdk:"type"`
Expand All @@ -31,7 +31,7 @@ type infrastructureStack struct {
Bindings *common.Bindings `tfsdk:"bindings"`
}

func (is *infrastructureStack) Attributes(ctx context.Context, d diag.Diagnostics, client *client.Client) (*gqlclient.StackAttributes, error) {
func (is *InfrastructureStack) Attributes(ctx context.Context, d diag.Diagnostics, client *client.Client) (*gqlclient.StackAttributes, error) {
attr := &gqlclient.StackAttributes{
Name: is.Name.ValueString(),
Type: gqlclient.StackType(is.Type.ValueString()),
Expand All @@ -58,7 +58,7 @@ func (is *infrastructureStack) Attributes(ctx context.Context, d diag.Diagnostic
return attr, nil
}

func (is *infrastructureStack) FilesAttributes(ctx context.Context, d diag.Diagnostics) []*gqlclient.StackFileAttributes {
func (is *InfrastructureStack) FilesAttributes(ctx context.Context, d diag.Diagnostics) []*gqlclient.StackFileAttributes {
if is.Files.IsNull() {
return nil
}
Expand All @@ -74,7 +74,7 @@ func (is *infrastructureStack) FilesAttributes(ctx context.Context, d diag.Diagn
return result
}

func (is *infrastructureStack) EnvironmentAttributes(ctx context.Context, d diag.Diagnostics) []*gqlclient.StackEnvironmentAttributes {
func (is *InfrastructureStack) EnvironmentAttributes(ctx context.Context, d diag.Diagnostics) []*gqlclient.StackEnvironmentAttributes {
if is.Environment.IsNull() {
return nil
}
Expand All @@ -94,7 +94,7 @@ func (is *infrastructureStack) EnvironmentAttributes(ctx context.Context, d diag
return result
}

func (is *infrastructureStack) From(stack *gqlclient.InfrastructureStackFragment, ctx context.Context, d diag.Diagnostics) {
func (is *InfrastructureStack) From(stack *gqlclient.InfrastructureStackFragment, ctx context.Context, d diag.Diagnostics) {
is.Id = types.StringPointerValue(stack.ID)
is.Name = types.StringValue(stack.Name)
is.Type = types.StringValue(string(stack.Type))
Expand Down Expand Up @@ -253,12 +253,12 @@ func (isc *InfrastructureStackConfiguration) HooksAttributes(ctx context.Context
return result
}

func (isc *InfrastructureStackConfiguration) Attributes(ctx context.Context, d diag.Diagnostics) gqlclient.StackConfigurationAttributes {
func (isc *InfrastructureStackConfiguration) Attributes(ctx context.Context, d diag.Diagnostics) *gqlclient.StackConfigurationAttributes {
if isc == nil {
return gqlclient.StackConfigurationAttributes{}
return nil
}

return gqlclient.StackConfigurationAttributes{
return &gqlclient.StackConfigurationAttributes{
Image: isc.Image.ValueStringPointer(),
Version: isc.Version.ValueStringPointer(),
Hooks: isc.HooksAttributes(ctx, d),
Expand Down
36 changes: 20 additions & 16 deletions internal/resource/provider_model.go → internal/model/provider.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
package resource
package model

import (
"github.com/hashicorp/terraform-plugin-framework/types"
console "github.com/pluralsh/console/go/client"
)

type provider struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Namespace types.String `tfsdk:"namespace"`
Editable types.Bool `tfsdk:"editable"`
Cloud types.String `tfsdk:"cloud"`
CloudSettings ProviderCloudSettings `tfsdk:"cloud_settings"`
type Provider struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Namespace types.String `tfsdk:"namespace"`
Editable types.Bool `tfsdk:"editable"`
Cloud types.String `tfsdk:"cloud"`
}

func (p *provider) From(cp *console.ClusterProviderFragment) {
func (p *Provider) From(cp *console.ClusterProviderFragment) {
p.Id = types.StringValue(cp.ID)
p.Name = types.StringValue(cp.Name)
p.Namespace = types.StringValue(cp.Namespace)
p.Editable = types.BoolPointerValue(cp.Editable)
p.Cloud = types.StringValue(cp.Cloud)
}

func (p *provider) Attributes() console.ClusterProviderAttributes {
type ProviderExtended struct {
Provider
CloudSettings ProviderCloudSettings `tfsdk:"cloud_settings"`
}

func (pe *ProviderExtended) Attributes() console.ClusterProviderAttributes {
return console.ClusterProviderAttributes{
Name: p.Name.ValueString(),
Namespace: p.Namespace.ValueStringPointer(),
Cloud: p.Cloud.ValueStringPointer(),
CloudSettings: p.CloudSettings.Attributes(),
Name: pe.Name.ValueString(),
Namespace: pe.Namespace.ValueStringPointer(),
Cloud: pe.Cloud.ValueStringPointer(),
CloudSettings: pe.CloudSettings.Attributes(),
}
}

func (p *provider) UpdateAttributes() console.ClusterProviderUpdateAttributes {
func (pe *ProviderExtended) UpdateAttributes() console.ClusterProviderUpdateAttributes {
return console.ClusterProviderUpdateAttributes{
CloudSettings: p.CloudSettings.Attributes(),
CloudSettings: pe.CloudSettings.Attributes(),
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource
package model

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion internal/resource/cluster_kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (k *KubeConfig) ToRESTMapper() (meta.RESTMapper, error) {
return nil, err
}

return restmapper.NewShortcutExpander(restmapper.NewDeferredDiscoveryRESTMapper(client), client), nil
return restmapper.NewShortcutExpander(restmapper.NewDeferredDiscoveryRESTMapper(client), client, nil), nil
}

func (k *KubeConfig) ToRawKubeConfigLoader() clientcmd.ClientConfig {
Expand Down
9 changes: 5 additions & 4 deletions internal/resource/custom_stack_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"terraform-provider-plural/internal/client"
"terraform-provider-plural/internal/common"
"terraform-provider-plural/internal/model"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -50,7 +51,7 @@ func (r *CustomStackRunResource) Configure(_ context.Context, req resource.Confi
}

func (r *CustomStackRunResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
data := new(customStackRun)
data := new(model.CustomStackRun)
resp.Diagnostics.Append(req.Plan.Get(ctx, data)...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -72,7 +73,7 @@ func (r *CustomStackRunResource) Create(ctx context.Context, req resource.Create
}

func (r *CustomStackRunResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
data := new(customStackRun)
data := new(model.CustomStackRun)
resp.Diagnostics.Append(req.State.Get(ctx, data)...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -89,7 +90,7 @@ func (r *CustomStackRunResource) Read(ctx context.Context, req resource.ReadRequ
}

func (r *CustomStackRunResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
data := new(customStackRun)
data := new(model.CustomStackRun)
resp.Diagnostics.Append(req.Plan.Get(ctx, data)...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -110,7 +111,7 @@ func (r *CustomStackRunResource) Update(ctx context.Context, req resource.Update
}

func (r *CustomStackRunResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
data := new(customStackRun)
data := new(model.CustomStackRun)
resp.Diagnostics.Append(req.State.Get(ctx, data)...)
if resp.Diagnostics.HasError() {
return
Expand Down
Loading

0 comments on commit 064ae34

Please sign in to comment.