updates the given module from cyclops API
(clientset *client.CyclopsV1Alpha1Client, moduleName string, values []string)
| 22 | |
| 23 | // updates the given module from cyclops API |
| 24 | func updateModule(clientset *client.CyclopsV1Alpha1Client, moduleName string, values []string) { |
| 25 | module, err := clientset.Modules("cyclops").Get(moduleName) |
| 26 | if err != nil { |
| 27 | fmt.Println("Failed to fetch module ", err) |
| 28 | return |
| 29 | } |
| 30 | |
| 31 | specValuesMap := make(map[string]interface{}) |
| 32 | err = json.Unmarshal(module.Spec.Values.Raw, &specValuesMap) |
| 33 | if err != nil { |
| 34 | fmt.Println("failed to decode json data:", err) |
| 35 | return |
| 36 | } |
| 37 | |
| 38 | for _, v := range values { |
| 39 | keyValue := strings.Split(v, "=") |
| 40 | if len(keyValue) != 2 { |
| 41 | fmt.Println("invalid key value pair: ", v) |
| 42 | return |
| 43 | } |
| 44 | key := keyValue[0] |
| 45 | value := keyValue[1] |
| 46 | |
| 47 | err = unstructured.SetNestedField(specValuesMap, value, strings.Split(key, ".")...) |
| 48 | if err != nil { |
| 49 | fmt.Println(err) |
| 50 | return |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | updatedSpecValues, err := json.Marshal(specValuesMap) |
| 55 | if err != nil { |
| 56 | fmt.Println("failed to encode to json: ", err) |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | module.Spec.Values = apiextensionv1.JSON{Raw: updatedSpecValues} |
| 61 | module.TypeMeta = v1.TypeMeta{ |
| 62 | APIVersion: "cyclops-ui.com/v1alpha1", |
| 63 | Kind: "Module", |
| 64 | } |
| 65 | |
| 66 | _, err = clientset.Modules("cyclops").Update(module) |
| 67 | if err != nil { |
| 68 | fmt.Println("failed to update module: ", err) |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | fmt.Printf("successfully updated %v", moduleName) |
| 73 | } |
| 74 | |
| 75 | var ( |
| 76 | UpdateModuleCMD = &cobra.Command{ |