github.com/oam-dev/kubevela@v1.9.11/pkg/config/provider/provider.go (about) 1 /* 2 Copyright 2023 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package provider 18 19 import ( 20 "errors" 21 "strings" 22 23 "sigs.k8s.io/controller-runtime/pkg/client" 24 25 monitorContext "github.com/kubevela/pkg/monitor/context" 26 wfContext "github.com/kubevela/workflow/pkg/context" 27 "github.com/kubevela/workflow/pkg/cue/model/value" 28 "github.com/kubevela/workflow/pkg/types" 29 30 "github.com/oam-dev/kubevela/pkg/config" 31 ) 32 33 const ( 34 // ProviderName is provider name 35 ProviderName = "config" 36 ) 37 38 // ErrRequestInvalid means the request is invalid 39 var ErrRequestInvalid = errors.New("the request is in valid") 40 41 type provider struct { 42 factory config.Factory 43 } 44 45 // CreateConfigProperties the request body for creating a config 46 type CreateConfigProperties struct { 47 Name string `json:"name"` 48 Namespace string `json:"namespace"` 49 Template string `json:"template,omitempty"` 50 Config map[string]interface{} `json:"config"` 51 } 52 53 // Response the response body 54 type Response struct { 55 Status string `json:"status"` 56 Message string `json:"message"` 57 } 58 59 func (p *provider) Create(ctx monitorContext.Context, _ wfContext.Context, v *value.Value, _ types.Action) error { 60 var ccp CreateConfigProperties 61 if err := v.UnmarshalTo(&ccp); err != nil { 62 return ErrRequestInvalid 63 } 64 name := ccp.Template 65 namespace := "vela-system" 66 if strings.Contains(ccp.Template, "/") { 67 namespacedName := strings.SplitN(ccp.Template, "/", 2) 68 namespace = namespacedName[0] 69 name = namespacedName[1] 70 } 71 configItem, err := p.factory.ParseConfig(ctx.GetContext(), config.NamespacedName{ 72 Name: name, 73 Namespace: namespace, 74 }, config.Metadata{ 75 NamespacedName: config.NamespacedName{ 76 Name: ccp.Name, 77 Namespace: ccp.Namespace, 78 }, 79 Properties: ccp.Config, 80 }) 81 if err != nil { 82 return err 83 } 84 return p.factory.CreateOrUpdateConfig(ctx.GetContext(), configItem, ccp.Namespace) 85 } 86 87 func (p *provider) Read(ctx monitorContext.Context, _ wfContext.Context, v *value.Value, _ types.Action) error { 88 var nn config.NamespacedName 89 if err := v.UnmarshalTo(&nn); err != nil { 90 return ErrRequestInvalid 91 } 92 content, err := p.factory.ReadConfig(ctx.GetContext(), nn.Namespace, nn.Name) 93 if err != nil { 94 return err 95 } 96 return v.FillObject(content, "config") 97 } 98 99 func (p *provider) List(ctx monitorContext.Context, _ wfContext.Context, v *value.Value, _ types.Action) error { 100 template, err := v.GetString("template") 101 if err != nil { 102 return ErrRequestInvalid 103 } 104 namespace, err := v.GetString("namespace") 105 if err != nil { 106 return ErrRequestInvalid 107 } 108 if strings.Contains(template, "/") { 109 namespacedName := strings.SplitN(template, "/", 2) 110 template = namespacedName[1] 111 } 112 configs, err := p.factory.ListConfigs(ctx.GetContext(), namespace, template, "", false) 113 if err != nil { 114 return err 115 } 116 var contents = []map[string]interface{}{} 117 for _, c := range configs { 118 contents = append(contents, map[string]interface{}{ 119 "name": c.Name, 120 "alias": c.Alias, 121 "description": c.Description, 122 "config": c.Properties, 123 }) 124 } 125 return v.FillObject(contents, "configs") 126 } 127 128 func (p *provider) Delete(ctx monitorContext.Context, _ wfContext.Context, v *value.Value, _ types.Action) error { 129 var nn config.NamespacedName 130 if err := v.UnmarshalTo(&nn); err != nil { 131 return errors.New("the request is in valid") 132 } 133 return p.factory.DeleteConfig(ctx.GetContext(), nn.Namespace, nn.Name) 134 } 135 136 // Install register handlers to provider discover. 137 func Install(p types.Providers, cli client.Client, apply config.Dispatcher) { 138 prd := &provider{ 139 factory: config.NewConfigFactoryWithDispatcher(cli, apply), 140 } 141 p.Register(ProviderName, map[string]types.Handler{ 142 "create": prd.Create, 143 "read": prd.Read, 144 "list": prd.List, 145 "delete": prd.Delete, 146 }) 147 }