This commit is contained in:
2025-11-18 16:20:26 +03:00
parent 4b3f21d9be
commit e42fbcef39
397 changed files with 17560 additions and 1501 deletions

View File

@@ -41,6 +41,7 @@ func flattenResgroup(d *schema.ResourceData, rgData *rg.RecordRG) {
d.Set("vins", rgData.VINS)
d.Set("computes", rgData.VMs)
d.Set("sdn_access_group_id", rgData.SDNAccessGroupID)
d.Set("storage_policy_ids", rgData.StoragePolicyIDs)
}
func flattenRgAcl(rgACLs rg.ListACL) []map[string]interface{} {
@@ -64,13 +65,14 @@ func flattenRgAcl(rgACLs rg.ListACL) []map[string]interface{} {
func flattenRgResourceLimits(rl rg.ResourceLimits) []map[string]interface{} {
res := make([]map[string]interface{}, 0)
temp := map[string]interface{}{
"cu_c": rl.CUC,
"cu_d": rl.CuD,
"cu_dm": rl.CUDM,
"cu_i": rl.CUI,
"cu_m": rl.CUM,
"cu_np": rl.CUNP,
"gpu_units": rl.GPUUnits,
"cu_c": rl.CUC,
"cu_d": rl.CuD,
"cu_dm": rl.CUDM,
"cu_i": rl.CUI,
"cu_m": rl.CUM,
"cu_np": rl.CUNP,
"gpu_units": rl.GPUUnits,
"storage_policy": flattenRgStoragePolicy(rl.StoragePolicies),
}
res = append(res, temp)
@@ -413,6 +415,7 @@ func flattenRgList(rgl *rg.ListRG) []map[string]interface{} {
"vins": rg.VINS,
"vms": rg.VMs,
"sdn_access_group_id": rg.SDNAccessGroupID,
"storage_policy_ids": rg.StoragePolicyIDs,
}
res = append(res, temp)
}
@@ -475,6 +478,7 @@ func flattenResourceRG(d *schema.ResourceData, rgData *rg.RecordRG) {
d.Set("gid", rgData.GID)
d.Set("rg_name", rgData.Name)
d.Set("resource_limits", flattenRgResourceLimits(rgData.ResourceLimits))
d.Set("storage_policy", flattenRgStoragePolicy(rgData.ResourceLimits.StoragePolicies))
d.Set("description", rgData.Description)
d.Set("uniq_pools", rgData.UniqPools)
d.Set("cpu_allocation_parameter", rgData.CPUAllocationParameter)
@@ -498,4 +502,19 @@ func flattenResourceRG(d *schema.ResourceData, rgData *rg.RecordRG) {
d.Set("vins", rgData.VINS)
d.Set("vms", rgData.VMs)
d.Set("sdn_access_group_id", rgData.SDNAccessGroupID)
d.Set("storage_policy_ids", rgData.StoragePolicyIDs)
}
func flattenRgStoragePolicy(spList []rg.StoragePolicy) []map[string]interface{} {
res := make([]map[string]interface{}, 0, len(spList))
for _, sp := range spList {
temp := map[string]interface{}{
"id": sp.ID,
"limit": sp.Limit,
}
res = append(res, temp)
}
return res
}

View File

@@ -141,6 +141,27 @@ func resourceResgroupCreate(ctx context.Context, d *schema.ResourceData, m inter
req.SDNAccessGroupID = sdnAccessGroupID.(string)
}
if storagePolicies, ok := d.GetOk("storage_policy"); ok {
var id uint64
var limit int
if storagePolicies.(*schema.Set).Len() > 0 {
spList := storagePolicies.(*schema.Set).List()
for _, spInterface := range spList {
sps := spInterface.(map[string]interface{})
id = uint64(sps["id"].(int))
limit = sps["limit"].(int)
spModel := rg.StoragePolicy{
ID: id,
Limit: limit,
}
req.StoragePolicies = append(req.StoragePolicies, spModel)
}
}
}
rgID, err := c.CloudBroker().RG().Create(ctx, req)
if err != nil {
d.SetId("")
@@ -367,50 +388,52 @@ func resourceResgroupUpdate(ctx context.Context, d *schema.ResourceData, m inter
}
if d.HasChange("resource_limits") {
resLimits := d.Get("resource_limits").([]interface{})[0]
resLimitsConv := resLimits.(map[string]interface{})
if resLimitsConv["cu_m"] != nil {
maxMemCap := int64(resLimitsConv["cu_m"].(float64))
if maxMemCap == 0 {
req.MaxMemoryCapacity = -1
} else {
req.MaxMemoryCapacity = maxMemCap
if _, ok := d.GetOk("resource_limits"); ok {
resLimits := d.Get("resource_limits").([]interface{})[0]
resLimitsConv := resLimits.(map[string]interface{})
if resLimitsConv["cu_m"] != nil {
maxMemCap := int64(resLimitsConv["cu_m"].(float64))
if maxMemCap == 0 {
req.MaxMemoryCapacity = -1
} else {
req.MaxMemoryCapacity = maxMemCap
}
}
}
if resLimitsConv["cu_dm"] != nil {
maxDiskCap := int64(resLimitsConv["cu_dm"].(float64))
if maxDiskCap == 0 {
req.MaxVDiskCapacity = -1
} else {
req.MaxVDiskCapacity = maxDiskCap
if resLimitsConv["cu_dm"] != nil {
maxDiskCap := int64(resLimitsConv["cu_dm"].(float64))
if maxDiskCap == 0 {
req.MaxVDiskCapacity = -1
} else {
req.MaxVDiskCapacity = maxDiskCap
}
}
}
if resLimitsConv["cu_c"] != nil {
maxCPUCap := int64(resLimitsConv["cu_c"].(float64))
if maxCPUCap == 0 {
req.MaxCPUCapacity = -1
} else {
req.MaxCPUCapacity = maxCPUCap
if resLimitsConv["cu_c"] != nil {
maxCPUCap := int64(resLimitsConv["cu_c"].(float64))
if maxCPUCap == 0 {
req.MaxCPUCapacity = -1
} else {
req.MaxCPUCapacity = maxCPUCap
}
}
}
if resLimitsConv["cu_i"] != nil {
maxNumPublicIP := int64(resLimitsConv["cu_i"].(float64))
if maxNumPublicIP == 0 {
req.MaxNumPublicIP = -1
} else {
req.MaxNumPublicIP = maxNumPublicIP
if resLimitsConv["cu_i"] != nil {
maxNumPublicIP := int64(resLimitsConv["cu_i"].(float64))
if maxNumPublicIP == 0 {
req.MaxNumPublicIP = -1
} else {
req.MaxNumPublicIP = maxNumPublicIP
}
}
}
if resLimitsConv["cu_np"] != nil {
maxNP := int64(resLimitsConv["cu_np"].(float64))
if maxNP == 0 {
req.MaxNetworkPeerTransfer = -1
} else {
req.MaxNetworkPeerTransfer = maxNP
if resLimitsConv["cu_np"] != nil {
maxNP := int64(resLimitsConv["cu_np"].(float64))
if maxNP == 0 {
req.MaxNetworkPeerTransfer = -1
} else {
req.MaxNetworkPeerTransfer = maxNP
}
}
}
doGeneralUpdate = true
doGeneralUpdate = true
}
}
if d.HasChange("description") {
@@ -418,6 +441,31 @@ func resourceResgroupUpdate(ctx context.Context, d *schema.ResourceData, m inter
doGeneralUpdate = true
}
if d.HasChange("storage_policy") {
needUpdate, updateEl, err := utilityRGUpdateStPolicy(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
if needUpdate {
// spList := storagePolicies.(*schema.Set).List()
var id uint64
var limit int
for _, spInterface := range updateEl {
// sps := spInterface.(map[string]interface{})
id = uint64(spInterface["id"].(int))
limit = spInterface["limit"].(int)
spModel := rg.StoragePolicy{
ID: id,
Limit: limit,
}
req.StoragePolicies = append(req.StoragePolicies, spModel)
}
doGeneralUpdate = needUpdate
}
}
if d.HasChange("uniq_pools") {
uniqPools := d.Get("uniq_pools").([]interface{})
if len(uniqPools) == 0 {
@@ -721,6 +769,10 @@ func ResourceResgroup() *schema.Resource {
if diff.HasChange("def_net") {
diff.SetNewComputed("def_net_id")
}
if diff.HasChange("storage_policy") {
diff.SetNewComputed("storage_policy_ids")
diff.SetNewComputed("resource_limits")
}
if diff.HasChanges() {
diff.SetNewComputed("updated_by")
diff.SetNewComputed("updated_time")

View File

@@ -152,6 +152,22 @@ func dataSourceRgSchemaMake() map[string]*schema.Schema {
Type: schema.TypeFloat,
Computed: true,
},
"storage_policy": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"limit": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
},
},
@@ -203,6 +219,13 @@ func dataSourceRgSchemaMake() map[string]*schema.Schema {
Type: schema.TypeString,
Computed: true,
},
"storage_policy_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
}
}
@@ -1654,6 +1677,23 @@ func dataSourceRgListDeletedSchemaMake() map[string]*schema.Schema {
Type: schema.TypeFloat,
Computed: true,
},
"storage_policy": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Required: true,
},
"limit": {
Type: schema.TypeInt,
Optional: true,
},
},
},
},
},
},
},
@@ -1701,6 +1741,13 @@ func dataSourceRgListDeletedSchemaMake() map[string]*schema.Schema {
Type: schema.TypeString,
},
},
"storage_policy_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
},
},
},
@@ -1924,6 +1971,23 @@ func dataSourceRgListSchemaMake() map[string]*schema.Schema {
Type: schema.TypeFloat,
Computed: true,
},
"storage_policy": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Required: true,
},
"limit": {
Type: schema.TypeInt,
Optional: true,
},
},
},
},
},
},
},
@@ -1975,6 +2039,13 @@ func dataSourceRgListSchemaMake() map[string]*schema.Schema {
Type: schema.TypeString,
Computed: true,
},
"storage_policy_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
},
},
},
@@ -2244,6 +2315,22 @@ func resourceRgSchemaMake() map[string]*schema.Schema {
Type: schema.TypeFloat,
Computed: true,
},
"storage_policy": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"limit": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
},
},
@@ -2392,6 +2479,24 @@ func resourceRgSchemaMake() map[string]*schema.Schema {
Computed: true,
Description: "ID of the SDN access group",
},
"storage_policy": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Required: true,
},
"limit": {
Type: schema.TypeInt,
Optional: true,
Default: -1,
},
},
},
},
"acl": {
Type: schema.TypeList,
@@ -2510,6 +2615,14 @@ func resourceRgSchemaMake() map[string]*schema.Schema {
},
Description: "List of VM ids in this resource group.",
},
"storage_policy_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
}
return res

View File

@@ -36,6 +36,7 @@ import (
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
log "github.com/sirupsen/logrus"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/rg"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)
@@ -58,3 +59,92 @@ func utilityResgroupCheckPresence(ctx context.Context, d *schema.ResourceData, m
return rgData, nil
}
func utilityRGUpdateStPolicy(ctx context.Context, d *schema.ResourceData, m interface{}) (bool, []map[string]interface{}, error) {
c := m.(*controller.ControllerCfg)
rgId, _ := strconv.ParseUint(d.Id(), 10, 64)
addSP, delSP, updateSP := utilityGetStoragePolicyUpdate(ctx, d, m)
needUpdate := len(updateSP) > 0
if len(addSP) > 0 {
for _, spMap := range addSP {
req := rg.AddStoragePolicyRequest{
RGID: rgId,
StoragePolicyID: uint64(spMap["id"].(int)),
Limit: spMap["limit"].(int),
}
log.Debugf("utilityRGUpdateStPolicy: starting to add storage policy ID:%d for rg %d", req.StoragePolicyID, req.RGID)
_, err := c.CloudBroker().RG().AddStoragePolicy(ctx, req)
if err != nil {
return needUpdate, updateSP, err
}
}
}
if len(delSP) > 0 {
for _, spMap := range delSP {
req := rg.DelStoragePolicyRequest{
RGID: rgId,
StoragePolicyID: uint64(spMap["id"].(int)),
}
log.Debugf("utilityRGUpdateStPolicy: starting to delete storage policy ID:%d from rg %d", req.StoragePolicyID, req.RGID)
_, err := c.CloudBroker().RG().DelStoragePolicy(ctx, req)
if err != nil {
return needUpdate, updateSP, err
}
}
}
return needUpdate, updateSP, nil
}
func utilityGetStoragePolicyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) (added, deleted, updated []map[string]interface{}) {
added = make([]map[string]interface{}, 0)
deleted = make([]map[string]interface{}, 0)
updated = make([]map[string]interface{}, 0)
oldSet, newSet := d.GetChange("storage_policy")
oldList := oldSet.(*schema.Set).List()
newList := newSet.(*schema.Set).List()
for _, oldSP := range oldList {
oldMap := oldSP.(map[string]interface{})
found := false
for _, newSP := range newList {
newMap := newSP.(map[string]interface{})
if oldMap["id"] == newMap["id"] {
found = true
if oldMap["limit"] != newMap["limit"] {
updated = append(added, newMap)
}
break
}
if found {
break
}
}
if found {
continue
}
deleted = append(deleted, oldMap)
}
for _, newSP := range newList {
newMap := newSP.(map[string]interface{})
found := false
for _, oldSP := range oldList {
oldMap := oldSP.(map[string]interface{})
if oldMap["id"] == newMap["id"] {
found = true
break
}
}
if found {
continue
}
added = append(added, newMap)
}
return
}