-
Notifications
You must be signed in to change notification settings - Fork 20
/
get_volume_attachment.go
94 lines (88 loc) · 4.86 KB
/
get_volume_attachment.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*******************************************************************************
* 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 (
"errors"
"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"
)
// GetVolumeAttachment get the volume attachment based on the request
func (vpcs *VPCSession) GetVolumeAttachment(volumeAttachmentRequest provider.VolumeAttachmentRequest) (*provider.VolumeAttachmentResponse, error) {
vpcs.Logger.Debug("Entry of GetVolumeAttachment method...", zap.Reflect("volumeAttachmentRequest", volumeAttachmentRequest))
defer vpcs.Logger.Debug("Exit from GetVolumeAttachment method...")
var err error
vpcs.Logger.Info("Validating basic inputs for GetVolumeAttachment method...", zap.Reflect("volumeAttachRequest", volumeAttachmentRequest))
err = vpcs.validateAttachVolumeRequest(volumeAttachmentRequest)
if err != nil {
return nil, err
}
var volumeAttachmentResponse *provider.VolumeAttachmentResponse
volumeAttachment := models.NewVolumeAttachment(volumeAttachmentRequest)
if len(volumeAttachment.ID) > 0 {
//Get volume attachmet by ID if its specified
volumeAttachmentResponse, err = vpcs.getVolumeAttachmentByID(volumeAttachment)
} else {
// Get volume attachment by Volume ID. This is inefficient operation which requires iteration over volume attachment list
volumeAttachmentResponse, err = vpcs.getVolumeAttachmentByVolumeID(volumeAttachment)
}
vpcs.Logger.Info("Volume attachment response", zap.Reflect("volumeAttachmentResponse", volumeAttachmentResponse), zap.Error(err))
return volumeAttachmentResponse, err
}
func (vpcs *VPCSession) getVolumeAttachmentByID(volumeAttachmentRequest models.VolumeAttachment) (*provider.VolumeAttachmentResponse, error) {
vpcs.Logger.Debug("Entry of getVolumeAttachmentByID()")
defer vpcs.Logger.Debug("Exit from getVolumeAttachmentByID()")
vpcs.Logger.Info("Getting VolumeAttachment from VPC provider...")
var err error
var volumeAttachmentResult *models.VolumeAttachment
err = retry(vpcs.Logger, func() error {
volumeAttachmentResult, err = vpcs.APIClientVolAttachMgr.GetVolumeAttachment(&volumeAttachmentRequest, vpcs.Logger)
return err
})
if err != nil {
// API call is failed
userErr := userError.GetUserError(string(userError.VolumeAttachFindFailed), err, volumeAttachmentRequest.Volume.ID, *volumeAttachmentRequest.InstanceID)
return nil, userErr
}
volumeAttachmentResponse := volumeAttachmentResult.ToVolumeAttachmentResponse()
vpcs.Logger.Info("Successfuly retrived volume attachment", zap.Reflect("volumeAttachmentResponse", volumeAttachmentResponse))
return volumeAttachmentResponse, err
}
func (vpcs *VPCSession) getVolumeAttachmentByVolumeID(volumeAttachmentRequest models.VolumeAttachment) (*provider.VolumeAttachmentResponse, error) {
vpcs.Logger.Debug("Entry of getVolumeAttachmentByVolumeID()")
defer vpcs.Logger.Debug("Exit from getVolumeAttachmentByVolumeID()")
vpcs.Logger.Info("Getting VolumeAttachmentList from VPC provider...")
var volumeAttachmentList *models.VolumeAttachmentList
var err error
err = retry(vpcs.Logger, func() error {
volumeAttachmentList, err = vpcs.APIClientVolAttachMgr.ListVolumeAttachment(&volumeAttachmentRequest, vpcs.Logger)
return err
})
if err != nil {
// API call is failed
userErr := userError.GetUserError(string(userError.VolumeAttachFindFailed), err, volumeAttachmentRequest.Volume.ID, *volumeAttachmentRequest.InstanceID)
return nil, userErr
}
// Iterate over the volume attachment list for given instance
for _, volumeAttachmentItem := range volumeAttachmentList.VolumeAttachments {
// Check if volume ID is matching with requested volume ID
if volumeAttachmentItem.Volume.ID == volumeAttachmentRequest.Volume.ID {
vpcs.Logger.Info("Successfully found volume attachment", zap.Reflect("volumeAttachment", volumeAttachmentItem))
volumeResponse := volumeAttachmentItem.ToVolumeAttachmentResponse()
vpcs.Logger.Info("Successfully fetched volume attachment from VPC provider", zap.Reflect("volumeResponse", volumeResponse))
return volumeResponse, nil
}
}
// No volume attahment found in the list. So return error
userErr := userError.GetUserError(string(userError.VolumeAttachFindFailed), errors.New("No VolumeAttachment Found"), volumeAttachmentRequest.Volume.ID, *volumeAttachmentRequest.InstanceID)
vpcs.Logger.Error("Volume attachment not found", zap.Error(err))
return nil, userErr
}