Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions internal/flags/devicegroups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2024 Itential Inc. All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited
// Proprietary and confidential

package flags

import (
"github.com/spf13/cobra"
)

type DeviceGroupCreateOptions struct {
Description string
}

func (o *DeviceGroupCreateOptions) Flags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.Description, "description", o.Description, "Short description of model")
}
22 changes: 18 additions & 4 deletions internal/handlers/descriptors/devicegroups.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
---
##############################################################################
# Reader commands
##############################################################################
#
get:
use: device-groups
group: configuration-manager
Expand All @@ -14,3 +10,21 @@ describe:
group: configuration-manager
description: |
Display details about a device group

create:
use: device-group <name>
group: configuration-manager
description: |
Create new device-group

delete:
use: device-group <name>
group: configuration-manager
description: |
Delete existing device-group

clear:
use: device-groups
group: configuration-manager
description: |
Delete all device-groups
5 changes: 4 additions & 1 deletion internal/handlers/devicegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package handlers

import (
"github.com/itential/ipctl/internal/flags"
"github.com/itential/ipctl/internal/runners"
"github.com/itential/ipctl/pkg/client"
"github.com/itential/ipctl/pkg/config"
Expand All @@ -14,6 +15,8 @@ func NewDeviceGroupHandler(c client.Client, cfg *config.Config, desc Descriptors
return NewAssetHandler(
runners.NewDeviceGroupRunner(c, cfg),
desc[deviceGroupsDescriptor],
nil,
&AssetHandlerFlags{
Create: &flags.DeviceGroupCreateOptions{},
},
)
}
76 changes: 75 additions & 1 deletion internal/runners/devicegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"strings"

"github.com/itential/ipctl/internal/flags"
"github.com/itential/ipctl/pkg/client"
"github.com/itential/ipctl/pkg/config"
"github.com/itential/ipctl/pkg/logger"
Expand All @@ -26,7 +27,13 @@ func NewDeviceGroupRunner(client client.Client, cfg *config.Config) *DeviceGroup
}
}

// Get is the implementation of the command `get devices`
/*
*******************************************************************************
Reader interface
*******************************************************************************
*/

// Get implements the `get devices ...` command
func (r *DeviceGroupRunner) Get(in Request) (*Response, error) {
logger.Trace()

Expand All @@ -49,6 +56,7 @@ func (r *DeviceGroupRunner) Get(in Request) (*Response, error) {

}

// Describe implements the `describe device-group ...` command
func (r *DeviceGroupRunner) Describe(in Request) (*Response, error) {
logger.Trace()

Expand All @@ -64,3 +72,69 @@ func (r *DeviceGroupRunner) Describe(in Request) (*Response, error) {
WithObject(res),
), nil
}

/*
*******************************************************************************
Writer interface
*******************************************************************************
*/

// Create implements the `create device-group ...` command
func (r *DeviceGroupRunner) Create(in Request) (*Response, error) {
logger.Trace()

options := in.Options.(*flags.DeviceGroupCreateOptions)

name := in.Args[0]

res, err := r.service.Create(services.NewDeviceGroup(name, options.Description))
if err != nil {
return nil, err
}

return NewResponse(
fmt.Sprintf("Successfully created new device-group `%s` (%s)", res.Name, res.Id),
WithObject(res),
), nil
}

// Delete implements the `delete device-group ...` command
func (r *DeviceGroupRunner) Delete(in Request) (*Response, error) {
logger.Trace()

name := in.Args[0]

deviceGroup, err := r.service.GetByName(name)
if err != nil {
return nil, err
}

if err := r.service.Delete(deviceGroup.Id); err != nil {
return nil, err
}

return NewResponse(
fmt.Sprintf("Successfully deleted device-group `%s` (%s)", deviceGroup.Name, deviceGroup.Id),
WithObject(deviceGroup),
), nil
}

// Clear implements the `clear device-group ...` command
func (r *DeviceGroupRunner) Clear(in Request) (*Response, error) {
logger.Trace()

groups, err := r.service.GetAll()
if err != nil {
return nil, err
}

for _, ele := range groups {
if err := r.service.Delete(ele.Id); err != nil {
return nil, err
}
}

return NewResponse(
fmt.Sprintf("Deleted %v device-groups", len(groups)),
), nil
}
39 changes: 33 additions & 6 deletions pkg/services/device_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package services

import (
"errors"
"fmt"
"net/http"

Expand All @@ -28,9 +29,12 @@ type DeviceGroupService struct {
client *ServiceClient
}

func NewDevice(name, desc string) DeviceGroup {
func NewDeviceGroup(name, desc string) DeviceGroup {
logger.Trace()
return DeviceGroup{Name: name, Description: desc}
return DeviceGroup{
Name: name,
Description: desc,
}
}

func NewDeviceGroupService(iapClient client.Client) *DeviceGroupService {
Expand Down Expand Up @@ -60,16 +64,39 @@ func (svc *DeviceGroupService) GetAll() ([]DeviceGroup, error) {
}

return res, nil
}

func (svc *DeviceGroupService) GetByName(name string) (*DeviceGroup, error) {
logger.Trace()

groups, err := svc.GetAll()
if err != nil {
return nil, err
}

var deviceGroup *DeviceGroup

for _, ele := range groups {
if ele.Name == name {
deviceGroup = &ele
break
}
}

if deviceGroup == nil {
return nil, errors.New("device group not found")
}

return deviceGroup, nil
}

func (svc *DeviceGroupService) Create(name, desc string, devices []string) (*DeviceGroup, error) {
func (svc *DeviceGroupService) Create(in DeviceGroup) (*DeviceGroup, error) {
logger.Trace()

body := map[string]interface{}{
"groupName": name,
"groupDescription": desc,
"deviceNames": devices,
"groupName": in.Name,
"groupDescription": in.Description,
"deviceNames": "",
}

type Response struct {
Expand Down