1.0.0
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/ic"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/image/models"
|
||||
)
|
||||
|
||||
func CheckParamsExistenceImage(ctx context.Context, plan *models.ImageResourceModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
// if err := ic.ExistGID(ctx, uint64(plan.GID.ValueInt64()), c); err != nil {
|
||||
// diags.AddError("Error check input values", err.Error())
|
||||
// }
|
||||
|
||||
if !plan.AccountID.IsUnknown() {
|
||||
if err := ic.ExistAccount(ctx, uint64(plan.AccountID.ValueInt64()), c); err != nil {
|
||||
diags.AddError("Error check input values", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return diags
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
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/image"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/image/models"
|
||||
)
|
||||
|
||||
func ImageDataSourceCheckPresence(ctx context.Context, state *models.RecordImageModel, c *decort.DecortClient) (*image.RecordImage, error) {
|
||||
tflog.Info(ctx, "Get info about image with ID", map[string]any{"image_id": state.ImageId.ValueInt64()})
|
||||
|
||||
req := image.GetRequest{ImageID: uint64(state.ImageId.ValueInt64())}
|
||||
|
||||
if state.ShowAll.IsNull() {
|
||||
req.ShowAll = false
|
||||
} else {
|
||||
req.ShowAll = state.ShowAll.ValueBool()
|
||||
}
|
||||
|
||||
image, err := c.CloudAPI().Image().Get(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot get info about image with error: %w", err)
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Getting info about image successfully", map[string]any{"image_id": state.ImageId.ValueInt64()})
|
||||
return image, nil
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/image"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/image/models"
|
||||
)
|
||||
|
||||
func ImageListCheckPresence(ctx context.Context, state *models.ListImagesModel, c *decort.DecortClient) (*image.ListImages, error) {
|
||||
tflog.Info(ctx, "Get image list info")
|
||||
|
||||
req := image.ListRequest{}
|
||||
|
||||
if !state.SEPID.IsNull() {
|
||||
req.SEPID = uint64(state.SEPID.ValueInt64())
|
||||
}
|
||||
if !state.ByID.IsNull() {
|
||||
req.ByID = uint64(state.ByID.ValueInt64())
|
||||
}
|
||||
if !state.Name.IsNull() {
|
||||
req.Name = state.Name.ValueString()
|
||||
}
|
||||
if !state.Status.IsNull() {
|
||||
req.Status = state.Status.ValueString()
|
||||
}
|
||||
if !state.Architecture.IsNull() {
|
||||
req.Architecture = state.Architecture.ValueString()
|
||||
}
|
||||
if !state.TypeImage.IsNull() {
|
||||
req.TypeImage = state.TypeImage.ValueString()
|
||||
}
|
||||
if !state.ImageSize.IsNull() {
|
||||
req.ImageSize = uint64(state.ImageSize.ValueInt64())
|
||||
}
|
||||
if !state.SEPName.IsNull() {
|
||||
req.SEPName = state.SEPName.ValueString()
|
||||
}
|
||||
if !state.PoolName.IsNull() {
|
||||
req.Pool = state.PoolName.ValueString()
|
||||
}
|
||||
if !state.Public.IsNull() {
|
||||
req.Public = state.Public.ValueBool()
|
||||
}
|
||||
if !state.HotResize.IsNull() {
|
||||
req.HotResize = state.HotResize.ValueBool()
|
||||
}
|
||||
if !state.Bootable.IsNull() {
|
||||
req.Bootable = state.Bootable.ValueBool()
|
||||
}
|
||||
if !state.SortBy.IsNull() {
|
||||
req.SortBy = state.SortBy.ValueString()
|
||||
}
|
||||
if !state.Size.IsNull() {
|
||||
req.Size = uint64(state.Size.ValueInt64())
|
||||
}
|
||||
if !state.Page.IsNull() {
|
||||
req.Page = uint64(state.Page.ValueInt64())
|
||||
}
|
||||
|
||||
imageList, err := c.CloudAPI().Image().List(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Getting image list info, successfully")
|
||||
return imageList, nil
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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/decort-golang-sdk/pkg/cloudapi/image"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/image/models"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/status"
|
||||
)
|
||||
|
||||
func createRequestResourceImage(ctx context.Context, plan *models.ImageResourceModel) image.CreateRequest {
|
||||
tflog.Info(ctx, "Start createRequestResourceImage", map[string]any{"name": plan.ImageName.ValueString()})
|
||||
req := image.CreateRequest{
|
||||
Name: plan.ImageName.ValueString(),
|
||||
URL: plan.URL.ValueString(),
|
||||
BootType: plan.BootType.ValueString(),
|
||||
ImageType: plan.ImageType.ValueString(),
|
||||
}
|
||||
result := make([]string, 0, len(plan.Drivers.Elements()))
|
||||
for _, val := range plan.Drivers.Elements() {
|
||||
result = append(result, strings.Trim(val.String(), "\""))
|
||||
}
|
||||
req.Drivers = result
|
||||
|
||||
if plan.HotResize.IsUnknown() {
|
||||
req.HotResize = false
|
||||
} else {
|
||||
req.HotResize = plan.HotResize.ValueBool()
|
||||
}
|
||||
if !plan.Username.IsUnknown() {
|
||||
req.Username = plan.Username.ValueString()
|
||||
}
|
||||
if !plan.Password.IsUnknown() {
|
||||
req.Password = plan.Password.ValueString()
|
||||
}
|
||||
if !plan.AccountID.IsUnknown() {
|
||||
req.AccountID = uint64(plan.AccountID.ValueInt64())
|
||||
}
|
||||
if !plan.UsernameDL.IsNull() {
|
||||
req.UsernameDL = plan.UsernameDL.ValueString()
|
||||
}
|
||||
if !plan.PasswordDL.IsNull() {
|
||||
req.PasswordDL = plan.PasswordDL.ValueString()
|
||||
}
|
||||
if !plan.SepID.IsUnknown() {
|
||||
req.SEPID = uint64(plan.SepID.ValueInt64())
|
||||
}
|
||||
if !plan.PoolName.IsUnknown() {
|
||||
req.Pool = plan.PoolName.ValueString()
|
||||
}
|
||||
if plan.Architecture.IsUnknown() {
|
||||
req.Architecture = "X86_64"
|
||||
} else {
|
||||
req.Architecture = plan.Architecture.ValueString()
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "End createRequestResourceImage", map[string]any{"name": plan.ImageName.ValueString()})
|
||||
return req
|
||||
}
|
||||
|
||||
func ResourceImageCreate(ctx context.Context, plan *models.ImageResourceModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Start ResourceImageCreate", map[string]any{"name": plan.ImageName.ValueString()})
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
// Make request and get response
|
||||
imageId, err := c.CloudAPI().Image().Create(ctx, createRequestResourceImage(ctx, plan))
|
||||
if err != nil {
|
||||
tflog.Error(ctx, "Error response for create image")
|
||||
diags.AddError("Unable to Create image", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
plan.Id = types.StringValue(strconv.Itoa(int(imageId)))
|
||||
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))
|
||||
|
||||
tflog.Info(ctx, "End ResourceImageCreate", map[string]any{"image_id": imageId})
|
||||
return nil
|
||||
}
|
||||
|
||||
func ImageResourceCheckPresence(ctx context.Context, imageId uint64, c *decort.DecortClient) (*image.RecordImage, error) {
|
||||
tflog.Info(ctx, "Get info about image with ID", map[string]any{"image_id": imageId})
|
||||
|
||||
image, err := c.CloudAPI().Image().Get(ctx, image.GetRequest{ImageID: imageId})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot get info about image with error: %w", err)
|
||||
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Getting info about image successfully", map[string]any{"image_id": imageId})
|
||||
return image, nil
|
||||
}
|
||||
|
||||
func ImageReadStatus(ctx context.Context, imageId uint64, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Read status image with ID", map[string]any{"image": imageId})
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
image, err := ImageResourceCheckPresence(ctx, imageId, c)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot get info about image ", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
switch image.Status {
|
||||
case status.Modeled:
|
||||
diags.AddError("Error:", fmt.Sprintf("The image is in status: %s, please, contact support for more information", image.Status))
|
||||
return diags
|
||||
case status.Destroying:
|
||||
diags.AddError("Error:", fmt.Sprintf("The image is in progress with status: %s", image.Status))
|
||||
return diags
|
||||
case status.Destroyed, status.Purged:
|
||||
diags.AddError("Error:", "The resource cannot be updated because it has been destroyed")
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Read status image successfully", map[string]any{"image_id": imageId})
|
||||
return nil
|
||||
}
|
||||
|
||||
func ImageUpdateName(ctx context.Context, imageId uint64, name string, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Update name image with ID", map[string]any{"image_id": imageId})
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
_, err := c.CloudAPI().Image().Rename(ctx, image.RenameRequest{ImageID: imageId, Name: name})
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot update name image with ID - %d", imageId), err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Update name image successfully", map[string]any{"image_id": imageId})
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"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/decort-golang-sdk/pkg/cloudapi/image"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/image/models"
|
||||
)
|
||||
|
||||
func createRequestResourceImageVirtual(ctx context.Context, plan *models.ImageVirtualResourceModel) image.CreateVirtualRequest {
|
||||
tflog.Info(ctx, "Start createRequestResourceImageVirtual", map[string]any{"name": plan.ImageName.ValueString()})
|
||||
req := image.CreateVirtualRequest{
|
||||
Name: plan.ImageName.ValueString(),
|
||||
TargetID: uint64(plan.LinkTo.ValueInt64()),
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "End createRequestResourceImageVirtual", map[string]any{"name": plan.ImageName.ValueString()})
|
||||
return req
|
||||
}
|
||||
|
||||
func ResourceImageVirtualCreate(ctx context.Context, plan *models.ImageVirtualResourceModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Start ResourceImageVirtualCreate", map[string]any{"name": plan.ImageName.ValueString()})
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
// Make request and get response
|
||||
imageId, err := c.CloudAPI().Image().CreateVirtual(ctx, createRequestResourceImageVirtual(ctx, plan))
|
||||
if err != nil {
|
||||
tflog.Error(ctx, "Error response for create image_virtual")
|
||||
diags.AddError("Unable to Create image_virtual", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
plan.Id = types.StringValue(strconv.Itoa(int(imageId)))
|
||||
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))
|
||||
|
||||
tflog.Info(ctx, "End ResourceImageVirtualCreate", map[string]any{"image_id": imageId})
|
||||
return nil
|
||||
}
|
||||
|
||||
func ImageUpdateLink(ctx context.Context, imageId, linkTo uint64, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Update link_to image virtual with ID", map[string]any{"image_id": imageId})
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
_, err := c.CloudAPI().Image().Link(ctx, image.LinkRequest{ImageID: imageId, TargetID: linkTo})
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot update link_to image virtual with ID - %d", imageId), err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Update link_to image virtual successfully", map[string]any{"image_id": imageId})
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user