LoadInstanceDatabaseObject loads a db.Instance object, accounting for snapshots.
(ctx context.Context, tx *db.ClusterTx, project, name string)
| 287 | |
| 288 | // LoadInstanceDatabaseObject loads a db.Instance object, accounting for snapshots. |
| 289 | func LoadInstanceDatabaseObject(ctx context.Context, tx *db.ClusterTx, project, name string) (*cluster.Instance, error) { |
| 290 | var container *cluster.Instance |
| 291 | var err error |
| 292 | |
| 293 | if strings.Contains(name, instance.SnapshotDelimiter) { |
| 294 | parts := strings.SplitN(name, instance.SnapshotDelimiter, 2) |
| 295 | instanceName := parts[0] |
| 296 | snapshotName := parts[1] |
| 297 | |
| 298 | inst, err := cluster.GetInstance(ctx, tx.Tx(), project, instanceName) |
| 299 | if err != nil { |
| 300 | return nil, fmt.Errorf("Failed to fetch instance %q in project %q: %w", name, project, err) |
| 301 | } |
| 302 | |
| 303 | snapshot, err := cluster.GetInstanceSnapshot(ctx, tx.Tx(), project, instanceName, snapshotName) |
| 304 | if err != nil { |
| 305 | return nil, fmt.Errorf("Failed to fetch snapshot %q of instance %q in project %q: %w", snapshotName, instanceName, project, err) |
| 306 | } |
| 307 | |
| 308 | c := snapshot.ToInstance(inst.Name, inst.Node, inst.Type, inst.Architecture) |
| 309 | container = &c |
| 310 | } else { |
| 311 | container, err = cluster.GetInstance(ctx, tx.Tx(), project, name) |
| 312 | if err != nil { |
| 313 | return nil, fmt.Errorf("Failed to fetch instance %q in project %q: %w", name, project, err) |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return container, nil |
| 318 | } |
| 319 | |
| 320 | // LoadByProjectAndName loads an instance by project and name. |
| 321 | func LoadByProjectAndName(s *state.State, projectName string, instanceName string) (Instance, error) { |
no test coverage detected
searching dependent graphs…