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

chore: Generate Flex Cluster data source file and schemas #2725

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
55 changes: 55 additions & 0 deletions internal/service/flexcluster/data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//nolint:gocritic
package flexcluster

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource"
// "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
// "github.com/hashicorp/terraform-plugin-framework/types"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
)

var _ datasource.DataSource = &ds{}
var _ datasource.DataSourceWithConfigure = &ds{}

func DataSource() datasource.DataSource {
return &ds{
DSCommon: config.DSCommon{
DataSourceName: resourceName,
},
}
}

type ds struct {
config.DSCommon
}

func (d *ds) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
// TODO: Schema and model must be defined in data_source_schema.go. Details on scaffolding this file found in contributing/development-best-practices.md under "Scaffolding Schema and Model Definitions"
resp.Schema = DataSourceSchema(ctx)
oarbusi marked this conversation as resolved.
Show resolved Hide resolved
}

func (d *ds) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
// var tfModel TFModel
// resp.Diagnostics.Append(req.Config.Get(ctx, &tfModel)...)
// if resp.Diagnostics.HasError() {
// return
// }

// TODO: make get request to resource

// connV2 := d.Client.AtlasV2
//if err != nil {
// resp.Diagnostics.AddError("error fetching resource", err.Error())
// return
//}

// TODO: process response into new terraform state
// newFlexClusterModel, diags := NewTFModel(ctx, apiResp)
// if diags.HasError() {
// resp.Diagnostics.Append(diags...)
// return
// }
// resp.Diagnostics.Append(resp.State.Set(ctx, newFlexClusterModel)...)
}
108 changes: 108 additions & 0 deletions internal/service/flexcluster/data_source_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package flexcluster

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func DataSourceSchema(ctx context.Context) schema.Schema {
return schema.Schema{
Attributes: dataSourceSchema(false),
}
}

func dataSourceSchema(isPlural bool) map[string]schema.Attribute {
return map[string]schema.Attribute{
"project_id": schema.StringAttribute{
Required: !isPlural,
Computed: isPlural,
oarbusi marked this conversation as resolved.
Show resolved Hide resolved
MarkdownDescription: "Unique 24-hexadecimal digit string that identifies your project. Use the [/groups](#tag/Projects/operation/listProjects) endpoint to retrieve all projects to which the authenticated user has access.\n\n**NOTE**: Groups and projects are synonymous terms. Your group id is the same as your project id. For existing groups, your group/project id remains the same. The resource and corresponding endpoints use the term groups.",
},
"name": schema.StringAttribute{
Required: !isPlural,
Computed: isPlural,
MarkdownDescription: "Human-readable label that identifies the flex cluster.",
},
"provider_settings": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"backing_provider_name": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Cloud service provider on which MongoDB Cloud provisioned the flex cluster.",
},
"disk_size_gb": schema.Float64Attribute{
Computed: true,
MarkdownDescription: "Storage capacity available to the flex cluster expressed in gigabytes.",
},
"provider_name": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Human-readable label that identifies the cloud service provider.",
},
"region_name": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Human-readable label that identifies the geographic location of your MongoDB flex cluster. The region you choose can affect network latency for clients accessing your databases. For a complete list of region names, see [AWS](https://docs.atlas.mongodb.com/reference/amazon-aws/#std-label-amazon-aws), [GCP](https://docs.atlas.mongodb.com/reference/google-gcp/), and [Azure](https://docs.atlas.mongodb.com/reference/microsoft-azure/).",
},
},
Computed: true,
MarkdownDescription: "Group of cloud provider settings that configure the provisioned MongoDB flex cluster.",
},
"tags": schema.MapAttribute{
ElementType: types.StringType,
Computed: true,
MarkdownDescription: "Map that contains key-value pairs between 1 to 255 characters in length for tagging and categorizing the instance.",
},
"backup_settings": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"enabled": schema.BoolAttribute{
Computed: true,
MarkdownDescription: "Flag that indicates whether backups are performed for this flex cluster. Backup uses [TODO](TODO) for flex clusters.",
},
},
Computed: true,
MarkdownDescription: "Flex backup configuration",
},
"cluster_type": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Flex cluster topology.",
},
"connection_strings": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"standard": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Public connection string that you can use to connect to this cluster. This connection string uses the mongodb:// protocol.",
},
"standard_srv": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Public connection string that you can use to connect to this flex cluster. This connection string uses the `mongodb+srv://` protocol.",
},
},
Computed: true,
MarkdownDescription: "Collection of Uniform Resource Locators that point to the MongoDB database.",
},
"create_date": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Date and time when MongoDB Cloud created this instance. This parameter expresses its value in ISO 8601 format in UTC.",
},
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Unique 24-hexadecimal digit string that identifies the instance.",
},
"mongo_dbversion": schema.StringAttribute{
oarbusi marked this conversation as resolved.
Show resolved Hide resolved
Computed: true,
MarkdownDescription: "Version of MongoDB that the instance runs.",
},
"state_name": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Human-readable label that indicates the current operating condition of this instance.",
},
"termination_protection_enabled": schema.BoolAttribute{
Computed: true,
MarkdownDescription: "Flag that indicates whether termination protection is enabled on the cluster. If set to `true`, MongoDB Cloud won't delete the cluster. If set to `false`, MongoDB Cloud will delete the cluster.",
},
"version_release_system": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Method by which the cluster maintains the MongoDB versions.",
},
}
}
31 changes: 31 additions & 0 deletions internal/service/flexcluster/plural_data_source_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package flexcluster

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func DataSourcePluralSchema(ctx context.Context) schema.Schema {
return schema.Schema{
Attributes: map[string]schema.Attribute{
"project_id": schema.StringAttribute{
Required: true,
MarkdownDescription: "Unique 24-hexadecimal digit string that identifies your project. Use the [/groups](#tag/Projects/operation/listProjects) endpoint to retrieve all projects to which the authenticated user has access.\n\n**NOTE**: Groups and projects are synonymous terms. Your group id is the same as your project id. For existing groups, your group/project id remains the same. The resource and corresponding endpoints use the term groups.",
},
"flex_clusters": schema.ListNestedAttribute{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"flex_clusters": schema.ListNestedAttribute{
"results": schema.ListNestedAttribute{

Would prefer to have consistency with other plural data sources in our provider, flex_clusters might also be redundant under the context of using mongodbatlas_flex_clusters data source

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that in resource policies it's like in here (resource_policies). I added a discussion topic for thursday about this so that we decide and unify the approach on all future resources

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, feel free to merge and we can adjust later if needed

Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: dataSourceSchema(true),
},
MarkdownDescription: "List of returned documents that MongoDB Cloud provides when completing this request.",
},
},
}
}

type TFModelDSP struct {
ProjectId types.String `tfsdk:"project_id"`
FlexClusters []TFModel `tfsdk:"flex_clusters"`
}
10 changes: 10 additions & 0 deletions internal/service/flexcluster/tfplugingen/generator_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ resources:
create:
path: /api/atlas/v2/groups/{groupId}/flexClusters
method: POST

data_sources:
flex_cluster:
read:
path: /api/atlas/v2/groups/{groupId}/flexClusters/{name}
method: GET
flex_clusters:
read:
path: /api/atlas/v2/groups/{groupId}/flexClusters
method: GET