Skip to content

Commit

Permalink
embed provider and git repository types
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Aug 8, 2024
1 parent f9364f4 commit 6836b63
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 52 deletions.
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.

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
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
10 changes: 6 additions & 4 deletions internal/resource/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"terraform-provider-plural/internal/model"

"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -196,7 +198,7 @@ func (r *providerResource) Configure(_ context.Context, req resource.ConfigureRe
}

func (r *providerResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data provider
var data model.ProviderExtended
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -213,7 +215,7 @@ func (r *providerResource) Create(ctx context.Context, req resource.CreateReques
}

func (r *providerResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data provider
var data model.ProviderExtended
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -234,7 +236,7 @@ func (r *providerResource) Read(ctx context.Context, req resource.ReadRequest, r
}

func (r *providerResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data provider
var data model.ProviderExtended
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -250,7 +252,7 @@ func (r *providerResource) Update(ctx context.Context, req resource.UpdateReques
}

func (r *providerResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data provider
var data model.ProviderExtended
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
Expand Down

0 comments on commit 6836b63

Please sign in to comment.