Skip to content

Commit

Permalink
(CLOUDDEV-440): Change to new go client data_source_edgecenter_region
Browse files Browse the repository at this point in the history
  • Loading branch information
a.semenov committed Jan 16, 2024
1 parent 73e7b1c commit 25b1069
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
6 changes: 3 additions & 3 deletions edgecenter/data_source_edgecenter_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func dataSourceRegion() *schema.Resource {
}
}

func dataSourceRegionRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
func dataSourceRegionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start Region reading")

name := d.Get("name").(string)
config := m.(*Config)
provider := config.Provider
regionID, err := GetRegion(provider, 0, name)
clientV2 := config.CloudClient
regionID, err := GetRegion(ctx, clientV2, 0, name)
if err != nil {
return diag.FromErr(err)
}
Expand Down
43 changes: 42 additions & 1 deletion edgecenter/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

edgecloud "github.com/Edge-Center/edgecentercloud-go"
"github.com/Edge-Center/edgecentercloud-go/edgecenter"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/region/v1/regions"
)

const (
Expand Down Expand Up @@ -57,6 +58,45 @@ func ImportStringParser(infoStr string) (projectID int, regionID int, id3 string
return
}

// findRegionByNameLegacy to support backwards compatibility.
func findRegionByNameLegacy(arr []regions.Region, name string) (int, error) {
for _, el := range arr {
if el.DisplayName == name {
return el.ID, nil
}
}
return 0, fmt.Errorf("region with name %s not found", name)
}

// GetRegionLegacy to support backwards compatibility.
func GetRegionLegacy(provider *edgecloud.ProviderClient, regionID int, regionName string) (int, error) {
if regionID != 0 {
return regionID, nil
}
client, err := edgecenter.ClientServiceFromProvider(provider, edgecloud.EndpointOpts{
Name: RegionPoint,
Region: 0,
Project: 0,
Version: VersionPointV1,
})
if err != nil {
return 0, err
}

rs, err := regions.ListAll(client)
if err != nil {
return 0, err
}
log.Printf("[DEBUG] Regions: %v", rs)
regionID, err = findRegionByNameLegacy(rs, regionName)
if err != nil {
return 0, err
}
log.Printf("[DEBUG] The attempt to get the region is successful: regionID=%d", regionID)

return regionID, nil
}

// CreateClient creates a new edgecloud.ServiceClient.
func CreateClient(provider *edgecloud.ProviderClient, d *schema.ResourceData, endpoint string, version string) (*edgecloud.ServiceClient, error) {
projectID, err := GetProject(provider, d.Get("project_id").(int), d.Get("project_name").(string))
Expand All @@ -68,8 +108,9 @@ func CreateClient(provider *edgecloud.ProviderClient, d *schema.ResourceData, en

rawRegionID := d.Get("region_id")
rawRegionName := d.Get("region_name")

if rawRegionID != nil && rawRegionName != nil {
regionID, err = GetRegion(provider, rawRegionID.(int), rawRegionName.(string))
regionID, err = GetRegionLegacy(provider, rawRegionID.(int), rawRegionName.(string))
if err != nil {
return nil, fmt.Errorf("failed to get region: %w", err)
}
Expand Down
20 changes: 5 additions & 15 deletions edgecenter/utils_region.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package edgecenter

import (
"context"
"fmt"
"log"

edgecloud "github.com/Edge-Center/edgecentercloud-go"
"github.com/Edge-Center/edgecentercloud-go/edgecenter"
"github.com/Edge-Center/edgecentercloud-go/edgecenter/region/v1/regions"
edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2"
)

// findRegionByName searches for a region with the specified name in the provided region slice.
// Returns the region ID if found, otherwise returns an error.
func findRegionByName(arr []regions.Region, name string) (int, error) {
func findRegionByName(arr []edgecloudV2.Region, name string) (int, error) {
for _, el := range arr {
if el.DisplayName == name {
return el.ID, nil
Expand All @@ -24,21 +23,12 @@ func findRegionByName(arr []regions.Region, name string) (int, error) {
// If the regionID is provided, it will be returned directly.
// If regionName is provided instead, the function will search for the region by name and return its ID.
// Returns an error if the region is not found or there is an issue with the client.
func GetRegion(provider *edgecloud.ProviderClient, regionID int, regionName string) (int, error) {
func GetRegion(ctx context.Context, client *edgecloudV2.Client, regionID int, regionName string) (int, error) {
if regionID != 0 {
return regionID, nil
}
client, err := edgecenter.ClientServiceFromProvider(provider, edgecloud.EndpointOpts{
Name: RegionPoint,
Region: 0,
Project: 0,
Version: VersionPointV1,
})
if err != nil {
return 0, err
}

rs, err := regions.ListAll(client)
rs, _, err := client.Regions.List(ctx, nil)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit 25b1069

Please sign in to comment.