Skip to content

Commit

Permalink
added functionality to create volume from snapshot CRN (#73)
Browse files Browse the repository at this point in the history
* added functionality to create volume from snapshot CRN

Signed-off-by: Arashad Ahamad <[email protected]>

* fixed sample code to create volume from snapshot CRN

Signed-off-by: Arashad Ahamad <[email protected]>

---------

Signed-off-by: Arashad Ahamad <[email protected]>
  • Loading branch information
arahamad authored Jul 24, 2024
1 parent 6319cae commit 31ce700
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 27 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ coverage:
.PHONY: vet
vet:
go vet ${GOPACKAGES}

clean:
rm -rf libSample
6 changes: 4 additions & 2 deletions block/provider/create_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ func (vpcs *VPCSession) CreateVolume(volumeRequest provider.Volume) (volumeRespo
Name: volumeRequest.Az,
},
}
// adding snapshot ID in the request if it is provided to create the volume from snapshot
if len(volumeRequest.SnapshotID) > 0 {
// adding snapshot CRN and ID in the request, if it is provided to create the volume from snapshot
if len(volumeRequest.SnapshotCRN) > 0 {
volumeTemplate.SourceSnapshot = &models.Snapshot{CRN: volumeRequest.SnapshotCRN}
} else if len(volumeRequest.SnapshotID) > 0 { // This is for backward compatibility
volumeTemplate.SourceSnapshot = &models.Snapshot{ID: volumeRequest.SnapshotID}
}

Expand Down
2 changes: 1 addition & 1 deletion block/provider/create_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func TestCreateVolume(t *testing.T) {
Profile: &provider.Profile{Name: profileName},
ResourceGroup: &provider.ResourceGroup{ID: "default resource group id", Name: "default resource group"},
},
SnapshotID: "invalid snapshot Id",
Snapshot: provider.Snapshot{SnapshotID: "invalid snapshot Id"},
},
expectedErr: "{Code:SnapshotIDNotFound, Type:RetrivalFailed, Description:'Not a valid snapshot ID'}",
expectedReasonCode: "SnapshotIDNotFound",
Expand Down
2 changes: 1 addition & 1 deletion block/provider/delete_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestDeleteSnapshot(t *testing.T) {
expectedErr: "{Code:SnapshotIDNotFound, Type:RetrivalFailed, Description:'Not a valid snapshot ID",
expectedReasonCode: "SnapshotIDNotFound",
errorCode: &models.Error{
Errors: []models.ErrorItem{models.ErrorItem{
Errors: []models.ErrorItem{{
Code: models.ErrorCode(SnapshotNotFound),
}},
},
Expand Down
2 changes: 1 addition & 1 deletion block/provider/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var minVPCRetryGapAttempt = 3
// maxRetryAttempt ...
var maxVPCRetryAttempt = 46

//ConstantRetryGap ...
// ConstantRetryGap ...
const (
ConstantRetryGap = 10 // seconds
ConstMaxVPCRetryAttempt = 46
Expand Down
1 change: 1 addition & 0 deletions common/vpcclient/models/health_reason.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// Package models ...
package models

// VolumeHealthReason...
type VolumeHealthReason struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Expand Down
14 changes: 1 addition & 13 deletions common/vpcclient/models/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ package models

import "time"

// Snapshot ...
// type Snapshot struct {
// Href string `json:"href,omitempty"`
// ID string `json:"id,omitempty"`
// Name string `json:"name,omitempty"`
// ResourceGroup *ResourceGroup `json:"resource_group,omitempty"`
// CRN string `json:"crn,omitempty"`
// CreatedAt *time.Time `json:"created_at,omitempty"`
// Status StatusType `json:"status,omitempty"`
// Tags []string `json:"tags,omitempty"`
// }

// SnapshotList ...
type SnapshotList struct {
First *HReference `json:"first,omitempty"`
Expand All @@ -52,6 +40,7 @@ type Snapshot struct {
Href string `json:"href,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
CRN string `json:"crn,omitempty"`
MinimumCapacity int64 `json:"minimum_capacity,omitempty"`
ResourceGroup *ResourceGroup `json:"resource_group,omitempty"`
OperatingSystem *OperatingSystem `json:"operating_system,omitempty"`
Expand All @@ -68,7 +57,6 @@ type Snapshot struct {
CapturedAt *time.Time `json:"captured_at,omitempty"`

SourceImage *SourceImage `json:"source_image,omitempty"`
CRN string `json:"crn,omitempty"`
Clones *[]Clone `json:"clones,omitempty"`
BackupPolicyPlan *BackupPolicyPlan `json:"backup_policy_plan,omitempty"`
EncryptionKey *VolumeEncryptionKey `json:"encryption_key,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.6.7
github.com/IBM/ibm-csi-common v1.1.13
github.com/IBM/ibmcloud-volume-interface v1.2.4
github.com/IBM/ibmcloud-volume-interface v1.2.5
github.com/IBM/secret-common-lib v1.1.8
github.com/IBM/secret-utils-lib v1.1.8
github.com/fatih/structs v1.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/IBM/go-sdk-core/v5 v5.9.1 h1:06pXbD9Rgmqqe2HA5YAeQbB4eYRRFgIoOT+Kh3cp
github.com/IBM/go-sdk-core/v5 v5.9.1/go.mod h1:axE2JrRq79gIJTjKPBwV6gWHswvVptBjbcvvCPIxARM=
github.com/IBM/ibm-csi-common v1.1.13 h1:Rkr7CNS9JIfxCUcdtVwvBKjjeqMoJCWgw2jk35wOd/4=
github.com/IBM/ibm-csi-common v1.1.13/go.mod h1:I9fF6E92ihLP0qcw/Dqid5Zkf137VDP/tPfxGl652Cc=
github.com/IBM/ibmcloud-volume-interface v1.2.4 h1:4ULPklSo9ZN7S8d2bS/eBSV99HDowju55bnKT6pt+S0=
github.com/IBM/ibmcloud-volume-interface v1.2.4/go.mod h1:PMe4wOp+EEkVNwlo/iJahh+6/aIvjWo+nAdHB1oGBFE=
github.com/IBM/ibmcloud-volume-interface v1.2.5 h1:k/H9Xgpb5nYULjjs+hIqqfo6W+/3aFK4ljl7nHxBl3Y=
github.com/IBM/ibmcloud-volume-interface v1.2.5/go.mod h1:PMe4wOp+EEkVNwlo/iJahh+6/aIvjWo+nAdHB1oGBFE=
github.com/IBM/secret-common-lib v1.1.8 h1:FX/fSN108phDwlMF9YhFdVCULTWCcHneeuT+dBH6wnk=
github.com/IBM/secret-common-lib v1.1.8/go.mod h1:byLkbt2IGQVsomvjrtVazqXrxYgprzu7s3idcr6hCsk=
github.com/IBM/secret-utils-lib v1.1.8 h1:lepjOeYFFhUvihVIO4oVcL+RQ/vyv866NoUdt3j5Q80=
Expand Down
23 changes: 17 additions & 6 deletions samples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
provider_util "github.com/IBM/ibmcloud-volume-vpc/block/utils"
vpcconfig "github.com/IBM/ibmcloud-volume-vpc/block/vpcconfig"
"github.com/IBM/secret-utils-lib/pkg/k8s_utils"
utils "github.com/IBM/secret-utils-lib/pkg/utils"
uid "github.com/gofrs/uuid"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -80,6 +81,7 @@ func main() {

// Load config file
k8sClient, _ := k8s_utils.FakeGetk8sClientSet()
_ = k8s_utils.FakeCreateSecret(k8sClient, utils.DEFAULT, "./samples/sample-secret-config.toml")
conf, err := config.ReadConfig(k8sClient, logger)
if err != nil {
logger.Fatal("Error loading configuration")
Expand All @@ -98,6 +100,8 @@ func main() {
APIConfig: conf.API,
ServerConfig: conf.Server,
}
// enabling VPC provider as default
vpcBlockConfig.VPCConfig.Enabled = true
// Prepare provider registry
providerRegistry, err := provider_util.InitProviders(vpcBlockConfig, &k8sClient, logger)
if err != nil {
Expand All @@ -116,7 +120,7 @@ func main() {

valid := true
for valid {
fmt.Println("\n\nSelect your choice\n 1- Get volume details \n 2- Create snapshot \n 3- list snapshot \n 4- Create volume \n 5- Snapshot details \n 6- Create volume from snapshot\n 7- Delete volume \n 8- Delete Snapshot \n 9- Authorize volume \n 10- Create VPC Volume \n 11- Attach VPC volume \n 12- Detach VPC volume \n 13- Get volume by name \n 14- List volumes \n 15- Expand volume \n 16- Get volume Attachment \n 17- Get snapshot by name \n Your choice?:")
fmt.Println("\n\nSelect your choice\n 1- Get volume details \n 2- Create snapshot \n 3- list snapshot \n 4- Create volume \n 5- Snapshot details\n 6- Create volume from snapshot\n 7- Delete volume \n 8- Delete Snapshot \n 9- Authorize volume \n 10- Create VPC Volume \n 11- Attach VPC volume \n 12- Detach VPC volume \n 13- Get volume by name \n 14- List volumes \n 15- Expand volume \n 16- Get volume Attachment \n 17- Get snapshot by name \n Your choice?:")

var choiceN int
var volumeID string
Expand Down Expand Up @@ -251,11 +255,11 @@ func main() {
_, _ = fmt.Scanf("%s", &dcName)
volume.Az = dcName

fmt.Printf("\nPlease enter volume size in GB like 20, 40 80 etc : ")
fmt.Printf("\nPlease enter volume size in GB like 20, 40 80 etc: ")
_, _ = fmt.Scanf("%d", &volSize)
volume.Capacity = &volSize

fmt.Printf("\nPlease enter snapshotID : ")
fmt.Printf("\nPlease enter snapshotID: ")
_, _ = fmt.Scanf("%d", &snapshotID)
volume.SnapshotID = snapshotID

Expand Down Expand Up @@ -297,6 +301,7 @@ func main() {
var tags map[string]string
fmt.Printf("Please enter original volume ID to create the volume from snapshot: ")
_, _ = fmt.Scanf("%s", &volumeID)

fmt.Printf("Please enter snapshot ID for creating volume:")
_, _ = fmt.Scanf("%s", &snapshotID)
snapshotVol.SnapshotID = snapshotID
Expand Down Expand Up @@ -418,9 +423,15 @@ func main() {
_, _ = fmt.Scanf("%s", &zone)
volume.Az = zone

fmt.Printf("\nPlease enter snapshotID : ")
_, _ = fmt.Scanf("%s", &snapshotID)
volume.SnapshotID = snapshotID
snapshotCRN := ""
fmt.Printf("\nPlease enter snapshot CRN(if you want to create from snapshot CRN): ")
_, _ = fmt.Scanf("%s", &snapshotCRN)
volume.SnapshotCRN = snapshotCRN
if len(snapshotCRN) <= 1 {
fmt.Printf("\nPlease enter snapshotID : ")
_, _ = fmt.Scanf("%s", &snapshotID)
volume.SnapshotID = snapshotID
}

volume.VPCVolume.Tags = []string{"Testing VPC Volume"}
volumeObj, errr := sess.CreateVolume(*volume)
Expand Down
20 changes: 20 additions & 0 deletions samples/sample-secret-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[Bluemix]
iam_url = "https://iam.test.cloud.ibm.com"
iam_client_id = "bx"
iam_client_secret = "bx"
iam_api_key = ""
refresh_token = ""
pay_tier = "paid"
containers_api_route = "https://containers.test.cloud.ibm.com"
encryption = false
containers_api_route_private = "https://private.containers.test.cloud.ibm.com"

[VPC]
g2_token_exchange_endpoint_url = "https://iam.stage1.bluemix.net"
g2_riaas_endpoint_url = "https://us-south-stage01.iaasdev.cloud.ibm.com/"
g2_riaas_endpoint_private_url = "https://us-south-stage01.iaasdev.cloud.ibm.com"
g2_resource_group_id = ""
g2_api_key = ""
encryption = false
provider_type = "g2"
iks_token_exchange_endpoint_private_url = "https://private.containers.test.cloud.ibm.com"

0 comments on commit 31ce700

Please sign in to comment.