This commit is contained in:
2023-07-26 13:32:39 +03:00
parent f731cf246f
commit 272e385318
167 changed files with 5194 additions and 890 deletions

View File

@@ -214,6 +214,10 @@ func computeSnapshotsSchemaMake() map[string]*schema.Schema {
Type: schema.TypeString,
Computed: true,
},
"reference_id": {
Type: schema.TypeInt,
Computed: true,
},
"res_id": {
Type: schema.TypeString,
Computed: true,
@@ -634,20 +638,8 @@ func dataSourceComputeSchemaMake() map[string]*schema.Schema {
Computed: true,
},
"custom_fields": {
Type: schema.TypeList,
Type: schema.TypeString,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Computed: true,
},
"val": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"deleted_by": {
Type: schema.TypeString,
@@ -827,6 +819,14 @@ func dataSourceComputeSchemaMake() map[string]*schema.Schema {
Type: schema.TypeString,
Computed: true,
},
"stateless_sep_id": {
Type: schema.TypeInt,
Computed: true,
},
"stateless_sep_type": {
Type: schema.TypeString,
Computed: true,
},
}
}

View File

@@ -56,6 +56,7 @@ func dataSourceComputeListRead(ctx context.Context, d *schema.ResourceData, m in
id := uuid.New()
d.SetId(id.String())
d.Set("items", flattenComputeList(result))
d.Set("entry_count", computeList.EntryCount)
return nil
}
@@ -334,6 +335,56 @@ func itemComputeSchemaMake() map[string]*schema.Schema {
func dataSourceComputeListSchemaMake() map[string]*schema.Schema {
res := map[string]*schema.Schema{
"by_id": {
Type: schema.TypeInt,
Optional: true,
Description: "Find by ID",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Find by name",
},
"account_id": {
Type: schema.TypeInt,
Optional: true,
Description: "Find by AccountID",
},
"rg_name": {
Type: schema.TypeString,
Optional: true,
Description: "Find by resgroup name",
},
"rg_id": {
Type: schema.TypeInt,
Optional: true,
Description: "Find by RGID",
},
"tech_status": {
Type: schema.TypeString,
Optional: true,
Description: "Find by tech status",
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: "Find by status",
},
"ip_address": {
Type: schema.TypeString,
Optional: true,
Description: "Find by IP address",
},
"extnet_name": {
Type: schema.TypeString,
Optional: true,
Description: "Find by Extnet name",
},
"extnet_id": {
Type: schema.TypeInt,
Optional: true,
Description: "Find by Extnet ID",
},
"includedeleted": {
Type: schema.TypeBool,
Optional: true,
@@ -359,6 +410,10 @@ func dataSourceComputeListSchemaMake() map[string]*schema.Schema {
Schema: itemComputeSchemaMake(),
},
},
"entry_count": {
Type: schema.TypeInt,
Computed: true,
},
}
return res
}

View File

@@ -147,9 +147,9 @@ func flattenListACL(listAcl compute.ListACL) []map[string]interface{} {
return res
}
func flattenComputeList(computes compute.ListComputes) []map[string]interface{} {
func flattenComputeList(computes *compute.ListComputes) []map[string]interface{} {
res := make([]map[string]interface{}, 0)
for _, compute := range computes {
for _, compute := range computes.Data {
customFields, _ := json.Marshal(compute.CustomFields)
devices, _ := json.Marshal(compute.Devices)
temp := map[string]interface{}{
@@ -265,7 +265,7 @@ func flattenComputeDisksDemo(disksList compute.ListComputeDisks, extraDisks []in
func flattenNetwork(interfaces compute.ListInterfaces) []map[string]interface{} {
res := make([]map[string]interface{}, 0, len(interfaces))
//index := 0
for _, network := range interfaces {
temp := map[string]interface{}{
"net_id": network.NetID,
@@ -316,7 +316,6 @@ func flattenCompute(d *schema.ResourceData, computeRec compute.RecordCompute) er
d.Set("computeci_id", computeRec.ComputeCIID)
d.Set("created_by", computeRec.CreatedBy)
d.Set("created_time", computeRec.CreatedTime)
d.Set("custom_fields", flattenCustomFields(computeRec.CustomFields))
d.Set("deleted_by", computeRec.DeletedBy)
d.Set("deleted_time", computeRec.DeletedTime)
d.Set("description", computeRec.Description)
@@ -442,6 +441,7 @@ func flattenSnapshots(snapshots compute.SnapshotExtendList) []map[string]interfa
"guid": snapshot.GUID,
"label": snapshot.Label,
"res_id": snapshot.ResID,
"reference_id": snapshot.ReferenceID,
"snap_set_guid": snapshot.SnapSetGUID,
"snap_set_time": snapshot.SnapSetTime,
"timestamp": snapshot.TimeStamp,
@@ -503,18 +503,11 @@ func flattenListComputeDisks(disks compute.ListComputeDisks) []map[string]interf
return res
}
func flattenCustomFields(customFileds map[string]interface{}) []map[string]interface{} {
res := make([]map[string]interface{}, 0)
for key, val := range customFileds {
value, _ := json.Marshal(val)
temp := map[string]interface{}{
"key": key,
"val": string(value),
}
res = append(res, temp)
}
return res
func flattenCustomFields(customFields map[string]interface{}) string {
encoded, _ := json.Marshal(customFields)
return string(encoded)
}
func flattenOsUsers(osUsers compute.ListOSUser) []map[string]interface{} {
res := make([]map[string]interface{}, 0)
for _, user := range osUsers {
@@ -609,9 +602,9 @@ func flattenComputeAudits(computeAudits compute.ListAudits) []map[string]interfa
return res
}
func flattenPfwList(computePfws compute.ListPFWs) []map[string]interface{} {
func flattenPfwList(computePfws *compute.ListPFWs) []map[string]interface{} {
res := make([]map[string]interface{}, 0)
for _, computePfw := range computePfws {
for _, computePfw := range computePfws.Data {
temp := map[string]interface{}{
"pfw_id": computePfw.ID,
"local_ip": computePfw.LocalIP,
@@ -626,10 +619,10 @@ func flattenPfwList(computePfws compute.ListPFWs) []map[string]interface{} {
return res
}
func flattenUserList(d *schema.ResourceData, userList compute.RecordACL) {
d.Set("account_acl", flattenListACL(userList.AccountACL))
d.Set("compute_acl", flattenListACL(userList.ComputeACL))
d.Set("rg_acl", flattenListACL(userList.RGACL))
func flattenUserList(d *schema.ResourceData, userList *compute.ListUsers) {
d.Set("account_acl", flattenListACL(userList.Data.AccountACL))
d.Set("compute_acl", flattenListACL(userList.Data.ComputeACL))
d.Set("rg_acl", flattenListACL(userList.Data.RGACL))
}
func flattenComputeGetAudits(computeAudits compute.ListShortAudits) []map[string]interface{} {

View File

@@ -57,7 +57,7 @@ func existRgID(ctx context.Context, d *schema.ResourceData, m interface{}) (bool
return false, err
}
return len(rgList.FilterByID(rgId)) != 0, nil
return len(rgList.FilterByID(rgId).Data) != 0, nil
}
func existImageId(ctx context.Context, d *schema.ResourceData, m interface{}) (bool, error) {
@@ -70,11 +70,11 @@ func existImageId(ctx context.Context, d *schema.ResourceData, m interface{}) (b
return false, err
}
return len(imageList.FilterByID(imageId)) != 0, nil
return len(imageList.FilterByID(imageId).Data) != 0, nil
}
func existVinsIdInList(vinsId uint64, vinsList vins.ListVINS) bool {
for _, vins := range vinsList {
func existVinsIdInList(vinsId uint64, vinsList *vins.ListVINS) bool {
for _, vins := range vinsList.Data {
if vinsId == vins.ID {
return true
}
@@ -83,8 +83,8 @@ func existVinsIdInList(vinsId uint64, vinsList vins.ListVINS) bool {
return false
}
func existExtNetIdInList(extId uint64, extList extnet.ListExtNets) bool {
for _, ext := range extList {
func existExtNetIdInList(extId uint64, extList *extnet.ListExtNets) bool {
for _, ext := range extList.Data {
if extId == ext.ID {
return true
}

View File

@@ -4,7 +4,7 @@ Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Kasim Baybikov, <kmbaybikov@basistech.ru>
Tim Tkachev, <tvtkachev@basistech.ru>
Tim Tkachev, <tvkachev@basistech.ru>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -128,23 +128,32 @@ func resourceComputeCreate(ctx context.Context, d *schema.ResourceData, m interf
createReqPPC.IS = IS.(string)
createReqX86.IS = IS.(string)
}
if !d.Get("with_default_vins").(bool) {
createReqX86.Interfaces = make([]kvmx86.Interface, 0)
}
if networks, ok := d.GetOk("network"); ok {
if networks.(*schema.Set).Len() > 0 {
ns := networks.(*schema.Set).List()
defaultNetwork := ns[0].(map[string]interface{})
createReqPPC.NetType = defaultNetwork["net_type"].(string)
createReqPPC.NetID = uint64(defaultNetwork["net_id"].(int))
interfaces := make([]kvmx86.Interface, 0)
for _, elem := range ns {
netInterfaceVal := elem.(map[string]interface{})
reqInterface := kvmx86.Interface{
NetType: netInterfaceVal["net_type"].(string),
NetID: uint64(netInterfaceVal["net_id"].(int)),
}
createReqX86.NetType = defaultNetwork["net_type"].(string)
createReqX86.NetID = uint64(defaultNetwork["net_id"].(int))
ipaddr, ipSet := netInterfaceVal["ip_address"]
if ipSet {
reqInterface.IPAddr = ipaddr.(string)
}
ipaddr, ipSet := defaultNetwork["ip_address"] // "ip_address" key is optional
if ipSet {
createReqPPC.IPAddr = ipaddr.(string)
createReqX86.IPAddr = ipaddr.(string)
interfaces = append(interfaces, reqInterface)
}
createReqX86.Interfaces = interfaces
}
}
@@ -181,6 +190,18 @@ func resourceComputeCreate(ctx context.Context, d *schema.ResourceData, m interf
createReqX86.RAM = uint64(d.Get("ram").(int))
createReqX86.ImageID = uint64(d.Get("image_id").(int))
createReqX86.Driver = driver
if custom_fields, ok := d.GetOk("custom_fields"); ok {
val := custom_fields.(string)
val = strings.ReplaceAll(val, "\\", "")
val = strings.ReplaceAll(val, "\n", "")
val = strings.ReplaceAll(val, "\t", "")
val = strings.TrimSpace(val)
createReqX86.CustomFields = val
}
log.Debugf("resourceComputeCreate: creating Compute of type KVM VM x86")
apiResp, err := c.CloudAPI().KVMX86().Create(ctx, createReqX86)
if err != nil {
@@ -222,16 +243,6 @@ func resourceComputeCreate(ctx context.Context, d *schema.ResourceData, m interf
return diag.FromErr(err)
}
}
argVal, ok = d.GetOk("network")
if ok && argVal.(*schema.Set).Len() > 0 {
log.Debugf("resourceComputeCreate: calling utilityComputeNetworksConfigure to attach %d network(s)", argVal.(*schema.Set).Len())
err = utilityComputeNetworksConfigure(ctx, d, m, false, true, computeId)
if err != nil {
log.Errorf("resourceComputeCreate: error when attaching networks to a new Compute ID %d: %s", computeId, err)
cleanup = true
return diag.FromErr(err)
}
}
// Note bene: we created compute in a STOPPED state (this is required to properly attach 1st network interface),
// now we need to start it before we report the sequence complete
@@ -573,11 +584,11 @@ func resourceComputeUpdate(ctx context.Context, d *schema.ResourceData, m interf
switch networkData["net_type"].(string) {
case "VINS":
if vinsId, ok := existVinsId(ctx, m, networkData["net_id"].(int)); !ok {
return diag.Errorf("resourceComputeUpdate: can't update compute because vins ID %d is not allowed or does not exist", vinsId)
return diag.Errorf("resourceComputeCreate: can't create compute because vins ID %d is not allowed or does not exist", vinsId)
}
case "EXTNET":
if extNetId, ok := existExtNetId(ctx, m, networkData["net_id"].(int)); !ok {
return diag.Errorf("resourceComputeUpdate: can't update compute because extnet ID %d is not allowed or does not exist", extNetId)
return diag.Errorf("resourceComputeCreate: can't create compute because extnet ID %d is not allowed or does not exist", extNetId)
}
default:
@@ -1334,6 +1345,35 @@ func resourceComputeUpdate(ctx context.Context, d *schema.ResourceData, m interf
}
}
if d.HasChange("custom_fields") {
val := d.Get("custom_fields").(string)
val = strings.ReplaceAll(val, "\\", "")
val = strings.ReplaceAll(val, "\n", "")
val = strings.ReplaceAll(val, "\t", "")
val = strings.TrimSpace(val)
if len(val) > 0 {
req := compute.SetCustomFieldsRequest{
ComputeID: computeRec.ID,
CustomFields: val,
}
_, err := c.CloudAPI().Compute().SetCustomFields(ctx, req)
if err != nil {
return diag.FromErr(err)
}
} else {
req := compute.DeleteCustomFieldsRequest{
ComputeID: computeRec.ID,
}
_, err := c.CloudAPI().Compute().DeleteCustomFields(ctx, req)
if err != nil {
return diag.FromErr(err)
}
}
}
// we may reuse dataSourceComputeRead here as we maintain similarity
// between Compute resource and Compute data source schemas
defer resourceComputeRead(ctx, d, m)
@@ -1554,7 +1594,6 @@ func ResourceComputeSchemaMake() map[string]*schema.Schema {
Required: true,
Description: "Name of this compute. Compute names are case sensitive and must be unique in the resource group.",
},
"rg_id": {
Type: schema.TypeInt,
Required: true,
@@ -1562,50 +1601,43 @@ func ResourceComputeSchemaMake() map[string]*schema.Schema {
ValidateFunc: validation.IntAtLeast(1),
Description: "ID of the resource group where this compute should be deployed.",
},
"driver": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: statefuncs.StateFuncToUpper,
ValidateFunc: validation.StringInSlice([]string{"KVM_X86", "KVM_PPC"}, false), // observe case while validating
ValidateFunc: validation.StringInSlice([]string{"SVA_KVM_X86", "KVM_X86", "KVM_PPC"}, false), // observe case while validating
Description: "Hardware architecture of this compute instance.",
},
"cpu": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(1, constants.MaxCpusPerCompute),
Description: "Number of CPUs to allocate to this compute instance.",
},
"ram": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntAtLeast(constants.MinRamPerCompute),
Description: "Amount of RAM in MB to allocate to this compute instance.",
},
"image_id": {
Type: schema.TypeInt,
Required: true,
//ForceNew: true, //REDEPLOY
Description: "ID of the OS image to base this compute instance on.",
},
"boot_disk_size": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "This compute instance boot disk size in GB. Make sure it is large enough to accomodate selected OS image.",
},
"affinity_label": {
Type: schema.TypeString,
Optional: true,
Description: "Set affinity label for compute",
},
"affinity_rules": {
Type: schema.TypeList,
Optional: true,
@@ -1642,7 +1674,6 @@ func ResourceComputeSchemaMake() map[string]*schema.Schema {
},
},
},
"anti_affinity_rules": {
Type: schema.TypeList,
Optional: true,
@@ -1679,7 +1710,6 @@ func ResourceComputeSchemaMake() map[string]*schema.Schema {
},
},
},
"disks": {
Type: schema.TypeList,
Optional: true,
@@ -1687,6 +1717,22 @@ func ResourceComputeSchemaMake() map[string]*schema.Schema {
Schema: disksSubresourceSchemaMake(),
},
},
"custom_fields": {
Type: schema.TypeString,
Optional: true,
},
"stateless": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Compute will be stateless (SVA_KVM_X86) if set to True",
},
"with_default_vins": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Create compute with default resgroup ViNS (true) or without any interfaces (false). This parameter is ignored if network block is specified",
},
"boot_disk": {
Type: schema.TypeSet,
Computed: true,
@@ -1862,7 +1908,6 @@ func ResourceComputeSchemaMake() map[string]*schema.Schema {
Optional: true,
Description: "compute purpose",
},
// The rest are Compute properties, which are "computed" once it is created
"account_id": {
Type: schema.TypeInt,
@@ -1917,22 +1962,6 @@ func ResourceComputeSchemaMake() map[string]*schema.Schema {
Type: schema.TypeInt,
Computed: true,
},
"custom_fields": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Computed: true,
},
"val": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"deleted_by": {
Type: schema.TypeString,
Computed: true,

View File

@@ -44,14 +44,14 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func matchComputes(computeList compute.ListComputes) compute.ListComputes {
func matchComputes(computeList *compute.ListComputes) *compute.ListComputes {
matched, _ := regexp.Compile("[a-zA-Z]+\\d+-[a-zA-Z]+\\d+-[a-zA-Z]+\\d+")
result := computeList.FilterFunc(func(ic compute.ItemCompute) bool {
res := matched.Match([]byte(ic.Name))
return !res
})
return result
return &result
}
func utilityComputeExtraDisksConfigure(ctx context.Context, d *schema.ResourceData, m interface{}, do_delta bool) error {

View File

@@ -40,10 +40,40 @@ import (
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)
func utilityDataComputeListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (compute.ListComputes, error) {
func utilityDataComputeListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*compute.ListComputes, error) {
c := m.(*controller.ControllerCfg)
req := compute.ListRequest{}
if byId, ok := d.GetOk("by_id"); ok {
req.ByID = uint64(byId.(int))
}
if name, ok := d.GetOk("name"); ok {
req.Name = name.(string)
}
if accountId, ok := d.GetOk("account_id"); ok {
req.AccountID = uint64(accountId.(int))
}
if rgName, ok := d.GetOk("rg_name"); ok {
req.RGName = rgName.(string)
}
if rgId, ok := d.GetOk("rg_id"); ok {
req.RGID = uint64(rgId.(int))
}
if techStatus, ok := d.GetOk("tech_status"); ok {
req.TechStatus = techStatus.(string)
}
if status, ok := d.GetOk("status"); ok {
req.Status = status.(string)
}
if ipAddress, ok := d.GetOk("ip_address"); ok {
req.IPAddress = ipAddress.(string)
}
if extNetName, ok := d.GetOk("extnet_name"); ok {
req.ExtNetName = extNetName.(string)
}
if extnetId, ok := d.GetOk("extnet_id"); ok {
req.ExtNetID = uint64(extnetId.(int))
}
if includeDeleted, ok := d.GetOk("includedeleted"); ok {
req.IncludeDeleted = includeDeleted.(bool)
}

View File

@@ -40,7 +40,7 @@ import (
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)
func utilityComputePfwListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (compute.ListPFWs, error) {
func utilityComputePfwListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*compute.ListPFWs, error) {
c := m.(*controller.ControllerCfg)
req := compute.PFWListRequest{
ComputeID: uint64(d.Get("compute_id").(int)),

View File

@@ -40,7 +40,7 @@ import (
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)
func utilityComputeUserListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (compute.RecordACL, error) {
func utilityComputeUserListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*compute.ListUsers, error) {
c := m.(*controller.ControllerCfg)
req := compute.UserListRequest{
ComputeID: uint64(d.Get("compute_id").(int)),
@@ -48,8 +48,8 @@ func utilityComputeUserListCheckPresence(ctx context.Context, d *schema.Resource
computeUserList, err := c.CloudAPI().Compute().UserList(ctx, req)
if err != nil {
return *computeUserList, err
return computeUserList, err
}
return *computeUserList, err
return computeUserList, err
}