## Description
<!-- **Please link some issue here describing what you are trying to achieve.**
In case there is no issue present for your PR, please consider creating one.
At least please give us some description what you are trying to achieve and why your change is needed. -->
relates to #1234
## Checklist
- [ ] Issue was linked above
- [ ] Code format was applied: `make fmt`
- [ ] Examples were added / adjusted (see `examples/` directory)
- [x] Docs are up-to-date: `make generate-docs` (will be checked by CI)
- [ ] Unit tests got implemented or updated
- [ ] Acceptance tests got implemented or updated (see e.g. [here](f5f99d1709/stackit/internal/services/dns/dns_acc_test.go))
- [x] Unit tests are passing: `make test` (will be checked by CI)
- [x] No linter issues: `make lint` (will be checked by CI)
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Co-authored-by: marcel.henselin <marcel.henselin@stackit.cloud>
Reviewed-on: #81
148 lines
4.9 KiB
Go Template
148 lines
4.9 KiB
Go Template
package {{.PackageName}}
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"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"
|
|
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
|
"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"
|
|
|
|
{{.PackageName}}Pkg "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/{{.PackageName}}"
|
|
|
|
{{.PackageName}}Gen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/{{.PackageName}}/{{.NameSnake}}/datasources_gen"
|
|
)
|
|
|
|
var _ datasource.DataSource = (*{{.NameCamel}}DataSource)(nil)
|
|
|
|
const errorPrefix = "[{{.PackageNamePascal}} - {{.NamePascal}}]"
|
|
|
|
func New{{.NamePascal}}DataSource() datasource.DataSource {
|
|
return &{{.NameCamel}}DataSource{}
|
|
}
|
|
|
|
type dsModel struct {
|
|
{{.PackageName}}Gen.{{.NamePascal}}Model
|
|
TfId types.String `tfsdk:"id"`
|
|
}
|
|
|
|
type {{.NameCamel}}DataSource struct{
|
|
client *{{.PackageName}}Pkg.APIClient
|
|
providerData core.ProviderData
|
|
}
|
|
|
|
func (d *{{.NameCamel}}DataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_{{.PackageName}}_{{.NameSnake}}"
|
|
}
|
|
|
|
func (d *{{.NameCamel}}DataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
|
resp.Schema = {{.PackageName}}Gen.{{.NamePascal}}DataSourceSchema(ctx)
|
|
resp.Schema.Attributes["id"] = schema.StringAttribute{
|
|
Computed: true,
|
|
Description: "The terraform internal identifier.",
|
|
MarkdownDescription: "The terraform internal identifier.",
|
|
}
|
|
}
|
|
|
|
// Configure adds the provider configured client to the data source.
|
|
func (d *{{.NameCamel}}DataSource) 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
|
|
}
|
|
|
|
apiClientConfigOptions := []config.ConfigurationOption{
|
|
config.WithCustomAuth(d.providerData.RoundTripper),
|
|
utils.UserAgentConfigOption(d.providerData.Version),
|
|
}
|
|
if d.providerData.{{.PackageNamePascal}}CustomEndpoint != "" {
|
|
apiClientConfigOptions = append(
|
|
apiClientConfigOptions,
|
|
config.WithEndpoint(d.providerData.{{.PackageNamePascal}}CustomEndpoint),
|
|
)
|
|
} else {
|
|
apiClientConfigOptions = append(
|
|
apiClientConfigOptions,
|
|
config.WithRegion(d.providerData.GetRegion()),
|
|
)
|
|
}
|
|
apiClient, err := {{.PackageName}}Pkg.NewAPIClient(apiClientConfigOptions...)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError(
|
|
"Error configuring API client",
|
|
fmt.Sprintf(
|
|
"Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration",
|
|
err,
|
|
),
|
|
)
|
|
return
|
|
}
|
|
d.client = apiClient
|
|
tflog.Info(ctx, fmt.Sprintf("%s client configured", errorPrefix))
|
|
}
|
|
|
|
func (d *{{.NameCamel}}DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
|
var data dsModel
|
|
|
|
// 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)
|
|
{{.NameCamel}}Id := data.{{.NamePascal}}Id.ValueString()
|
|
|
|
ctx = tflog.SetField(ctx, "project_id", projectId)
|
|
ctx = tflog.SetField(ctx, "region", region)
|
|
|
|
// TODO: implement needed fields
|
|
ctx = tflog.SetField(ctx, "{{.NameCamel}}_id", {{.NameCamel}}Id)
|
|
|
|
// TODO: refactor to correct implementation
|
|
{{.NameCamel}}Resp, err := d.client.Get{{.NamePascal}}Request(ctx, projectId, region, {{.NameCamel}}Id).Execute()
|
|
if err != nil {
|
|
utils.LogError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
err,
|
|
"Reading {{.NameCamel}}",
|
|
fmt.Sprintf("{{.NameCamel}} with ID %q does not exist in project %q.", {{.NameCamel}}Id, 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)
|
|
|
|
|
|
data.TfId = utils.BuildInternalTerraformId(projectId, region, ..)
|
|
|
|
// TODO: fill remaining fields
|
|
{{- range .Fields }}
|
|
// data.{{.}} = types.Sometype(apiResponse.Get{{.}}())
|
|
{{- end -}}
|
|
|
|
// Save data into Terraform state
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
|
|
tflog.Info(ctx, fmt.Sprintf("%s read successful", errorPrefix))
|
|
}
|