This commit is contained in:
2025-08-04 16:11:16 +03:00
parent bae25296bb
commit 4b3f21d9be
239 changed files with 6585 additions and 784 deletions

View File

@@ -0,0 +1,116 @@
/*
Copyright (c) 2019-2024 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.
Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort
Please see README.md to learn where to place source code so that it
builds seamlessly.
Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/
package zone
import (
"context"
"strconv"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceZoneRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
zone, err := utilityZoneCheckPresence(ctx, d, m)
if err != nil {
d.SetId("") // ensure ID is empty in this case
return diag.FromErr(err)
}
d.SetId(strconv.Itoa(d.Get("zone_id").(int)))
flattenZone(d, zone)
return nil
}
func dataSourceZoneSchemaMake() map[string]*schema.Schema {
return map[string]*schema.Schema{
"zone_id": {
Type: schema.TypeInt,
Required: true,
},
"guid": {
Type: schema.TypeInt,
Computed: true,
},
"gid": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"deletable": {
Type: schema.TypeBool,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"created_time": {
Type: schema.TypeInt,
Computed: true,
},
"updated_time": {
Type: schema.TypeInt,
Computed: true,
},
"node_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
}
}
func DataSourceZone() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
ReadContext: dataSourceZoneRead,
Timeouts: &schema.ResourceTimeout{
Read: &constants.Timeout30s,
Default: &constants.Timeout60s,
},
Schema: dataSourceZoneSchemaMake(),
}
}

View File

@@ -0,0 +1,182 @@
/*
Copyright (c) 2019-2024 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.
Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort
Please see README.md to learn where to place source code so that it
builds seamlessly.
Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/
package zone
import (
"context"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceZoneListRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
zoneList, err := utilityZoneListCheckPresence(ctx, d, m)
if err != nil {
d.SetId("") // ensure ID is empty in this case
return diag.FromErr(err)
}
id := uuid.New()
d.SetId(id.String())
d.Set("items", flattenZoneList(zoneList))
d.Set("entry_count", zoneList.EntryCount)
return nil
}
func dataSourceZoneListSchemaMake() map[string]*schema.Schema {
res := map[string]*schema.Schema{
"by_id": {
Type: schema.TypeInt,
Optional: true,
Description: "Find by ID",
},
"gid": {
Type: schema.TypeInt,
Optional: true,
Description: "Find by Grid ID",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Find by name",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Find by description",
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: "Find by status",
},
"deletable": {
Type: schema.TypeBool,
Optional: true,
Description: "Find by deletable",
},
"node_id": {
Type: schema.TypeInt,
Optional: true,
Description: "Find by nodeId",
},
"sort_by": {
Type: schema.TypeString,
Optional: true,
Description: "sort by one of supported fields, format +|-(field)",
},
"page": {
Type: schema.TypeInt,
Optional: true,
Description: "Page number",
},
"size": {
Type: schema.TypeInt,
Optional: true,
Description: "Page size",
},
"items": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"zone_id": {
Type: schema.TypeInt,
Required: true,
},
"guid": {
Type: schema.TypeInt,
Computed: true,
},
"gid": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"deletable": {
Type: schema.TypeBool,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"created_time": {
Type: schema.TypeInt,
Computed: true,
},
"updated_time": {
Type: schema.TypeInt,
Computed: true,
},
"node_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
},
},
},
"entry_count": {
Type: schema.TypeInt,
Computed: true,
},
}
return res
}
func DataSourceZoneList() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
ReadContext: dataSourceZoneListRead,
Timeouts: &schema.ResourceTimeout{
Read: &constants.Timeout30s,
Default: &constants.Timeout60s,
},
Schema: dataSourceZoneListSchemaMake(),
}
}

View File

@@ -0,0 +1,82 @@
/*
Copyright (c) 2019-2024 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Kasim Baybikov, <kmbaybikov@basistech.ru>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.
Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort
Please see README.md to learn where to place source code so that it
builds seamlessly.
Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/
package zone
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
log "github.com/sirupsen/logrus"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/zone"
)
func flattenZone(d *schema.ResourceData, item *zone.RecordZone) error {
log.Debugf("flattenZone: start decoding RecordZone name %q / ID %d",
item.Name, item.ID)
d.Set("zone_id", int(item.ID))
d.Set("guid", int(item.GUID))
d.Set("gid", int(item.GID))
d.Set("name", item.Name)
d.Set("description", item.Description)
d.Set("deletable", item.Deletable)
d.Set("status", item.Status)
d.Set("created_time", item.CreatedTime)
d.Set("updated_time", item.UpdatedTime)
d.Set("node_ids", item.NodeIDs)
log.Debugf("flattenZone: decoded RecordZone name %q / ID %d, complete",
item.Name, item.ID)
return nil
}
func flattenZoneList(zone *zone.ListZones) []map[string]interface{} {
log.Debugf("flattenZoneList start")
res := make([]map[string]interface{}, 0, len(zone.Data))
for _, zone := range zone.Data {
temp := map[string]interface{}{
"zone_id": int(zone.ID),
"guid": int(zone.GUID),
"gid": int(zone.GID),
"name": zone.Name,
"description": zone.Description,
"deletable": zone.Deletable,
"status": zone.Status,
"created_time": zone.CreatedTime,
"updated_time": zone.UpdatedTime,
"node_ids": zone.NodeIDs,
}
res = append(res, temp)
}
log.Debugf("flattenZoneList end")
return res
}

View File

@@ -0,0 +1,229 @@
/*
Copyright (c) 2019-2024 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.
Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort
Please see README.md to learn where to place source code so that it
builds seamlessly.
Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/
package zone
import (
"context"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
log "github.com/sirupsen/logrus"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/zone"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/dc"
)
func resourceZoneCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
zoneName := d.Get("name").(string)
log.Debugf("resourceZoneCreate: called Zone with name %s", zoneName)
c := m.(*controller.ControllerCfg)
req := zone.CreateRequest{
Name: zoneName,
}
if desc, ok := d.GetOk("description"); ok {
req.Description = desc.(string)
}
zoneID, err := c.CloudBroker().Zone().Create(ctx, req)
if err != nil {
return diag.FromErr(err)
}
d.SetId(strconv.FormatUint(zoneID, 10))
warnings := dc.Warnings{}
if nodeIDs, ok := d.GetOk("node_ids"); ok {
nodeIDsArr := nodeIDs.([]interface{})
if len(nodeIDsArr) > 0 {
addedUint := make([]uint64, len(nodeIDsArr))
for i, v := range nodeIDsArr {
addedUint[i] = uint64(v.(int))
}
req := zone.AddNodeRequest{
ID: zoneID,
NodeIDs: addedUint,
}
if _, err := c.CloudBroker().Zone().AddNode(ctx, req); err != nil {
warnings.Add(err)
}
}
}
log.Debugf("resourceZoneCreate: create Zone with ID: %d, complete", zoneID)
return append(resourceZoneRead(ctx, d, m), warnings.Get()...)
}
func resourceZoneRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Debugf("resourceZoneRead: called Zone with id %s", d.Id())
zone, err := utilityZoneCheckPresence(ctx, d, m)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
flattenZone(d, zone)
log.Debugf("resourceZoneRead: read Zone with id %s, complete", d.Id())
return nil
}
func resourceZoneUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
zoneID, _ := strconv.ParseUint(d.Id(), 10, 64)
log.Debugf("resourceZoneUpdate: called Zone with id %d", zoneID)
if d.HasChanges("name,", "description", "node_ids") {
if err := utilityZoneUpdate(ctx, d, m, zoneID); err != nil {
return diag.FromErr(err)
}
}
log.Debugf("resourceZoneUpdate: update Zone with id %d, complete", zoneID)
return resourceZoneRead(ctx, d, m)
}
func resourceZoneDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Debugf("resourceZoneDelete: called Zone with id %s", d.Id())
zoneItem, err := utilityZoneCheckPresence(ctx, d, m)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
c := m.(*controller.ControllerCfg)
req := zone.DeleteRequest{
ID: zoneItem.ID,
}
_, err = c.CloudBroker().Zone().Delete(ctx, req)
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
}
func ResourceZone() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
CreateContext: resourceZoneCreate,
ReadContext: resourceZoneRead,
UpdateContext: resourceZoneUpdate,
DeleteContext: resourceZoneDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: &constants.Timeout20m,
Read: &constants.Timeout20m,
Update: &constants.Timeout20m,
Delete: &constants.Timeout20m,
Default: &constants.Timeout20m,
},
Schema: resourceZoneSchemaMake(),
}
}
func resourceZoneSchemaMake() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"node_ids": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"zone_id": {
Type: schema.TypeInt,
Computed: true,
},
"guid": {
Type: schema.TypeInt,
Computed: true,
},
"gid": {
Type: schema.TypeInt,
Computed: true,
},
"deletable": {
Type: schema.TypeBool,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"created_time": {
Type: schema.TypeInt,
Computed: true,
},
"updated_time": {
Type: schema.TypeInt,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
}
}

View File

@@ -0,0 +1,155 @@
/*
Copyright (c) 2019-2022 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Kasim Baybikov, <kmbaybikov@basistech.ru>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.
Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort
Please see README.md to learn where to place source code so that it
builds seamlessly.
Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/
package zone
import (
"context"
"strconv"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/zone"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
log "github.com/sirupsen/logrus"
)
func utilityZoneCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*zone.RecordZone, error) {
c := m.(*controller.ControllerCfg)
req := zone.GetRequest{}
if d.Id() != "" {
zoneId, _ := strconv.ParseUint(d.Id(), 10, 64)
req.ID = zoneId
} else {
req.ID = uint64(d.Get("zone_id").(int))
}
zoneData, err := c.CloudBroker().Zone().Get(ctx, req)
if err != nil {
return nil, err
}
return zoneData, nil
}
func utilityZoneUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, zoneID uint64) error {
c := m.(*controller.ControllerCfg)
if d.HasChanges("name,", "description") {
req := zone.UpdateRequest{
ID: zoneID,
}
if d.HasChange("name") {
req.Name = d.Get("name").(string)
}
if d.HasChange("description") {
req.Description = d.Get("description").(string)
}
_, err := c.CloudBroker().Zone().Update(ctx, req)
if err != nil {
return err
}
log.Debugf("utilityZoneUpdate: update zone with ID: %d, complete with params=%v", zoneID, req)
}
addedNodes := make([]interface{}, 0)
removedNodes := make([]interface{}, 0)
old_set, new_set := d.GetChange("node_ids")
oldSlice := old_set.([]interface{})
newSlice := new_set.([]interface{})
for _, oldElem := range oldSlice {
if !containsNodes(newSlice, oldElem) {
removedNodes = append(removedNodes, oldElem)
}
}
for _, newElem := range newSlice {
if !containsNodes(oldSlice, newElem) {
addedNodes = append(addedNodes, newElem)
}
}
log.Debugf("Found node_ids change with %v deletion(s) and %v addition(s) [zoneID=%v]", len(removedNodes), len(addedNodes), zoneID)
if len(addedNodes) > 0 {
addedUint := make([]uint64, len(addedNodes))
for i, v := range addedNodes {
addedUint[i] = uint64(v.(int))
}
req := zone.AddNodeRequest{
ID: zoneID,
NodeIDs: addedUint,
}
if _, err := c.CloudBroker().Zone().AddNode(ctx, req); err != nil {
return err
}
}
if len(removedNodes) > 0 {
removedUint := make([]uint64, len(removedNodes))
for i, v := range removedNodes {
removedUint[i] = uint64(v.(int))
}
req := zone.DelNodeRequest{
ID: zoneID,
NodeIDs: removedUint,
}
log.Debug("del")
log.Debug(req.NodeIDs)
if _, err := c.CloudBroker().Zone().DelNode(ctx, req); err != nil {
return err
}
}
return nil
}
func containsNodes(set []interface{}, check interface{}) bool {
for _, elem := range set {
elemConv := elem.(int)
checkConv := check.(int)
if elemConv == checkConv {
return true
}
}
return false
}

View File

@@ -0,0 +1,85 @@
/*
Copyright (c) 2019-2022 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Kasim Baybikov, <kmbaybikov@basistech.ru>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.
Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort
Please see README.md to learn where to place source code so that it
builds seamlessly.
Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/
package zone
import (
"context"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/zone"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func utilityZoneListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*zone.ListZones, error) {
c := m.(*controller.ControllerCfg)
req := zone.ListRequest{}
if byId, ok := d.GetOk("by_id"); ok {
req.ByID = uint64(byId.(int))
}
if gid, ok := d.GetOk("gid"); ok {
req.GID = uint64(gid.(int))
}
if name, ok := d.GetOk("name"); ok {
req.Name = name.(string)
}
if description, ok := d.GetOk("description"); ok {
req.Description = description.(string)
}
if status, ok := d.GetOk("status"); ok {
req.Status = status.(string)
}
if deletable, ok := d.GetOk("deletable"); ok {
req.Deletable = deletable.(bool)
}
if nodeID, ok := d.GetOk("nodeId"); ok {
req.NodeID = uint64(nodeID.(int))
}
if sortBy, ok := d.GetOk("sort_by"); ok {
req.SortBy = sortBy.(string)
}
if size, ok := d.GetOk("size"); ok {
req.Size = uint64(size.(int))
}
if page, ok := d.GetOk("page"); ok {
req.Page = uint64(page.(int))
}
zoneList, err := c.CloudBroker().Zone().List(ctx, req)
if err != nil {
return nil, err
}
return zoneList, nil
}