|
55 | 55 | volumeNameRegex = regexp.MustCompile(`^projects/(?P<projectNumber>[^/]+)/locations/(?P<location>[^/]+)/volumes/(?P<volume>[^/]+)$`) |
56 | 56 | snapshotNameRegex = regexp.MustCompile(`^projects/(?P<projectNumber>[^/]+)/locations/(?P<location>[^/]+)/volumes/(?P<volume>[^/]+)/snapshots/(?P<snapshot>[^/]+)$`) |
57 | 57 | networkNameRegex = regexp.MustCompile(`^projects/(?P<projectNumber>[^/]+)/global/networks/(?P<network>[^/]+)$`) |
| 58 | + zoneSuffixRegex = regexp.MustCompile(`-[a-z]$`) |
58 | 59 | ) |
59 | 60 |
|
60 | 61 | // ClientConfig holds configuration data for the API driver object. |
@@ -368,14 +369,16 @@ func (c Client) findAllLocationsFromCapacityPool(flexPoolsCount int) map[string] |
368 | 369 | return locations |
369 | 370 | } |
370 | 371 |
|
371 | | -// getLocationFromCapacityPools returns the location from the first available capacity pool. |
372 | | -// Returns an error if no capacity pools are available. |
373 | | -func (c Client) getLocationFromCapacityPools() (string, error) { |
374 | | - pools := c.CapacityPools() |
375 | | - if pools == nil || len(*pools) == 0 { |
376 | | - return "", fmt.Errorf("no capacity pools available") |
| 372 | +// getRegionalLocation returns the regional location from the backend config. |
| 373 | +// Host groups are regional resources and must use a regional location (e.g., "us-east4"), even when |
| 374 | +// the backend config specifies a zonal location. The zone suffix is stripped if present. |
| 375 | +// Zonal locations like "us-east4-a" are trimmed to "us-east4". |
| 376 | +// Regional locations like "us-east4" are returned as-is. |
| 377 | +func (c Client) getRegionalLocation() (string, error) { |
| 378 | + if c.config.Location == "" { |
| 379 | + return "", fmt.Errorf("backend config location is not set") |
377 | 380 | } |
378 | | - return (*pools)[0].Location, nil |
| 381 | + return zoneSuffixRegex.ReplaceAllString(c.config.Location, ""), nil |
379 | 382 | } |
380 | 383 |
|
381 | 384 | // /////////////////////////////////////////////////////////////////////////////// |
@@ -1878,9 +1881,9 @@ func (c Client) HostGroups(ctx context.Context) ([]*HostGroup, error) { |
1878 | 1881 |
|
1879 | 1882 | Logc(ctx).WithFields(logFields).Debug("Listing host groups via v1 protobuf SDK.") |
1880 | 1883 |
|
1881 | | - location, err := c.getLocationFromCapacityPools() |
| 1884 | + location, err := c.getRegionalLocation() |
1882 | 1885 | if err != nil { |
1883 | | - return []*HostGroup{}, nil |
| 1886 | + return nil, err |
1884 | 1887 | } |
1885 | 1888 |
|
1886 | 1889 | sdkCtx, cancel := context.WithTimeout(ctx, c.config.SDKTimeout) |
@@ -1917,7 +1920,7 @@ func (c Client) HostGroupByName(ctx context.Context, name string) (*HostGroup, e |
1917 | 1920 |
|
1918 | 1921 | Logc(ctx).WithFields(logFields).Debug("Fetching host group by name via v1 protobuf SDK.") |
1919 | 1922 |
|
1920 | | - location, err := c.getLocationFromCapacityPools() |
| 1923 | + location, err := c.getRegionalLocation() |
1921 | 1924 | if err != nil { |
1922 | 1925 | return nil, err |
1923 | 1926 | } |
@@ -1955,7 +1958,7 @@ func (c Client) CreateHostGroup(ctx context.Context, request *HostGroupCreateReq |
1955 | 1958 |
|
1956 | 1959 | Logc(ctx).WithFields(logFields).Debug("Creating host group via v1 protobuf SDK.") |
1957 | 1960 |
|
1958 | | - location, err := c.getLocationFromCapacityPools() |
| 1961 | + location, err := c.getRegionalLocation() |
1959 | 1962 | if err != nil { |
1960 | 1963 | return nil, err |
1961 | 1964 | } |
|
0 commit comments