Skip to content

Commit

Permalink
kubernetes - check resource name and region for idizing volumes and v…
Browse files Browse the repository at this point in the history
…olume snapshots
  • Loading branch information
varshavaradarajan committed Jan 29, 2021
1 parent 71907d6 commit 3a89d68
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
28 changes: 21 additions & 7 deletions commands/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,23 +1010,28 @@ func (s *KubernetesCommandService) RunKubernetesClusterDeleteSelective(c *CmdCon
}
}

cluster, err := kube.Get(clusterID)
if err != nil {
return err
}

var volIDs, snapshotIDs, lbIDs []string
for _, v := range volumes {
volumeID, err := iDize(c, v, "volume")
volumeID, err := iDize(c, v, "volume", cluster.RegionSlug)
if err != nil {
return err
}
volIDs = append(volIDs, volumeID)
}
for _, s := range volSnapshots {
snapID, err := iDize(c, s, "volume_snapshot")
snapID, err := iDize(c, s, "volume_snapshot", cluster.RegionSlug)
if err != nil {
return err
}
snapshotIDs = append(snapshotIDs, snapID)
}
for _, l := range loadBalancers {
lbID, err := iDize(c, l, "load_balancer")
lbID, err := iDize(c, l, "load_balancer", "")
if err != nil {
return err
}
Expand Down Expand Up @@ -2102,12 +2107,12 @@ func clusterByIDorName(kube do.KubernetesService, idOrName string) (*do.Kubernet
// use this as opposed to `clusterByIDorName` if you just care about getting
// a cluster ID and don't need the cluster object itself
func clusterIDize(c *CmdConfig, idOrName string) (string, error) {
return iDize(c, idOrName, "cluster")
return iDize(c, idOrName, "cluster", "")
}

// iDize attempts to make a resource ID/name string be a resource ID.
// use this if you just care about getting a resource ID and don't need the object itself
func iDize(c *CmdConfig, resourceIDOrName string, resType string) (string, error) {
func iDize(c *CmdConfig, resourceIDOrName string, resType string, regionSlug string) (string, error) {
if looksLikeUUID(resourceIDOrName) {
return resourceIDOrName, nil
}
Expand All @@ -2121,7 +2126,7 @@ func iDize(c *CmdConfig, resourceIDOrName string, resType string) (string, error
}

for _, v := range volumes {
if v.Name == resourceIDOrName {
if v.Name == resourceIDOrName && v.Region.Slug == regionSlug {
id := v.ID
ids = append(ids, id)
}
Expand All @@ -2133,7 +2138,7 @@ func iDize(c *CmdConfig, resourceIDOrName string, resType string) (string, error
}

for _, v := range volSnapshots {
if v.Name == resourceIDOrName {
if v.Name == resourceIDOrName && contains(v.Regions, regionSlug) {
id := v.ID
ids = append(ids, id)
}
Expand Down Expand Up @@ -2175,6 +2180,15 @@ func iDize(c *CmdConfig, resourceIDOrName string, resType string) (string, error
}
}

func contains(regions []string, region string) bool {
for _, r := range regions {
if r == region {
return true
}
}
return false
}

// poolByIDorName attempts to find a pool by ID or by name if the argument isn't an ID. If multiple
// pools have the same name, then an error with the pool IDs matching this name is returned.
func poolByIDorName(kube do.KubernetesService, clusterID, idOrName string) (*do.KubernetesNodePool, error) {
Expand Down
2 changes: 2 additions & 0 deletions commands/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ func TestKubernetesDeleteSelective(t *testing.T) {
VolumeSnapshots: []string{snapshotID.String()},
LoadBalancers: []string{lbID.String()},
}
tm.kubernetes.EXPECT().Get(testCluster.ID).Return(&testCluster, nil)
tm.kubernetes.EXPECT().DeleteSelective(testCluster.ID, r).Return(nil)

config.Args = append(config.Args, testCluster.ID)
Expand All @@ -721,6 +722,7 @@ func TestKubernetesDeleteSelective(t *testing.T) {
LoadBalancers: []string{lbID.String()},
}
tm.kubernetes.EXPECT().List().Return(testClusterList, nil)
tm.kubernetes.EXPECT().Get(testCluster.ID).Return(&testCluster, nil)
tm.volumes.EXPECT().List().Return(testVolumes, nil)
tm.snapshots.EXPECT().ListVolume().Return(testSnapshots, nil)
tm.loadBalancers.EXPECT().List().Return(testLoadBalancers, nil)
Expand Down

0 comments on commit 3a89d68

Please sign in to comment.