Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"regexp"
"time"

"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/myip"

Check failure on line 10 in internal/provider/provider.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (gofmt)
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
Expand Down Expand Up @@ -429,6 +430,7 @@
alertconfiguration.DataSource,
alertconfiguration.PluralDataSource,
projectipaccesslist.DataSource,
myip.DataSource,
atlasuser.DataSource,
atlasuser.PluralDataSource,
searchdeployment.DataSource,
Expand Down
68 changes: 68 additions & 0 deletions internal/service/myip/data_source_my_ip.go
Original file line number Diff line number Diff line change
@@ -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())
Copy link
Preview

Copilot AI Jun 23, 2025

Choose a reason for hiding this comment

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

The error message refers to an "access list entry" but this data source is fetching the current IP; update it to something like "error retrieving current IP address".

Suggested change
resp.Diagnostics.AddError("error getting access list entry", err.Error())
resp.Diagnostics.AddError("error retrieving current IP address", err.Error())

Copilot uses AI. Check for mistakes.

return
}

accessListEntry := &Model{
IPAddress: types.StringValue(info.CurrentIPv4Address),
}
resp.Diagnostics.Append(resp.State.Set(ctx, &accessListEntry)...)
Copy link
Preview

Copilot AI Jun 23, 2025

Choose a reason for hiding this comment

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

Passing &accessListEntry creates a double pointer (**Model) which will cause a type mismatch; pass accessListEntry directly to resp.State.Set.

Suggested change
resp.Diagnostics.Append(resp.State.Set(ctx, &accessListEntry)...)
resp.Diagnostics.Append(resp.State.Set(ctx, accessListEntry)...)

Copilot uses AI. Check for mistakes.

if resp.Diagnostics.HasError() {
return
}
}
Loading