4.10.0
This commit is contained in:
45
internal/service/cloudapi/trunk/data_source_trunk.go
Normal file
45
internal/service/cloudapi/trunk/data_source_trunk.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package trunk
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"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/terraform-provider-decort/internal/constants"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/dc"
|
||||
)
|
||||
|
||||
func dataSourceTrunkRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
|
||||
|
||||
log.Debugf("dataSourceTrunkRead: called with name %s", d.Get("name").(string))
|
||||
|
||||
w := dc.Warnings{}
|
||||
trunkItem, err := utilityTrunkCheckPresence(ctx, d, m)
|
||||
if err != nil {
|
||||
d.SetId("")
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
|
||||
id := uuid.New()
|
||||
d.SetId(id.String())
|
||||
flattenTrunk(d, trunkItem)
|
||||
|
||||
return w.Get()
|
||||
}
|
||||
|
||||
func DataSourceTrunk() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
SchemaVersion: 1,
|
||||
|
||||
ReadContext: dataSourceTrunkRead,
|
||||
|
||||
Timeouts: &schema.ResourceTimeout{
|
||||
Read: &constants.Timeout30s,
|
||||
Default: &constants.Timeout60s,
|
||||
},
|
||||
|
||||
Schema: dataSourceTrunkSchemaMake(),
|
||||
}
|
||||
}
|
||||
43
internal/service/cloudapi/trunk/data_source_trunk_list.go
Normal file
43
internal/service/cloudapi/trunk/data_source_trunk_list.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package trunk
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/constants"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/dc"
|
||||
)
|
||||
|
||||
func dataSourceTrunkListRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
|
||||
w := dc.Warnings{}
|
||||
trunkList, err := utilityTrunkListCheckPresence(ctx, d, m)
|
||||
if err != nil {
|
||||
d.SetId("")
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
|
||||
id := uuid.New()
|
||||
d.SetId(id.String())
|
||||
|
||||
d.Set("items", flattenTrunkList(trunkList))
|
||||
d.Set("entry_count", trunkList.EntryCount)
|
||||
|
||||
return w.Get()
|
||||
}
|
||||
|
||||
func DataSourceTrunkList() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
SchemaVersion: 1,
|
||||
|
||||
ReadContext: dataSourceTrunkListRead,
|
||||
|
||||
Timeouts: &schema.ResourceTimeout{
|
||||
Read: &constants.Timeout30s,
|
||||
Default: &constants.Timeout60s,
|
||||
},
|
||||
|
||||
Schema: dataSourceTrunkListSchemaMake(),
|
||||
}
|
||||
}
|
||||
56
internal/service/cloudapi/trunk/flattens.go
Normal file
56
internal/service/cloudapi/trunk/flattens.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package trunk
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/trunk"
|
||||
)
|
||||
|
||||
func flattenTrunk(d *schema.ResourceData, trunkItem *trunk.ItemTrunk) {
|
||||
log.Debugf("flattenTrunk: decoded Trunk ID %d",
|
||||
trunkItem.ID)
|
||||
|
||||
d.Set("trunk_id", trunkItem.ID)
|
||||
d.Set("guid", trunkItem.GUID)
|
||||
d.Set("name", trunkItem.Name)
|
||||
d.Set("mac", trunkItem.MAC)
|
||||
d.Set("description", trunkItem.Description)
|
||||
d.Set("account_ids", trunkItem.AccountIDs)
|
||||
d.Set("ovs_bridge", trunkItem.OVSBridge)
|
||||
d.Set("native_vlan_id", trunkItem.NativeVLANID)
|
||||
d.Set("status", trunkItem.Status)
|
||||
d.Set("trunk_tags", trunkItem.TrunkTags)
|
||||
d.Set("created_at", trunkItem.CreatedAt)
|
||||
d.Set("created_by", trunkItem.CreatedBy)
|
||||
d.Set("updated_at", trunkItem.UpdatedAt)
|
||||
d.Set("updated_by", trunkItem.UpdatedBy)
|
||||
d.Set("deleted_at", trunkItem.DeletedAt)
|
||||
d.Set("deleted_by", trunkItem.DeletedBy)
|
||||
}
|
||||
|
||||
func flattenTrunkList(trunkList *trunk.ListTrunks) []map[string]interface{} {
|
||||
res := make([]map[string]interface{}, 0, len(trunkList.Data))
|
||||
for _, trunkItem := range trunkList.Data {
|
||||
temp := map[string]interface{}{
|
||||
"account_ids": trunkItem.AccountIDs,
|
||||
"created_at": trunkItem.CreatedAt,
|
||||
"created_by": trunkItem.CreatedBy,
|
||||
"deleted_at": trunkItem.DeletedAt,
|
||||
"deleted_by": trunkItem.DeletedBy,
|
||||
"description": trunkItem.Description,
|
||||
"guid": trunkItem.GUID,
|
||||
"id": trunkItem.ID,
|
||||
"mac": trunkItem.MAC,
|
||||
"name": trunkItem.Name,
|
||||
"native_vlan_id": trunkItem.NativeVLANID,
|
||||
"ovs_bridge": trunkItem.OVSBridge,
|
||||
"status": trunkItem.Status,
|
||||
"trunk_tags": trunkItem.TrunkTags,
|
||||
"updated_at": trunkItem.UpdatedAt,
|
||||
"updated_by": trunkItem.UpdatedBy,
|
||||
}
|
||||
res = append(res, temp)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
241
internal/service/cloudapi/trunk/schema.go
Normal file
241
internal/service/cloudapi/trunk/schema.go
Normal file
@@ -0,0 +1,241 @@
|
||||
package trunk
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func dataSourceTrunkSchemaMake() map[string]*schema.Schema {
|
||||
log.Debugf("dataSourceTrunkSchemaMake: invoked")
|
||||
|
||||
res := map[string]*schema.Schema{
|
||||
"trunk_id": {
|
||||
Type: schema.TypeInt,
|
||||
Required: true,
|
||||
Description: "trunk id",
|
||||
},
|
||||
"guid": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
Description: "GUID",
|
||||
},
|
||||
"name": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "Name of the trunk",
|
||||
},
|
||||
"mac": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "MAC address",
|
||||
},
|
||||
"description": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "Description of the trunk",
|
||||
},
|
||||
"account_ids": {
|
||||
Type: schema.TypeSet,
|
||||
Computed: true,
|
||||
Elem: &schema.Schema{
|
||||
Type: schema.TypeInt,
|
||||
},
|
||||
Description: "List of account IDs with access to this trunk",
|
||||
},
|
||||
"ovs_bridge": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "OVS bridge name",
|
||||
},
|
||||
"native_vlan_id": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
Description: "Native VLAN ID",
|
||||
},
|
||||
"status": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "if the trunk is enabled",
|
||||
},
|
||||
"trunk_tags": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "List of trunk tags (values between 1-4095)",
|
||||
},
|
||||
"created_at": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
Description: "when the trunk was created",
|
||||
},
|
||||
"created_by": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "who created the trunk",
|
||||
},
|
||||
"updated_at": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
Description: "when the trunk was updated",
|
||||
},
|
||||
"updated_by": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "who updated the trunk",
|
||||
},
|
||||
"deleted_at": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
Description: "when the trunk was updated",
|
||||
},
|
||||
"deleted_by": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "who updated the trunk",
|
||||
},
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func dataSourceTrunkListSchemaMake() map[string]*schema.Schema {
|
||||
res := map[string]*schema.Schema{
|
||||
"trunk_ids": {
|
||||
Type: schema.TypeList,
|
||||
Optional: true,
|
||||
Elem: &schema.Schema{
|
||||
Type: schema.TypeInt,
|
||||
},
|
||||
Description: "ID of the trunk(s) to filter by",
|
||||
},
|
||||
"account_ids": {
|
||||
Type: schema.TypeList,
|
||||
Optional: true,
|
||||
Elem: &schema.Schema{
|
||||
Type: schema.TypeInt,
|
||||
},
|
||||
Description: "Account access ID(s) to filter by",
|
||||
},
|
||||
"trunk_tags": {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Description: "Trunk tags to filter by (value between 1-4095)",
|
||||
},
|
||||
"page": {
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
Description: "Page number.",
|
||||
},
|
||||
"size": {
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
Description: "Page size.",
|
||||
},
|
||||
"status": {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Description: "find by status",
|
||||
},
|
||||
"sort_by": {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Description: "Sort by one of supported fields, format ±<field>",
|
||||
},
|
||||
"items": {
|
||||
Type: schema.TypeList,
|
||||
Computed: true,
|
||||
Elem: &schema.Resource{
|
||||
Schema: map[string]*schema.Schema{
|
||||
"account_ids": {
|
||||
Type: schema.TypeSet,
|
||||
Computed: true,
|
||||
Elem: &schema.Schema{
|
||||
Type: schema.TypeInt,
|
||||
},
|
||||
Description: "List of account IDs with access to this trunk",
|
||||
},
|
||||
"created_at": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
Description: "when the trunk was created",
|
||||
},
|
||||
"created_by": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "who created the trunk",
|
||||
},
|
||||
"deleted_at": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
Description: "when the trunk was updated",
|
||||
},
|
||||
"deleted_by": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "who updated the trunk",
|
||||
},
|
||||
"description": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "Description of the trunk",
|
||||
},
|
||||
"guid": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
Description: "GUID",
|
||||
},
|
||||
"id": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
Description: "Trunk ID",
|
||||
},
|
||||
"mac": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "MAC address",
|
||||
},
|
||||
"name": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "Name of the trunk",
|
||||
},
|
||||
"native_vlan_id": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
Description: "Native VLAN ID",
|
||||
},
|
||||
"ovs_bridge": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "OVS bridge name",
|
||||
},
|
||||
"status": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "if the trunk is enabled",
|
||||
},
|
||||
"trunk_tags": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "List of trunk tags (values between 1-4095)",
|
||||
},
|
||||
"updated_at": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
Description: "when the trunk was updated",
|
||||
},
|
||||
"updated_by": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Description: "who updated the trunk",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"entry_count": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
},
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
45
internal/service/cloudapi/trunk/utility_trunk.go
Normal file
45
internal/service/cloudapi/trunk/utility_trunk.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package trunk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/trunk"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
|
||||
)
|
||||
|
||||
func utilityTrunkCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*trunk.ItemTrunk, error) {
|
||||
c := m.(*controller.ControllerCfg)
|
||||
req := trunk.GetRequest{}
|
||||
|
||||
if d.Get("trunk_id") != nil {
|
||||
if d.Get("trunk_id").(int) == 0 {
|
||||
id, err := strconv.ParseUint(d.Id(), 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.TrunkID = id
|
||||
} else {
|
||||
req.TrunkID = uint64(d.Get("trunk_id").(int))
|
||||
}
|
||||
} else {
|
||||
id, err := strconv.ParseUint(d.Id(), 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.TrunkID = id
|
||||
}
|
||||
|
||||
log.Debugf("utilityTrunkCheckPresence: get trunk network")
|
||||
|
||||
trunkItem, err := c.CloudAPI().Trunk().Get(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return trunkItem, nil
|
||||
}
|
||||
48
internal/service/cloudapi/trunk/utility_trunk_list.go
Normal file
48
internal/service/cloudapi/trunk/utility_trunk_list.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package trunk
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/trunk"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
|
||||
)
|
||||
|
||||
func utilityTrunkListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*trunk.ListTrunks, error) {
|
||||
c := m.(*controller.ControllerCfg)
|
||||
req := trunk.ListRequest{}
|
||||
|
||||
if trunkIDs, ok := d.GetOk("trunk_ids"); ok {
|
||||
IDs := trunkIDs.([]interface{})
|
||||
for _, id := range IDs {
|
||||
req.IDs = append(req.IDs, uint64(id.(int)))
|
||||
}
|
||||
}
|
||||
if accountIDs, ok := d.GetOk("account_ids"); ok {
|
||||
IDs := accountIDs.([]interface{})
|
||||
for _, id := range IDs {
|
||||
req.AccountIDs = append(req.AccountIDs, uint64(id.(int)))
|
||||
}
|
||||
}
|
||||
if trunkTags, ok := d.GetOk("trunk_tags"); ok {
|
||||
req.TrunkTags = trunkTags.(string)
|
||||
}
|
||||
if sortBy, ok := d.GetOk("sort_by"); ok {
|
||||
req.SortBy = sortBy.(string)
|
||||
}
|
||||
if page, ok := d.GetOk("page"); ok {
|
||||
req.Page = uint64(page.(int))
|
||||
}
|
||||
if size, ok := d.GetOk("size"); ok {
|
||||
req.Size = uint64(size.(int))
|
||||
}
|
||||
|
||||
log.Debugf("utilityTrunkListCheckPresence: load trunk network list")
|
||||
trunkList, err := c.CloudAPI().Trunk().List(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return trunkList, nil
|
||||
}
|
||||
Reference in New Issue
Block a user