Skip to content

Commit

Permalink
(CLOUDDEV-354): instances
Browse files Browse the repository at this point in the history
  • Loading branch information
anaxaim committed Dec 4, 2023
1 parent ce67c9b commit 9784171
Show file tree
Hide file tree
Showing 6 changed files with 822 additions and 8 deletions.
58 changes: 58 additions & 0 deletions edgecenter/converter/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package converter

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

edgecloud "github.com/Edge-Center/edgecentercloud-go"
)

func ListInterfaceToListInstanceVolumeCreate(volumes []interface{}) ([]edgecloud.InstanceVolumeCreate, error) {
vols := make([]edgecloud.InstanceVolumeCreate, len(volumes))
for i, volume := range volumes {
vol := volume.(map[string]interface{})
var V edgecloud.InstanceVolumeCreate
if err := MapStructureDecoder(&V, &vol, decoderConfig); err != nil {
return nil, err
}
vols[i] = V
}

return vols, nil
}

func ListInterfaceToListInstanceInterface(interfaces []interface{}) ([]edgecloud.InstanceInterface, error) {
ifs := make([]edgecloud.InstanceInterface, len(interfaces))
for idx, i := range interfaces {
inter := i.(map[string]interface{})
var I edgecloud.InstanceInterface
if err := MapStructureDecoder(&I, &inter, decoderConfig); err != nil {
return nil, err
}

floatingIPList := inter["floating_ip"].(*schema.Set).List()
if len(floatingIPList) > 0 {
fip := floatingIPList[0].(map[string]interface{})
if fip["source"].(string) == "new" {
I.FloatingIP.Source = edgecloud.NewFloatingIP
} else {
I.FloatingIP = &edgecloud.InterfaceFloatingIP{
Source: edgecloud.ExistingFloatingIP,
ExistingFloatingID: fip["existing_floating_id"].(string),
}
}
}

sgList := inter["security_groups"].([]interface{})
if len(sgList) > 0 {
sgs := make([]edgecloud.ID, 0, len(sgList))
for _, sg := range sgList {
sgs = append(sgs, edgecloud.ID{ID: sg.(string)})
}
I.SecurityGroups = sgs
}

ifs[idx] = I
}

return ifs, nil
}
43 changes: 42 additions & 1 deletion edgecenter/converter/map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package converter

import "fmt"
import (
"fmt"

"github.com/mitchellh/mapstructure"
)

var decoderConfig = &mapstructure.DecoderConfig{TagName: "json"}

// MapInterfaceToMapString converts a map[string]interface{} to map[string]string.
func MapInterfaceToMapString(m map[string]interface{}) map[string]string {
Expand All @@ -12,3 +18,38 @@ func MapInterfaceToMapString(m map[string]interface{}) map[string]string {

return mapString
}

// MapStructureDecoder decodes the given map into the provided structure using the specified decoder configuration.
func MapStructureDecoder(strct interface{}, v *map[string]interface{}, config *mapstructure.DecoderConfig) error {
config.Result = strct
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return fmt.Errorf("failed to create decoder: %w", err)
}

return decoder.Decode(*v)
}

// MapLeftDiff returns all elements in Left that are not in Right.
func MapLeftDiff(left, right map[string]struct{}) map[string]struct{} {
out := make(map[string]struct{})
for l := range left {
if _, ok := right[l]; !ok {
out[l] = struct{}{}
}
}

return out
}

// MapsIntersection returns all elements in Left that are in Right.
func MapsIntersection(left, right map[string]struct{}) map[string]struct{} {
out := make(map[string]struct{})
for l := range left {
if _, ok := right[l]; ok {
out[l] = struct{}{}
}
}

return out
}
8 changes: 1 addition & 7 deletions edgecenter/instance/datasource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func DataSourceEdgeCenterInstance() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceEdgeCenterInstanceRead,
Description: `A cloud instance is a virtual machine in a cloud environment. Could be used with baremetal also.`,
Description: `A cloud instance is a virtual machine in a cloud environment`,

Schema: map[string]*schema.Schema{
"project_id": {
Expand Down Expand Up @@ -53,11 +53,6 @@ then the first one will be used. it is recommended to use "id"`,
Computed: true,
Description: "name of the region",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "instance description",
},
"vm_state": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -196,7 +191,6 @@ func dataSourceEdgeCenterInstanceRead(ctx context.Context, d *schema.ResourceDat

d.Set("status", foundInstance.Status)
d.Set("region", foundInstance.Region)
d.Set("description", foundInstance.Description)
d.Set("vm_state", foundInstance.VMState)
d.Set("keypair_name", foundInstance.KeypairName)

Expand Down
Loading

0 comments on commit 9784171

Please sign in to comment.