This commit is contained in:
2024-08-26 18:22:06 +03:00
parent 6876b25f0e
commit 8ad6811e88
597 changed files with 52808 additions and 2129 deletions

View File

@@ -34,6 +34,7 @@ package sep
import (
"context"
"encoding/json"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -69,7 +70,7 @@ func resourceSepCreate(ctx context.Context, d *schema.ResourceData, m interface{
}
var consumedNIDs []uint64
for _, item := range d.Get("consumed_by").([]interface{}) {
for _, item := range d.Get("consumed_by").(*schema.Set).List() {
consumedNIDs = append(consumedNIDs, uint64(item.(int)))
}
@@ -172,84 +173,9 @@ func resourceSepDelete(ctx context.Context, d *schema.ResourceData, m interface{
}
func resourceSepUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Debugf("resourceSepUpdate: called for %s, id: %d", d.Get("name").(string), d.Get("sep_id").(int))
c := m.(*controller.ControllerCfg)
return diag.Errorf(
"SEP upgrade is not possible via terraform")
if diags := checkParamsExistence(ctx, d, c); diags != nil {
return diags
}
if d.HasChange("account_ids") {
err := resourceSepChangeAccess(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("access_to_pool") {
err := resourceSepChangeAccessToPool(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("decommission") {
err := resourceSepDecommission(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("upd_capacity_limit") {
err := resourceSepUpdateCapacityLimit(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("config") {
err := resourceSepUpdateConfig(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("field_edit") {
err := resourceSepFieldEdit(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("enable") {
err := resourceSepChangeEnabled(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("consumed_by") {
err := resourceSepUpdateNodes(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("provided_by") {
err := resourceSepUpdateProviders(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("pools") {
err := resourceSepChangePools(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
}
return resourceSepRead(ctx, d, m)
}
func resourceSepChangeAccess(ctx context.Context, d *schema.ResourceData, m interface{}) error {
@@ -371,10 +297,47 @@ func resourceSepChangePools(ctx context.Context, d *schema.ResourceData, m inter
newPoolsList := newPoolsInterface.(*schema.Set).Difference(oldPoolsInterface.(*schema.Set)).List()
for _, pool := range newPoolsList {
poolItem := pool.(map[string]interface{})
accessAccountIDs := []uint64{}
for _, v := range poolItem["access_account_ids"].([]interface{}) {
accessAccountIDs = append(accessAccountIDs, uint64(v.(int)))
}
accessResGroupIDs := []uint64{}
for _, v := range poolItem["access_res_group_ids"].([]interface{}) {
accessResGroupIDs = append(accessResGroupIDs, uint64(v.(int)))
}
types := []string{}
for _, v := range poolItem["types"].([]interface{}) {
types = append(types, v.(string))
}
uris := []UrisModel{}
list := poolItem["uris"].(*schema.Set).List()
for _, v := range list {
if m, ok := v.(map[string]interface{}); ok {
uris = append(uris, UrisModel{IP: m["ip"].(string),
Port: uint64(m["port"].(int)),
})
}
}
poolValue := PoolModel{
AccessAccountIDs: accessAccountIDs,
AccessResGroupIDs: accessResGroupIDs,
Name: poolItem["name"].(string),
Types: types,
Uris: uris,
UsageLimit: uint64(poolItem["usage_limit"].(int)),
}
marshalPool, _ := json.Marshal(poolValue)
log.Debugf(string(marshalPool))
addPoolReq := sep.AddPoolRequest{
SEPID: uint64(d.Get("sep_id").(int)),
Sync: true,
Pool: pool.(string),
Pool: string(marshalPool),
}
_, err := c.CloudBroker().SEP().AddPool(ctx, addPoolReq)
@@ -386,43 +349,6 @@ func resourceSepChangePools(ctx context.Context, d *schema.ResourceData, m inter
return nil
}
func resourceSepDecommission(ctx context.Context, d *schema.ResourceData, m interface{}) error {
c := m.(*controller.ControllerCfg)
decommission := d.Get("decommission").(bool)
if decommission {
req := sep.DecommissionRequest{
SEPID: uint64(d.Get("sep_id").(int)),
ClearPhisically: d.Get("clear_physically").(bool),
}
_, err := c.CloudBroker().SEP().Decommission(ctx, req)
if err != nil {
return err
}
}
return nil
}
func resourceSepUpdateCapacityLimit(ctx context.Context, d *schema.ResourceData, m interface{}) error {
c := m.(*controller.ControllerCfg)
updCapacityLimit := d.Get("upd_capacity_limit").(bool)
if updCapacityLimit {
req := sep.UpdateCapacityLimitRequest{
SEPID: uint64(d.Get("sep_id").(int)),
}
_, err := c.CloudBroker().SEP().UpdateCapacityLimit(ctx, req)
if err != nil {
return err
}
}
return nil
}
func resourceSepUpdateConfig(ctx context.Context, d *schema.ResourceData, m interface{}) error {
c := m.(*controller.ControllerCfg)
@@ -581,3 +507,18 @@ func ResourceSep() *schema.Resource {
Schema: resourceSepSchemaMake(),
}
}
type PoolModel struct {
AccessAccountIDs []uint64 `json:"accessAccountIds"`
AccessResGroupIDs []uint64 `json:"accessResGroupIds"`
Name string `json:"name"`
Types []string `json:"types"`
Uris []UrisModel `json:"uris"`
UsageLimit uint64 `json:"usage_limit"`
}
type UrisModel struct {
IP string `json:"ip"`
Port uint64 `json:"port"`
}

View File

@@ -533,67 +533,78 @@ func resourceSepSchemaMake() map[string]*schema.Schema {
Type: schema.TypeInt,
},
},
"clear_physically": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "clear disks and images physically",
},
"decommission": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "unlink everything that exists from SEP",
},
"enable": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "enable SEP after creation",
},
"field_edit": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"field_name": {
Type: schema.TypeString,
Required: true,
Description: "field name",
},
"field_value": {
Type: schema.TypeString,
Required: true,
Description: "field value",
},
"field_type": {
Type: schema.TypeString,
Required: true,
Description: "field type",
},
},
},
},
"sep_id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "sep type des id",
},
"upd_capacity_limit": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Update SEP capacity limit",
},
"pools": {
Type: schema.TypeSet,
Optional: true,
Description: "add/delete pools to/from sep",
Elem: &schema.Schema{
Type: schema.TypeString,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"access_account_ids": {
Type: schema.TypeList,
Required: true,
Description: "access account ids",
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"access_res_group_ids": {
Type: schema.TypeList,
Required: true,
Description: "access res group ids",
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "name",
},
"types": {
Type: schema.TypeList,
Optional: true,
Description: "types",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"uris": {
Type: schema.TypeSet,
Optional: true,
Description: "uris",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Required: true,
Description: "ip",
},
"port": {
Type: schema.TypeInt,
Required: true,
Description: "port",
},
},
},
},
"usage_limit": {
Type: schema.TypeInt,
Required: true,
Description: "usage limit",
},
},
},
},
"ckey": {
@@ -610,9 +621,10 @@ func resourceSepSchemaMake() map[string]*schema.Schema {
},
},
"config": {
Type: schema.TypeString,
Required: true,
Description: "sep config string",
Type: schema.TypeString,
Required: true,
Description: "sep config string",
DiffSuppressFunc: resourceSepDiffSupperss,
},
"consumed_by": {
Type: schema.TypeSet,

View File

@@ -34,6 +34,8 @@ package sep
import (
"context"
"encoding/json"
"reflect"
"strconv"
log "github.com/sirupsen/logrus"
@@ -67,3 +69,15 @@ func utilitySepCheckPresence(ctx context.Context, d *schema.ResourceData, m inte
return sep, nil
}
func resourceSepDiffSupperss(key, oldVal, newVal string, d *schema.ResourceData) bool {
var v1, v2 interface{}
json.Unmarshal([]byte(newVal), &v1)
json.Unmarshal([]byte(oldVal), &v2)
if reflect.DeepEqual(v1, v2) {
log.Debugf("resourceSepDiffSupperss: key=%s, oldVal=%q, newVal=%q -> suppress=TRUE", key, oldVal, newVal)
return true
}
log.Debugf("resourceSepDiffSupperss: key=%s, oldVal=%q, newVal=%q -> suppress=FALSE", key, oldVal, newVal)
return false
}