-
Notifications
You must be signed in to change notification settings - Fork 77
feat(sqlserverflex): add flavors datasource #1643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cgoetz-inovex
wants to merge
2
commits into
main
Choose a base branch
from
feat/STACKITTPR-747-sqlserver-flavors-ds
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+542
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "stackit_sqlserverflex_flavors Data Source - stackit" | ||
| subcategory: "" | ||
| description: |- | ||
| SqlserverFlex flavors data source schema | ||
| --- | ||
|
|
||
| # stackit_sqlserverflex_flavors (Data Source) | ||
|
|
||
| SqlserverFlex flavors data source schema | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```terraform | ||
| data "stackit_sqlserverflex_flavors" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| // region taken from provider | ||
| } | ||
|
|
||
| // example usage with instance | ||
| resource "stackit_sqlserverflex_instance" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| name = "example" | ||
| flavor_id = one([for f in data.stackit_sqlserverflex_flavors.example.flavors : f.id if f.cpu == 4 && f.memory == 16]) | ||
| } | ||
| ``` | ||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `project_id` (String) The project ID. | ||
|
|
||
| ### Optional | ||
|
|
||
| - `region` (String) SqlserverFlex flavors data source region. If undefined the providers region is used. | ||
| - `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts)) | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `flavors` (Attributes List) A list of available flavors. (see [below for nested schema](#nestedatt--flavors)) | ||
| - `id` (String) Terraform's internal data source ID, structured as "`project_id`,`region`" | ||
|
|
||
| <a id="nestedatt--timeouts"></a> | ||
| ### Nested Schema for `timeouts` | ||
|
|
||
| Optional: | ||
|
|
||
| - `read` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). | ||
|
|
||
|
|
||
| <a id="nestedatt--flavors"></a> | ||
| ### Nested Schema for `flavors` | ||
|
|
||
| Read-Only: | ||
|
|
||
| - `cpu` (Number) The CPU count of the instance. | ||
| - `description` (String) Description of the flavor. | ||
| - `id` (String) ID of the flavor. | ||
| - `max_gb` (Number) Maximum storage, which can be ordered for the flavor in Gigabyte. | ||
| - `memory` (Number) The memory (Gibibyte) of the instance. | ||
| - `min_gb` (Number) Minimum storage, which is required to order in Gigabyte. | ||
| - `node_type` (String) Defines the node type (either single or HA). | ||
| - `storage_classes` (Attributes List) ⚠️TODO, also nested attr descriptions are freestyled (see [below for nested schema](#nestedatt--flavors--storage_classes)) | ||
|
|
||
| <a id="nestedatt--flavors--storage_classes"></a> | ||
| ### Nested Schema for `flavors.storage_classes` | ||
|
|
||
| Read-Only: | ||
|
|
||
| - `class` (String) Class of the instance. | ||
| - `max_io_per_sec` (Number) Maximum I/O per second. | ||
| - `max_through_in_mb` (Number) Maximum throughput in Megabyte. | ||
12 changes: 12 additions & 0 deletions
12
examples/data-sources/stackit_sqlserverflex_flavors/data-source.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| data "stackit_sqlserverflex_flavors" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| // region taken from provider | ||
| } | ||
|
|
||
| // example usage with instance | ||
| resource "stackit_sqlserverflex_instance" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| name = "example" | ||
| flavor_id = one([for f in data.stackit_sqlserverflex_flavors.example.flavors : f.id if f.cpu == 4 && f.memory == 16]) | ||
| } | ||
|
|
257 changes: 257 additions & 0 deletions
257
stackit/internal/services/sqlserverflex/flavors/datasource.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| package flavors | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts" | ||
| "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" | ||
| sqlserverflex "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v3api" | ||
|
|
||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" | ||
| sqlserverflexUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/sqlserverflex/utils" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" | ||
| ) | ||
|
|
||
| var ( | ||
| _ datasource.DataSource = new(flavors) | ||
| _ datasource.DataSourceWithConfigure = new(flavors) | ||
| ) | ||
|
|
||
| type model struct { | ||
| ID types.String `tfsdk:"id"` | ||
| ProjectId types.String `tfsdk:"project_id"` | ||
| Region types.String `tfsdk:"region"` | ||
| Flavors []flavor `tfsdk:"flavors"` | ||
| Timeouts timeouts.Value `tfsdk:"timeouts"` | ||
| } | ||
|
|
||
| type flavor struct { | ||
| Id types.String `tfsdk:"id"` | ||
| Description types.String `tfsdk:"description"` | ||
| CPU types.Int64 `tfsdk:"cpu"` | ||
| Memory types.Int64 `tfsdk:"memory"` | ||
| MinGB types.Int32 `tfsdk:"min_gb"` | ||
| MaxGB types.Int32 `tfsdk:"max_gb"` | ||
| NodeType types.String `tfsdk:"node_type"` | ||
| StorageClasses []storageClass `tfsdk:"storage_classes"` | ||
| } | ||
|
|
||
| type storageClass struct { | ||
| Class types.String `tfsdk:"class"` | ||
| MaxIOPerSec types.Int32 `tfsdk:"max_io_per_sec"` | ||
| MaxThroughInMB types.Int32 `tfsdk:"max_through_in_mb"` | ||
| } | ||
|
|
||
| type flavors struct { | ||
| client *sqlserverflex.APIClient | ||
| providerData core.ProviderData | ||
| } | ||
|
|
||
| func NewFlavorsDataSource() datasource.DataSource { | ||
| return new(flavors) | ||
| } | ||
|
|
||
| func (f *flavors) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
| resp.TypeName = req.ProviderTypeName + "_sqlserverflex_flavors" | ||
| } | ||
|
|
||
| func (f *flavors) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
| var ok bool | ||
| f.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| apiClient := sqlserverflexUtils.ConfigureClient(ctx, &f.providerData, &resp.Diagnostics) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
| f.client = apiClient | ||
| tflog.Info(ctx, "SqlserverFlex client configured") | ||
| } | ||
|
|
||
| func (f *flavors) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
| resp.Schema = schema.Schema{ | ||
| Description: "SqlserverFlex flavors data source 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: "The project ID.", | ||
| Required: true, | ||
| Validators: []validator.String{ | ||
| validate.UUID(), | ||
| validate.NoSeparator(), | ||
| }, | ||
| }, | ||
| "region": schema.StringAttribute{ | ||
| Description: "SqlserverFlex flavors data source region. If undefined the providers region is used.", | ||
| Optional: true, | ||
| Computed: true, | ||
| }, | ||
| "timeouts": timeouts.Attributes(ctx), | ||
| "flavors": schema.ListNestedAttribute{ | ||
| Description: "A list of available flavors.", | ||
| Computed: true, | ||
| NestedObject: schema.NestedAttributeObject{ | ||
| Attributes: map[string]schema.Attribute{ | ||
| "id": schema.StringAttribute{ | ||
| Description: "ID of the flavor.", | ||
| Computed: true, | ||
| }, | ||
| "description": schema.StringAttribute{ | ||
| Description: "Description of the flavor.", | ||
| Computed: true, | ||
| }, | ||
| "cpu": schema.Int64Attribute{ | ||
| Description: "The CPU count of the instance.", | ||
| Computed: true, | ||
| }, | ||
| "memory": schema.Int64Attribute{ | ||
| Description: "The memory (Gibibyte) of the instance.", | ||
| Computed: true, | ||
| }, | ||
| "min_gb": schema.Int64Attribute{ | ||
| Description: "Minimum storage, which is required to order in Gigabyte.", | ||
| Computed: true, | ||
| }, | ||
| "max_gb": schema.Int64Attribute{ | ||
| Description: "Maximum storage, which can be ordered for the flavor in Gigabyte.", | ||
| Computed: true, | ||
| }, | ||
| "node_type": schema.StringAttribute{ | ||
| Description: "Defines the node type (either single or HA).", | ||
| Computed: true, | ||
| }, | ||
| "storage_classes": schema.ListNestedAttribute{ | ||
| Description: "⚠️TODO, also nested attr descriptions are freestyled", | ||
| Computed: true, | ||
| NestedObject: schema.NestedAttributeObject{ | ||
| Attributes: map[string]schema.Attribute{ | ||
| "class": schema.StringAttribute{ | ||
| Description: "Class of the instance.", | ||
| Computed: true, | ||
| }, | ||
| "max_io_per_sec": schema.Int32Attribute{ | ||
| Description: "Maximum I/O per second.", | ||
| Computed: true, | ||
| }, | ||
| "max_through_in_mb": schema.Int32Attribute{ | ||
| Description: "Maximum throughput in Megabyte.", | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (f *flavors) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform | ||
| var model model | ||
| diags := req.Config.Get(ctx, &model) | ||
| resp.Diagnostics.Append(diags...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| readTimeout, diags := model.Timeouts.Read(ctx, core.DefaultOperationTimeout) | ||
| resp.Diagnostics.Append(diags...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
| ctx, cancel := context.WithTimeout(ctx, readTimeout) | ||
| defer cancel() | ||
|
|
||
| projectId := model.ProjectId.ValueString() | ||
| region := f.providerData.GetRegionWithOverride(model.Region) | ||
| ctx = utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{ | ||
| "project_id": projectId, | ||
| "region": region, | ||
| }) | ||
|
|
||
| ctx = core.InitProviderContext(ctx) | ||
|
|
||
| const pageSize = 100 | ||
| flavorsResp, err := f.client.DefaultAPI.ListFlavors(ctx, projectId, region).Size(pageSize).Execute() | ||
| if err != nil { | ||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Reading flavors", fmt.Sprintf("Error calling ListFlavors: %v", err)) | ||
| return | ||
| } | ||
| if flavorsResp.Pagination.TotalRows > pageSize { | ||
| core.LogAndAddWarning(ctx, &resp.Diagnostics, | ||
| "Truncated results", | ||
| fmt.Sprintf("Due to API limitations we currently do not support more than %d flavors, but %d exist. The result is truncated", pageSize, flavorsResp.Pagination.TotalRows), | ||
| ) | ||
| } | ||
|
|
||
| ctx = core.LogResponse(ctx) | ||
|
|
||
| err = mapFields(flavorsResp, &model) | ||
| if err != nil { | ||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Reading Flavors", fmt.Sprintf("Processing API payload: %v", err)) | ||
| return | ||
| } | ||
|
|
||
| resp.Diagnostics.Append(resp.State.Set(ctx, model)...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
| tflog.Info(ctx, "SqlserverFlex flavor read") | ||
| } | ||
|
|
||
| func mapFields(resp *sqlserverflex.ListFlavorsResponse, m *model) error { | ||
| if resp == nil { | ||
| return fmt.Errorf("nil response") | ||
| } | ||
| if m == nil { | ||
| return fmt.Errorf("nil model") | ||
| } | ||
|
|
||
| m.ID = utils.BuildInternalTerraformId(m.ProjectId.ValueString(), m.Region.ValueString()) | ||
|
|
||
| slices.SortFunc(resp.Flavors, func(a, b sqlserverflex.ListFlavors) int { | ||
| return strings.Compare(a.Id, b.Id) | ||
| }) | ||
|
|
||
| for _, respFlavor := range resp.Flavors { | ||
| modelFlavor := flavor{ | ||
| Id: types.StringValue(respFlavor.Id), | ||
| Description: types.StringValue(respFlavor.Description), | ||
| CPU: types.Int64Value(respFlavor.Cpu), | ||
| Memory: types.Int64Value(respFlavor.Memory), | ||
| MinGB: types.Int32Value(respFlavor.MinGB), | ||
| MaxGB: types.Int32Value(respFlavor.MaxGB), | ||
| NodeType: types.StringValue(respFlavor.NodeType), | ||
| } | ||
|
|
||
| slices.SortFunc(respFlavor.StorageClasses, func(a, b sqlserverflex.FlavorStorageClassesStorageClass) int { | ||
| return strings.Compare(a.Class, b.Class) | ||
| }) | ||
|
|
||
| for _, respSC := range respFlavor.StorageClasses { | ||
| modelSC := storageClass{ | ||
| Class: types.StringValue(respSC.Class), | ||
| MaxIOPerSec: types.Int32Value(respSC.MaxIoPerSec), | ||
| MaxThroughInMB: types.Int32Value(respSC.MaxThroughInMb), | ||
| } | ||
| modelFlavor.StorageClasses = append(modelFlavor.StorageClasses, modelSC) | ||
| } | ||
| m.Flavors = append(m.Flavors, modelFlavor) | ||
| } | ||
| return nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
waiting her for response from the service team for a better description, will fix before merge