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

@@ -269,6 +269,23 @@ func resourceLimitsSchemaMake() 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,
},
},
},
},
}
return res
@@ -418,6 +435,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,
},
},
}
return res
}

View File

@@ -265,6 +265,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,
},
},
},
},
},

View File

@@ -250,6 +250,13 @@ func dataSourceRgListDeletedSchemaMake() map[string]*schema.Schema {
Type: schema.TypeString,
},
},
"storage_policy_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
},
},
},

View File

@@ -94,6 +94,7 @@ func flattenResgroup(d *schema.ResourceData, details rg.RecordResourceGroup) err
d.Set("account_name", details.AccountName)
d.Set("acl", flattenRgAcl(details.ACL))
d.Set("compute_features", details.ComputeFeatures)
d.Set("storage_policy", flattenRgStoragePolicy(details.ResourceLimits.StoragePolicies))
d.Set("vms", details.Computes)
d.Set("created_by", details.CreatedBy)
d.Set("created_time", details.CreatedTime)
@@ -116,6 +117,8 @@ func flattenResgroup(d *schema.ResourceData, details rg.RecordResourceGroup) err
d.Set("cpu_allocation_parameter", details.CPUAllocationParameter)
d.Set("cpu_allocation_ratio", details.CPUAllocationRatio)
d.Set("sdn_access_group_id", details.SDNAccessGroupID)
d.Set("resource_limits", flattenRgResourceLimits(details.ResourceLimits))
d.Set("storage_policy_ids", details.StoragePolicyIDs)
return nil
}
@@ -204,6 +207,7 @@ func flattenRg(d *schema.ResourceData, itemRg rg.RecordResourceGroup) {
d.Set("cpu_allocation_parameter", itemRg.CPUAllocationParameter)
d.Set("cpu_allocation_ratio", itemRg.CPUAllocationRatio)
d.Set("sdn_access_group_id", itemRg.SDNAccessGroupID)
d.Set("storage_policy_ids", itemRg.StoragePolicyIDs)
}
func flattenRgAudits(rgAudits rg.ListAudits) []map[string]interface{} {
@@ -257,6 +261,7 @@ func flattenRgList(rgl *rg.ListResourceGroups) []map[string]interface{} {
"cpu_allocation_parameter": rg.CPUAllocationParameter,
"cpu_allocation_ratio": rg.CPUAllocationRatio,
"sdn_access_group_id": rg.SDNAccessGroupID,
"storage_policy_ids": rg.StoragePolicyIDs,
}
res = append(res, temp)
}
@@ -282,19 +287,34 @@ 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_i": rl.CUI,
"cu_m": rl.CUM,
"cu_dm": rl.CUDM,
"cu_np": rl.CUNP,
"gpu_units": rl.GPUUnits,
"cu_c": rl.CUC,
"cu_d": rl.CUD,
"cu_i": rl.CUI,
"cu_m": rl.CUM,
"cu_dm": rl.CUDM,
"cu_np": rl.CUNP,
"gpu_units": rl.GPUUnits,
"storage_policy": flattenRgStoragePolicy(rl.StoragePolicies),
}
res = append(res, temp)
return res
}
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
}
func flattenRules(list rg.ListRules) []map[string]interface{} {
res := make([]map[string]interface{}, 0, len(list))
for _, rule := range list {

View File

@@ -164,6 +164,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)
}
}
}
apiResp, err := c.CloudAPI().RG().Create(ctx, req)
if err != nil {
return diag.FromErr(err)
@@ -404,7 +425,16 @@ func resourceResgroupUpdate(ctx context.Context, d *schema.ResourceData, m inter
return diag.FromErr(fmt.Errorf("resourceResgroupUpdate: RG ID %s: changing ext_net_id for existing RG is not allowed", d.Id()))
}
if d.HasChanges("name", "quota", "description", "uniq_pools") {
needUpdateSP := false
if d.HasChange("storage_policy") {
needUpdate, err := utilityRGUpdateStPolicy(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
needUpdateSP = needUpdate
}
if d.HasChanges("name", "quota", "description", "uniq_pools") || needUpdateSP {
if err := utilityUpdateRG(ctx, d, m, rgData.ID); err != nil {
return diag.FromErr(err)
}
@@ -655,7 +685,6 @@ func ResourceRgSchemaMake() map[string]*schema.Schema {
},
},
},
"def_net": {
Type: schema.TypeSet,
Optional: true,
@@ -677,7 +706,24 @@ func ResourceRgSchemaMake() map[string]*schema.Schema {
},
},
},
"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,
},
},
},
},
"description": {
Type: schema.TypeString,
Optional: true,
@@ -835,6 +881,65 @@ func ResourceRgSchemaMake() map[string]*schema.Schema {
Type: schema.TypeFloat,
Computed: true,
},
"storage_policy_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"resource_limits": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cu_c": {
Type: schema.TypeFloat,
Computed: true,
},
"cu_d": {
Type: schema.TypeFloat,
Computed: true,
},
"cu_dm": {
Type: schema.TypeFloat,
Computed: true,
},
"cu_i": {
Type: schema.TypeFloat,
Computed: true,
},
"cu_m": {
Type: schema.TypeFloat,
Computed: true,
},
"cu_np": {
Type: schema.TypeFloat,
Computed: true,
},
"gpu_units": {
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,
},
},
},
},
},
},
},
}
}
@@ -863,6 +968,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

@@ -124,6 +124,31 @@ func utilityUpdateRG(ctx context.Context, d *schema.ResourceData, m interface{},
}
}
if d.HasChange("storage_policy") {
log.Debugf("resourceResgroupUpdate: storage policies specified.")
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)
}
}
}
}
if d.HasChange("description") {
log.Debugf("resourceResgroupUpdate: description specified - looking for deltas from the old settings.")
req.Description = d.Get("description").(string)
@@ -139,9 +164,112 @@ func utilityUpdateRG(ctx context.Context, d *schema.ResourceData, m interface{},
}
}
_, _, updateStPolicy := utilityGetStoragePolicyUpdate(ctx, d, m)
if len(updateStPolicy) != 0 {
storagePolicies := make([]rg.StoragePolicy, 0, len(updateStPolicy))
for _, stPolicyVal := range updateStPolicy {
reqStPolicy := rg.StoragePolicy{
ID: uint64(stPolicyVal["id"].(int)),
Limit: stPolicyVal["limit"].(int),
}
storagePolicies = append(storagePolicies, reqStPolicy)
}
req.StoragePolicies = storagePolicies
}
_, err := c.CloudAPI().RG().Update(ctx, req)
if err != nil {
return err
}
return nil
}
func utilityRGUpdateStPolicy(ctx context.Context, d *schema.ResourceData, m interface{}) (bool, 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 resource group %d", req.StoragePolicyID, req.RGID)
_, err := c.CloudAPI().RG().AddStoragePolicy(ctx, req)
if err != nil {
return needUpdate, 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 resource group %d", req.StoragePolicyID, req.RGID)
_, err := c.CloudAPI().RG().DelStoragePolicy(ctx, req)
if err != nil {
return needUpdate, err
}
}
}
return needUpdate, 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
}