-
Notifications
You must be signed in to change notification settings - Fork 34
/
save_config_builder.go
48 lines (39 loc) · 1.82 KB
/
save_config_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package usecases
import (
"fmt"
"github.com/rs/zerolog/log"
"github.com/berops/claudie/proto/pb"
)
// SaveConfigBuilder is a gRPC service: the function saves config to the DB after receiving it from Builder
func (u *Usecases) SaveConfigBuilder(request *pb.SaveConfigRequest) (*pb.SaveConfigResponse, error) {
config := request.GetConfig()
log.Info().Msgf("Saving config %s from Builder", config.Name)
// Save new config to the DB, update csState as dsState
config.CsChecksum = config.DsChecksum
config.BuilderTTL = 0
// In builder microservice, the desired state is also updated i.e. in terraformer (node IPs, etc) thus
// we need to update it in the database.
// However, if deletion has been triggered, the desired state should be nil
if dbConf, err := u.DB.GetConfig(config.Id, pb.IdType_HASH); err != nil {
log.Warn().Msgf("Got error while checking the desired state in the database : %v", err)
} else {
if dbConf.DesiredState != nil {
if err := u.DB.UpdateDs(config); err != nil {
return nil, fmt.Errorf("error while updating desired state: %w", err)
}
}
}
// Update the current state so its equal to the desired state
if err := u.DB.UpdateCs(config); err != nil {
return nil, fmt.Errorf("error while updating csChecksum for %s : %w", config.Name, err)
}
if err := u.DB.UpdateBuilderTTL(config.Name, config.BuilderTTL); err != nil {
return nil, fmt.Errorf("error while updating builderTTL for %s : %w", config.Name, err)
}
// Update workflow state for k8s clusters. (attached LB clusters included)
if err := u.DB.UpdateAllStates(config.Name, config.State); err != nil {
return nil, fmt.Errorf("error while saving workflow state config %s in MongoDB: %w", config.Name, err)
}
log.Info().Msgf("Config %s successfully saved from Builder", config.Name)
return &pb.SaveConfigResponse{Config: config}, nil
}