1.0.0
This commit is contained in:
91
internal/service/cloudapi/stack/data_source_stack.go
Normal file
91
internal/service/cloudapi/stack/data_source_stack.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package stack
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"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-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/constants"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/flattens"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/models"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/schemas"
|
||||
)
|
||||
|
||||
// Ensure the implementation satisfies the expected interfaces.
|
||||
var (
|
||||
_ datasource.DataSource = &dataSourceStack{}
|
||||
)
|
||||
|
||||
func NewDataSourceStack() datasource.DataSource {
|
||||
return &dataSourceStack{}
|
||||
}
|
||||
|
||||
// dataSourceStack is the data source implementation.
|
||||
type dataSourceStack struct {
|
||||
client *decort.DecortClient
|
||||
}
|
||||
|
||||
func (d *dataSourceStack) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
// Read Terraform configuration data into the model
|
||||
var state models.InfoStackModel
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
tflog.Error(ctx, "Read dataSourceStack: Error get state")
|
||||
return
|
||||
}
|
||||
id := uint64(state.StackID.ValueInt64())
|
||||
tflog.Info(ctx, "Read dataSourceStack: got state successfully", map[string]any{"stack_id": id})
|
||||
|
||||
// Set timeouts
|
||||
readTimeout, diags := state.Timeouts.Read(ctx, constants.Timeout30s)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
tflog.Error(ctx, "Read dataSourceStack: Error set timeout")
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "Read dataSourceStack: set timeouts successfully", map[string]any{
|
||||
"stack_id": id,
|
||||
"readTimeout": readTimeout})
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, readTimeout)
|
||||
defer cancel()
|
||||
|
||||
// Map response body to schema
|
||||
resp.Diagnostics.Append(flattens.StackDataSource(ctx, &state, d.client)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
tflog.Error(ctx, "Read dataSourceStack: Error flatten data source")
|
||||
return
|
||||
}
|
||||
|
||||
// Set refreshed state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
tflog.Error(ctx, "Read dataSourceStack: Error set state")
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "End read stack", map[string]any{"stack_id": id})
|
||||
}
|
||||
|
||||
func (d *dataSourceStack) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Attributes: schemas.MakeSchemaDataSourceStack(),
|
||||
Blocks: map[string]schema.Block{
|
||||
"timeouts": timeouts.Block(ctx),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dataSourceStack) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_stack"
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the data source.
|
||||
func (d *dataSourceStack) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
tflog.Info(ctx, "Get Configure dataSourceStack")
|
||||
d.client = client.DataSource(ctx, &req, resp)
|
||||
tflog.Info(ctx, "Getting Configure dataSourceStack successfully")
|
||||
}
|
||||
89
internal/service/cloudapi/stack/data_source_stack_list.go
Normal file
89
internal/service/cloudapi/stack/data_source_stack_list.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package stack
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"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-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/constants"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/flattens"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/models"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/schemas"
|
||||
)
|
||||
|
||||
// Ensure the implementation satisfies the expected interfaces.
|
||||
var (
|
||||
_ datasource.DataSource = &dataSourceStackList{}
|
||||
)
|
||||
|
||||
func NewDataSourceStackList() datasource.DataSource {
|
||||
return &dataSourceStackList{}
|
||||
}
|
||||
|
||||
// dataSourceStackList is the data source implementation.
|
||||
type dataSourceStackList struct {
|
||||
client *decort.DecortClient
|
||||
}
|
||||
|
||||
func (d *dataSourceStackList) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
// Read Terraform configuration data into the model
|
||||
var state models.ListStacksModel
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
tflog.Error(ctx, "Read dataSourceStackList: Error get state")
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "Read dataSourceStackList: got state successfully")
|
||||
|
||||
// Set timeouts
|
||||
readTimeout, diags := state.Timeouts.Read(ctx, constants.Timeout30s)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
tflog.Error(ctx, "Read dataSourceStackList: Error set timeout")
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "Read dataSourceStackList: set timeouts successfully", map[string]any{
|
||||
"readTimeout": readTimeout})
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, readTimeout)
|
||||
defer cancel()
|
||||
|
||||
// Map response body to schema
|
||||
resp.Diagnostics.Append(flattens.StackListDataSource(ctx, &state, d.client)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
tflog.Error(ctx, "Read dataSourceStackList: Error flatten data source")
|
||||
return
|
||||
}
|
||||
|
||||
// Set refreshed state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
tflog.Error(ctx, "Read dataSourceStackList: Error set state")
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "End read stack")
|
||||
}
|
||||
|
||||
func (d *dataSourceStackList) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Attributes: schemas.MakeSchemaDataSourceStackList(),
|
||||
Blocks: map[string]schema.Block{
|
||||
"timeouts": timeouts.Block(ctx),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dataSourceStackList) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_stack_list"
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the data source.
|
||||
func (d *dataSourceStackList) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
tflog.Info(ctx, "Get Configure dataSourceStackList")
|
||||
d.client = client.DataSource(ctx, &req, resp)
|
||||
tflog.Info(ctx, "Getting Configure dataSourceStackList successfully")
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package flattens
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/flattens"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/models"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/utilities"
|
||||
)
|
||||
|
||||
// StackDataSource flattens data source for stack.
|
||||
// Return error in case data source is not found on the platform.
|
||||
// Flatten errors are added to tflog.
|
||||
func StackDataSource(ctx context.Context, state *models.InfoStackModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Start flattens.StackDataSource")
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
id := uint64(state.StackID.ValueInt64())
|
||||
|
||||
record, err := utilities.StackCheckPresence(ctx, id, c)
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot get info about stack with ID %v", id), err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "flattens.StackDataSource: before flatten", map[string]any{"stack_id": state.StackID.ValueInt64(), "record": record})
|
||||
|
||||
*state = models.InfoStackModel{
|
||||
StackID: state.StackID,
|
||||
Timeouts: state.Timeouts,
|
||||
|
||||
CPUAllocationRatio: types.Float64Value(record.CPUAllocationRatio),
|
||||
Descr: types.StringValue(record.Descr),
|
||||
Drivers: flattens.FlattenSimpleTypeToList(ctx, types.StringType, record.Drivers),
|
||||
MemAllocationRatio: types.Float64Value(record.MemAllocationRatio),
|
||||
Name: types.StringValue(record.Name),
|
||||
Status: types.StringValue(record.Status),
|
||||
Type: types.StringValue(record.Type),
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "flattens.StackDataSource: after flatten", map[string]any{"stack_id": state.StackID.ValueInt64()})
|
||||
|
||||
tflog.Info(ctx, "End flattens.StackDataSource", map[string]any{"stack_id": state.StackID.ValueInt64()})
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package flattens
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/models"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/utilities"
|
||||
)
|
||||
|
||||
// StackListDataSource flattens data source for stack list.
|
||||
// Return error in case data source is not found on the platform.
|
||||
// Flatten errors are added to tflog.
|
||||
func StackListDataSource(ctx context.Context, state *models.ListStacksModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Start flattens.StackListDataSource")
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
record, err := utilities.StackListCheckPresence(ctx, state, c)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot get info about stack list", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "flattens.StackListDataSource: before flatten", map[string]any{"record": record})
|
||||
|
||||
*state = models.ListStacksModel{
|
||||
//optional fields
|
||||
Timeouts: state.Timeouts,
|
||||
ByID: state.ByID,
|
||||
Name: state.Name,
|
||||
Status: state.Status,
|
||||
Type: state.Type,
|
||||
SortBy: state.SortBy,
|
||||
Page: state.Page,
|
||||
Size: state.Size,
|
||||
|
||||
//compute fields
|
||||
EntryCount: types.Int64Value(int64(record.EntryCount)),
|
||||
}
|
||||
|
||||
items := make([]models.ItemStackModel, 0, len(record.Data))
|
||||
for _, item := range record.Data {
|
||||
i := models.ItemStackModel{
|
||||
ID: types.Int64Value(int64(item.ID)),
|
||||
Name: types.StringValue(item.Name),
|
||||
Status: types.StringValue(item.Status),
|
||||
Type: types.StringValue(item.Type),
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
state.Data = items
|
||||
|
||||
tflog.Info(ctx, "flattens.StackListDataSource: after flatten")
|
||||
|
||||
tflog.Info(ctx, "End flattens.StackListDataSource")
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
type InfoStackModel struct {
|
||||
// request required fields
|
||||
StackID types.Int64 `tfsdk:"stack_id"`
|
||||
|
||||
// request optional fields
|
||||
Timeouts timeouts.Value `tfsdk:"timeouts"`
|
||||
|
||||
// response fields
|
||||
CPUAllocationRatio types.Float64 `tfsdk:"cpu_allocation_ratio"`
|
||||
Descr types.String `tfsdk:"descr"`
|
||||
Drivers types.List `tfsdk:"drivers"`
|
||||
MemAllocationRatio types.Float64 `tfsdk:"mem_allocation_ratio"`
|
||||
Name types.String `tfsdk:"name"`
|
||||
Status types.String `tfsdk:"status"`
|
||||
Type types.String `tfsdk:"type"`
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
type ListStacksModel struct {
|
||||
// optional fields
|
||||
ByID types.Int64 `tfsdk:"by_id"`
|
||||
Name types.String `tfsdk:"name"`
|
||||
Status types.String `tfsdk:"status"`
|
||||
Type types.String `tfsdk:"type"`
|
||||
SortBy types.String `tfsdk:"sort_by"`
|
||||
Page types.Int64 `tfsdk:"page"`
|
||||
Size types.Int64 `tfsdk:"size"`
|
||||
Timeouts timeouts.Value `tfsdk:"timeouts"`
|
||||
|
||||
//compute fields
|
||||
Data []ItemStackModel `tfsdk:"items"`
|
||||
EntryCount types.Int64 `tfsdk:"entry_count"`
|
||||
}
|
||||
|
||||
type ItemStackModel struct {
|
||||
ID types.Int64 `tfsdk:"stack_id"`
|
||||
Name types.String `tfsdk:"name"`
|
||||
Status types.String `tfsdk:"status"`
|
||||
Type types.String `tfsdk:"type"`
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package schemas
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
func MakeSchemaDataSourceStack() map[string]schema.Attribute {
|
||||
return map[string]schema.Attribute{
|
||||
"stack_id": schema.Int64Attribute{
|
||||
Required: true,
|
||||
},
|
||||
"cpu_allocation_ratio": schema.Float64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
"descr": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"drivers": schema.ListAttribute{
|
||||
Computed: true,
|
||||
ElementType: types.StringType,
|
||||
},
|
||||
"mem_allocation_ratio": schema.Float64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
"name": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"status": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"type": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package schemas
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
)
|
||||
|
||||
func MakeSchemaDataSourceStackList() map[string]schema.Attribute {
|
||||
return map[string]schema.Attribute{
|
||||
"by_id": schema.Int64Attribute{
|
||||
Optional: true,
|
||||
},
|
||||
"name": schema.StringAttribute{
|
||||
Optional: true,
|
||||
},
|
||||
"type": schema.StringAttribute{
|
||||
Optional: true,
|
||||
},
|
||||
"status": schema.StringAttribute{
|
||||
Optional: true,
|
||||
},
|
||||
"sort_by": schema.StringAttribute{
|
||||
Optional: true,
|
||||
},
|
||||
"page": schema.Int64Attribute{
|
||||
Optional: true,
|
||||
},
|
||||
"size": schema.Int64Attribute{
|
||||
Optional: true,
|
||||
},
|
||||
"items": schema.ListNestedAttribute{
|
||||
Computed: true,
|
||||
NestedObject: schema.NestedAttributeObject{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"stack_id": schema.Int64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
"name": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"status": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"type": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"entry_count": schema.Int64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/stack"
|
||||
)
|
||||
|
||||
func StackCheckPresence(ctx context.Context, stackID uint64, c *decort.DecortClient) (*stack.InfoStack,
|
||||
error) {
|
||||
req := stack.GetRequest{
|
||||
StackId: stackID,
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "StackCheckPresence: before call CloudAPI().Stack().Get", map[string]any{"req": req})
|
||||
stack, err := c.CloudAPI().Stack().Get(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot get info about stack with error: %w", err)
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "StackCheckPresence: response from CloudAPI().Stack().Get", map[string]any{"response": stack})
|
||||
|
||||
return stack, err
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/stack"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/models"
|
||||
)
|
||||
|
||||
func StackListCheckPresence(ctx context.Context, plan *models.ListStacksModel, c *decort.DecortClient) (*stack.ListStacks,
|
||||
error) {
|
||||
req := stack.ListRequest{}
|
||||
|
||||
if !plan.ByID.IsNull() {
|
||||
req.ByID = uint64(plan.ByID.ValueInt64())
|
||||
}
|
||||
if !plan.Name.IsNull() {
|
||||
req.Name = plan.Name.ValueString()
|
||||
}
|
||||
if !plan.Status.IsNull() {
|
||||
req.Status = plan.Status.ValueString()
|
||||
}
|
||||
if !plan.Type.IsNull() {
|
||||
req.Type = plan.Type.ValueString()
|
||||
}
|
||||
if !plan.SortBy.IsNull() {
|
||||
req.SortBy = plan.SortBy.ValueString()
|
||||
}
|
||||
if !plan.Page.IsNull() {
|
||||
req.Page = uint64(plan.Page.ValueInt64())
|
||||
}
|
||||
if !plan.Size.IsNull() {
|
||||
req.Size = uint64(plan.Size.ValueInt64())
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "StackListCheckPresence: before call CloudAPI().Stack().List", map[string]any{"req": req})
|
||||
stack, err := c.CloudAPI().Stack().List(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("StackListCheckPresence: cannot get info about stack list")
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "StackListCheckPresence: response from CloudAPI().Stack().Get", map[string]any{"response": stack})
|
||||
|
||||
return stack, err
|
||||
}
|
||||
Reference in New Issue
Block a user