github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/command/thing/delete.go (about) 1 // This file is part of arduino-cloud-cli. 2 // 3 // Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published 7 // by the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 package thing 19 20 import ( 21 "context" 22 "errors" 23 24 "github.com/arduino/arduino-cloud-cli/config" 25 "github.com/arduino/arduino-cloud-cli/internal/iot" 26 ) 27 28 // DeleteParams contains the parameters needed to 29 // delete a thing from Arduino IoT Cloud. 30 // ID and Tags parameters are mutually exclusive 31 // and one among them is required: An error is returned 32 // if they are both nil or if they are both not nil. 33 type DeleteParams struct { 34 ID *string 35 Tags map[string]string 36 } 37 38 // Delete command is used to delete a thing 39 // from Arduino IoT Cloud. 40 func Delete(ctx context.Context, params *DeleteParams, cred *config.Credentials) error { 41 if params.ID == nil && params.Tags == nil { 42 return errors.New("provide either ID or Tags") 43 } else if params.ID != nil && params.Tags != nil { 44 return errors.New("cannot use both ID and Tags. only one of them should be not nil") 45 } 46 47 iotClient, err := iot.NewClient(cred) 48 if err != nil { 49 return err 50 } 51 52 thingIDs := []string{} 53 if params.ID != nil { 54 thingIDs = append(thingIDs, *params.ID) 55 } 56 if params.Tags != nil { 57 th, err := iotClient.ThingList(ctx, nil, nil, false, params.Tags) 58 if err != nil { 59 return err 60 } 61 for _, t := range th { 62 thingIDs = append(thingIDs, t.Id) 63 } 64 } 65 66 for _, id := range thingIDs { 67 err = iotClient.ThingDelete(ctx, id) 68 if err != nil { 69 return err 70 } 71 } 72 73 return nil 74 }