RenameInstance renames the instance's root volume and any snapshot volumes.
(inst instance.Instance, newName string, op *operations.Operation)
| 2260 | |
| 2261 | // RenameInstance renames the instance's root volume and any snapshot volumes. |
| 2262 | func (b *backend) RenameInstance(inst instance.Instance, newName string, op *operations.Operation) error { |
| 2263 | l := b.logger.AddContext(logger.Ctx{"project": inst.Project().Name, "instance": inst.Name(), "newName": newName}) |
| 2264 | l.Debug("RenameInstance started") |
| 2265 | defer l.Debug("RenameInstance finished") |
| 2266 | |
| 2267 | if inst.IsSnapshot() { |
| 2268 | return errors.New("Instance cannot be a snapshot") |
| 2269 | } |
| 2270 | |
| 2271 | if internalInstance.IsSnapshot(newName) { |
| 2272 | return errors.New("New name cannot be a snapshot") |
| 2273 | } |
| 2274 | |
| 2275 | // Check we can convert the instance to the volume types needed. |
| 2276 | volType, err := InstanceTypeToVolumeType(inst.Type()) |
| 2277 | if err != nil { |
| 2278 | return err |
| 2279 | } |
| 2280 | |
| 2281 | volDBType, err := VolumeTypeToDBType(volType) |
| 2282 | if err != nil { |
| 2283 | return err |
| 2284 | } |
| 2285 | |
| 2286 | reverter := revert.New() |
| 2287 | defer reverter.Fail() |
| 2288 | |
| 2289 | volume, err := VolumeDBGet(b, inst.Project().Name, inst.Name(), volType) |
| 2290 | if err != nil && !response.IsNotFoundError(err) { |
| 2291 | return err |
| 2292 | } |
| 2293 | |
| 2294 | var snapshots []string |
| 2295 | |
| 2296 | err = b.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error { |
| 2297 | var err error |
| 2298 | |
| 2299 | // Get any snapshots the instance has in the format <instance name>/<snapshot name>. |
| 2300 | snapshots, err = tx.GetInstanceSnapshotsNames(ctx, inst.Project().Name, inst.Name()) |
| 2301 | |
| 2302 | return err |
| 2303 | }) |
| 2304 | if err != nil { |
| 2305 | return err |
| 2306 | } |
| 2307 | |
| 2308 | if len(snapshots) > 0 { |
| 2309 | reverter.Add(func() { |
| 2310 | _ = b.removeInstanceSnapshotSymlinkIfUnused(inst.Type(), inst.Project().Name, newName) |
| 2311 | _ = b.ensureInstanceSnapshotSymlink(inst.Type(), inst.Project().Name, inst.Name()) |
| 2312 | }) |
| 2313 | } |
| 2314 | |
| 2315 | volStorageName := project.Instance(inst.Project().Name, inst.Name()) |
| 2316 | newVolStorageName := project.Instance(inst.Project().Name, newName) |
| 2317 | contentType := InstanceContentType(inst) |
| 2318 | |
| 2319 | vol := b.GetVolume(volType, contentType, volStorageName, volume.Config) |
nothing calls this directly
no test coverage detected