Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Portions copyright 2019-present Open Networking Foundation |
| 3 | * Original copyright 2019-present Ciena Corporation |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package commands |
| 18 | |
| 19 | import ( |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 20 | "context" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 21 | "fmt" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 22 | flags "github.com/jessevdk/go-flags" |
| 23 | "github.com/jhump/protoreflect/dynamic" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 24 | "strings" |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 25 | "time" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 28 | const ( |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 29 | DEFAULT_CREATE_FORMAT = "table{{ .Id }}\t{{ .Message }}" |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 30 | DEFAULT_DELETE_FORMAT = "table{{ .Id }}\t{{ .Message }}" |
| 31 | DEFAULT_UPDATE_FORMAT = "table{{ .Id }}\t{{ .Message }}" |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 32 | DEFAULT_SYNC_FORMAT = "table{{ .Id }}\t{{ .Message }}" |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 35 | type ModelNameString string |
| 36 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 37 | type ModelList struct { |
Scott Baker | a00418a | 2019-06-03 16:15:28 -0700 | [diff] [blame^] | 38 | ListOutputOptions |
Scott Baker | 5201c0b | 2019-05-15 15:35:56 -0700 | [diff] [blame] | 39 | ShowHidden bool `long:"showhidden" description:"Show hidden fields in default output"` |
| 40 | ShowFeedback bool `long:"showfeedback" description:"Show feedback fields in default output"` |
| 41 | ShowBookkeeping bool `long:"showbookkeeping" description:"Show bookkeeping fields in default output"` |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 42 | Filter string `short:"f" long:"filter" description:"Comma-separated list of filters"` |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 43 | Args struct { |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 44 | ModelName ModelNameString |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 45 | } `positional-args:"yes" required:"yes"` |
| 46 | } |
| 47 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 48 | type ModelUpdate struct { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 49 | OutputOptions |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 50 | Unbuffered bool `short:"u" long:"unbuffered" description:"Do not buffer console output and suppress default output processor"` |
| 51 | Filter string `short:"f" long:"filter" description:"Comma-separated list of filters"` |
| 52 | SetFields string `long:"set-field" description:"Comma-separated list of field=value to set"` |
| 53 | SetJSON string `long:"set-json" description:"JSON dictionary to use for settings fields"` |
| 54 | Sync bool `long:"sync" description:"Synchronize before returning"` |
| 55 | SyncTimeout time.Duration `long:"synctimeout" default:"600s" description:"Timeout for --sync option"` |
| 56 | Args struct { |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 57 | ModelName ModelNameString |
| 58 | } `positional-args:"yes" required:"yes"` |
| 59 | IDArgs struct { |
| 60 | ID []int32 |
| 61 | } `positional-args:"yes" required:"no"` |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 64 | type ModelDelete struct { |
| 65 | OutputOptions |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 66 | Unbuffered bool `short:"u" long:"unbuffered" description:"Do not buffer console output and suppress default output processor"` |
| 67 | Filter string `short:"f" long:"filter" description:"Comma-separated list of filters"` |
| 68 | Args struct { |
| 69 | ModelName ModelNameString |
| 70 | } `positional-args:"yes" required:"yes"` |
| 71 | IDArgs struct { |
| 72 | ID []int32 |
| 73 | } `positional-args:"yes" required:"no"` |
| 74 | } |
| 75 | |
| 76 | type ModelCreate struct { |
| 77 | OutputOptions |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 78 | Unbuffered bool `short:"u" long:"unbuffered" description:"Do not buffer console output"` |
| 79 | SetFields string `long:"set-field" description:"Comma-separated list of field=value to set"` |
| 80 | SetJSON string `long:"set-json" description:"JSON dictionary to use for settings fields"` |
| 81 | Sync bool `long:"sync" description:"Synchronize before returning"` |
| 82 | SyncTimeout time.Duration `long:"synctimeout" default:"600s" description:"Timeout for --sync option"` |
| 83 | Args struct { |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 84 | ModelName ModelNameString |
| 85 | } `positional-args:"yes" required:"yes"` |
| 86 | } |
| 87 | |
| 88 | type ModelSync struct { |
| 89 | OutputOptions |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 90 | Unbuffered bool `short:"u" long:"unbuffered" description:"Do not buffer console output and suppress default output processor"` |
| 91 | Filter string `short:"f" long:"filter" description:"Comma-separated list of filters"` |
| 92 | SyncTimeout time.Duration `long:"synctimeout" default:"600s" description:"Timeout for synchronization"` |
| 93 | Args struct { |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 94 | ModelName ModelNameString |
| 95 | } `positional-args:"yes" required:"yes"` |
| 96 | IDArgs struct { |
| 97 | ID []int32 |
| 98 | } `positional-args:"yes" required:"no"` |
| 99 | } |
| 100 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 101 | type ModelOpts struct { |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 102 | List ModelList `command:"list"` |
| 103 | Update ModelUpdate `command:"update"` |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 104 | Delete ModelDelete `command:"delete"` |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 105 | Create ModelCreate `command:"create"` |
| 106 | Sync ModelSync `command:"sync"` |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | type ModelStatusOutputRow struct { |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 110 | Id interface{} `json:"id"` |
| 111 | Message string `json:"message"` |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | type ModelStatusOutput struct { |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 115 | Rows []ModelStatusOutputRow |
| 116 | Unbuffered bool |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | var modelOpts = ModelOpts{} |
| 120 | |
| 121 | func RegisterModelCommands(parser *flags.Parser) { |
| 122 | parser.AddCommand("model", "model commands", "Commands to query and manipulate XOS models", &modelOpts) |
| 123 | } |
| 124 | |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 125 | // Initialize ModelStatusOutput structure, creating a row for each model that will be output |
| 126 | func InitModelStatusOutput(unbuffered bool, count int) ModelStatusOutput { |
| 127 | return ModelStatusOutput{Rows: make([]ModelStatusOutputRow, count), Unbuffered: unbuffered} |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 130 | // Update model status output row for the model |
| 131 | // If unbuffered is set then we will output directly to the console. Regardless of the unbuffered |
| 132 | // setting, we always update the row, as callers may check that row for status. |
| 133 | // Args: |
| 134 | // output - ModelStatusOutput struct to update |
| 135 | // i - index of row to update |
| 136 | // id - id of model, <nil> if no model exists |
| 137 | // status - status text to set if there is no error |
| 138 | // errror - if non-nil, then apply error text instead of status text |
| 139 | // final - true if successful status should be reported, false if successful status is yet to come |
| 140 | |
| 141 | func UpdateModelStatusOutput(output *ModelStatusOutput, i int, id interface{}, status string, err error, final bool) { |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 142 | if err != nil { |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 143 | if output.Unbuffered { |
| 144 | fmt.Printf("%v: %s\n", id, HumanReadableError(err)) |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 145 | } |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 146 | output.Rows[i] = ModelStatusOutputRow{Id: id, Message: HumanReadableError(err)} |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 147 | } else { |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 148 | if output.Unbuffered && final { |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 149 | fmt.Println(id) |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 150 | } |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 151 | output.Rows[i] = ModelStatusOutputRow{Id: id, Message: status} |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 155 | func (options *ModelList) Execute(args []string) error { |
| 156 | conn, descriptor, err := InitReflectionClient() |
| 157 | if err != nil { |
| 158 | return err |
| 159 | } |
| 160 | |
| 161 | defer conn.Close() |
| 162 | |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 163 | err = CheckModelName(descriptor, string(options.Args.ModelName)) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 164 | if err != nil { |
| 165 | return err |
| 166 | } |
| 167 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 168 | queries, err := CommaSeparatedQueryToMap(options.Filter, true) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 169 | if err != nil { |
| 170 | return err |
| 171 | } |
| 172 | |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 173 | models, err := ListOrFilterModels(context.Background(), conn, descriptor, string(options.Args.ModelName), queries) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 174 | if err != nil { |
| 175 | return err |
| 176 | } |
| 177 | |
| 178 | field_names := make(map[string]bool) |
Scott Baker | 5201c0b | 2019-05-15 15:35:56 -0700 | [diff] [blame] | 179 | data := make([]map[string]interface{}, len(models)) |
| 180 | for i, val := range models { |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 181 | data[i] = make(map[string]interface{}) |
| 182 | for _, field_desc := range val.GetKnownFields() { |
| 183 | field_name := field_desc.GetName() |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 184 | |
| 185 | isGuiHidden := strings.Contains(field_desc.GetFieldOptions().String(), "1005:1") |
| 186 | isFeedback := strings.Contains(field_desc.GetFieldOptions().String(), "1006:1") |
| 187 | isBookkeeping := strings.Contains(field_desc.GetFieldOptions().String(), "1007:1") |
| 188 | |
| 189 | if isGuiHidden && (!options.ShowHidden) { |
| 190 | continue |
| 191 | } |
| 192 | |
| 193 | if isFeedback && (!options.ShowFeedback) { |
| 194 | continue |
| 195 | } |
| 196 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 197 | if isBookkeeping && (!options.ShowBookkeeping) { |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 198 | continue |
| 199 | } |
| 200 | |
| 201 | if field_desc.IsRepeated() { |
| 202 | continue |
| 203 | } |
| 204 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 205 | data[i][field_name] = val.GetFieldByName(field_name) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 206 | |
| 207 | field_names[field_name] = true |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | var default_format strings.Builder |
| 212 | default_format.WriteString("table") |
| 213 | first := true |
| 214 | for field_name, _ := range field_names { |
| 215 | if first { |
| 216 | fmt.Fprintf(&default_format, "{{ .%s }}", field_name) |
| 217 | first = false |
| 218 | } else { |
| 219 | fmt.Fprintf(&default_format, "\t{{ .%s }}", field_name) |
| 220 | } |
| 221 | } |
| 222 | |
Scott Baker | a00418a | 2019-06-03 16:15:28 -0700 | [diff] [blame^] | 223 | FormatAndGenerateListOutput(&options.ListOutputOptions, default_format.String(), "{{.id}}", data) |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 224 | |
| 225 | return nil |
| 226 | } |
| 227 | |
| 228 | func (options *ModelUpdate) Execute(args []string) error { |
| 229 | conn, descriptor, err := InitReflectionClient() |
| 230 | if err != nil { |
| 231 | return err |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 234 | defer conn.Close() |
| 235 | |
| 236 | err = CheckModelName(descriptor, string(options.Args.ModelName)) |
| 237 | if err != nil { |
| 238 | return err |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 241 | if (len(options.IDArgs.ID) == 0 && len(options.Filter) == 0) || |
| 242 | (len(options.IDArgs.ID) != 0 && len(options.Filter) != 0) { |
| 243 | return fmt.Errorf("Use either an ID or a --filter to specify which models to update") |
| 244 | } |
| 245 | |
| 246 | queries, err := CommaSeparatedQueryToMap(options.Filter, true) |
| 247 | if err != nil { |
| 248 | return err |
| 249 | } |
| 250 | |
| 251 | updates, err := CommaSeparatedQueryToMap(options.SetFields, true) |
| 252 | if err != nil { |
| 253 | return err |
| 254 | } |
| 255 | |
| 256 | modelName := string(options.Args.ModelName) |
| 257 | |
| 258 | var models []*dynamic.Message |
| 259 | |
| 260 | if len(options.IDArgs.ID) > 0 { |
| 261 | models = make([]*dynamic.Message, len(options.IDArgs.ID)) |
| 262 | for i, id := range options.IDArgs.ID { |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 263 | models[i], err = GetModel(context.Background(), conn, descriptor, modelName, id) |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 264 | if err != nil { |
| 265 | return err |
| 266 | } |
| 267 | } |
| 268 | } else { |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 269 | models, err = ListOrFilterModels(context.Background(), conn, descriptor, modelName, queries) |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 270 | if err != nil { |
| 271 | return err |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if len(models) == 0 { |
| 276 | return fmt.Errorf("Filter matches no objects") |
| 277 | } else if len(models) > 1 { |
| 278 | if !Confirmf("Filter matches %d objects. Continue [y/n] ? ", len(models)) { |
| 279 | return fmt.Errorf("Aborted by user") |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | fields := make(map[string]interface{}) |
| 284 | |
| 285 | if len(options.SetJSON) > 0 { |
| 286 | fields["_json"] = []byte(options.SetJSON) |
| 287 | } |
| 288 | |
| 289 | for fieldName, value := range updates { |
| 290 | value = value[1:] |
| 291 | proto_value, err := TypeConvert(descriptor, modelName, fieldName, value) |
| 292 | if err != nil { |
| 293 | return err |
| 294 | } |
| 295 | fields[fieldName] = proto_value |
| 296 | } |
| 297 | |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 298 | modelStatusOutput := InitModelStatusOutput(options.Unbuffered, len(models)) |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 299 | for i, model := range models { |
| 300 | id := model.GetFieldByName("id").(int32) |
| 301 | fields["id"] = id |
| 302 | err := UpdateModel(conn, descriptor, modelName, fields) |
| 303 | |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 304 | UpdateModelStatusOutput(&modelStatusOutput, i, id, "Updated", err, !options.Sync) |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 307 | if options.Sync { |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 308 | ctx, cancel := context.WithTimeout(context.Background(), options.SyncTimeout) |
| 309 | defer cancel() |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 310 | for i, model := range models { |
| 311 | id := model.GetFieldByName("id").(int32) |
| 312 | if modelStatusOutput.Rows[i].Message == "Updated" { |
| 313 | conditional_printf(!options.Quiet, "Wait for sync: %d ", id) |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 314 | conn, _, err = GetModelWithRetry(ctx, conn, descriptor, modelName, id, GM_UNTIL_ENACTED|Ternary_uint32(options.Quiet, GM_QUIET, 0)) |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 315 | conditional_printf(!options.Quiet, "\n") |
| 316 | UpdateModelStatusOutput(&modelStatusOutput, i, id, "Enacted", err, true) |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | if !options.Unbuffered { |
| 322 | FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_UPDATE_FORMAT, DEFAULT_UPDATE_FORMAT, modelStatusOutput.Rows) |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | return nil |
| 326 | } |
| 327 | |
| 328 | func (options *ModelDelete) Execute(args []string) error { |
| 329 | conn, descriptor, err := InitReflectionClient() |
| 330 | if err != nil { |
| 331 | return err |
| 332 | } |
| 333 | |
| 334 | defer conn.Close() |
| 335 | |
| 336 | err = CheckModelName(descriptor, string(options.Args.ModelName)) |
| 337 | if err != nil { |
| 338 | return err |
| 339 | } |
| 340 | |
| 341 | if (len(options.IDArgs.ID) == 0 && len(options.Filter) == 0) || |
| 342 | (len(options.IDArgs.ID) != 0 && len(options.Filter) != 0) { |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 343 | return fmt.Errorf("Use either an ID or a --filter to specify which models to delete") |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | queries, err := CommaSeparatedQueryToMap(options.Filter, true) |
| 347 | if err != nil { |
| 348 | return err |
| 349 | } |
| 350 | |
| 351 | modelName := string(options.Args.ModelName) |
| 352 | |
| 353 | var ids []int32 |
| 354 | |
| 355 | if len(options.IDArgs.ID) > 0 { |
| 356 | ids = options.IDArgs.ID |
| 357 | } else { |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 358 | models, err := ListOrFilterModels(context.Background(), conn, descriptor, modelName, queries) |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 359 | if err != nil { |
| 360 | return err |
| 361 | } |
| 362 | ids = make([]int32, len(models)) |
| 363 | for i, model := range models { |
| 364 | ids[i] = model.GetFieldByName("id").(int32) |
| 365 | } |
| 366 | if len(ids) == 0 { |
| 367 | return fmt.Errorf("Filter matches no objects") |
| 368 | } else if len(ids) > 1 { |
| 369 | if !Confirmf("Filter matches %d objects. Continue [y/n] ? ", len(models)) { |
| 370 | return fmt.Errorf("Aborted by user") |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 375 | modelStatusOutput := InitModelStatusOutput(options.Unbuffered, len(ids)) |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 376 | for i, id := range ids { |
| 377 | err = DeleteModel(conn, descriptor, modelName, id) |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 378 | UpdateModelStatusOutput(&modelStatusOutput, i, id, "Deleted", err, true) |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 381 | if !options.Unbuffered { |
| 382 | FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_DELETE_FORMAT, DEFAULT_DELETE_FORMAT, modelStatusOutput.Rows) |
| 383 | } |
| 384 | |
| 385 | return nil |
| 386 | } |
| 387 | |
| 388 | func (options *ModelCreate) Execute(args []string) error { |
| 389 | conn, descriptor, err := InitReflectionClient() |
| 390 | if err != nil { |
| 391 | return err |
| 392 | } |
| 393 | |
| 394 | defer conn.Close() |
| 395 | |
| 396 | err = CheckModelName(descriptor, string(options.Args.ModelName)) |
| 397 | if err != nil { |
| 398 | return err |
| 399 | } |
| 400 | |
| 401 | updates, err := CommaSeparatedQueryToMap(options.SetFields, true) |
| 402 | if err != nil { |
| 403 | return err |
| 404 | } |
| 405 | |
| 406 | modelName := string(options.Args.ModelName) |
| 407 | |
| 408 | fields := make(map[string]interface{}) |
| 409 | |
| 410 | if len(options.SetJSON) > 0 { |
| 411 | fields["_json"] = []byte(options.SetJSON) |
| 412 | } |
| 413 | |
| 414 | for fieldName, value := range updates { |
| 415 | value = value[1:] |
| 416 | proto_value, err := TypeConvert(descriptor, modelName, fieldName, value) |
| 417 | if err != nil { |
| 418 | return err |
| 419 | } |
| 420 | fields[fieldName] = proto_value |
| 421 | } |
| 422 | |
| 423 | modelStatusOutput := InitModelStatusOutput(options.Unbuffered, 1) |
| 424 | |
| 425 | err = CreateModel(conn, descriptor, modelName, fields) |
| 426 | UpdateModelStatusOutput(&modelStatusOutput, 0, fields["id"], "Created", err, !options.Sync) |
| 427 | |
| 428 | if options.Sync { |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 429 | ctx, cancel := context.WithTimeout(context.Background(), options.SyncTimeout) |
| 430 | defer cancel() |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 431 | if modelStatusOutput.Rows[0].Message == "Created" { |
| 432 | id := fields["id"].(int32) |
| 433 | conditional_printf(!options.Quiet, "Wait for sync: %d ", id) |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 434 | conn, _, err = GetModelWithRetry(ctx, conn, descriptor, modelName, id, GM_UNTIL_ENACTED|Ternary_uint32(options.Quiet, GM_QUIET, 0)) |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 435 | conditional_printf(!options.Quiet, "\n") |
| 436 | UpdateModelStatusOutput(&modelStatusOutput, 0, id, "Enacted", err, true) |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | if !options.Unbuffered { |
| 441 | FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_CREATE_FORMAT, DEFAULT_CREATE_FORMAT, modelStatusOutput.Rows) |
| 442 | } |
| 443 | |
| 444 | return nil |
| 445 | } |
| 446 | |
| 447 | func (options *ModelSync) Execute(args []string) error { |
| 448 | conn, descriptor, err := InitReflectionClient() |
| 449 | if err != nil { |
| 450 | return err |
| 451 | } |
| 452 | |
| 453 | defer conn.Close() |
| 454 | |
| 455 | err = CheckModelName(descriptor, string(options.Args.ModelName)) |
| 456 | if err != nil { |
| 457 | return err |
| 458 | } |
| 459 | |
| 460 | if (len(options.IDArgs.ID) == 0 && len(options.Filter) == 0) || |
| 461 | (len(options.IDArgs.ID) != 0 && len(options.Filter) != 0) { |
| 462 | return fmt.Errorf("Use either an ID or a --filter to specify which models to sync") |
| 463 | } |
| 464 | |
| 465 | queries, err := CommaSeparatedQueryToMap(options.Filter, true) |
| 466 | if err != nil { |
| 467 | return err |
| 468 | } |
| 469 | |
| 470 | modelName := string(options.Args.ModelName) |
| 471 | |
| 472 | var ids []int32 |
| 473 | |
| 474 | if len(options.IDArgs.ID) > 0 { |
| 475 | ids = options.IDArgs.ID |
| 476 | } else { |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 477 | models, err := ListOrFilterModels(context.Background(), conn, descriptor, modelName, queries) |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 478 | if err != nil { |
| 479 | return err |
| 480 | } |
| 481 | ids = make([]int32, len(models)) |
| 482 | for i, model := range models { |
| 483 | ids[i] = model.GetFieldByName("id").(int32) |
| 484 | } |
| 485 | if len(ids) == 0 { |
| 486 | return fmt.Errorf("Filter matches no objects") |
| 487 | } else if len(ids) > 1 { |
| 488 | if !Confirmf("Filter matches %d objects. Continue [y/n] ? ", len(models)) { |
| 489 | return fmt.Errorf("Aborted by user") |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 494 | ctx, cancel := context.WithTimeout(context.Background(), options.SyncTimeout) |
| 495 | defer cancel() |
| 496 | |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 497 | modelStatusOutput := InitModelStatusOutput(options.Unbuffered, len(ids)) |
| 498 | for i, id := range ids { |
| 499 | conditional_printf(!options.Quiet, "Wait for sync: %d ", id) |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 500 | conn, _, err = GetModelWithRetry(ctx, conn, descriptor, modelName, id, GM_UNTIL_ENACTED|Ternary_uint32(options.Quiet, GM_QUIET, 0)) |
Scott Baker | 8b8a2b5 | 2019-05-21 08:56:28 -0700 | [diff] [blame] | 501 | conditional_printf(!options.Quiet, "\n") |
| 502 | UpdateModelStatusOutput(&modelStatusOutput, i, id, "Enacted", err, true) |
| 503 | } |
| 504 | |
| 505 | if !options.Unbuffered { |
| 506 | FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_SYNC_FORMAT, DEFAULT_SYNC_FORMAT, modelStatusOutput.Rows) |
Scott Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 507 | } |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 508 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 509 | return nil |
| 510 | } |
Scott Baker | 63ce82e | 2019-05-15 09:01:42 -0700 | [diff] [blame] | 511 | |
| 512 | func (modelName *ModelNameString) Complete(match string) []flags.Completion { |
| 513 | conn, descriptor, err := InitReflectionClient() |
| 514 | if err != nil { |
| 515 | return nil |
| 516 | } |
| 517 | |
| 518 | defer conn.Close() |
| 519 | |
| 520 | models, err := GetModelNames(descriptor) |
| 521 | if err != nil { |
| 522 | return nil |
| 523 | } |
| 524 | |
| 525 | list := make([]flags.Completion, 0) |
| 526 | for k := range models { |
| 527 | if strings.HasPrefix(k, match) { |
| 528 | list = append(list, flags.Completion{Item: k}) |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | return list |
| 533 | } |