This commit is contained in:
2026-06-15 13:23:15 +03:00
parent 6b82f23445
commit e459fcb4c4
19 changed files with 268 additions and 104 deletions

View File

@@ -33,6 +33,7 @@ package vfpool
import (
"context"
"fmt"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -85,6 +86,12 @@ func resourceVFPoolCreate(ctx context.Context, d *schema.ResourceData, m interfa
}
config, configOk := d.GetOk("config")
enableVal := d.Get("enable").(bool)
if enableVal && !configOk {
return diag.FromErr(fmt.Errorf("enable requires config to be set"))
}
if configOk {
configArray := config.(*schema.Set).List()
req.Config = make([]vfpool.Config, 0, len(configArray))
@@ -117,10 +124,8 @@ func resourceVFPoolCreate(ctx context.Context, d *schema.ResourceData, m interfa
warnings := dc.Warnings{}
if enable, ok := d.GetOk("enable"); ok {
if err := utilityVFPoolEnabled(ctx, m, enable.(bool), vfPoolID, configOk); err != nil {
warnings.Add(err)
}
if err := utilityVFPoolEnabled(ctx, m, enableVal, vfPoolID); err != nil {
warnings.Add(err)
}
log.Debugf("resourceVFPoolCreate: create VFPool with ID: %d, complete", vfPoolID)
@@ -147,20 +152,12 @@ func resourceVFPoolUpdate(ctx context.Context, d *schema.ResourceData, m interfa
log.Debugf("resourceVFPoolUpdate: called VFPool with id %d", vfPoolID)
_, ok := d.GetOk("config")
if d.HasChanges("name,", "description", "account_access", "rg_access,", "config") {
if d.HasChanges("name", "description", "account_access", "rg_access", "config", "enable") {
if err := utilityVFPoolUpdate(ctx, d, m, vfPoolID); err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("enable") {
if err := utilityVFPoolEnabled(ctx, m, d.Get("enable").(bool), vfPoolID, ok); err != nil {
return diag.FromErr(err)
}
}
log.Debugf("resourceVFPoolUpdate: update VFPool with id %d, complete", vfPoolID)
return resourceVFPoolRead(ctx, d, m)

View File

@@ -64,37 +64,26 @@ func utilityVFpoolCheckPresence(ctx context.Context, d *schema.ResourceData, m i
return vfpoolData, nil
}
func utilityVFPoolEnabled(ctx context.Context, m interface{}, enable bool, vfPoolID uint64, configOk bool) error {
func utilityVFPoolEnabled(ctx context.Context, m interface{}, enable bool, vfPoolID uint64) error {
c := m.(*controller.ControllerCfg)
if enable && configOk {
req := vfpool.EnableRequest{
VFPoolID: vfPoolID,
}
_, err := c.CloudBroker().VFPool().Enable(ctx, req)
if err != nil {
return err
}
}
if enable && !configOk {
return fmt.Errorf("you must provide configuration for this resource, after enabling it")
}
if !enable {
req := vfpool.DisableRequest{
VFPoolID: vfPoolID,
}
_, err := c.CloudBroker().VFPool().Disable(ctx, req)
if err != nil {
return err
}
var err error
if enable {
_, err = c.CloudBroker().VFPool().Enable(ctx, vfpool.EnableRequest{VFPoolID: vfPoolID})
} else {
_, err = c.CloudBroker().VFPool().Disable(ctx, vfpool.DisableRequest{VFPoolID: vfPoolID})
}
log.Debugf("utilityVFPoolEnabled: enable=%v vfPool ID %d after completing its resource configuration", enable, vfPoolID)
return nil
return err
}
func utilityVFPoolUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, vfPoolID uint64) error {
hasConfig := len(d.Get("config").(*schema.Set).List()) > 0
if d.Get("enable").(bool) && !hasConfig {
return fmt.Errorf("enable requires config to be set")
}
c := m.(*controller.ControllerCfg)
vfPool, err := utilityVFpoolCheckPresence(ctx, d, m)
@@ -190,7 +179,7 @@ func utilityVFPoolUpdate(ctx context.Context, d *schema.ResourceData, m interfac
}
log.Debugf("utilityVFPoolUpdate: update vfPool with ID: %d, complete with params=%v", vfPoolID, req)
if len(d.Get("config").(*schema.Set).List()) > 0 && d.Get("enable").(bool) {
if hasConfig && d.Get("enable").(bool) {
reqEnable := vfpool.EnableRequest{
VFPoolID: vfPoolID,
}
@@ -201,8 +190,6 @@ func utilityVFPoolUpdate(ctx context.Context, d *schema.ResourceData, m interfac
return err
}
log.Debugf("utilityVFPoolUpdate: enable vfPool with ID: %d, complete", vfPoolID)
} else {
return (fmt.Errorf("the vfPool is not enabled after update, you must provide configuration for this resource, after enabling it"))
}
return nil