Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 4s
CI Workflow / CI (pull_request) Failing after 13m53s
CI Workflow / Code coverage report (pull_request) Has been skipped
CI Workflow / Test readiness for publishing provider (pull_request) Successful in 16m47s
118 lines
3.7 KiB
Go
118 lines
3.7 KiB
Go
package sqlserverflexbeta
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion"
|
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/core"
|
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
|
|
|
sqlserverflexbetaPkg "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
|
|
|
sqlserverflexbetaUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/utils"
|
|
|
|
sqlserverflexbetaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/user/datasources_gen"
|
|
)
|
|
|
|
var _ datasource.DataSource = (*userDataSource)(nil)
|
|
|
|
const errorPrefix = "[Sqlserverflexbeta - User]"
|
|
|
|
func NewUserDataSource() datasource.DataSource {
|
|
return &userDataSource{}
|
|
}
|
|
|
|
type userDataSource struct {
|
|
client *sqlserverflexbetaPkg.APIClient
|
|
providerData core.ProviderData
|
|
}
|
|
|
|
func (d *userDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_sqlserverflexbeta_user"
|
|
}
|
|
|
|
func (d *userDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
|
resp.Schema = sqlserverflexbetaGen.UserDataSourceSchema(ctx)
|
|
}
|
|
|
|
// Configure adds the provider configured client to the data source.
|
|
func (d *userDataSource) Configure(
|
|
ctx context.Context,
|
|
req datasource.ConfigureRequest,
|
|
resp *datasource.ConfigureResponse,
|
|
) {
|
|
var ok bool
|
|
d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
apiClient := sqlserverflexbetaUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
d.client = apiClient
|
|
tflog.Info(ctx, fmt.Sprintf("%s client configured", errorPrefix))
|
|
}
|
|
|
|
func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
|
var data sqlserverflexbetaGen.UserModel
|
|
|
|
// Read Terraform configuration data into the model
|
|
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
ctx = core.InitProviderContext(ctx)
|
|
|
|
projectId := data.ProjectId.ValueString()
|
|
region := d.providerData.GetRegionWithOverride(data.Region)
|
|
userId := data.UserId.ValueString()
|
|
|
|
ctx = tflog.SetField(ctx, "project_id", projectId)
|
|
ctx = tflog.SetField(ctx, "region", region)
|
|
ctx = tflog.SetField(ctx, "user_id", userId)
|
|
|
|
userResp, err := d.client.GetUserRequest(ctx, projectId, region, userId).Execute()
|
|
if err != nil {
|
|
utils.LogError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
err,
|
|
"Reading user",
|
|
fmt.Sprintf("user with ID %q does not exist in project %q.", userId, projectId),
|
|
map[int]string{
|
|
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId),
|
|
},
|
|
)
|
|
resp.State.RemoveResource(ctx)
|
|
return
|
|
}
|
|
|
|
ctx = core.LogResponse(ctx)
|
|
|
|
// Todo: Read API call logic
|
|
|
|
// Example data value setting
|
|
// data.Id = types.StringValue("example-id")
|
|
|
|
err = mapResponseToModel(ctx, userResp, &data, resp.Diagnostics)
|
|
if err != nil {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
fmt.Sprintf("%s Read", errorPrefix),
|
|
fmt.Sprintf("Processing API payload: %v", err),
|
|
)
|
|
return
|
|
}
|
|
|
|
// Save data into Terraform state
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|