diff --git a/pkg/models/common/api/tag.go b/pkg/models/common/api/tag.go index e87777fe..64ab4c50 100644 --- a/pkg/models/common/api/tag.go +++ b/pkg/models/common/api/tag.go @@ -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"` } diff --git a/pkg/models/region.go b/pkg/models/region.go index 78749384..5b479c1b 100644 --- a/pkg/models/region.go +++ b/pkg/models/region.go @@ -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 { diff --git a/pkg/provider/data_source_projects.go b/pkg/provider/data_source_projects.go index d58cc50b..254a717b 100644 --- a/pkg/provider/data_source_projects.go +++ b/pkg/provider/data_source_projects.go @@ -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" ) @@ -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. @@ -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) { @@ -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, @@ -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, + }, + }, + }, + }, }, }, }, @@ -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 { diff --git a/pkg/provider/data_source_region.go b/pkg/provider/data_source_region.go index b247ac58..9d7f4edb 100644 --- a/pkg/provider/data_source_region.go +++ b/pkg/provider/data_source_region.go @@ -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, + }, + }, + }, + }, }, }, }, @@ -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, - }, - }, - }, - }, }, } }