in webv2/api/rules.go [25:120]
func ApplyRule(w http.ResponseWriter, r *http.Request) {
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Body Read Error : %v", err), http.StatusInternalServerError)
return
}
var rule internal.Rule
err = json.Unmarshal(reqBody, &rule)
if err != nil {
http.Error(w, fmt.Sprintf("Request Body parse error : %v", err), http.StatusBadRequest)
return
}
sessionState := session.GetSessionState()
sessionState.Conv.ConvLock.Lock()
defer sessionState.Conv.ConvLock.Unlock()
if rule.Type == constants.GlobalDataTypeChange {
d, err := json.Marshal(rule.Data)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
typeMap := map[string]string{}
err = json.Unmarshal(d, &typeMap)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
setGlobalDataType(typeMap)
} else if rule.Type == constants.AddIndex {
d, err := json.Marshal(rule.Data)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
newIdx := ddl.CreateIndex{}
err = json.Unmarshal(d, &newIdx)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
addedIndex, err := addIndex(newIdx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rule.Data = addedIndex
} else if rule.Type == constants.EditColumnMaxLength {
d, err := json.Marshal(rule.Data)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
var colMaxLength types.ColMaxLength
err = json.Unmarshal(d, &colMaxLength)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
setSpColMaxLength(colMaxLength, rule.AssociatedObjects)
} else if rule.Type == constants.AddShardIdPrimaryKey {
d, err := json.Marshal(rule.Data)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
var shardIdPrimaryKey types.ShardIdPrimaryKey
err = json.Unmarshal(d, &shardIdPrimaryKey)
if err != nil {
http.Error(w, "Invalid rule data", http.StatusInternalServerError)
return
}
tableName := checkInterleaving()
if tableName != "" {
http.Error(w, fmt.Sprintf("Rule cannot be added because some tables, eg: %v are interleaved. Please remove interleaving and try again.", tableName), http.StatusBadRequest)
return
}
setShardIdColumnAsPrimaryKey(shardIdPrimaryKey.AddedAtTheStart)
addShardIdColumnToForeignKeys(shardIdPrimaryKey.AddedAtTheStart)
} else {
http.Error(w, "Invalid rule type", http.StatusInternalServerError)
return
}
ruleId := internal.GenerateRuleId()
rule.Id = ruleId
sessionState.Conv.Rules = append(sessionState.Conv.Rules, rule)
session.UpdateSessionFile()
convm := session.ConvWithMetadata{
SessionMetadata: sessionState.SessionMetadata,
Conv: *sessionState.Conv,
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(convm)
}