|
80 | 80 | // zones/zone/machineTypes/machine-type
|
81 | 81 | machineTypeRegex = regexp.MustCompile(machineTypePattern)
|
82 | 82 |
|
| 83 | + storagePoolFieldsRegex = regexp.MustCompile(`^projects/([^/]+)/zones/([^/]+)/storagePools/([^/]+)$`) |
| 84 | + |
83 | 85 | // userErrorCodeMap tells how API error types are translated to error codes.
|
84 | 86 | userErrorCodeMap = map[int]codes.Code{
|
85 | 87 | http.StatusForbidden: codes.PermissionDenied,
|
@@ -393,15 +395,72 @@ func isValidDiskEncryptionKmsKey(DiskEncryptionKmsKey string) bool {
|
393 | 395 | return kmsKeyPattern.MatchString(DiskEncryptionKmsKey)
|
394 | 396 | }
|
395 | 397 |
|
396 |
| -// TODO(amacaskill): Implement this function. |
397 | 398 | // ParseStoragePools returns an error if none of the given storagePools
|
398 | 399 | // (delimited by a comma) are in the format
|
399 | 400 | // projects/project/zones/zone/storagePools/storagePool.
|
400 |
| -func ParseStoragePools(storagePools string) ([]string, error) { |
401 |
| - return nil, status.Errorf(codes.Unimplemented, "") |
| 401 | +func ParseStoragePools(storagePools string) ([]StoragePool, error) { |
| 402 | + spSlice := strings.Split(storagePools, ",") |
| 403 | + parsedStoragePools := []StoragePool{} |
| 404 | + for _, sp := range spSlice { |
| 405 | + project, location, spName, err := fieldsFromStoragePoolResourceName(sp) |
| 406 | + if err != nil { |
| 407 | + return nil, err |
| 408 | + } |
| 409 | + spObj := StoragePool{Project: project, Zone: location, Name: spName, ResourceName: sp} |
| 410 | + parsedStoragePools = append(parsedStoragePools, spObj) |
| 411 | + |
| 412 | + } |
| 413 | + return parsedStoragePools, nil |
402 | 414 | }
|
403 | 415 |
|
404 |
| -// TODO(amacaskill): Implement this function. |
405 |
| -func StoragePoolInZone(storagePools []string, zone string) string { |
406 |
| - return "" |
| 416 | +// fieldsFromResourceName returns the project, zone, and Storage Pool name from the given |
| 417 | +// Storage Pool resource name. The resource name must be in the format |
| 418 | +// projects/project/zones/zone/storagePools/storagePool. |
| 419 | +// All other formats are invalid, and an error will be returned. |
| 420 | +func fieldsFromStoragePoolResourceName(resourceName string) (project, location, spName string, err error) { |
| 421 | + fieldMatches := storagePoolFieldsRegex.FindStringSubmatch(resourceName) |
| 422 | + // Field matches should have 4 strings: [resourceName, project, zone, storagePool]. The first |
| 423 | + // match is the entire string. |
| 424 | + if len(fieldMatches) != 4 { |
| 425 | + err := fmt.Errorf("invalid Storage Pool resource name. Got %s, expected projects/project/zones/zone/storagePools/storagePool", resourceName) |
| 426 | + return "", "", "", err |
| 427 | + } |
| 428 | + project = fieldMatches[1] |
| 429 | + location = fieldMatches[2] |
| 430 | + spName = fieldMatches[3] |
| 431 | + return |
| 432 | +} |
| 433 | + |
| 434 | +// StoragePoolZones returns the unique zones of the given storage pool resource names. |
| 435 | +// Returns an error if multiple storage pools in 1 zone are found. |
| 436 | +func StoragePoolZones(storagePools []StoragePool) ([]string, error) { |
| 437 | + zonesSet := sets.String{} |
| 438 | + var zones []string |
| 439 | + for _, sp := range storagePools { |
| 440 | + if zonesSet.Has(sp.Zone) { |
| 441 | + return nil, fmt.Errorf("found multiple storage pools in zone %s. Only one storage pool per zone is allowed", sp.Zone) |
| 442 | + } |
| 443 | + zonesSet.Insert(sp.Zone) |
| 444 | + zones = append(zones, sp.Zone) |
| 445 | + } |
| 446 | + return zones, nil |
| 447 | +} |
| 448 | + |
| 449 | +func StoragePoolInZone(storagePools []StoragePool, zone string) *StoragePool { |
| 450 | + for _, pool := range storagePools { |
| 451 | + if zone == pool.Zone { |
| 452 | + return &pool |
| 453 | + } |
| 454 | + } |
| 455 | + return nil |
| 456 | +} |
| 457 | + |
| 458 | +func UnorderedSlicesEqual(slice1 []string, slice2 []string) bool { |
| 459 | + set1 := sets.NewString(slice1...) |
| 460 | + set2 := sets.NewString(slice2...) |
| 461 | + spZonesNotInReq := set1.Difference(set2) |
| 462 | + if spZonesNotInReq.Len() != 0 { |
| 463 | + return false |
| 464 | + } |
| 465 | + return true |
407 | 466 | }
|
0 commit comments