Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: region and project data source #601

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/models/common/api/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

// mapstructure annotations are used by faraway away replica only and should be removed once we migrate faraway replica resouce to terraform plugin framework
type Tag struct {
Color *string `json:"color,omitempty" mapstructure:"color"`
TagId string `json:"tagId" mapstructure:"tag_id"`
TagName string `json:"tagName" mapstructure:"tag_name"`
Color *string `json:"color,omitempty" tfsdk:"color" mapstructure:"color"`
TagId string `json:"tagId" tfsdk:"tag_id" mapstructure:"tag_id"`
TagName string `json:"tagName" tfsdk:"tag_name" mapstructure:"tag_name"`
}
2 changes: 1 addition & 1 deletion pkg/models/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Region struct {
Name string `json:"regionName,omitempty" tfsdk:"name"`
Status string `json:"status,omitempty" tfsdk:"status"`
Continent string `json:"continent,omitempty" tfsdk:"continent"`
Tags []commonApi.Tag `json:"tags,omitempty"`
Tags []commonApi.Tag `json:"tags,omitempty" tfsdk:"tags"`
}

func (r Region) String() string {
Expand Down
65 changes: 58 additions & 7 deletions pkg/provider/data_source_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"time"

"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/api"
"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models"
commonTerraform "github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models/common/terraform"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

Expand All @@ -24,7 +25,6 @@ type projectsDataSource struct {

func (p projectsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_projects"

}

// Configure adds the provider configured client to the data source.
Expand All @@ -37,9 +37,9 @@ func (p *projectsDataSource) Configure(_ context.Context, req datasource.Configu
}

type projectsDataSourceData struct {
ID *string `tfsdk:"id"`
Query *string `tfsdk:"query"`
Projects []*models.Project `tfsdk:"projects"`
ID *string `tfsdk:"id"`
Query *string `tfsdk:"query"`
Projects []*Project `tfsdk:"projects"`
}

func (p projectsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
Expand All @@ -55,6 +55,10 @@ func (p projectsDataSource) Schema(ctx context.Context, req datasource.SchemaReq
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "Resource ID of the project.",
Computed: true,
},
"project_id": schema.StringAttribute{
Description: "Project ID of the project.",
Computed: true,
Expand Down Expand Up @@ -88,6 +92,24 @@ func (p projectsDataSource) Schema(ctx context.Context, req datasource.SchemaReq
},
},
},
"tags": schema.SetNestedAttribute{
Description: "Show existing tags associated with this resource",
Optional: true,
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"tag_id": schema.StringAttribute{
Computed: true,
},
"tag_name": schema.StringAttribute{
Computed: true,
},
"color": schema.StringAttribute{
Computed: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -118,12 +140,41 @@ func (p projectsDataSource) Read(ctx context.Context, req datasource.ReadRequest
return
}

data.Projects = list
for _, project := range list {
appendProj := &Project{
ID: &project.ProjectId,
ProjectID: &project.ProjectId,
ProjectName: &project.ProjectName,
UserCount: &project.UserCount,
ClusterCount: &project.ClusterCount,
}

cloudProviders := []cloudProvider{}
for _, cp := range project.CloudProviders {
cloudProviders = append(cloudProviders, cloudProvider{
CloudProviderId: cp.CloudProviderId,
CloudProviderName: cp.CloudProviderName,
})
}
appendProj.CloudProviders = cloudProviders

tags := []commonTerraform.Tag{}
for _, tag := range project.Tags {
tags = append(tags, commonTerraform.Tag{
TagId: types.StringValue(tag.TagId),
TagName: types.StringValue(tag.TagName),
Color: types.StringPointerValue(tag.Color),
})
}
appendProj.Tags = tags

data.Projects = append(data.Projects, appendProj)
}

resourceID := strconv.FormatInt(time.Now().Unix(), 10)
data.ID = &resourceID
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)

}

func NewProjectsDataSource() datasource.DataSource {
Expand Down
34 changes: 17 additions & 17 deletions pkg/provider/data_source_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ func (r *regionsDataSource) Schema(ctx context.Context, req datasource.SchemaReq
Description: "Continent that region belongs to.",
Computed: true,
},
"tags": schema.SetNestedAttribute{
Description: "show tags associated with this resource",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"tag_id": schema.StringAttribute{
Computed: true,
},
"tag_name": schema.StringAttribute{
Computed: true,
},
"color": schema.StringAttribute{
Computed: true,
},
},
},
},
},
},
},
Expand All @@ -90,23 +107,6 @@ func (r *regionsDataSource) Schema(ctx context.Context, req datasource.SchemaReq
Description: "Unique region ID. For example, \"germanywestcentral\" in the Azure cloud provider, \"eu-west-1\" in the AWS cloud provider.",
Optional: true,
},
"tags": schema.SetNestedAttribute{
Description: "show tags associated with this resource",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"tag_id": schema.StringAttribute{
Computed: true,
},
"tag_name": schema.StringAttribute{
Computed: true,
},
"color": schema.StringAttribute{
Computed: true,
},
},
},
},
},
}
}
Expand Down
Loading