github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/common/tools/configtxlator/rest/configtxlator_handlers.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 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 rest 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "io/ioutil" 23 "net/http" 24 25 "github.com/hyperledger/fabric/common/tools/configtxlator/sanitycheck" 26 "github.com/hyperledger/fabric/common/tools/configtxlator/update" 27 cb "github.com/hyperledger/fabric/protos/common" 28 29 "github.com/golang/protobuf/proto" 30 ) 31 32 func fieldBytes(fieldName string, r *http.Request) ([]byte, error) { 33 fieldFile, _, err := r.FormFile(fieldName) 34 if err != nil { 35 return nil, err 36 } 37 defer fieldFile.Close() 38 39 return ioutil.ReadAll(fieldFile) 40 } 41 42 func fieldConfigProto(fieldName string, r *http.Request) (*cb.Config, error) { 43 fieldBytes, err := fieldBytes(fieldName, r) 44 if err != nil { 45 return nil, fmt.Errorf("error reading field bytes: %s", err) 46 } 47 48 config := &cb.Config{} 49 err = proto.Unmarshal(fieldBytes, config) 50 if err != nil { 51 return nil, fmt.Errorf("error unmarshaling field bytes: %s", err) 52 } 53 54 return config, nil 55 } 56 57 func ComputeUpdateFromConfigs(w http.ResponseWriter, r *http.Request) { 58 originalConfig, err := fieldConfigProto("original", r) 59 if err != nil { 60 w.WriteHeader(http.StatusBadRequest) 61 fmt.Fprintf(w, "Error with field 'original': %s\n", err) 62 return 63 } 64 65 updatedConfig, err := fieldConfigProto("updated", r) 66 if err != nil { 67 w.WriteHeader(http.StatusBadRequest) 68 fmt.Fprintf(w, "Error with field 'updated': %s\n", err) 69 return 70 } 71 72 configUpdate, err := update.Compute(originalConfig, updatedConfig) 73 if err != nil { 74 w.WriteHeader(http.StatusBadRequest) 75 fmt.Fprintf(w, "Error computing update: %s\n", err) 76 return 77 } 78 79 configUpdate.ChannelId = r.FormValue("channel") 80 81 encoded, err := proto.Marshal(configUpdate) 82 if err != nil { 83 w.WriteHeader(http.StatusInternalServerError) 84 fmt.Fprintf(w, "Error marshaling config update: %s\n", err) 85 return 86 } 87 88 w.WriteHeader(http.StatusOK) 89 w.Header().Set("Content-Type", "application/octet-stream") 90 w.Write(encoded) 91 } 92 93 func SanityCheckConfig(w http.ResponseWriter, r *http.Request) { 94 buf, err := ioutil.ReadAll(r.Body) 95 if err != nil { 96 w.WriteHeader(http.StatusBadRequest) 97 fmt.Fprintln(w, err) 98 return 99 } 100 101 config := &cb.Config{} 102 err = proto.Unmarshal(buf, config) 103 if err != nil { 104 w.WriteHeader(http.StatusBadRequest) 105 fmt.Fprintf(w, "Error unmarshaling data to common.Config': %s\n", err) 106 return 107 } 108 109 fmt.Printf("Sanity checking %+v\n", config) 110 sanityCheckMessages, err := sanitycheck.Check(config) 111 if err != nil { 112 w.WriteHeader(http.StatusInternalServerError) 113 fmt.Fprintf(w, "Error performing sanity check: %s\n", err) 114 return 115 } 116 117 resBytes, err := json.Marshal(sanityCheckMessages) 118 if err != nil { 119 w.WriteHeader(http.StatusInternalServerError) 120 fmt.Fprintf(w, "Error marshaling result to JSON: %s\n", err) 121 return 122 } 123 w.WriteHeader(http.StatusOK) 124 w.Write(resBytes) 125 }