Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
49 changes: 49 additions & 0 deletions docs/data-sources/automation_templates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackit_automation_templates Data Source - stackit"
subcategory: ""
description: |-
Automation templates datasource schema.
~> This datasource is in beta and may be subject to breaking changes in the future. Use with caution. See our guide https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources for how to opt-in to use beta resources.
---

# stackit_automation_templates (Data Source)

Automation templates datasource schema.

~> This datasource is in beta and may be subject to breaking changes in the future. Use with caution. See our [guide](https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources) for how to opt-in to use beta resources.

## Example Usage

```terraform
data "stackit_automation_templates" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
# region is taken from the provider configuration
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `project_id` (String) STACKIT project ID.

### Optional

- `region` (String) Automation templates data source region. If undefined, the provider region is used.

### Read-Only

- `id` (String) Terraform's internal data source ID, structured as "`project_id`,`region`".
- `templates` (Attributes List) List of available templates. (see [below for nested schema](#nestedatt--templates))

<a id="nestedatt--templates"></a>
### Nested Schema for `templates`

Read-Only:

- `create_time` (String) Create timestamp of the template.
- `description` (String) Description of the template.
- `id` (String) Template ID.
- `name` (String) Name of the template.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ See this [example](https://professional-service.git.onstackit.cloud/professional
- `alb_custom_endpoint` (String) Custom endpoint for the Application Load Balancer service
- `alb_waf_custom_endpoint` (String) Custom endpoint for the Application Load Balancer Web Application Firewall service
- `authorization_custom_endpoint` (String) Custom endpoint for the Membership service
- `automation_custom_endpoint` (String) Custom endpoint for the Automation service
- `cdn_custom_endpoint` (String) Custom endpoint for the CDN service
- `credentials_path` (String) Path of JSON from where the credentials are read. Takes precedence over the env var `STACKIT_CREDENTIALS_PATH`. Default value is `~/.stackit/credentials.json`.
- `default_region` (String) Region will be used as the default location for regional services. Not all services require a region, some are global
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "stackit_automation_templates" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
# region is taken from the provider configuration
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/stackitcloud/stackit-sdk-go/core v0.26.0
github.com/stackitcloud/stackit-sdk-go/services/alb v0.17.1
github.com/stackitcloud/stackit-sdk-go/services/albwaf v0.13.2
github.com/stackitcloud/stackit-sdk-go/services/automation v0.1.0
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.20.0
github.com/stackitcloud/stackit-sdk-go/services/certificates v1.9.1
github.com/stackitcloud/stackit-sdk-go/services/dns v0.22.1
Expand Down Expand Up @@ -59,6 +60,7 @@ require (
require (
github.com/hashicorp/go-retryablehttp v0.7.8 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/stackitcloud/stackit-sdk-go/experimental v0.1.0 // indirect
github.com/stretchr/testify v1.11.1 // indirect
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,16 @@ github.com/skeema/knownhosts v1.3.1 h1:X2osQ+RAjK76shCbvhHHHVl3ZlgDm8apHEHFqRjnB
github.com/skeema/knownhosts v1.3.1/go.mod h1:r7KTdC8l4uxWRyK2TpQZ/1o5HaSzh06ePQNxPwTcfiY=
github.com/stackitcloud/stackit-sdk-go/core v0.26.0 h1:jQEb9gkehfp6VCP6TcYk7BI10cz4l0KM2L6hqYBH2QA=
github.com/stackitcloud/stackit-sdk-go/core v0.26.0/go.mod h1:WU1hhxnjXw2EV7CYa1nlEvNpMiRY6CvmIOaHuL3pOaA=
github.com/stackitcloud/stackit-sdk-go/experimental v0.1.0 h1:VzFEk54aKXTYCxx4P0g5iviGQ+1LJvpnVeHfhunNjLQ=
github.com/stackitcloud/stackit-sdk-go/experimental v0.1.0/go.mod h1:P565RBOFE21j+J2vLxdw4qaJcJC2p0BH+Qqqp4E7i7I=
github.com/stackitcloud/stackit-sdk-go/services/alb v0.17.1 h1:YOMgUO/0/FZPxOmkKIbEIjzpzJ2VFhfPn3ErID9qC40=
github.com/stackitcloud/stackit-sdk-go/services/alb v0.17.1/go.mod h1:eK6oRB5Tmpt6KbXQ4UYBGg2LgW5bPtVoncL9E8JSRww=
github.com/stackitcloud/stackit-sdk-go/services/albwaf v0.13.2 h1:zY0ArhTg++8oOAlytCRck3u41ZakAvU74ZkwBuMnipg=
github.com/stackitcloud/stackit-sdk-go/services/albwaf v0.13.2/go.mod h1:4M9G1I64kZwlXO32ZoIpt0GAN4SpZ1SYerwCVVIBGoE=
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.15.3 h1:YEuymF48gnBNqBsraBN5pycBDEyXcFYy/2bCdF+H3eY=
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.15.3/go.mod h1:T/JF25XGJ3GqER/1L2N//DgY8x5tY7gA3N+/0nvmOWY=
github.com/stackitcloud/stackit-sdk-go/services/automation v0.1.0 h1:25T6OXF+rahWSw+Hhc97CnIkFPzI4yge0fgR5lx4/pk=
github.com/stackitcloud/stackit-sdk-go/services/automation v0.1.0/go.mod h1:vLus9q8y75L+viMJej92c5gF9BD8IDVzm5Q/ae9XX1c=
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.20.0 h1:p/8f/+Z+OtqX0cau2CqkidjfO3FkHPc7ymdrF1zv7UY=
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.20.0/go.mod h1:MHB1N3EQ9GuAduAQoNS+gb1MjrWJieszbpOso9TQv5s=
github.com/stackitcloud/stackit-sdk-go/services/certificates v1.9.1 h1:tf9+nPZ/1Wq7jRAnOzvgBTAS038GlX4ws5Ndw1LdCsc=
Expand Down
1 change: 1 addition & 0 deletions stackit/internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type ProviderData struct {
ALBCustomEndpoint string
AlbWafCustomEndpoint string
AuthorizationCustomEndpoint string
AutomationCustomEndpoint string
CdnCustomEndpoint string
DnsCustomEndpoint string
DremioCustomEndpoint string
Expand Down
37 changes: 37 additions & 0 deletions stackit/internal/services/automation/automation_acc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package automation_test

import (
_ "embed"
"testing"

"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil"
)

//go:embed testdata/datasource-templates.tf
var templatesDataSourceConfig string

func TestAccAutomationTemplatesDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
ConfigVariables: config.Variables{
"project_id": config.StringVariable(testutil.ProjectId),
},
Config: testutil.NewConfigBuilder().Region(testutil.Region).EnableBetaResources(true).BuildProviderConfig() + "\n" + templatesDataSourceConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.stackit_automation_templates.templates", "project_id", testutil.ProjectId),
resource.TestCheckResourceAttr("data.stackit_automation_templates.templates", "region", testutil.Region),
resource.TestCheckResourceAttrSet("data.stackit_automation_templates.templates", "id"),
resource.TestCheckResourceAttrSet("data.stackit_automation_templates.templates", "templates.0.id"),
resource.TestCheckResourceAttrSet("data.stackit_automation_templates.templates", "templates.0.name"),
resource.TestCheckResourceAttrSet("data.stackit_automation_templates.templates", "templates.0.description"),
resource.TestCheckResourceAttrSet("data.stackit_automation_templates.templates", "templates.0.create_time"),
),
},
},
})
}
198 changes: 198 additions & 0 deletions stackit/internal/services/automation/templates/datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package templates

import (
"context"
"fmt"
"slices"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/experimental/paginate"
automation "github.com/stackitcloud/stackit-sdk-go/services/automation/v1betaapi"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features"
automationUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/automation/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
)

var (
_ datasource.DataSource = new(templatesDataSource)
_ datasource.DataSourceWithConfigure = new(templatesDataSource)
)

type model struct {
ID types.String `tfsdk:"id"`
ProjectId types.String `tfsdk:"project_id"`
Region types.String `tfsdk:"region"`
Templates []template `tfsdk:"templates"`
}

type template struct {
Id types.String `tfsdk:"id"`
CreateTime types.String `tfsdk:"create_time"`
Description types.String `tfsdk:"description"`
Name types.String `tfsdk:"name"`
}

type templatesDataSource struct {
client *automation.APIClient
providerData core.ProviderData
}

func NewAutomationTemplatesDataSource() datasource.DataSource {
return new(templatesDataSource)
}

func (d *templatesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_automation_templates"
}

func (d *templatesDataSource) 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
}
features.CheckBetaResourcesEnabled(ctx, &d.providerData, &resp.Diagnostics, "stackit_automation_templates", core.Datasource)
if resp.Diagnostics.HasError() {
return
}

apiClient := automationUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
d.client = apiClient
tflog.Info(ctx, "Automation templates client configured")
}

func (d *templatesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: features.AddBetaDescription("Automation templates datasource schema.", core.Datasource),
Description: "Automation templates datasource schema.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "Terraform's internal data source ID, structured as \"`project_id`,`region`\".",
Computed: true,
},
"project_id": schema.StringAttribute{
Description: "STACKIT project ID.",
Required: true,
Validators: []validator.String{
validate.UUID(),
validate.NoSeparator(),
},
},
"region": schema.StringAttribute{
Description: "Automation templates data source region. If undefined, the provider region is used.",
Optional: true,
Computed: true,
},
"templates": schema.ListNestedAttribute{
Description: "List of available templates.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "Template ID.",
Computed: true,
},
"name": schema.StringAttribute{
Description: "Name of the template.",
Computed: true,
},
"description": schema.StringAttribute{
Description: "Description of the template.",
Computed: true,
},
"create_time": schema.StringAttribute{
Description: "Create timestamp of the template.",
Computed: true,
},
},
},
},
},
}
}

func (d *templatesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
var model model
resp.Diagnostics.Append(req.Config.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}

projectId := model.ProjectId.ValueString()
region := d.providerData.GetRegionWithOverride(model.Region)
model.Region = types.StringValue(region)
ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
"project_id": projectId,
"region": region,
})
if resp.Diagnostics.HasError() {
return
}

ctx = core.InitProviderContext(ctx)

templatesReq := d.client.DefaultAPI.ListVolumeTemplates(ctx, projectId, region)
templates, err := paginate.All(templatesReq)

if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Reading templates", fmt.Sprintf("Calling ListVolumeTemplates: %v", err))
return
}
if templates == nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Reading templates", "ListVolumeTemplates returned an empty response")
return
}

ctx = core.LogResponse(ctx)

if err := mapFields(templates, &model); err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Reading templates", fmt.Sprintf("Processing API payload: %v", err))
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, model)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "Automation templates read")
}

func mapFields(templates []automation.Template, m *model) error {
if m == nil {
return fmt.Errorf("nil model")
}

m.ID = utils.BuildInternalTerraformId(m.ProjectId.ValueString(), m.Region.ValueString())

if templates == nil {
m.Templates = nil
} else {
m.Templates = make([]template, 0, len(templates))
slices.SortFunc(templates, func(a, b automation.Template) int {
return strings.Compare(a.Id, b.Id)
})

for _, respTemplate := range templates {
m.Templates = append(m.Templates, template{
Id: types.StringValue(respTemplate.Id),
Name: types.StringValue(respTemplate.Name),
Description: types.StringValue(respTemplate.Description),
CreateTime: types.StringValue(respTemplate.CreateTime.Format(time.RFC3339)),
})
}
}
return nil
}
Loading
Loading