4.0.0
This commit is contained in:
@@ -1,37 +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 pfw
|
||||
|
||||
const ComputePfwListAPI = "/restmachine/cloudapi/compute/pfwList"
|
||||
const ComputePfwAddAPI = "/restmachine/cloudapi/compute/pfwAdd"
|
||||
const ComputePfwDelAPI = "/restmachine/cloudapi/compute/pfwDel"
|
||||
15
internal/service/cloudapi/pfw/flattens.go
Normal file
15
internal/service/cloudapi/pfw/flattens.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package pfw
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/compute"
|
||||
)
|
||||
|
||||
func flattenPFW(d *schema.ResourceData, pfw *compute.ItemPFW) {
|
||||
d.Set("compute_id", pfw.VMID)
|
||||
d.Set("public_port_start", pfw.PublicPortStart)
|
||||
d.Set("public_port_end", pfw.PublicPortEnd)
|
||||
d.Set("local_ip", pfw.LocalIP)
|
||||
d.Set("local_base_port", pfw.LocalPort)
|
||||
d.Set("proto", pfw.Protocol)
|
||||
}
|
||||
@@ -1,45 +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 pfw
|
||||
|
||||
type PfwRecord struct {
|
||||
ID int `json:"id"`
|
||||
LocalIP string `json:"localIp"`
|
||||
LocalPort int `json:"localPort"`
|
||||
Protocol string `json:"protocol"`
|
||||
PublicPortEnd int `json:"publicPortEnd"`
|
||||
PublicPortStart int `json:"publicPortStart"`
|
||||
ComputeID int `json:"vmId"`
|
||||
}
|
||||
|
||||
type ComputePfwListResp []PfwRecord
|
||||
@@ -35,13 +35,12 @@ package pfw
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
|
||||
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"
|
||||
)
|
||||
@@ -50,22 +49,23 @@ func resourcePfwCreate(ctx context.Context, d *schema.ResourceData, m interface{
|
||||
log.Debugf("resourcePfwCreate: called for compute %d", d.Get("compute_id").(int))
|
||||
|
||||
c := m.(*controller.ControllerCfg)
|
||||
urlValues := &url.Values{}
|
||||
urlValues.Add("computeId", strconv.Itoa(d.Get("compute_id").(int)))
|
||||
urlValues.Add("publicPortStart", strconv.Itoa(d.Get("public_port_start").(int)))
|
||||
urlValues.Add("localBasePort", strconv.Itoa(d.Get("local_base_port").(int)))
|
||||
urlValues.Add("proto", d.Get("proto").(string))
|
||||
|
||||
if portEnd, ok := d.GetOk("public_port_end"); ok {
|
||||
urlValues.Add("publicPortEnd", strconv.Itoa(portEnd.(int)))
|
||||
req := compute.PFWAddRequest{
|
||||
ComputeID: uint64(d.Get("compute_id").(int)),
|
||||
PublicPortStart: uint64(d.Get("public_port_start").(int)),
|
||||
LocalBasePort: uint64(d.Get("local_base_port").(int)),
|
||||
Proto: d.Get("proto").(string),
|
||||
}
|
||||
|
||||
pfwId, err := c.DecortAPICall(ctx, "POST", ComputePfwAddAPI, urlValues)
|
||||
if portEnd, ok := d.GetOk("public_port_end"); ok {
|
||||
req.PublicPortEnd = uint64(portEnd.(int))
|
||||
}
|
||||
|
||||
pfwId, err := c.CloudAPI().Compute().PFWAdd(ctx, req)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
|
||||
d.SetId(fmt.Sprintf("%d-%s", d.Get("compute_id").(int), pfwId))
|
||||
d.SetId(fmt.Sprintf("%d-%d", d.Get("compute_id").(int), pfwId))
|
||||
|
||||
pfw, err := utilityPfwCheckPresence(ctx, d, m)
|
||||
if err != nil {
|
||||
@@ -77,24 +77,19 @@ func resourcePfwCreate(ctx context.Context, d *schema.ResourceData, m interface{
|
||||
d.Set("public_port_end", pfw.PublicPortEnd)
|
||||
}
|
||||
|
||||
return nil
|
||||
return resourcePfwRead(ctx, d, m)
|
||||
}
|
||||
|
||||
func resourcePfwRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
|
||||
log.Debugf("resourcePfwRead: called for compute %d, rule %s", d.Get("compute_id").(int), d.Id())
|
||||
|
||||
pfw, err := utilityPfwCheckPresence(ctx, d, m)
|
||||
if pfw == nil {
|
||||
if err != nil {
|
||||
d.SetId("")
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
|
||||
d.Set("compute_id", pfw.ComputeID)
|
||||
d.Set("public_port_start", pfw.PublicPortStart)
|
||||
d.Set("public_port_end", pfw.PublicPortEnd)
|
||||
d.Set("local_ip", pfw.LocalIP)
|
||||
d.Set("local_base_port", pfw.LocalPort)
|
||||
d.Set("proto", pfw.Protocol)
|
||||
flattenPFW(d, pfw)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -103,23 +98,23 @@ func resourcePfwDelete(ctx context.Context, d *schema.ResourceData, m interface{
|
||||
log.Debugf("resourcePfwDelete: called for compute %d, rule %s", d.Get("compute_id").(int), d.Id())
|
||||
|
||||
pfw, err := utilityPfwCheckPresence(ctx, d, m)
|
||||
if pfw == nil {
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
c := m.(*controller.ControllerCfg)
|
||||
urlValues := &url.Values{}
|
||||
urlValues.Add("computeId", strconv.Itoa(d.Get("compute_id").(int)))
|
||||
urlValues.Add("ruleId", strconv.Itoa(pfw.ID))
|
||||
|
||||
_, err = c.DecortAPICall(ctx, "POST", ComputePfwDelAPI, urlValues)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
|
||||
c := m.(*controller.ControllerCfg)
|
||||
req := compute.PFWDelRequest{
|
||||
ComputeID: uint64(d.Get("compute_id").(int)),
|
||||
PFWID: pfw.ID,
|
||||
}
|
||||
|
||||
_, err = c.CloudAPI().Compute().PFWDel(ctx, req)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
|
||||
d.SetId("")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -34,27 +34,23 @@ package pfw
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"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 utilityPfwCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*PfwRecord, error) {
|
||||
func utilityPfwCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*compute.ItemPFW, error) {
|
||||
c := m.(*controller.ControllerCfg)
|
||||
urlValues := &url.Values{}
|
||||
|
||||
urlValues.Add("computeId", strconv.Itoa(d.Get("compute_id").(int)))
|
||||
resp, err := c.DecortAPICall(ctx, "POST", ComputePfwListAPI, urlValues)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
req := compute.PFWListRequest{
|
||||
ComputeID: uint64(d.Get("compute_id").(int)),
|
||||
}
|
||||
|
||||
if resp == "" {
|
||||
return nil, nil
|
||||
pfws, err := c.CloudAPI().Compute().PFWList(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
idS := strings.Split(d.Id(), "-")[1]
|
||||
@@ -63,13 +59,8 @@ func utilityPfwCheckPresence(ctx context.Context, d *schema.ResourceData, m inte
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var pfws []PfwRecord
|
||||
if err := json.Unmarshal([]byte(resp), &pfws); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, pfw := range pfws {
|
||||
if pfw.ID == id {
|
||||
if pfw.ID == uint64(id) {
|
||||
return &pfw, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user