Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.

Commit 8667918

Browse files
DEV-59446 delete namespace code refactored
1 parent 2bce9b2 commit 8667918

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

pkg/devicegroup/devicegroup.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,24 @@ func Find(parentID int32, name string, client *client.LMSdkGo) (*models.DeviceGr
131131
return deviceGroup, nil
132132
}
133133

134-
// FindDeviceGroupByID searches for a device group by ID.
135-
func FindDeviceGroupByID(groupID int32, client *client.LMSdkGo) (*models.DeviceGroup, error) {
136-
params := lm.NewGetDeviceGroupByIDParams()
137-
params.SetID(groupID)
134+
// FindDeviceGroupByName searches for a device group by name.
135+
func FindDeviceGroupByName(name string, client *client.LMSdkGo) ([]*models.DeviceGroup, error) {
136+
params := lm.NewGetDeviceGroupListParams()
138137
fields := "name,id,parentId,subGroups"
139138
params.SetFields(&fields)
140-
restResponse, err := client.LM.GetDeviceGroupByID(params)
139+
filter := fmt.Sprintf("name:\"%s\"", name)
140+
params.SetFilter(&filter)
141+
restResponse, err := client.LM.GetDeviceGroupList(params)
141142
if err != nil {
142-
return nil, fmt.Errorf("failed to get device group (id=%v): %v", groupID, err)
143+
return nil, fmt.Errorf("failed to get device group (name=%v): %v", name, err)
143144
}
144145

145-
var deviceGroup *models.DeviceGroup
146+
var deviceGroups []*models.DeviceGroup
146147
if restResponse != nil && restResponse.Payload != nil {
147-
deviceGroup = restResponse.Payload
148+
deviceGroups = restResponse.Payload.Items
148149
}
149150

150-
return deviceGroup, nil
151+
return deviceGroups, nil
151152
}
152153

153154
// Exists returns true if the specified device group exists in the account
@@ -205,6 +206,19 @@ func DeleteSubGroup(deviceGroup *models.DeviceGroup, name string, client *client
205206
return nil
206207
}
207208

209+
// DeleteGroup deletes a device group with the specified deviceGroupID.
210+
func DeleteGroup(deviceGroup *models.DeviceGroup, client *client.LMSdkGo) error {
211+
params := lm.NewDeleteDeviceGroupByIDParams()
212+
params.ID = deviceGroup.ID
213+
deleteChildren := true
214+
params.SetDeleteChildren(&deleteChildren)
215+
deleteHard := true
216+
params.SetDeleteHard(&deleteHard)
217+
log.Infof("Deleting deviceGroup:\"%s\" ID:\"%d\" ParentID:\"%d\"", *deviceGroup.Name, deviceGroup.ID, deviceGroup.ParentID)
218+
_, err := client.LM.DeleteDeviceGroupByID(params)
219+
return err
220+
}
221+
208222
func create(name, appliesTo string, disableAlerting bool, parentID int32, client *client.LMSdkGo) (*models.DeviceGroup, error) {
209223
params := lm.NewAddDeviceGroupParams()
210224
params.SetBody(&models.DeviceGroup{

pkg/watch/namespace/namespace.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,24 @@ func (w *Watcher) DeleteFunc() func(obj interface{}) {
9595
namespace := obj.(*v1.Namespace)
9696
log.Debugf("Handle deleting namespace event: %s", namespace.Name)
9797

98-
for name, groupID := range w.DeviceGroups {
99-
deviceGroup, err := devicegroup.FindDeviceGroupByID(groupID, w.LMClient)
100-
if err != nil {
101-
log.Warnf("Failed to find namespace %s: %v", name, err)
102-
return
103-
}
104-
// We should only be returned a device group if it is namespaced.
105-
if deviceGroup == nil {
106-
log.Warnf("Device group (id=%v name=%s) not found: %v", groupID, name, err)
107-
continue
108-
}
109-
err = devicegroup.DeleteSubGroup(deviceGroup, namespace.Name, w.LMClient)
110-
if err != nil {
111-
log.Errorf("Failed to delete namespace %q: %v", namespace.Name, err)
112-
return
113-
}
98+
deviceGroups, err := devicegroup.FindDeviceGroupByName(namespace.Name, w.LMClient)
99+
if err != nil {
100+
log.Errorf("Error: %v", err)
101+
return
114102
}
115103

104+
for _, groupID := range w.DeviceGroups {
105+
for _, d := range deviceGroups {
106+
if d.ParentID == groupID {
107+
log.Infof("Found deviceGroup:\"%s\" with id:\"%d\"", *d.Name, d.ID)
108+
err = devicegroup.DeleteGroup(d, w.LMClient)
109+
if err != nil {
110+
log.Errorf("Failed to delete namespace %q: %v", namespace.Name, err)
111+
return
112+
}
113+
break
114+
}
115+
}
116+
}
116117
}
117118
}

0 commit comments

Comments
 (0)