/
detach_volume.go
61 lines (56 loc) · 2.88 KB
/
detach_volume.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*******************************************************************************
* IBM Confidential
* OCO Source Materials
* IBM Cloud Container Service, 5737-D43
* (C) Copyright IBM Corp. 2018, 2019 All Rights Reserved.
* The source code for this program is not published or otherwise divested of
* its trade secrets, irrespective of what has been deposited with
* the U.S. Copyright Office.
******************************************************************************/
package provider
import (
"github.com/IBM/ibmcloud-storage-volume-lib/lib/provider"
userError "github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/vpc/messages"
"github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/vpc/vpcclient/models"
"go.uber.org/zap"
"net/http"
)
// DetachVolume detach volume based on given volume attachment request
func (vpcs *VPCSession) DetachVolume(volumeAttachmentTemplate provider.VolumeAttachmentRequest) (*http.Response, error) {
vpcs.Logger.Debug("Entry of DetachVolume method...")
defer vpcs.Logger.Debug("Exit from DetachVolume method...")
var err error
vpcs.Logger.Info("Validating basic inputs for detach method...", zap.Reflect("volumeAttachmentTemplate", volumeAttachmentTemplate))
err = vpcs.validateAttachVolumeRequest(volumeAttachmentTemplate)
if err != nil {
return nil, err
}
var response *http.Response
// First , check if volume is already attached to given instance
vpcs.Logger.Info("Checking if volume is already attached ")
currentVolAttachment, err := vpcs.GetVolumeAttachment(volumeAttachmentTemplate)
if err == nil && currentVolAttachment.Status != StatusDetaching {
// If no error and current volume is not already in detaching state ( i.e in attached or attaching state) attemp to detach
vpcs.Logger.Info("Found volume attachment", zap.Reflect("currentVolAttachment", currentVolAttachment))
volumeAttachment := models.NewVolumeAttachment(volumeAttachmentTemplate)
volumeAttachment.ID = currentVolAttachment.VPCVolumeAttachment.ID
vpcs.Logger.Info("Detaching volume from VPC provider...")
err = retry(vpcs.Logger, func() error {
response, err = vpcs.APIClientVolAttachMgr.DetachVolume(&volumeAttachment, vpcs.Logger)
return err
})
if err != nil {
userErr := userError.GetUserError(string(userError.VolumeDetachFailed), err, volumeAttachmentTemplate.VolumeID, volumeAttachmentTemplate.InstanceID, volumeAttachment.ID)
vpcs.Logger.Error("Volume detach failed with error", zap.Error(err))
return response, userErr
}
vpcs.Logger.Info("Successfully detached volume from VPC provider", zap.Reflect("resp", response))
return response, nil
}
vpcs.Logger.Info("No volume attachment found for", zap.Reflect("currentVolAttachment", currentVolAttachment), zap.Error(err))
// consider volume detach success if its already in Detaching or VolumeAttachment is not found
response = &http.Response{
StatusCode: http.StatusOK,
}
return response, nil
}