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

@@ -3,16 +3,13 @@ package account
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/account"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/flattens"
)
func flattenResourceAccount(d *schema.ResourceData, acc *account.RecordAccount) {
d.Set("dc_location", acc.DCLocation)
d.Set("ckey", acc.CKey)
d.Set("acl", flattenAccAcl(acc.ACL))
d.Set("company", acc.Company)
d.Set("companyurl", acc.CompanyURL)
d.Set("compute_features", acc.ComputeFeatures)
d.Set("cpu_allocation_parameter", acc.CPUAllocationParameter)
d.Set("cpu_allocation_ratio", acc.CPUAllocationRatio)
d.Set("created_by", acc.CreatedBy)
@@ -27,6 +24,7 @@ func flattenResourceAccount(d *schema.ResourceData, acc *account.RecordAccount)
d.Set("account_id", acc.ID)
d.Set("account_name", acc.Name)
d.Set("resource_limits", flattenRgResourceLimits(acc.ResourceLimits))
d.Set("storage_policy", flattenSTPolicy(acc.ResourceLimits.StoragePolicies))
d.Set("resource_types", acc.ResTypes)
d.Set("send_access_emails", acc.SendAccessEmails)
d.Set("status", acc.Status)
@@ -34,12 +32,11 @@ func flattenResourceAccount(d *schema.ResourceData, acc *account.RecordAccount)
d.Set("updated_time", acc.UpdatedTime)
d.Set("version", acc.Version)
d.Set("vins", acc.VINS)
d.Set("zone_ids", acc.ZoneIDs)
d.Set("zone_ids", flattenZonesInResource(acc.ZoneIDs))
}
func flattenDataAccount(d *schema.ResourceData, acc *account.RecordAccount) {
d.Set("dc_location", acc.DCLocation)
d.Set("ckey", acc.CKey)
d.Set("acl", flattenAccAcl(acc.ACL))
d.Set("company", acc.Company)
d.Set("companyurl", acc.CompanyURL)
@@ -142,11 +139,26 @@ func flattenAccResource(r account.Resource) []map[string]interface{} {
"gpu": r.GPU,
"ram": r.RAM,
"seps": flattenAccountSeps(r.SEPs),
"policies": flattenAccountPolicies(r.Policies),
}
res = append(res, temp)
return res
}
func flattenAccountPolicies(policies map[string]account.Policy) []map[string]interface{} {
res := make([]map[string]interface{}, 0)
for k, dataVal := range policies {
temp := map[string]interface{}{
"id": k,
"disk_size": dataVal.DiskSize,
"disk_size_max": dataVal.DiskSizeMax,
"seps": flattenAccountSeps(dataVal.SEPs),
}
res = append(res, temp)
}
return res
}
func flattenAccAcl(acls []account.ACLWithEmails) []map[string]interface{} {
res := make([]map[string]interface{}, 0, len(acls))
for _, acls := range acls {
@@ -172,19 +184,32 @@ func flattenAccAcl(acls []account.ACLWithEmails) []map[string]interface{} {
func flattenRgResourceLimits(rl account.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": flattenSTPolicy(rl.StoragePolicies),
}
res = append(res, temp)
return res
}
func flattenSTPolicy(ast []account.StoragePolicy) []map[string]interface{} {
res := make([]map[string]interface{}, 0, len(ast))
for _, item := range ast {
temp := map[string]interface{}{
"id": item.ID,
"limit": item.Limit,
}
res = append(res, temp)
}
return res
}
func flattenRgAcl(rgAcls []account.ACL) []map[string]interface{} {
res := make([]map[string]interface{}, len(rgAcls))
for _, rgAcl := range rgAcls {
@@ -206,8 +231,6 @@ func flattenListDeleted(al *account.ListAccounts) []map[string]interface{} {
for _, acc := range al.Data {
temp := map[string]interface{}{
"dc_location": acc.DCLocation,
"ckey": acc.CKey,
"meta": flattens.FlattenMeta(acc.Meta),
"acl": flattenRgAcl(acc.ACL),
"company": acc.Company,
"companyurl": acc.CompanyURL,
@@ -228,6 +251,7 @@ func flattenListDeleted(al *account.ListAccounts) []map[string]interface{} {
"resource_types": acc.ResTypes,
"send_access_emails": acc.SendAccessEmails,
"status": acc.Status,
"storage_policy_ids": acc.StoragePolicyIDs,
"uniq_pools": acc.UniqPools,
"default_zone_id": acc.DefaultZoneID,
"zone_ids": acc.ZoneIDs,
@@ -245,8 +269,6 @@ func flattenAccountList(al *account.ListAccounts) []map[string]interface{} {
for _, acc := range al.Data {
temp := map[string]interface{}{
"dc_location": acc.DCLocation,
"ckey": acc.CKey,
"meta": flattens.FlattenMeta(acc.Meta),
"acl": flattenRgAcl(acc.ACL),
"company": acc.Company,
"companyurl": acc.CompanyURL,
@@ -267,6 +289,7 @@ func flattenAccountList(al *account.ListAccounts) []map[string]interface{} {
"resource_types": acc.ResTypes,
"send_access_emails": acc.SendAccessEmails,
"status": acc.Status,
"storage_policy_ids": acc.StoragePolicyIDs,
"uniq_pools": acc.UniqPools,
"default_zone_id": acc.DefaultZoneID,
"zone_ids": acc.ZoneIDs,
@@ -452,3 +475,11 @@ func flattenZones(zones []account.ZoneID) []map[string]interface{} {
}
return res
}
func flattenZonesInResource(zones []account.ZoneID) []int64 {
res := make([]int64, 0)
for _, zone := range zones {
res = append(res, zone.ID)
}
return res
}

View File

@@ -144,6 +144,23 @@ func resourceAccountCreate(ctx context.Context, d *schema.ResourceData, m interf
}
}
if stPolicy, ok := d.GetOk("storage_policy"); ok {
sp := stPolicy.(*schema.Set).List()
storagePolicies := make([]account.StoragePolicy, 0)
for _, elem := range sp {
stPolicyVal := elem.(map[string]interface{})
reqStPolicy := account.StoragePolicy{
ID: uint64(stPolicyVal["id"].(int)),
Limit: stPolicyVal["limit"].(int),
}
storagePolicies = append(storagePolicies, reqStPolicy)
}
req.StoragePolicies = storagePolicies
}
if compFeaturesInterface, ok := d.GetOk("compute_features"); ok {
compFeaturesInterfaces := compFeaturesInterface.(*schema.Set).List()
@@ -372,7 +389,16 @@ func resourceAccountUpdate(ctx context.Context, d *schema.ResourceData, m interf
}
}
if d.HasChanges("account_name", "send_access_emails", "uniq_pools", "resource_limits", "desc", "default_zone_id") {
needUpdateSP := false
if d.HasChange("storage_policy") {
needUpdate, err := utilityAccountUpdateStPolicy(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
needUpdateSP = needUpdate
}
if d.HasChanges("account_name", "send_access_emails", "uniq_pools", "resource_limits", "desc", "default_zone_id") || needUpdateSP {
if err := utilityAccountUpdate(ctx, d, m); err != nil {
return diag.FromErr(err)
}
@@ -411,14 +437,18 @@ func resourceAccountUpdate(ctx context.Context, d *schema.ResourceData, m interf
if d.HasChange("zone_ids") {
old, new := d.GetChange("zone_ids")
//toAddSet := old.(*schema.Set).Difference(new.(*schema.Set))
toRemoveSet := new.(*schema.Set).Difference(old.(*schema.Set))
toRemoveSet := old.(*schema.Set).Difference(new.(*schema.Set))
toAddSet := new.(*schema.Set).Difference(old.(*schema.Set))
/*if err := utilityZoneIDsUpdate(ctx, d, m, toAddSet); err != nil {
return diag.FromErr(err)
}*/
if err := utilityZoneIDsRemove(ctx, d, m, toRemoveSet); err != nil {
return diag.FromErr(err)
if len(toAddSet.List()) > 0 {
if err := utilityZoneIDsAdd(ctx, d, m, toAddSet); err != nil {
return diag.FromErr(err)
}
}
if len(toRemoveSet.List()) > 0 {
if err := utilityZoneIDsRemove(ctx, d, m, toRemoveSet); err != nil {
return diag.FromErr(err)
}
}
}

View File

@@ -39,7 +39,7 @@ func resourceAccountSchemaMake() map[string]*schema.Schema {
Description: "if true send emails when a user is granted access to resources",
},
"zone_ids": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{
@@ -148,6 +148,39 @@ func resourceAccountSchemaMake() map[string]*schema.Schema {
Optional: true,
Computed: true,
},
"storage_policy": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"limit": {
Type: schema.TypeInt,
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,
Required: true,
},
},
},
},
@@ -168,17 +201,6 @@ func resourceAccountSchemaMake() map[string]*schema.Schema {
Type: schema.TypeString,
Computed: true,
},
"ckey": {
Type: schema.TypeString,
Computed: true,
},
// "meta": {
// Type: schema.TypeList,
// Computed: true,
// Elem: &schema.Schema{
// Type: schema.TypeString,
// },
// },
"acl": {
Type: schema.TypeList,
Computed: true,
@@ -523,17 +545,6 @@ func dataSourceAccountListDeletedSchemaMake() map[string]*schema.Schema {
Type: schema.TypeString,
Computed: true,
},
"ckey": {
Type: schema.TypeString,
Computed: true,
},
"meta": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"acl": {
Type: schema.TypeList,
Computed: true,
@@ -662,6 +673,22 @@ func dataSourceAccountListDeletedSchemaMake() map[string]*schema.Schema {
Type: schema.TypeFloat,
Computed: true,
},
"storage_policy": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"limit": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
},
},
@@ -680,6 +707,13 @@ func dataSourceAccountListDeletedSchemaMake() map[string]*schema.Schema {
Type: schema.TypeString,
Computed: true,
},
"storage_policy_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"default_zone_id": {
Type: schema.TypeInt,
Computed: true,
@@ -998,6 +1032,50 @@ func dataSourceAccountResourceConsumptionGetSchemaMake() map[string]*schema.Sche
Type: schema.TypeInt,
Computed: true,
},
"policies": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"disk_size": {
Type: schema.TypeFloat,
Computed: true,
},
"disk_size_max": {
Type: schema.TypeFloat,
Computed: true,
},
"seps": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"sep_id": {
Type: schema.TypeString,
Computed: true,
},
"data_name": {
Type: schema.TypeString,
Computed: true,
},
"disk_size": {
Type: schema.TypeFloat,
Computed: true,
},
"disk_size_max": {
Type: schema.TypeFloat,
Computed: true,
},
},
},
},
},
},
},
"ram": {
Type: schema.TypeInt,
Computed: true,
@@ -1058,6 +1136,50 @@ func dataSourceAccountResourceConsumptionGetSchemaMake() map[string]*schema.Sche
Type: schema.TypeInt,
Computed: true,
},
"policies": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"disk_size": {
Type: schema.TypeFloat,
Computed: true,
},
"disk_size_max": {
Type: schema.TypeFloat,
Computed: true,
},
"seps": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"sep_id": {
Type: schema.TypeString,
Computed: true,
},
"data_name": {
Type: schema.TypeString,
Computed: true,
},
"disk_size": {
Type: schema.TypeFloat,
Computed: true,
},
"disk_size_max": {
Type: schema.TypeFloat,
Computed: true,
},
},
},
},
},
},
},
"ram": {
Type: schema.TypeInt,
Computed: true,
@@ -1167,6 +1289,11 @@ func dataSourceAccountListSchemaMake() map[string]*schema.Schema {
Optional: true,
Description: "Page size",
},
"zone_id": {
Type: schema.TypeInt,
Optional: true,
Description: "Zone ID",
},
"items": {
Type: schema.TypeList,
Computed: true,
@@ -1176,17 +1303,6 @@ func dataSourceAccountListSchemaMake() map[string]*schema.Schema {
Type: schema.TypeString,
Computed: true,
},
"ckey": {
Type: schema.TypeString,
Computed: true,
},
"meta": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"acl": {
Type: schema.TypeList,
Computed: true,
@@ -1319,6 +1435,22 @@ func dataSourceAccountListSchemaMake() map[string]*schema.Schema {
Type: schema.TypeFloat,
Computed: true,
},
"storage_policy": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"limit": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
},
},
@@ -1337,6 +1469,13 @@ func dataSourceAccountListSchemaMake() map[string]*schema.Schema {
Type: schema.TypeString,
Computed: true,
},
"storage_policy_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"uniq_pools": {
Type: schema.TypeList,
Computed: true,
@@ -1417,6 +1556,50 @@ func dataSourceAccountResourceConsumptionListSchemaMake() map[string]*schema.Sch
Type: schema.TypeInt,
Computed: true,
},
"policies": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"disk_size": {
Type: schema.TypeFloat,
Computed: true,
},
"disk_size_max": {
Type: schema.TypeFloat,
Computed: true,
},
"seps": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"sep_id": {
Type: schema.TypeString,
Computed: true,
},
"data_name": {
Type: schema.TypeString,
Computed: true,
},
"disk_size": {
Type: schema.TypeFloat,
Computed: true,
},
"disk_size_max": {
Type: schema.TypeFloat,
Computed: true,
},
},
},
},
},
},
},
"ram": {
Type: schema.TypeInt,
Computed: true,
@@ -1477,6 +1660,50 @@ func dataSourceAccountResourceConsumptionListSchemaMake() map[string]*schema.Sch
Type: schema.TypeInt,
Computed: true,
},
"policies": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"disk_size": {
Type: schema.TypeFloat,
Computed: true,
},
"disk_size_max": {
Type: schema.TypeFloat,
Computed: true,
},
"seps": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"sep_id": {
Type: schema.TypeString,
Computed: true,
},
"data_name": {
Type: schema.TypeString,
Computed: true,
},
"disk_size": {
Type: schema.TypeFloat,
Computed: true,
},
"disk_size_max": {
Type: schema.TypeFloat,
Computed: true,
},
},
},
},
},
},
},
"ram": {
Type: schema.TypeInt,
Computed: true,
@@ -1961,16 +2188,12 @@ func dataSourceAccountSchemaMake() map[string]*schema.Schema {
Type: schema.TypeString,
Computed: true,
},
"ckey": {
Type: schema.TypeString,
Computed: true,
},
"default_zone_id": {
Type: schema.TypeInt,
Computed: true,
},
"zone_ids": {
Type: schema.TypeList,
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
@@ -2116,6 +2339,22 @@ func dataSourceAccountSchemaMake() map[string]*schema.Schema {
Type: schema.TypeFloat,
Computed: true,
},
"storage_policy": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"limit": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
},
},
@@ -2156,6 +2395,13 @@ func dataSourceAccountSchemaMake() map[string]*schema.Schema {
Type: schema.TypeInt,
},
},
"storage_policy_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
}
}

View File

@@ -211,7 +211,8 @@ func utilityAccountUpdate(ctx context.Context, d *schema.ResourceData, m interfa
accountId, _ := strconv.ParseUint(d.Id(), 10, 64)
req := account.UpdateRequest{
AccountID: accountId,
AccountID: accountId,
SendAccessEmails: d.Get("send_access_emails").(bool),
}
if d.HasChange("account_name") {
@@ -222,18 +223,14 @@ func utilityAccountUpdate(ctx context.Context, d *schema.ResourceData, m interfa
req.Description = d.Get("desc").(string)
}
if d.HasChange("send_access_emails") {
req.SendAccessEmails = d.Get("send_access_emails").(bool)
}
if d.HasChange("default_zone_id") {
req.DefaultZoneID = uint64(d.Get("default_zone_id").(int))
}
if d.HasChange("uniq_pools") {
uniq_pools := d.Get("uniq_pools").([]interface{})
uniqPools := d.Get("uniq_pools").([]interface{})
for _, pool := range uniq_pools {
for _, pool := range uniqPools {
req.UniqPools = append(req.UniqPools, pool.(string))
}
}
@@ -292,6 +289,20 @@ func utilityAccountUpdate(ctx context.Context, d *schema.ResourceData, m interfa
}
}
_, _, updateStPolicy := utilityGetStoragePolicyUpdate(ctx, d, m)
if len(updateStPolicy) != 0 {
storagePolicies := make([]account.StoragePolicy, 0, len(updateStPolicy))
for _, stPolicyVal := range updateStPolicy {
reqStPolicy := account.StoragePolicy{
ID: uint64(stPolicyVal["id"].(int)),
Limit: stPolicyVal["limit"].(int),
}
storagePolicies = append(storagePolicies, reqStPolicy)
}
req.StoragePolicies = storagePolicies
}
_, err := c.CloudBroker().Account().Update(ctx, req)
if err != nil {
return err
@@ -300,6 +311,95 @@ func utilityAccountUpdate(ctx context.Context, d *schema.ResourceData, m interfa
return nil
}
func utilityAccountUpdateStPolicy(ctx context.Context, d *schema.ResourceData, m interface{}) (bool, error) {
c := m.(*controller.ControllerCfg)
accountId, _ := 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 := account.AddStoragePolicyRequest{
AccountID: accountId,
StoragePolicyID: uint64(spMap["id"].(int)),
Limit: spMap["limit"].(int),
}
log.Debugf("utilityAccountUpdateStPolicy: starting to add storage policy ID:%d for account %d", req.StoragePolicyID, req.AccountID)
_, err := c.CloudBroker().Account().AddStoragePolicy(ctx, req)
if err != nil {
return needUpdate, err
}
}
}
if len(delSP) > 0 {
for _, spMap := range delSP {
req := account.DelStoragePolicyRequest{
AccountID: accountId,
StoragePolicyID: uint64(spMap["id"].(int)),
}
log.Debugf("utilityAccountUpdateStPolicy: starting to delete storage policy ID:%d from account %d", req.StoragePolicyID, req.AccountID)
_, err := c.CloudBroker().Account().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
}
func utilityAccountAvailiableTemplatesUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, afterCreate bool) error {
c := m.(*controller.ControllerCfg)

View File

@@ -72,6 +72,10 @@ func utilityAccountListCheckPresence(ctx context.Context, d *schema.ResourceData
req.Status = status.(string)
}
if zoneID, ok := d.GetOk("zone_id"); ok {
req.ZoneID = uint64(zoneID.(int))
}
log.Debugf("utilityAccountListCheckPresence: load account list")
accountList, err := c.CloudBroker().Account().List(ctx, req)
if err != nil {