From 439feec67de52bdfc0ca1127cbbdf6021731acb1 Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Mon, 23 Jun 2025 15:23:29 +0100 Subject: [PATCH 1/2] feat: add a data source for current IP --- internal/provider/provider.go | 2 + internal/service/myip/data_source_my_ip.go | 68 ++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 internal/service/myip/data_source_my_ip.go diff --git a/internal/provider/provider.go b/internal/provider/provider.go index c05a5cec1e..14b8ba7bf5 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -7,6 +7,7 @@ import ( "regexp" "time" + "github.com/mongodb/terraform-provider-mongodbatlas/internal/service/myip" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" @@ -429,6 +430,7 @@ func (p *MongodbtlasProvider) DataSources(context.Context) []func() datasource.D alertconfiguration.DataSource, alertconfiguration.PluralDataSource, projectipaccesslist.DataSource, + myip.DataSource, atlasuser.DataSource, atlasuser.PluralDataSource, searchdeployment.DataSource, diff --git a/internal/service/myip/data_source_my_ip.go b/internal/service/myip/data_source_my_ip.go new file mode 100644 index 0000000000..5440890a61 --- /dev/null +++ b/internal/service/myip/data_source_my_ip.go @@ -0,0 +1,68 @@ +package myip + +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" +) + +const ( + myIP = "my_ip" +) + +type ds struct { + config.DSCommon +} + +func DataSource() datasource.DataSource { + return &ds{ + DSCommon: config.DSCommon{ + DataSourceName: myIP, + }, + } +} + +var _ datasource.DataSource = &ds{} +var _ datasource.DataSourceWithConfigure = &ds{} + +type Model struct { + IPAddress types.String `tfsdk:"ip_address"` +} + +func (d *ds) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + MarkdownDescription: "My IP", + Attributes: map[string]schema.Attribute{ + "ip_address": schema.StringAttribute{ + MarkdownDescription: "The IP.", + Computed: true, + }, + }, + } +} + +func (d *ds) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var databaseDSUserConfig *Model + var err error + resp.Diagnostics.Append(req.Config.Get(ctx, &databaseDSUserConfig)...) + if resp.Diagnostics.HasError() { + return + } + + info, _, err := d.Client.Atlas.IPInfo.Get(ctx) + if err != nil { + resp.Diagnostics.AddError("error getting access list entry", err.Error()) + return + } + + accessListEntry := &Model{ + IPAddress: types.StringValue(info.CurrentIPv4Address), + } + resp.Diagnostics.Append(resp.State.Set(ctx, &accessListEntry)...) + if resp.Diagnostics.HasError() { + return + } +} From 1c8989a4081dc48383839d95b67cd7e5b3d8e49f Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Mon, 23 Jun 2025 15:25:22 +0100 Subject: [PATCH 2/2] Update provider.go --- internal/provider/provider.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 14b8ba7bf5..428ebfdf5a 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -7,7 +7,6 @@ import ( "regexp" "time" - "github.com/mongodb/terraform-provider-mongodbatlas/internal/service/myip" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" @@ -22,6 +21,7 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" "github.com/hashicorp/terraform-plugin-mux/tf5to6server" "github.com/hashicorp/terraform-plugin-mux/tf6muxserver" + "github.com/mongodb/terraform-provider-mongodbatlas/internal/service/myip" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"