Skip to content

Commit

Permalink
feat: csp tag datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
wai-wong-edb committed Aug 19, 2024
1 parent 01f80c7 commit f9a0089
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 25 deletions.
24 changes: 0 additions & 24 deletions pkg/provider/data_source_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,10 @@ var (
_ datasource.DataSourceWithConfigure = &clusterDataSource{}
)

type PgConfigDatasourceModel struct {
Value types.String `tfsdk:"value"`
Name types.String `tfsdk:"name"`
}

type StorageDatasourceModel struct {
Throughput types.String `tfsdk:"throughput"`
VolumeProperties types.String `tfsdk:"volume_properties"`
VolumeType types.String `tfsdk:"volume_type"`
Iops types.String `tfsdk:"iops"`
Size types.String `tfsdk:"size"`
}

type ClusterArchitectureDatasourceModel struct {
Nodes types.Int64 `tfsdk:"nodes"`
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}

type clusterDatasourceModel struct {
ClusterResourceModel
}

type AllowedIpRangesDatasourceModel struct {
CidrBlock types.String `tfsdk:"cidr_block"`
Description types.String `tfsdk:"description"`
}

type clusterDataSource struct {
client *api.ClusterClient
}
Expand Down
138 changes: 138 additions & 0 deletions pkg/provider/data_source_csp_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package provider

import (
"context"

"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/api"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var (
_ datasource.DataSource = &cSPTagDataSource{}
_ datasource.DataSourceWithConfigure = &cSPTagDataSource{}
)

type cSPTagDatasourceModel struct {
CSPTagResourceModel
}

type cSPTagDataSource struct {
client *api.CSPTagClient
}

func (c *cSPTagDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_csp_tag"
}

// Configure adds the provider configured client to the data source.
func (c *cSPTagDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

c.client = req.ProviderData.(*api.API).CSPTagClient()
}

func (c *cSPTagDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "CSP Tags will enable users to categorize and organize resources across types and improve the efficiency of resource retrieval",
// using Blocks for backward compatible
Blocks: map[string]schema.Block{
"timeouts": timeouts.Block(ctx,
timeouts.Opts{Create: true, Delete: true, Update: true},
),
},
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"project_id": schema.StringAttribute{
Required: true,
},
"cloud_provider_id": schema.StringAttribute{
Required: true,
},
"add_tags": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"csp_tag_key": schema.StringAttribute{
Computed: true,
},
"csp_tag_value": schema.StringAttribute{
Computed: true,
},
},
},
},
"delete_tags": schema.ListAttribute{
Computed: true,
ElementType: types.StringType,
},
"edit_tags": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"csp_tag_id": schema.StringAttribute{
Computed: true,
},
"csp_tag_key": schema.StringAttribute{
Computed: true,
},
"csp_tag_value": schema.StringAttribute{
Computed: true,
},
"status": schema.StringAttribute{
Computed: true,
},
},
},
},
"csp_tags": schema.ListNestedAttribute{
Description: "CSP Tags on cluster",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"csp_tag_id": schema.StringAttribute{
Computed: true,
},
"csp_tag_key": schema.StringAttribute{
Computed: true,
},
"csp_tag_value": schema.StringAttribute{
Computed: true,
},
"status": schema.StringAttribute{
Computed: true,
},
},
},
},
},
}
}

func (c *cSPTagDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data cSPTagDatasourceModel
diags := req.Config.Get(ctx, &data.CSPTagResourceModel)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

if err := readCSPTag(ctx, c.client, &data.CSPTagResourceModel); err != nil {
if !appendDiagFromBAErr(err, &resp.Diagnostics) {
resp.Diagnostics.AddError("Error reading cluster", err.Error())
}
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, data.CSPTagResourceModel)...)
}

func NewCSPTagDataSource() datasource.DataSource {
return &cSPTagDataSource{}
}
1 change: 1 addition & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (b bigAnimalProvider) DataSources(ctx context.Context) []func() datasource.
NewRegionsDataSource,
NewAnalyticsClusterDataSource,
NewTagDataSource,
NewCSPTagDataSource,
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/resource_csp_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (tr *cSPTagResource) Metadata(ctx context.Context, req resource.MetadataReq

func (tf *cSPTagResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Tags will enable users to categorize and organize resources across types and improve the efficiency of resource retrieval",
MarkdownDescription: "CSP Tags will enable users to categorize and organize resources across types and improve the efficiency of resource retrieval",
Blocks: map[string]schema.Block{
"timeouts": timeouts.Block(ctx,
timeouts.Opts{Create: true, Delete: true, Update: true}),
Expand Down

0 comments on commit f9a0089

Please sign in to comment.