Skip to content

Commit 3fa4664

Browse files
authored
Merge pull request #119 from pure-jliao/pwx-27619
[pwx-27619] Add options for selected cloudops APIs
2 parents 78e71ce + 0ec1ead commit 3fa4664

17 files changed

Lines changed: 843 additions & 222 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
coverage.txt
2+
.idea/

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ vendor-tidy:
4040
vendor:
4141
go mod vendor
4242

43-
lint:
44-
(mkdir -p tools && cd tools && GO111MODULE=off && go get -v golang.org/x/lint/golint)
43+
$(GOPATH)/bin/golint:
44+
GO111MODULE=off go get -u golang.org/x/lint/golint
45+
lint: $(GOPATH)/bin/golint
4546
for file in $$(find . -name '*.go' | grep -v vendor | \
4647
grep -v '\.pb\.go' | \
4748
grep -v '\.pb\.gw\.go' | \

aws/aws.go

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -322,19 +322,21 @@ func (s *awsOps) InspectInstanceGroupForInstance(instanceID string) (*cloudops.I
322322
return nil, &cloudops.ErrNoInstanceGroup{}
323323
}
324324

325-
func (s *awsOps) ApplyTags(volumeID string, labels map[string]string) error {
325+
func (s *awsOps) ApplyTags(volumeID string, labels map[string]string, options map[string]string) error {
326326
req := &ec2.CreateTagsInput{
327327
Resources: []*string{&volumeID},
328328
Tags: s.tags(labels),
329+
DryRun: dryRun(options),
329330
}
330331
_, err := s.ec2.CreateTags(req)
331332
return err
332333
}
333334

334-
func (s *awsOps) RemoveTags(volumeID string, labels map[string]string) error {
335+
func (s *awsOps) RemoveTags(volumeID string, labels map[string]string, options map[string]string) error {
335336
req := &ec2.DeleteTagsInput{
336337
Resources: []*string{&volumeID},
337338
Tags: s.tags(labels),
339+
DryRun: dryRun(options),
338340
}
339341
_, err := s.ec2.DeleteTags(req)
340342
return err
@@ -660,15 +662,15 @@ func (s *awsOps) FreeDevices(
660662

661663
func (s *awsOps) rollbackCreate(id string, createErr error) error {
662664
logrus.Warnf("Rollback create volume %v, Error %v", id, createErr)
663-
err := s.Delete(id)
665+
err := s.Delete(id, nil)
664666
if err != nil {
665667
logrus.Warnf("Rollback failed volume %v, Error %v", id, err)
666668
}
667669
return createErr
668670
}
669671

670672
func (s *awsOps) refreshVol(id *string) (*ec2.Volume, error) {
671-
vols, err := s.Inspect([]*string{id})
673+
vols, err := s.Inspect([]*string{id}, nil)
672674
if err != nil {
673675
return nil, err
674676
}
@@ -707,8 +709,11 @@ func (s *awsOps) GetDeviceID(vol interface{}) (string, error) {
707709
}
708710
}
709711

710-
func (s *awsOps) Inspect(volumeIds []*string) ([]interface{}, error) {
711-
req := &ec2.DescribeVolumesInput{VolumeIds: volumeIds}
712+
func (s *awsOps) Inspect(volumeIds []*string, options map[string]string) ([]interface{}, error) {
713+
req := &ec2.DescribeVolumesInput{
714+
VolumeIds: volumeIds,
715+
DryRun: dryRun(options),
716+
}
712717
resp, err := s.ec2.DescribeVolumes(req)
713718
if err != nil {
714719
return nil, err
@@ -739,6 +744,7 @@ func (s *awsOps) Enumerate(
739744
volumeIds []*string,
740745
labels map[string]string,
741746
setIdentifier string,
747+
742748
) (map[string][]interface{}, error) {
743749
sets := make(map[string][]interface{})
744750

@@ -778,6 +784,7 @@ func (s *awsOps) Enumerate(
778784
func (s *awsOps) Create(
779785
v interface{},
780786
labels map[string]string,
787+
options map[string]string,
781788
) (interface{}, error) {
782789
vol, ok := v.(*ec2.Volume)
783790
if !ok {
@@ -797,6 +804,7 @@ func (s *awsOps) Create(
797804
VolumeType: vol.VolumeType,
798805
SnapshotId: vol.SnapshotId,
799806
Throughput: vol.Throughput,
807+
DryRun: dryRun(options),
800808
}
801809

802810
if len(s.outpostARN) > 0 {
@@ -846,11 +854,14 @@ func (s *awsOps) Create(
846854
}
847855

848856
func (s *awsOps) DeleteFrom(id, _ string) error {
849-
return s.Delete(id)
857+
return s.Delete(id, nil)
850858
}
851859

852-
func (s *awsOps) Delete(id string) error {
853-
req := &ec2.DeleteVolumeInput{VolumeId: &id}
860+
func (s *awsOps) Delete(id string, options map[string]string) error {
861+
req := &ec2.DeleteVolumeInput{
862+
VolumeId: &id,
863+
DryRun: dryRun(options),
864+
}
854865
_, err := s.ec2.DeleteVolume(req)
855866
return err
856867
}
@@ -879,6 +890,7 @@ func (s *awsOps) Attach(volumeID string, options map[string]string) (string, err
879890
Device: &device,
880891
InstanceId: &s.instance,
881892
VolumeId: &volumeID,
893+
DryRun: dryRun(options),
882894
}
883895
if _, err := s.ec2.AttachVolume(req); err != nil {
884896
if strings.Contains(err.Error(), "is already in use") {
@@ -904,20 +916,21 @@ func (s *awsOps) Attach(volumeID string, options map[string]string) (string, err
904916
return "", fmt.Errorf("failed to attach any of the free devices. Attempted: %v", devices)
905917
}
906918

907-
func (s *awsOps) Detach(volumeID string) error {
908-
return s.detachInternal(volumeID, s.instance)
919+
func (s *awsOps) Detach(volumeID string, options map[string]string) error {
920+
return s.detachInternal(volumeID, s.instance, options)
909921
}
910922

911923
func (s *awsOps) DetachFrom(volumeID, instanceName string) error {
912-
return s.detachInternal(volumeID, instanceName)
924+
return s.detachInternal(volumeID, instanceName, nil)
913925
}
914926

915-
func (s *awsOps) detachInternal(volumeID, instanceName string) error {
927+
func (s *awsOps) detachInternal(volumeID, instanceName string, options map[string]string) error {
916928
force := false
917929
req := &ec2.DetachVolumeInput{
918930
InstanceId: &instanceName,
919931
VolumeId: &volumeID,
920932
Force: &force,
933+
DryRun: dryRun(options),
921934
}
922935
if _, err := s.ec2.DetachVolume(req); err != nil {
923936
return err
@@ -932,6 +945,7 @@ func (s *awsOps) detachInternal(volumeID, instanceName string) error {
932945
func (s *awsOps) Expand(
933946
volumeID string,
934947
newSizeInGiB uint64,
948+
options map[string]string,
935949
) (uint64, error) {
936950
vol, err := s.refreshVol(&volumeID)
937951
if err != nil {
@@ -948,6 +962,7 @@ func (s *awsOps) Expand(
948962
request := &ec2.ModifyVolumeInput{
949963
VolumeId: vol.VolumeId,
950964
Size: &newSizeInGiBInt64,
965+
DryRun: dryRun(options),
951966
}
952967
output, err := s.ec2.ModifyVolume(request)
953968
if err != nil {
@@ -995,16 +1010,19 @@ func (s *awsOps) Expand(
9951010
func (s *awsOps) Snapshot(
9961011
volumeID string,
9971012
readonly bool,
1013+
options map[string]string,
9981014
) (interface{}, error) {
9991015
request := &ec2.CreateSnapshotInput{
10001016
VolumeId: &volumeID,
1017+
DryRun: dryRun(options),
10011018
}
10021019
return s.ec2.CreateSnapshot(request)
10031020
}
10041021

1005-
func (s *awsOps) SnapshotDelete(snapID string) error {
1022+
func (s *awsOps) SnapshotDelete(snapID string, options map[string]string) error {
10061023
request := &ec2.DeleteSnapshotInput{
10071024
SnapshotId: &snapID,
1025+
DryRun: dryRun(options),
10081026
}
10091027

10101028
_, err := s.ec2.DeleteSnapshot(request)
@@ -1201,3 +1219,8 @@ func reverse(a []string) []string {
12011219

12021220
return reversed
12031221
}
1222+
1223+
func dryRun(options map[string]string) *bool {
1224+
_, ok := options[cloudops.DryRunOption]
1225+
return &ok
1226+
}

azure/azure.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func (a *azureOps) SetInstanceGroupSize(instanceGroupID string,
346346

347347
instanceGroupSize := int32(count)
348348
agentPoolProperties := containerservice.ManagedClusterAgentPoolProfileProperties{
349-
Count: &instanceGroupSize,
349+
Count: &instanceGroupSize,
350350
OsType: containerservice.Linux,
351351
}
352352

@@ -368,6 +368,7 @@ func (a *azureOps) SetInstanceGroupSize(instanceGroupID string,
368368
func (a *azureOps) Create(
369369
template interface{},
370370
labels map[string]string,
371+
options map[string]string,
371372
) (interface{}, error) {
372373
d, ok := template.(*compute.Disk)
373374
if !ok {
@@ -494,7 +495,7 @@ func (a *azureOps) handleAttachError(err error) error {
494495
// but did not succeed. We need to explicitly remove it to proceed.
495496
matches := attachFailureMessageRegex.FindStringSubmatch(re.ServiceError.Message)
496497
if len(matches) == 2 {
497-
detachErr := a.Detach(matches[1])
498+
detachErr := a.Detach(matches[1], nil)
498499
if detachErr != nil {
499500
logrus.Warnf("Failed to detach disk %v: %v", matches[1], detachErr)
500501
}
@@ -504,7 +505,7 @@ func (a *azureOps) handleAttachError(err error) error {
504505
return err
505506
}
506507

507-
func (a *azureOps) Detach(diskName string) error {
508+
func (a *azureOps) Detach(diskName string, options map[string]string) error {
508509
return a.detachInternal(diskName, a.instance)
509510
}
510511

@@ -559,7 +560,7 @@ func (a *azureOps) detachInternal(diskName, instance string) error {
559560
return a.waitForDetach(diskName, instance)
560561
}
561562

562-
func (a *azureOps) Delete(diskName string) error {
563+
func (a *azureOps) Delete(diskName string, options map[string]string) error {
563564
ctx := context.Background()
564565
future, err := a.disksClient.Delete(ctx, a.resourceGroupName, diskName)
565566
if err != nil {
@@ -576,12 +577,13 @@ func (a *azureOps) Delete(diskName string) error {
576577
}
577578

578579
func (a *azureOps) DeleteFrom(diskName, _ string) error {
579-
return a.Delete(diskName)
580+
return a.Delete(diskName, nil)
580581
}
581582

582583
func (a *azureOps) Expand(
583584
diskName string,
584585
newSizeInGiB uint64,
586+
options map[string]string,
585587
) (uint64, error) {
586588
disk, err := a.disksClient.Get(
587589
context.Background(),
@@ -644,7 +646,7 @@ func (a *azureOps) FreeDevices(
644646
}
645647
}
646648

647-
func (a *azureOps) Inspect(diskNames []*string) ([]interface{}, error) {
649+
func (a *azureOps) Inspect(diskNames []*string, options map[string]string) ([]interface{}, error) {
648650
var disks []interface{}
649651

650652
for _, diskName := range diskNames {
@@ -823,7 +825,7 @@ func (a *azureOps) devicePath(diskName string) (string, error) {
823825
)
824826
}
825827

826-
func (a *azureOps) Snapshot(diskName string, readonly bool) (interface{}, error) {
828+
func (a *azureOps) Snapshot(diskName string, readonly bool, options map[string]string) (interface{}, error) {
827829
if !readonly {
828830
return nil, fmt.Errorf("read-write snapshots are not supported in Azure")
829831
}
@@ -861,7 +863,7 @@ func (a *azureOps) Snapshot(diskName string, readonly bool) (interface{}, error)
861863
return &snap, err
862864
}
863865

864-
func (a *azureOps) SnapshotDelete(snapName string) error {
866+
func (a *azureOps) SnapshotDelete(snapName string, options map[string]string) error {
865867
ctx := context.Background()
866868
future, err := a.snapshotsClient.Delete(ctx, a.resourceGroupName, snapName)
867869
if err != nil {
@@ -877,7 +879,7 @@ func (a *azureOps) SnapshotDelete(snapName string) error {
877879
return err
878880
}
879881

880-
func (a *azureOps) ApplyTags(diskName string, labels map[string]string) error {
882+
func (a *azureOps) ApplyTags(diskName string, labels map[string]string, options map[string]string) error {
881883
if len(labels) == 0 {
882884
return nil
883885
}
@@ -921,7 +923,7 @@ func (a *azureOps) ApplyTags(diskName string, labels map[string]string) error {
921923
return err
922924
}
923925

924-
func (a *azureOps) RemoveTags(diskName string, labels map[string]string) error {
926+
func (a *azureOps) RemoveTags(diskName string, labels map[string]string, options map[string]string) error {
925927
if len(labels) == 0 {
926928
return nil
927929
}

0 commit comments

Comments
 (0)