CreateScalewayImage creates the image and delete old image and snapshot if same name
(instanceID, volumeID, name string)
| 465 | |
| 466 | // CreateScalewayImage creates the image and delete old image and snapshot if same name |
| 467 | func (s *ScalewayClient) CreateScalewayImage(instanceID, volumeID, name string) error { |
| 468 | oldImageID, err := s.getImageID(name, defaultScalewayCommercialType, defaultArch) |
| 469 | if err == nil { |
| 470 | log.Debugf("deleting image %s", oldImageID) |
| 471 | err = s.instanceAPI.DeleteImage(&instance.DeleteImageRequest{ |
| 472 | ImageID: oldImageID, |
| 473 | }) |
| 474 | if err != nil { |
| 475 | return err |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | oldSnapshotsResp, err := s.instanceAPI.ListSnapshots(&instance.ListSnapshotsRequest{ |
| 480 | Name: &name, |
| 481 | }, scw.WithAllPages()) |
| 482 | if err == nil { |
| 483 | for _, oldSnapshot := range oldSnapshotsResp.Snapshots { |
| 484 | log.Debugf("deleting snapshot %s", oldSnapshot.ID) |
| 485 | err = s.instanceAPI.DeleteSnapshot(&instance.DeleteSnapshotRequest{ |
| 486 | SnapshotID: oldSnapshot.ID, |
| 487 | }) |
| 488 | if err != nil { |
| 489 | return err |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | log.Debugf("creating snapshot %s with volume %s", name, volumeID) |
| 495 | snapshotResp, err := s.instanceAPI.CreateSnapshot(&instance.CreateSnapshotRequest{ |
| 496 | VolumeID: volumeID, |
| 497 | Name: name, |
| 498 | }) |
| 499 | if err != nil { |
| 500 | return err |
| 501 | } |
| 502 | |
| 503 | log.Debugf("creating image %s with snapshot %s", name, snapshotResp.Snapshot.ID) |
| 504 | imageResp, err := s.instanceAPI.CreateImage(&instance.CreateImageRequest{ |
| 505 | Name: name, |
| 506 | Arch: instance.Arch(defaultArch), |
| 507 | RootVolume: snapshotResp.Snapshot.ID, |
| 508 | }) |
| 509 | if err != nil { |
| 510 | return err |
| 511 | } |
| 512 | |
| 513 | log.Infof("Image %s with ID %s created", name, imageResp.Image.ID) |
| 514 | |
| 515 | return nil |
| 516 | } |
| 517 | |
| 518 | // DeleteInstanceAndVolumes deletes the instance and the volumes attached |
| 519 | func (s *ScalewayClient) DeleteInstanceAndVolumes(instanceID string) error { |
no test coverage detected