diff --git a/edgecenter/data_source_edgecenter_region.go b/edgecenter/data_source_edgecenter_region.go index dd6494cb..c5455ac0 100644 --- a/edgecenter/data_source_edgecenter_region.go +++ b/edgecenter/data_source_edgecenter_region.go @@ -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) } diff --git a/edgecenter/utils.go b/edgecenter/utils.go index 6bfd5696..ff4545e6 100644 --- a/edgecenter/utils.go +++ b/edgecenter/utils.go @@ -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 ( @@ -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)) @@ -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) } diff --git a/edgecenter/utils_region.go b/edgecenter/utils_region.go index c7af5aca..14875e24 100644 --- a/edgecenter/utils_region.go +++ b/edgecenter/utils_region.go @@ -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 @@ -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 }