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