This commit is contained in:
2026-06-02 11:28:16 +03:00
parent af79f6ab3e
commit c734dcfff7
254 changed files with 10439 additions and 3751 deletions

View File

@@ -30,8 +30,10 @@ func flattenImage(d *schema.ResourceData, img *image.RecordImage) {
d.Set("guid", img.GUID)
d.Set("history", flattenHistory(img.History))
d.Set("hot_resize", img.HotResize)
d.Set("independent", img.Independent)
d.Set("last_modified", img.LastModified)
d.Set("link_to", img.LinkTo)
d.Set("links_to", img.LinksTo)
d.Set("milestones", img.Milestones)
d.Set("name", img.Name)
d.Set("network_interface_naming", img.NetworkInterfaceNaming)
@@ -52,6 +54,7 @@ func flattenImage(d *schema.ResourceData, img *image.RecordImage) {
d.Set("storage_policy_id", img.StoragePolicyID)
d.Set("tech_status", img.TechStatus)
d.Set("to_clean", img.ToClean)
d.Set("target_ids", img.LinksTo)
d.Set("image_type", img.Type)
d.Set("url", img.URL)
d.Set("username", img.Username)
@@ -111,8 +114,10 @@ func flattenImageList(il *image.ListImages) []map[string]interface{} {
"guid": item.GUID,
"history": flattenHistory(item.History),
"hot_resize": item.HotResize,
"independent": item.Independent,
"last_modified": item.LastModified,
"link_to": item.LinkTo,
"links_to": item.LinksTo,
"milestones": item.Milestones,
"name": item.Name,
"network_interface_naming": item.NetworkInterfaceNaming,

View File

@@ -59,10 +59,11 @@ func resourceImageFromBlankComputeCreate(ctx context.Context, d *schema.Resource
}
req := compute.CreateTemplateFromBlankRequest{
ComputeID: uint64(d.Get("compute_id").(int)),
Name: d.Get("name").(string),
BootType: d.Get("boot_type").(string),
ImageType: d.Get("image_type").(string),
ComputeID: uint64(d.Get("compute_id").(int)),
Name: d.Get("name").(string),
BootType: d.Get("boot_type").(string),
ImageType: d.Get("image_type").(string),
StoragePolicyID: uint64(d.Get("storage_policy_id").(int)),
}
if username, ok := d.GetOk("username"); ok {

View File

@@ -0,0 +1,274 @@
package image
import (
"context"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
log "github.com/sirupsen/logrus"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/image"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/dc"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/status"
)
func resourceMultiImageCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Debugf("resourceMultiImageCreate: called for image %s", d.Get("name").(string))
c := m.(*controller.ControllerCfg)
targetIDsRaw := d.Get("target_ids").([]interface{})
targetIDs := make([]uint64, 0, len(targetIDsRaw))
for _, id := range targetIDsRaw {
targetIDs = append(targetIDs, uint64(id.(int)))
}
req := image.CreateMultiImageRequest{
Name: d.Get("name").(string),
TargetIDs: targetIDs,
AccountID: uint64(d.Get("account_id").(int)),
}
imageId, err := c.CloudBroker().Image().CreateMultiImage(ctx, req)
if err != nil {
return diag.FromErr(err)
}
d.SetId(strconv.FormatUint(imageId, 10))
d.Set("image_id", imageId)
var w dc.Warnings
if !d.Get("enabled").(bool) {
if err := resourceImageChangeEnabled(ctx, d, m); err != nil {
w.Add(err)
}
}
if _, ok := d.GetOk("shared_with"); ok {
if err := resourceImageShare(ctx, d, m); err != nil {
w.Add(err)
}
}
if _, ok := d.GetOk("computeci_id"); ok {
if err := resourceImageChangeComputeci(ctx, d, m); err != nil {
w.Add(err)
}
}
updReq := image.EditRequest{}
updReq.ImageID = uint64(d.Get("image_id").(int))
if _, ok := d.GetOk("username"); ok {
updReq.Username = d.Get("username").(string)
}
if _, ok := d.GetOk("password"); ok {
updReq.Password = d.Get("password").(string)
}
if _, ok := d.GetOk("account_id"); ok {
updReq.AccountID = uint64(d.Get("account_id").(int))
}
if _, ok := d.GetOkExists("bootable"); ok {
updReq.Bootable = d.Get("bootable").(bool)
}
if _, ok := d.GetOkExists("hot_resize"); ok {
updReq.HotResize = d.Get("hot_resize").(bool)
}
if _, ok := d.GetOk("network_interface_naming"); ok {
updReq.NetworkInterfaceNaming = d.Get("network_interface_naming").(string)
}
_, err = c.CloudBroker().Image().Edit(ctx, updReq)
if err != nil {
w.Add(err)
}
return append(resourceMultiImageRead(ctx, d, m), w.Get()...)
}
func resourceMultiImageRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Debugf("resourceMultiImageRead: called for %s id: %s", d.Get("name").(string), d.Id())
img, err := utilityImageCheckPresence(ctx, d, m)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
switch img.Status {
case status.Modeled:
return diag.Errorf("The image is in status: %s, please, contact support for more information", img.Status)
case status.Destroyed, status.Purged:
d.SetId("")
return diag.Errorf("The resource cannot be updated because it has been destroyed")
}
flattenImage(d, img)
return nil
}
func resourceMultiImageDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Debugf("resourceMultiImageDelete: called for %s, id: %s", d.Get("name").(string), d.Id())
_, err := utilityImageCheckPresence(ctx, d, m)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
c := m.(*controller.ControllerCfg)
req := image.DeleteRequest{
ImageID: uint64(d.Get("image_id").(int)),
}
_, err = c.CloudBroker().Image().Delete(ctx, req)
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
}
func resourceMultiImageUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Debugf("resourceMultiImageUpdate: called for %s, id: %s", d.Get("name").(string), d.Id())
img, err := utilityImageCheckPresence(ctx, d, m)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
switch img.Status {
case status.Modeled:
return diag.Errorf("The image is in status: %s, please, contact support for more information", img.Status)
case status.Destroyed, status.Purged:
d.SetId("")
return diag.Errorf("The resource cannot be updated because it has been destroyed")
}
if d.HasChange("enabled") {
err := resourceImageChangeEnabled(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("shared_with") {
err := resourceImageShare(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("computeci_id") {
err := resourceImageChangeComputeci(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChanges("name", "username", "password", "account_id", "bootable", "hot_resize") {
err := resourceImageEdit(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("target_ids") {
err := resourceMultiImageUpdateLinks(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
return resourceMultiImageRead(ctx, d, m)
}
func resourceMultiImageUpdateLinks(ctx context.Context, d *schema.ResourceData, m interface{}) error {
log.Debugf("resourceMultiImageUpdateLinks: called for %s, id: %s", d.Get("name").(string), d.Id())
c := m.(*controller.ControllerCfg)
imageID := uint64(d.Get("image_id").(int))
oldRaw, newRaw := d.GetChange("target_ids")
oldIDs := oldRaw.([]interface{})
newIDs := newRaw.([]interface{})
oldSet := make(map[uint64]bool)
for _, id := range oldIDs {
oldSet[uint64(id.(int))] = true
}
newSet := make(map[uint64]bool)
for _, id := range newIDs {
newSet[uint64(id.(int))] = true
}
toAdd := make([]uint64, 0)
for id := range newSet {
if !oldSet[id] {
toAdd = append(toAdd, id)
}
}
toRemove := make([]uint64, 0)
for id := range oldSet {
if !newSet[id] {
toRemove = append(toRemove, id)
}
}
if len(toAdd) > 0 {
req := image.MultiImageAddLinksRequest{
ImageID: imageID,
TargetIDs: toAdd,
}
_, err := c.CloudBroker().Image().MultiImageAddLinks(ctx, req)
if err != nil {
return err
}
}
if len(toRemove) > 0 {
req := image.MultiImageDelLinksRequest{
ImageID: imageID,
TargetIDs: toRemove,
}
_, err := c.CloudBroker().Image().MultiImageDelLinks(ctx, req)
if err != nil {
return err
}
}
return nil
}
func ResourceMultiImage() *schema.Resource {
return &schema.Resource{
SchemaVersion: 2,
CreateContext: resourceMultiImageCreate,
ReadContext: resourceMultiImageRead,
UpdateContext: resourceMultiImageUpdate,
DeleteContext: resourceMultiImageDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: &constants.Timeout600s,
Read: &constants.Timeout300s,
Update: &constants.Timeout300s,
Delete: &constants.Timeout300s,
Default: &constants.Timeout300s,
},
Schema: resourceMultiImageSchemaMake(),
}
}

View File

@@ -252,6 +252,10 @@ func dataSourceImageListSchemaMake() map[string]*schema.Schema {
Computed: true,
Description: "image id",
},
"independent": {
Type: schema.TypeBool,
Computed: true,
},
"last_modified": {
Type: schema.TypeInt,
Computed: true,
@@ -260,6 +264,13 @@ func dataSourceImageListSchemaMake() map[string]*schema.Schema {
Type: schema.TypeInt,
Computed: true,
},
"links_to": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"milestones": {
Type: schema.TypeInt,
Computed: true,
@@ -518,6 +529,10 @@ func dataSourceImageSchemaMake() map[string]*schema.Schema {
Computed: true,
Description: "Does this machine supports hot resize",
},
"independent": {
Type: schema.TypeBool,
Computed: true,
},
"last_modified": {
Type: schema.TypeInt,
Computed: true,
@@ -526,6 +541,13 @@ func dataSourceImageSchemaMake() map[string]*schema.Schema {
Type: schema.TypeInt,
Computed: true,
},
"links_to": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"milestones": {
Type: schema.TypeInt,
Computed: true,
@@ -751,10 +773,12 @@ func resourceCDROMImageSchemaMake() map[string]*schema.Schema {
},
"status": {
Type: schema.TypeString,
Computed: true},
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true},
Computed: true,
},
"user_group_id": {
Type: schema.TypeString,
Computed: true,
@@ -1037,10 +1061,12 @@ func resourceImageSchemaMake() map[string]*schema.Schema {
},
"status": {
Type: schema.TypeString,
Computed: true},
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true},
Computed: true,
},
"user_group_id": {
Type: schema.TypeString,
Computed: true,
@@ -1072,11 +1098,22 @@ func resourceImageSchemaMake() map[string]*schema.Schema {
Computed: true,
Description: "image id",
},
"independent": {
Type: schema.TypeBool,
Computed: true,
},
"link_to": {
Type: schema.TypeInt,
Computed: true,
Description: "",
},
"links_to": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"milestones": {
Type: schema.TypeInt,
Computed: true,
@@ -1334,6 +1371,10 @@ func resourceVirtualImageSchemaMake() map[string]*schema.Schema {
Computed: true,
Description: "Image id",
},
"independent": {
Type: schema.TypeBool,
Computed: true,
},
"last_modified": {
Type: schema.TypeInt,
Computed: true,
@@ -1425,6 +1466,259 @@ func resourceVirtualImageSchemaMake() map[string]*schema.Schema {
}
}
func resourceMultiImageSchemaMake() map[string]*schema.Schema {
return map[string]*schema.Schema{
"target_ids": {
Type: schema.TypeList,
Required: true,
Description: "IDs of real images to link this multi image to",
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the multi image",
},
"account_id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Account id to make the image exclusive",
},
"bootable": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
Description: "Does this image boot OS",
},
"computeci_id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"hot_resize": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
Description: "Does this machine supports hot resize",
},
"password": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Optional password for the image",
},
"username": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Optional username for the image",
},
"shared_with": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"unc_path": {
Type: schema.TypeString,
Computed: true,
Description: "unc path",
},
"acl": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"explicit": {
Type: schema.TypeBool,
Computed: true,
},
"guid": {
Type: schema.TypeString,
Computed: true,
},
"right": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"user_group_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"architecture": {
Type: schema.TypeString,
Computed: true,
},
"boot_type": {
Type: schema.TypeString,
Computed: true,
},
"deleted_time": {
Type: schema.TypeInt,
Computed: true,
},
"desc": {
Type: schema.TypeString,
Computed: true,
},
"drivers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"gid": {
Type: schema.TypeInt,
Computed: true,
},
"guid": {
Type: schema.TypeInt,
Computed: true,
},
"history": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"guid": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeInt,
Computed: true,
},
"timestamp": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
"image_id": {
Type: schema.TypeInt,
Computed: true,
},
"independent": {
Type: schema.TypeBool,
Computed: true,
},
"last_modified": {
Type: schema.TypeInt,
Computed: true,
},
"link_to": {
Type: schema.TypeInt,
Computed: true,
},
"links_to": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"milestones": {
Type: schema.TypeInt,
Computed: true,
},
"pool_name": {
Type: schema.TypeString,
Computed: true,
},
"present_to": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"provider_name": {
Type: schema.TypeString,
Computed: true,
},
"purge_attempts": {
Type: schema.TypeInt,
Computed: true,
},
"reference_id": {
Type: schema.TypeString,
Computed: true,
},
"res_id": {
Type: schema.TypeString,
Computed: true,
},
"res_name": {
Type: schema.TypeString,
Computed: true,
},
"rescuecd": {
Type: schema.TypeBool,
Computed: true,
},
"sep_id": {
Type: schema.TypeInt,
Computed: true,
},
"size": {
Type: schema.TypeInt,
Computed: true,
},
"snapshot_id": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"tech_status": {
Type: schema.TypeString,
Computed: true,
},
"to_clean": {
Type: schema.TypeBool,
Computed: true,
},
"image_type": {
Type: schema.TypeString,
Computed: true,
},
"url": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Computed: true,
},
}
}
func resourceImageFromBlankComputeSchemaMake() map[string]*schema.Schema {
return map[string]*schema.Schema{
"compute_id": {
@@ -1437,6 +1731,11 @@ func resourceImageFromBlankComputeSchemaMake() map[string]*schema.Schema {
Required: true,
Description: "Name of the rescue disk",
},
"storage_policy_id": {
Type: schema.TypeInt,
Required: true,
Description: "Storage policy ID",
},
"boot_type": {
Type: schema.TypeString,
Required: true,
@@ -1550,10 +1849,12 @@ func resourceImageFromBlankComputeSchemaMake() map[string]*schema.Schema {
},
"status": {
Type: schema.TypeString,
Computed: true},
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true},
Computed: true,
},
"user_group_id": {
Type: schema.TypeString,
Computed: true,
@@ -1620,6 +1921,13 @@ func resourceImageFromBlankComputeSchemaMake() map[string]*schema.Schema {
Type: schema.TypeInt,
Computed: true,
},
"links_to": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"milestones": {
Type: schema.TypeInt,
Computed: true,
@@ -1815,10 +2123,12 @@ func resourceImageFromPlatformDiskSchemaMake() map[string]*schema.Schema {
},
"status": {
Type: schema.TypeString,
Computed: true},
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true},
Computed: true,
},
"user_group_id": {
Type: schema.TypeString,
Computed: true,
@@ -1881,6 +2191,13 @@ func resourceImageFromPlatformDiskSchemaMake() map[string]*schema.Schema {
Type: schema.TypeInt,
Computed: true,
},
"links_to": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"milestones": {
Type: schema.TypeInt,
Computed: true,