This commit is contained in:
KasimBaybikov
2023-05-04 10:08:25 +03:00
parent 9bad8a6947
commit 8ca233dd32
288 changed files with 6645 additions and 11464 deletions

View File

@@ -1,38 +0,0 @@
/*
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 snapshot
const snapshotCreateAPI = "/restmachine/cloudapi/compute/snapshotCreate"
const snapshotDeleteAPI = "/restmachine/cloudapi/compute/snapshotDelete"
const snapshotRollbackAPI = "/restmachine/cloudapi/compute/snapshotRollback"
const snapshotListAPI = "/restmachine/cloudapi/compute/snapshotList"

View File

@@ -41,21 +41,6 @@ import (
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
)
func flattenSnapshotList(gl SnapshotList) []map[string]interface{} {
res := make([]map[string]interface{}, 0)
for _, item := range gl {
temp := map[string]interface{}{
"label": item.Label,
"guid": item.Guid,
"disks": item.Disks,
"timestamp": item.Timestamp,
}
res = append(res, temp)
}
return res
}
func dataSourceSnapshotListRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
snapshotList, err := utilitySnapshotListCheckPresence(ctx, d, m)
if err != nil {

View File

@@ -0,0 +1,28 @@
package snapshot
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/compute"
)
func flattenSnapshotList(gl compute.ListSnapShots) []map[string]interface{} {
res := make([]map[string]interface{}, 0)
for _, item := range gl {
temp := map[string]interface{}{
"label": item.Label,
"guid": item.GUID,
"disks": item.Disks,
"timestamp": item.Timestamp,
}
res = append(res, temp)
}
return res
}
func flattenSnapshot(d *schema.ResourceData, snapshot *compute.ItemSnapshot) {
d.Set("timestamp", snapshot.Timestamp)
d.Set("guid", snapshot.GUID)
d.Set("disks", snapshot.Disks)
d.Set("label", snapshot.Label)
}

View File

@@ -1,42 +0,0 @@
/*
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 snapshot
type Snapshot struct {
Disks []int `json:"disks"`
Guid string `json:"guid"`
Label string `json:"label"`
Timestamp uint64 `json:"timestamp"`
}
type SnapshotList []Snapshot

View File

@@ -34,13 +34,12 @@ package snapshot
import (
"context"
"net/url"
"strconv"
"strings"
"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/cloudapi/compute"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)
@@ -49,11 +48,12 @@ func resourceSnapshotCreate(ctx context.Context, d *schema.ResourceData, m inter
log.Debugf("resourceSnapshotCreate: called for snapshot %s", d.Get("label").(string))
c := m.(*controller.ControllerCfg)
urlValues := &url.Values{}
urlValues.Add("label", d.Get("label").(string))
urlValues.Add("computeId", strconv.Itoa(d.Get("compute_id").(int)))
req := compute.SnapshotCreateRequest{
Label: d.Get("label").(string),
ComputeID: uint64(d.Get("compute_id").(int)),
}
snapshotId, err := c.DecortAPICall(ctx, "POST", snapshotCreateAPI, urlValues)
snapshotId, err := c.CloudAPI().Compute().SnapshotCreate(ctx, req)
if err != nil {
return diag.FromErr(err)
}
@@ -63,12 +63,7 @@ func resourceSnapshotCreate(ctx context.Context, d *schema.ResourceData, m inter
d.SetId(snapshotId)
d.Set("guid", snapshotId)
diagnostics := resourceSnapshotRead(ctx, d, m)
if diagnostics != nil {
return diagnostics
}
return nil
return resourceSnapshotRead(ctx, d, m)
}
func resourceSnapshotRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
@@ -77,10 +72,7 @@ func resourceSnapshotRead(ctx context.Context, d *schema.ResourceData, m interfa
return diag.FromErr(err)
}
d.Set("timestamp", snapshot.Timestamp)
d.Set("guid", snapshot.Guid)
d.Set("disks", snapshot.Disks)
d.Set("label", snapshot.Label)
flattenSnapshot(d, snapshot)
return nil
}
@@ -89,20 +81,22 @@ func resourceSnapshotDelete(ctx context.Context, d *schema.ResourceData, m inter
log.Debugf("resourceSnapshotDelete: called for %s, id: %s", d.Get("label").(string), d.Id())
c := m.(*controller.ControllerCfg)
urlValues := &url.Values{}
urlValues.Add("computeId", strconv.Itoa(d.Get("compute_id").(int)))
urlValues.Add("label", d.Get("label").(string))
req := compute.SnapshotDeleteRequest{
ComputeID: uint64(d.Get("compute_id").(int)),
Label: d.Get("label").(string),
}
_, err := c.DecortAPICall(ctx, "POST", snapshotDeleteAPI, urlValues)
_, err := c.CloudAPI().Compute().SnapshotDelete(ctx, req)
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
}
func resourceSnapshotEdit(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
func resourceSnapshotUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
if d.HasChange("rollback") {
if d.Get("rollback").(bool) {
err := resourceSnapshotRollback(ctx, d, m)
@@ -117,12 +111,12 @@ func resourceSnapshotEdit(ctx context.Context, d *schema.ResourceData, m interfa
func resourceSnapshotRollback(ctx context.Context, d *schema.ResourceData, m interface{}) error {
c := m.(*controller.ControllerCfg)
urlValues := &url.Values{}
req := compute.SnapshotRollbackRequest{
ComputeID: uint64(d.Get("compute_id").(int)),
Label: d.Get("label").(string),
}
urlValues.Add("computeId", strconv.Itoa(d.Get("compute_id").(int)))
urlValues.Add("label", d.Get("label").(string))
_, err := c.DecortAPICall(ctx, "POST", snapshotRollbackAPI, urlValues)
_, err := c.CloudAPI().Compute().SnapshotRollback(ctx, req)
if err != nil {
return err
}
@@ -175,7 +169,7 @@ func ResourceSnapshot() *schema.Resource {
CreateContext: resourceSnapshotCreate,
ReadContext: resourceSnapshotRead,
UpdateContext: resourceSnapshotEdit,
UpdateContext: resourceSnapshotUpdate,
DeleteContext: resourceSnapshotDelete,
Importer: &schema.ResourceImporter{

View File

@@ -37,9 +37,10 @@ import (
"errors"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/compute"
)
func utilitySnapshotCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*Snapshot, error) {
func utilitySnapshotCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*compute.ItemSnapshot, error) {
snapShotList, err := utilitySnapshotListCheckPresence(ctx, d, m)
if err != nil {
return nil, err
@@ -54,7 +55,7 @@ func utilitySnapshotCheckPresence(ctx context.Context, d *schema.ResourceData, m
}
for _, s := range snapShotList {
if s.Guid == findId {
if s.GUID == findId {
return &s, nil
}
}

View File

@@ -34,33 +34,22 @@ package snapshot
import (
"context"
"encoding/json"
"net/url"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/compute"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)
func utilitySnapshotListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (SnapshotList, error) {
func utilitySnapshotListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (compute.ListSnapShots, error) {
c := m.(*controller.ControllerCfg)
urlValues := &url.Values{}
urlValues.Add("computeId", strconv.Itoa(d.Get("compute_id").(int)))
req := compute.SnapshotListRequest{
ComputeID: uint64(d.Get("compute_id").(int)),
}
resp, err := c.DecortAPICall(ctx, "POST", snapshotListAPI, urlValues)
snapshotList, err := c.CloudAPI().Compute().SnapshotList(ctx, req)
if err != nil {
return nil, err
}
if resp == "" {
return nil, nil
}
snapshotList := SnapshotList{}
if err := json.Unmarshal([]byte(resp), &snapshotList); err != nil {
return nil, err
}
return snapshotList, nil
}