MCPcopy Index your code
hub / github.com/APIParkLab/APIPark / Create

Method Create

service/cluster/cluster.go:126–189  ·  view source on GitHub ↗
(ctx context.Context, name string, resume string, address string)

Source from the content-addressed store, hash-verified

124}
125
126func (s *imlClusterService) Create(ctx context.Context, name string, resume string, address string) (*Cluster, error) {
127 apintoInfo, err := admin.Admin(address).Info(ctx)
128 if err != nil {
129 return nil, err
130 }
131 operator := utils.UserId(ctx)
132
133 // check cluster
134 query, err := s.store.FirstQuery(ctx, "`uuid` = ?", []interface{}{apintoInfo.Cluster}, "id desc")
135 if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
136 return nil, err
137 }
138 if query != nil {
139 return nil, errors.New("cluster already exists")
140 }
141 // check node
142 nodeIds := utils.SliceToSlice(apintoInfo.Nodes, func(i *admin.Node) string {
143 return i.Id
144 })
145 nodeNames := utils.SliceToSlice(apintoInfo.Nodes, func(i *admin.Node) string {
146 return i.Name
147 })
148
149 nodeExist, err := s.nodeStore.FirstQuery(ctx, "`uuid` in (?) or `name` in (?)", []interface{}{nodeIds, nodeNames}, "id desc")
150 if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
151 return nil, err
152 }
153 if nodeExist != nil {
154 return nil, errors.New("node already exists")
155 }
156
157 en := &cluster.Cluster{
158 Id: 0,
159 UUID: apintoInfo.Cluster,
160 Name: name,
161 Resume: resume,
162 Cluster: apintoInfo.Cluster,
163 Creator: operator,
164 Updater: operator,
165 CreateAt: time.Now(),
166 UpdateAt: time.Now(),
167 }
168 nodeEn, addrEn := s.genNodeEntity(apintoInfo.Cluster, apintoInfo.Nodes)
169 err = s.store.Transaction(ctx, func(ctx context.Context) error {
170
171 err := s.store.Insert(ctx, en)
172 if err != nil {
173 return err
174 }
175 err = s.nodeStore.Insert(ctx, nodeEn...)
176 if err != nil {
177 return err
178 }
179 err = s.nodeAddressStore.Insert(ctx, addrEn...)
180 if err != nil {
181 return err
182 }
183 return nil

Callers

nothing calls this directly

Calls 4

genNodeEntityMethod · 0.95
AdminFunction · 0.92
FromEntityFunction · 0.70
InfoMethod · 0.65

Tested by

no test coverage detected