gitee.com/zhaochuninhefei/fabric-ca-gm@v0.0.2/cmd/fabric-ca-client/command/affiliation.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 command
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"gitee.com/zhaochuninhefei/fabric-ca-gm/internal/pkg/api"
    23  	calog "gitee.com/zhaochuninhefei/fabric-ca-gm/internal/pkg/log"
    24  	log "gitee.com/zhaochuninhefei/zcgolog/zclog"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  type affiliationArgs struct {
    29  	affiliation string
    30  	add         api.AddAffiliationRequest
    31  	modify      api.ModifyAffiliationRequest
    32  	remove      api.RemoveAffiliationRequest
    33  }
    34  
    35  func (c *ClientCmd) newAffiliationCommand() *cobra.Command {
    36  	affiliationCmd := &cobra.Command{
    37  		Use:   "affiliation",
    38  		Short: "Manage affiliations",
    39  		Long:  "Manage affiliations",
    40  	}
    41  	affiliationCmd.AddCommand(c.newListAffiliationCommand())
    42  	affiliationCmd.AddCommand(c.newAddAffiliationCommand())
    43  	affiliationCmd.AddCommand(c.newModifyAffiliationCommand())
    44  	affiliationCmd.AddCommand(c.newRemoveAffiliationCommand())
    45  	return affiliationCmd
    46  }
    47  
    48  func (c *ClientCmd) newListAffiliationCommand() *cobra.Command {
    49  	affiliationListCmd := &cobra.Command{
    50  		Use:   "list",
    51  		Short: "List affiliations",
    52  		Long:  "List affiliations visible to caller",
    53  		PreRunE: func(cmd *cobra.Command, args []string) error {
    54  			c.SetDefaultLogLevel(calog.WARNING)
    55  			err := c.ConfigInit()
    56  			if err != nil {
    57  				return err
    58  			}
    59  
    60  			log.Debugf("Client configuration settings: %+v", c.clientCfg)
    61  
    62  			return nil
    63  		},
    64  		RunE: c.runListAffiliation,
    65  	}
    66  	flags := affiliationListCmd.Flags()
    67  	flags.StringVarP(
    68  		&c.dynamicAffiliation.affiliation, "affiliation", "", "", "Get affiliation information from the fabric-ca server")
    69  	return affiliationListCmd
    70  }
    71  
    72  func (c *ClientCmd) newAddAffiliationCommand() *cobra.Command {
    73  	affiliationAddCmd := &cobra.Command{
    74  		Use:     "add <affiliation>",
    75  		Short:   "Add affiliation",
    76  		Long:    "Add affiliation",
    77  		PreRunE: c.affiliationPreRunE,
    78  		RunE:    c.runAddAffiliation,
    79  	}
    80  	flags := affiliationAddCmd.Flags()
    81  	flags.BoolVarP(
    82  		&c.dynamicAffiliation.add.Force, "force", "", false, "Creates parent affiliations if they do not exist")
    83  	return affiliationAddCmd
    84  }
    85  
    86  func (c *ClientCmd) newModifyAffiliationCommand() *cobra.Command {
    87  	affiliationModifyCmd := &cobra.Command{
    88  		Use:     "modify <affiliation>",
    89  		Short:   "Modify affiliation",
    90  		Long:    "Modify existing affiliation",
    91  		PreRunE: c.affiliationPreRunE,
    92  		RunE:    c.runModifyAffiliation,
    93  	}
    94  	flags := affiliationModifyCmd.Flags()
    95  	flags.StringVarP(
    96  		&c.dynamicAffiliation.modify.NewName, "name", "", "", "Rename the affiliation")
    97  	flags.BoolVarP(
    98  		&c.dynamicAffiliation.modify.Force, "force", "", false, "Forces identities using old affiliation to use new affiliation")
    99  	return affiliationModifyCmd
   100  }
   101  
   102  func (c *ClientCmd) newRemoveAffiliationCommand() *cobra.Command {
   103  	affiliationRemoveCmd := &cobra.Command{
   104  		Use:     "remove <affiliation>",
   105  		Short:   "Remove affiliation",
   106  		Long:    "Remove affiliation",
   107  		PreRunE: c.affiliationPreRunE,
   108  		RunE:    c.runRemoveAffiliation,
   109  	}
   110  	flags := affiliationRemoveCmd.Flags()
   111  	flags.BoolVarP(
   112  		&c.dynamicAffiliation.remove.Force, "force", "", false, "Forces removal of any child affiliations and any identities associated with removed affiliations")
   113  	return affiliationRemoveCmd
   114  }
   115  
   116  // The client side logic for listing affiliation information
   117  func (c *ClientCmd) runListAffiliation(cmd *cobra.Command, args []string) error {
   118  	log.Debugf("Entered runListAffiliation: %+v", c.dynamicAffiliation)
   119  
   120  	id, err := c.LoadMyIdentity()
   121  	if err != nil {
   122  		return err
   123  	}
   124  
   125  	if c.dynamicAffiliation.affiliation != "" {
   126  		resp, err := id.GetAffiliation(c.dynamicAffiliation.affiliation, c.clientCfg.CAName)
   127  		if err != nil {
   128  			return err
   129  		}
   130  
   131  		printTree(resp)
   132  		return nil
   133  	}
   134  
   135  	resp, err := id.GetAllAffiliations(c.clientCfg.CAName)
   136  	if err != nil {
   137  		return err
   138  	}
   139  
   140  	printTree(resp)
   141  	return nil
   142  }
   143  
   144  // The client side logic for adding an affiliation
   145  func (c *ClientCmd) runAddAffiliation(cmd *cobra.Command, args []string) error {
   146  	log.Debugf("Entered runAddAffiliation: %+v", c.dynamicAffiliation)
   147  
   148  	id, err := c.LoadMyIdentity()
   149  	if err != nil {
   150  		return err
   151  	}
   152  
   153  	req := &api.AddAffiliationRequest{}
   154  	req.Name = args[0]
   155  	req.CAName = c.clientCfg.CAName
   156  	req.Force = c.dynamicAffiliation.add.Force
   157  
   158  	resp, err := id.AddAffiliation(req)
   159  	if err != nil {
   160  		return err
   161  	}
   162  
   163  	fmt.Printf("Successfully added affiliation: %+v\n", resp.Name)
   164  
   165  	return nil
   166  }
   167  
   168  // The client side logic for modifying an affiliation
   169  func (c *ClientCmd) runModifyAffiliation(cmd *cobra.Command, args []string) error {
   170  	log.Debugf("Entered runModifyAffiliation: %+v", c.dynamicAffiliation)
   171  
   172  	id, err := c.LoadMyIdentity()
   173  	if err != nil {
   174  		return err
   175  	}
   176  
   177  	req := &api.ModifyAffiliationRequest{}
   178  	req.Name = args[0]
   179  	req.NewName = c.dynamicAffiliation.modify.NewName
   180  	req.CAName = c.clientCfg.CAName
   181  	req.Force = c.dynamicAffiliation.modify.Force
   182  
   183  	resp, err := id.ModifyAffiliation(req)
   184  	if err != nil {
   185  		return err
   186  	}
   187  
   188  	fmt.Printf("Successfully modified affiliation: %+v\n", resp)
   189  
   190  	return nil
   191  }
   192  
   193  // The client side logic for removing an affiliation
   194  func (c *ClientCmd) runRemoveAffiliation(cmd *cobra.Command, args []string) error {
   195  	log.Debugf("Entered runRemoveAffiliation: %+v", c.dynamicAffiliation)
   196  
   197  	id, err := c.LoadMyIdentity()
   198  	if err != nil {
   199  		return err
   200  	}
   201  
   202  	req := &api.RemoveAffiliationRequest{}
   203  	req.Name = args[0]
   204  	req.CAName = c.clientCfg.CAName
   205  	req.Force = c.dynamicAffiliation.remove.Force
   206  
   207  	resp, err := id.RemoveAffiliation(req)
   208  	if err != nil {
   209  		return err
   210  	}
   211  
   212  	fmt.Printf("Successfully removed affiliation: %+v\n", resp)
   213  
   214  	return nil
   215  }
   216  
   217  func (c *ClientCmd) affiliationPreRunE(cmd *cobra.Command, args []string) error {
   218  	err := argsCheck(args, "affiliation")
   219  	if err != nil {
   220  		return err
   221  	}
   222  
   223  	err = c.ConfigInit()
   224  	if err != nil {
   225  		return err
   226  	}
   227  
   228  	log.Debugf("Client configuration settings: %+v", c.clientCfg)
   229  
   230  	return nil
   231  }
   232  
   233  func printTree(resp *api.AffiliationResponse) {
   234  	root := resp.Name
   235  	if root == "" {
   236  		root = "."
   237  	}
   238  	fmt.Printf("affiliation: %s\n", root)
   239  	printChildren(resp.Affiliations, 1)
   240  }
   241  
   242  func printChildren(children []api.AffiliationInfo, level int) {
   243  	if len(children) == 0 {
   244  		return
   245  	}
   246  	for _, child := range children {
   247  		spaces := ""
   248  		for i := 0; i < level; i++ {
   249  			spaces = spaces + "   "
   250  		}
   251  		fmt.Printf("%saffiliation: %s\n", spaces, child.Name)
   252  		printChildren(child.Affiliations, level+1)
   253  	}
   254  }