github.com/vmware/govmomi@v0.51.0/cli/sso/user/update.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package user
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  
    11  	"github.com/vmware/govmomi/cli"
    12  	"github.com/vmware/govmomi/cli/sso"
    13  	"github.com/vmware/govmomi/ssoadmin"
    14  	"github.com/vmware/govmomi/ssoadmin/types"
    15  )
    16  
    17  type update struct {
    18  	userDetails
    19  }
    20  
    21  func init() {
    22  	cli.Register("sso.user.update", &update{})
    23  }
    24  
    25  func (cmd *update) Description() string {
    26  	return `Update SSO users.
    27  
    28  Examples:
    29    govc sso.user.update -C "$(cat cert.pem)" NAME
    30    govc sso.user.update -p password NAME`
    31  }
    32  
    33  // merge uses the current value of a field if the corresponding flag was not set.
    34  // Otherwise the API call would set the current fields to empty strings.
    35  func merge(src *string, current string) {
    36  	if *src == "" {
    37  		*src = current
    38  	}
    39  }
    40  
    41  func (cmd *update) Run(ctx context.Context, f *flag.FlagSet) error {
    42  	if f.NArg() != 1 {
    43  		return flag.ErrHelp
    44  	}
    45  	id := f.Arg(0)
    46  
    47  	return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error {
    48  		user, err := c.FindUser(ctx, id)
    49  		if err != nil {
    50  			return err
    51  		}
    52  
    53  		if user.Kind == "person" {
    54  			current, cerr := c.FindPersonUser(ctx, id)
    55  			if cerr != nil {
    56  				return cerr
    57  			}
    58  
    59  			merge(&cmd.AdminPersonDetails.Description, current.Details.Description)
    60  			merge(&cmd.FirstName, current.Details.FirstName)
    61  			merge(&cmd.LastName, current.Details.LastName)
    62  			merge(&cmd.EmailAddress, current.Details.EmailAddress)
    63  			if err := c.UpdatePersonUser(ctx, id, cmd.AdminPersonDetails); err != nil {
    64  				return err
    65  			}
    66  
    67  			if cmd.password != "" {
    68  				return c.ResetPersonPassword(ctx, id, cmd.password)
    69  			}
    70  
    71  			return nil
    72  		}
    73  
    74  		if cmd.actas != nil {
    75  			action := c.GrantWSTrustRole
    76  			if !*cmd.actas {
    77  				action = c.RevokeWSTrustRole
    78  			}
    79  			if _, err := action(ctx, user.Id, types.RoleActAsUser); err != nil {
    80  				return err
    81  			}
    82  		}
    83  
    84  		if cmd.role != "" {
    85  			if _, err := c.SetRole(ctx, user.Id, cmd.role); err != nil {
    86  				return err
    87  			}
    88  		}
    89  
    90  		cmd.solution.Certificate = cmd.Certificate()
    91  		cmd.solution.Description = cmd.AdminPersonDetails.Description
    92  
    93  		current, cerr := c.FindSolutionUser(ctx, id)
    94  		if cerr != nil {
    95  			return cerr
    96  		}
    97  
    98  		merge(&cmd.solution.Certificate, current.Details.Certificate)
    99  		merge(&cmd.solution.Description, current.Details.Description)
   100  		return c.UpdateSolutionUser(ctx, id, cmd.solution)
   101  	})
   102  }