/
create.go
145 lines (140 loc) · 3.95 KB
/
create.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package consortium
import (
"bytes"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/kfsoftware/hlf-operator/controllers/testutils"
"github.com/kfsoftware/hlf-operator/controllers/utils"
"github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io"
)
type createConsortiumCmd struct {
configPath string
consortiumName string
peers []string
ordererOrg string
systemChannelID string
user string
}
func (c *createConsortiumCmd) validate() error {
return nil
}
func (c *createConsortiumCmd) run() error {
oclient, err := helpers.GetKubeOperatorClient()
if err != nil {
return err
}
configBackend := config.FromFile(c.configPath)
sdk, err := fabsdk.New(configBackend)
if err != nil {
return err
}
ordService, err := helpers.GetOrderingServiceByFullName(oclient, c.ordererOrg)
if err != nil {
return err
}
mspID := ordService.Spec.MspID
org1AdminClientContext := sdk.Context(
fabsdk.WithUser(c.user),
fabsdk.WithOrg(mspID),
)
ordClient, err := resmgmt.New(org1AdminClientContext)
if err != nil {
return err
}
block, err := ordClient.QueryConfigBlockFromOrderer(c.systemChannelID)
if err != nil {
return err
}
systemChannelConfig, err := resource.ExtractConfigFromBlock(block)
if err != nil {
return err
}
ns := ""
_, peers, err := helpers.GetClusterPeers(oclient, ns)
if err != nil {
return err
}
var peerOrgs []testutils.PeerOrganization
for _, peer := range peers {
if !utils.Contains(c.peers, peer.Name) {
continue
}
certAuth, err := helpers.GetCertAuthByURL(
oclient,
peer.Spec.Secret.Enrollment.Component.Cahost,
peer.Spec.Secret.Enrollment.Component.Caport,
)
if err != nil {
return err
}
var nodes []testutils.PeerNode
peerOrgs = append(peerOrgs, testutils.PeerOrganization{
RootCert: certAuth.Status.CACert,
TLSRootCert: certAuth.Status.TLSCACert,
MspID: peer.Spec.MspID,
Peers: nodes,
})
}
if len(peerOrgs) == 0 {
return errors.Errorf("No peer orgs specified")
}
modifiedConfig, err := testutils.AddConsortiumToConfig(
systemChannelConfig,
testutils.AddConsortiumRequest{
Name: c.consortiumName,
Organizations: peerOrgs,
},
)
if err != nil {
return err
}
confUpdate, err := resmgmt.CalculateConfigUpdate(
c.systemChannelID,
systemChannelConfig,
modifiedConfig,
)
if err != nil {
return err
}
configEnvelopeBytes, err := testutils.GetConfigEnvelopeBytes(confUpdate)
if err != nil {
return err
}
configReader := bytes.NewReader(configEnvelopeBytes)
saveResponse, err := ordClient.SaveChannel(resmgmt.SaveChannelRequest{
ChannelID: c.systemChannelID,
ChannelConfig: configReader,
})
if err != nil {
return err
}
log.Infof("Channel updated, txID=%s", saveResponse.TransactionID)
return nil
}
func NewCreateConsortiumCMD(io.Writer, io.Writer) *cobra.Command {
c := &createConsortiumCmd{}
cmd := &cobra.Command{
Use: "create",
RunE: func(cmd *cobra.Command, args []string) error {
if err := c.validate(); err != nil {
return err
}
return c.run()
},
}
persistentFlags := cmd.PersistentFlags()
persistentFlags.StringVarP(&c.consortiumName, "name", "n", "", "Name of the new consortium")
persistentFlags.StringVarP(&c.ordererOrg, "orderer-org", "", "", "Ordering service name")
persistentFlags.StringVarP(&c.systemChannelID, "system-channel-id", "", "", "System channel name")
persistentFlags.StringVarP(&c.configPath, "config", "", "", "Configuration file for the SDK")
persistentFlags.StringVarP(&c.user, "user", "", "", "User used to issue the transaction")
persistentFlags.StringSliceVarP(&c.peers, "peers", "p", nil, "Organizations belonging to the consortium")
cmd.MarkPersistentFlagRequired("name")
return cmd
}