This commit is contained in:
asteam
2024-12-04 13:18:58 +03:00
parent 003e4d656e
commit 76ea459b3d
417 changed files with 30051 additions and 975 deletions

View File

@@ -0,0 +1,91 @@
package dpdknet
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/dpdknet/flattens"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/dpdknet/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/dpdknet/schemas"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &dataSourceDPDK{}
)
func NewDataSourceDPDK() datasource.DataSource {
return &dataSourceDPDK{}
}
// dataSourceDPDK is the data source implementation.
type dataSourceDPDK struct {
client *decort.DecortClient
}
func (d *dataSourceDPDK) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
// Read Terraform configuration data into the model
var state models.DataSourceDPDKModel
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
tflog.Error(ctx, "Read dataSourceDPDK: Error get state")
return
}
dpdkId := uint64(state.DPDKID.ValueInt64())
tflog.Info(ctx, "Read dataSourceDPDK: got state successfully", map[string]any{"dpdk_id": dpdkId})
// Set timeouts
readTimeout, diags := state.Timeouts.Read(ctx, constants.Timeout180s)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
tflog.Error(ctx, "Read dataSourceDPDK: Error set timeout")
return
}
tflog.Info(ctx, "Read dataSourceDPDK: set timeouts successfully", map[string]any{
"dpdk_id": dpdkId,
"readTimeout": readTimeout})
ctx, cancel := context.WithTimeout(ctx, readTimeout)
defer cancel()
// Map response body to schema
resp.Diagnostics.Append(flattens.DPDKDataSource(ctx, &state, d.client)...)
if resp.Diagnostics.HasError() {
tflog.Error(ctx, "Read dataSourceDPDK: Error flatten")
return
}
// Set refreshed state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
if resp.Diagnostics.HasError() {
tflog.Error(ctx, "Read dataSourceDPDK: Error set state")
return
}
tflog.Info(ctx, "End read dataSourceDPDK", map[string]any{"dpdk_id": dpdkId})
}
func (d *dataSourceDPDK) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: schemas.MakeSchemaDataSourceDPDK(),
Blocks: map[string]schema.Block{
"timeouts": timeouts.Block(ctx),
},
}
}
func (d *dataSourceDPDK) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_dpdknet"
}
// Configure adds the provider configured client to the data source.
func (d *dataSourceDPDK) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
tflog.Info(ctx, "Get Configure dataSourceDPDK")
d.client = client.DataSource(ctx, &req, resp)
tflog.Info(ctx, "Getting Configure dataSourceDPDK successfully")
}

View File

@@ -0,0 +1,88 @@
package dpdknet
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/dpdknet/flattens"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/dpdknet/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/dpdknet/schemas"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &dataSourceDPDKList{}
)
func NewDataSourceDPDKList() datasource.DataSource {
return &dataSourceDPDKList{}
}
// dataSourceDPDKList is the data source implementation.
type dataSourceDPDKList struct {
client *decort.DecortClient
}
func (d *dataSourceDPDKList) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
// Read Terraform configuration data into the model
var state models.DataSourceDPDKListModel
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
tflog.Error(ctx, "Read dataSourceDPDKList: Error get state")
return
}
// Set timeouts
readTimeout, diags := state.Timeouts.Read(ctx, constants.Timeout180s)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
tflog.Error(ctx, "Read dataSourceDPDKList: Error set timeout")
return
}
tflog.Info(ctx, "Read dataSourceDPDKList: 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.DPDKListDataSource(ctx, &state, d.client)...)
if resp.Diagnostics.HasError() {
tflog.Error(ctx, "Read dataSourceDPDKList: Error flatten")
return
}
// Set refreshed state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
if resp.Diagnostics.HasError() {
tflog.Error(ctx, "Read dataSourceDPDKList: Error set state")
return
}
tflog.Info(ctx, "End read dataSourceDPDKList")
}
func (d *dataSourceDPDKList) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: schemas.MakeSchemaDataSourceDPDKList(),
Blocks: map[string]schema.Block{
"timeouts": timeouts.Block(ctx),
},
}
}
func (d *dataSourceDPDKList) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_dpdknet_list"
}
// Configure adds the provider configured client to the data source.
func (d *dataSourceDPDKList) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
tflog.Info(ctx, "Get Configure dataSourceDPDKList")
d.client = client.DataSource(ctx, &req, resp)
tflog.Info(ctx, "Getting Configure dataSourceDPDKList successfully")
}

View File

@@ -0,0 +1,52 @@
package flattens
import (
"context"
"github.com/google/uuid"
"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/dpdknet/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/dpdknet/utilities"
)
func DPDKDataSource(ctx context.Context, state *models.DataSourceDPDKModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.DPDKDataSource")
diags := diag.Diagnostics{}
dpdkId := uint64(state.DPDKID.ValueInt64())
recordDPDK, diags := utilities.DPDKDataSourceCheckPresence(ctx, dpdkId, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.DPDKDataSource: before flatten", map[string]any{"dpdk_id": dpdkId})
id := uuid.New()
*state = models.DataSourceDPDKModel{
DPDKID: state.DPDKID,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
AccountAccess: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, recordDPDK.AccountAccess),
CreatedTime: types.Int64Value(int64(recordDPDK.CreatedTime)),
Desc: types.StringValue(recordDPDK.Description),
GID: types.Int64Value(int64(recordDPDK.GID)),
GUID: types.Int64Value(int64(recordDPDK.GUID)),
Name: types.StringValue(recordDPDK.Name),
RGAccess: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, recordDPDK.RGAccess),
Status: types.StringValue(recordDPDK.Status),
OVSBridge: types.StringValue(recordDPDK.OVSBridge),
VlanID: types.Int64Value(int64(recordDPDK.VlanID)),
ComputeIDs: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, recordDPDK.ComputeIDs),
UpdatedTime: types.Int64Value(int64(recordDPDK.UpdatedTime)),
}
tflog.Info(ctx, "End flattens.DPDKDataSource", map[string]any{"dpdk_id": dpdkId})
return nil
}

View File

@@ -0,0 +1,72 @@
package flattens
import (
"context"
"github.com/google/uuid"
"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/dpdknet/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/dpdknet/utilities"
)
func DPDKListDataSource(ctx context.Context, state *models.DataSourceDPDKListModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.DPDKListDataSource")
diags := diag.Diagnostics{}
dpdkList, err := utilities.DPDKListDataSourceCheckPresence(ctx, state, c)
if err != nil {
diags.AddError("Cannot get info about list DPDK", err.Error())
return diags
}
tflog.Info(ctx, "flattens.DPDKListDataSource: before flatten")
id := uuid.New()
*state = models.DataSourceDPDKListModel{
ByID: state.ByID,
GID: state.GID,
Name: state.Name,
Desc: state.Desc,
Status: state.Status,
ComputeIDs: state.ComputeIDs,
SortBy: state.SortBy,
Page: state.Page,
Size: state.Size,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
}
items := make([]models.ItemDPDKListModel, 0, len(dpdkList.Data))
for _, dpdkItem := range dpdkList.Data {
item := models.ItemDPDKListModel{
AccountAccess: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, dpdkItem.AccountAccess),
CreatedTime: types.Int64Value(int64(dpdkItem.CreatedTime)),
Desc: types.StringValue(dpdkItem.Description),
DPDKID: types.Int64Value(int64(dpdkItem.ID)),
GID: types.Int64Value(int64(dpdkItem.GID)),
GUID: types.Int64Value(int64(dpdkItem.GUID)),
Name: types.StringValue(dpdkItem.Name),
RGAccess: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, dpdkItem.RGAccess),
Status: types.StringValue(dpdkItem.Status),
OVSBridge: types.StringValue(dpdkItem.OVSBridge),
VlanID: types.Int64Value(int64(dpdkItem.VlanID)),
ComputeIDs: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, dpdkItem.ComputeIDs),
UpdatedTime: types.Int64Value(int64(dpdkItem.UpdatedTime)),
}
items = append(items, item)
}
state.Items = items
state.EntryCount = types.Int64Value(int64(dpdkList.EntryCount))
tflog.Info(ctx, "End flattens.DPDKListDataSource")
return nil
}

View File

@@ -0,0 +1,27 @@
package models
import (
"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/types"
)
type DataSourceDPDKModel struct {
//required field
DPDKID types.Int64 `tfsdk:"dpdk_id"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
//response field
AccountAccess types.List `tfsdk:"account_access"`
CreatedTime types.Int64 `tfsdk:"created_time"`
Desc types.String `tfsdk:"desc"`
GID types.Int64 `tfsdk:"gid"`
GUID types.Int64 `tfsdk:"guid"`
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
RGAccess types.List `tfsdk:"rg_access"`
Status types.String `tfsdk:"status"`
OVSBridge types.String `tfsdk:"ovs_bridge"`
VlanID types.Int64 `tfsdk:"vlan_id"`
ComputeIDs types.List `tfsdk:"compute_ids"`
UpdatedTime types.Int64 `tfsdk:"updated_time"`
}

View File

@@ -0,0 +1,42 @@
package models
import (
"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/types"
)
type DataSourceDPDKListModel struct {
Timeouts timeouts.Value `tfsdk:"timeouts"`
// optional attributes
ByID types.Int64 `tfsdk:"by_id"`
GID types.Int64 `tfsdk:"gid"`
Name types.String `tfsdk:"name"`
Desc types.String `tfsdk:"desc"`
Status types.String `tfsdk:"status"`
ComputeIDs types.List `tfsdk:"compute_ids"`
SortBy types.String `tfsdk:"sort_by"`
Page types.Int64 `tfsdk:"page"`
Size types.Int64 `tfsdk:"size"`
// computed attributes
Id types.String `tfsdk:"id"`
Items []ItemDPDKListModel `tfsdk:"items"`
EntryCount types.Int64 `tfsdk:"entry_count"`
}
type ItemDPDKListModel struct {
DPDKID types.Int64 `tfsdk:"dpdk_id"`
AccountAccess types.List `tfsdk:"account_access"`
CreatedTime types.Int64 `tfsdk:"created_time"`
Desc types.String `tfsdk:"desc"`
GID types.Int64 `tfsdk:"gid"`
GUID types.Int64 `tfsdk:"guid"`
Name types.String `tfsdk:"name"`
RGAccess types.List `tfsdk:"rg_access"`
Status types.String `tfsdk:"status"`
OVSBridge types.String `tfsdk:"ovs_bridge"`
VlanID types.Int64 `tfsdk:"vlan_id"`
ComputeIDs types.List `tfsdk:"compute_ids"`
UpdatedTime types.Int64 `tfsdk:"updated_time"`
}

View File

@@ -0,0 +1,56 @@
package schemas
import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
func MakeSchemaDataSourceDPDK() map[string]schema.Attribute {
return map[string]schema.Attribute{
"dpdk_id": schema.Int64Attribute{
Required: true,
},
"account_access": schema.ListAttribute{
Computed: true,
ElementType: types.Int64Type,
},
"created_time": schema.Int64Attribute{
Computed: true,
},
"desc": schema.StringAttribute{
Computed: true,
},
"gid": schema.Int64Attribute{
Computed: true,
},
"guid": schema.Int64Attribute{
Computed: true,
},
"name": schema.StringAttribute{
Computed: true,
},
"rg_access": schema.ListAttribute{
Computed: true,
ElementType: types.Int64Type,
},
"status": schema.StringAttribute{
Computed: true,
},
"ovs_bridge": schema.StringAttribute{
Computed: true,
},
"vlan_id": schema.Int64Attribute{
Computed: true,
},
"compute_ids": schema.ListAttribute{
Computed: true,
ElementType: types.Int64Type,
},
"updated_time": schema.Int64Attribute{
Computed: true,
},
"id": schema.StringAttribute{
Computed: true,
},
}
}

View File

@@ -0,0 +1,97 @@
package schemas
import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
func MakeSchemaDataSourceDPDKList() map[string]schema.Attribute {
return map[string]schema.Attribute{
// optional attributes
"by_id": schema.Int64Attribute{
Optional: true,
},
"gid": schema.Int64Attribute{
Optional: true,
},
"name": schema.StringAttribute{
Optional: true,
},
"desc": schema.StringAttribute{
Optional: true,
},
"status": schema.StringAttribute{
Optional: true,
},
"compute_ids": schema.ListAttribute{
Optional: true,
ElementType: types.Int64Type,
},
"sort_by": schema.StringAttribute{
Optional: true,
},
"page": schema.Int64Attribute{
Optional: true,
},
"size": schema.Int64Attribute{
Optional: true,
},
// computed attributes
"id": schema.StringAttribute{
Computed: true,
},
"items": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"dpdk_id": schema.Int64Attribute{
Required: true,
},
"account_access": schema.ListAttribute{
Computed: true,
ElementType: types.Int64Type,
},
"created_time": schema.Int64Attribute{
Computed: true,
},
"desc": schema.StringAttribute{
Computed: true,
},
"gid": schema.Int64Attribute{
Computed: true,
},
"guid": schema.Int64Attribute{
Computed: true,
},
"name": schema.StringAttribute{
Computed: true,
},
"rg_access": schema.ListAttribute{
Computed: true,
ElementType: types.Int64Type,
},
"status": schema.StringAttribute{
Computed: true,
},
"ovs_bridge": schema.StringAttribute{
Computed: true,
},
"vlan_id": schema.Int64Attribute{
Computed: true,
},
"compute_ids": schema.ListAttribute{
Computed: true,
ElementType: types.Int64Type,
},
"updated_time": schema.Int64Attribute{
Computed: true,
},
},
},
},
"entry_count": schema.Int64Attribute{
Computed: true,
},
}
}

View File

@@ -0,0 +1,27 @@
package utilities
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-log/tflog"
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/dpdknet"
)
func DPDKDataSourceCheckPresence(ctx context.Context, dpdkId uint64, c *decort.DecortClient) (*dpdknet.RecordDPDKNet, diag.Diagnostics) {
tflog.Info(ctx, fmt.Sprintf("DPDKDataSourceCheckPresence: Get info about DPDK net with ID - %d", dpdkId))
diags := diag.Diagnostics{}
recordDPDK, err := c.CloudAPI().DPDKNet().Get(ctx, dpdknet.GetRequest{DPDKID: dpdkId})
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about DPDK net with ID %d", dpdkId), err.Error())
return nil, diags
}
tflog.Info(ctx, "DPDKDataSourceCheckPresence: response from CloudBroker().DPDKNet().Get", map[string]any{"dpdk_id": dpdkId, "response": recordDPDK})
return recordDPDK, nil
}

View File

@@ -0,0 +1,61 @@
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/dpdknet"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/dpdknet/models"
)
func DPDKListDataSourceCheckPresence(ctx context.Context, plan *models.DataSourceDPDKListModel, c *decort.DecortClient) (*dpdknet.ListDPDKNet, error) {
tflog.Info(ctx, fmt.Sprintf("DPDKListDataSourceCheckPresence: Get DPDK list info"))
listDPDKReq := dpdknet.ListRequest{}
if !plan.ByID.IsNull() {
listDPDKReq.ByID = uint64(plan.ByID.ValueInt64())
}
if !plan.GID.IsNull() {
listDPDKReq.GID = uint64(plan.GID.ValueInt64())
}
if !plan.Name.IsNull() {
listDPDKReq.Name = plan.Name.ValueString()
}
if !plan.Desc.IsNull() {
listDPDKReq.Description = plan.Desc.ValueString()
}
if !plan.Status.IsNull() {
listDPDKReq.Status = plan.Status.ValueString()
}
if !plan.ComputeIDs.IsNull() {
computeIDs := make([]uint64, 0, len(plan.ComputeIDs.Elements()))
diags := plan.ComputeIDs.ElementsAs(ctx, &computeIDs, false)
if diags.HasError() {
tflog.Error(ctx, "DPDKListDataSourceCheckPresence: cannot populate computeIDs with plan.ComputeIDs List elements")
return nil, fmt.Errorf("cannot populate computeIDs with plan.ComputeIDs List elements")
}
listDPDKReq.ComputeIDs = computeIDs
}
if !plan.SortBy.IsNull() {
listDPDKReq.SortBy = plan.SortBy.ValueString()
}
if !plan.Page.IsNull() {
listDPDKReq.Page = uint64(plan.Page.ValueInt64())
}
if !plan.Size.IsNull() {
listDPDKReq.Size = uint64(plan.Size.ValueInt64())
}
tflog.Info(ctx, "DPDKListDataSourceCheckPresence: before call CloudAPI().DPDKNet().List", map[string]any{"response": listDPDKReq})
dpdkList, err := c.CloudAPI().DPDKNet().List(ctx, listDPDKReq)
if err != nil {
return nil, fmt.Errorf("cannot get info about data source list DPDK net with error: %w", err)
}
tflog.Info(ctx, "DPDKListDataSourceCheckPresence: response from CloudAPI().DPDKNet().List", map[string]any{"response": dpdkList})
return dpdkList, err
}