ValidName validates an instance name. There are different validation rules for instance snapshot names so it takes an argument indicating whether the name is to be used for a snapshot or not.
(instanceName string, isSnapshot bool)
| 683 | // ValidName validates an instance name. There are different validation rules for instance snapshot names |
| 684 | // so it takes an argument indicating whether the name is to be used for a snapshot or not. |
| 685 | func ValidName(instanceName string, isSnapshot bool) error { |
| 686 | if isSnapshot { |
| 687 | parentName, snapshotName, _ := api.GetParentAndSnapshotName(instanceName) |
| 688 | err := validate.IsHostname(parentName) |
| 689 | if err != nil { |
| 690 | return fmt.Errorf("Invalid instance name: %w", err) |
| 691 | } |
| 692 | |
| 693 | // Snapshot part is more flexible, but doesn't allow space or / character. |
| 694 | if strings.ContainsAny(snapshotName, " /") { |
| 695 | return errors.New("Invalid instance snapshot name: Cannot contain space or / characters") |
| 696 | } |
| 697 | } else { |
| 698 | if strings.Contains(instanceName, instance.SnapshotDelimiter) { |
| 699 | return fmt.Errorf("The character %q is reserved for snapshots", instance.SnapshotDelimiter) |
| 700 | } |
| 701 | |
| 702 | err := validate.IsHostname(instanceName) |
| 703 | if err != nil { |
| 704 | return fmt.Errorf("Invalid instance name: %w", err) |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | return nil |
| 709 | } |
| 710 | |
| 711 | // CreateInternal creates an instance record and storage volume record in the database and sets up devices. |
| 712 | // Accepts a reverter that revert steps this function does will be added to. It is up to the caller to |
no test coverage detected
searching dependent graphs…