github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/kinesis/update.go (about)

     1  package kinesis
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/fastly/go-fastly/v9/fastly"
     7  
     8  	"github.com/fastly/cli/pkg/argparser"
     9  	"github.com/fastly/cli/pkg/commands/logging/common"
    10  	"github.com/fastly/cli/pkg/errors"
    11  	"github.com/fastly/cli/pkg/global"
    12  	"github.com/fastly/cli/pkg/manifest"
    13  	"github.com/fastly/cli/pkg/text"
    14  )
    15  
    16  // UpdateCommand calls the Fastly API to update an Amazon Kinesis logging endpoint.
    17  type UpdateCommand struct {
    18  	argparser.Base
    19  	Manifest manifest.Data
    20  
    21  	// Required.
    22  	EndpointName   string // Can't shadow argparser.Base method Name().
    23  	ServiceName    argparser.OptionalServiceNameID
    24  	ServiceVersion argparser.OptionalServiceVersion
    25  
    26  	// Optional.
    27  	AutoClone         argparser.OptionalAutoClone
    28  	NewName           argparser.OptionalString
    29  	StreamName        argparser.OptionalString
    30  	AccessKey         argparser.OptionalString
    31  	SecretKey         argparser.OptionalString
    32  	IAMRole           argparser.OptionalString
    33  	Region            argparser.OptionalString
    34  	Format            argparser.OptionalString
    35  	FormatVersion     argparser.OptionalInt
    36  	ResponseCondition argparser.OptionalString
    37  	Placement         argparser.OptionalString
    38  }
    39  
    40  // NewUpdateCommand returns a usable command registered under the parent.
    41  func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand {
    42  	c := UpdateCommand{
    43  		Base: argparser.Base{
    44  			Globals: g,
    45  		},
    46  	}
    47  	c.CmdClause = parent.Command("update", "Update a Kinesis logging endpoint on a Fastly service version")
    48  
    49  	// Required.
    50  	c.CmdClause.Flag("name", "The name of the Kinesis logging object").Short('n').Required().StringVar(&c.EndpointName)
    51  	c.RegisterFlag(argparser.StringFlagOpts{
    52  		Name:        argparser.FlagVersionName,
    53  		Description: argparser.FlagVersionDesc,
    54  		Dst:         &c.ServiceVersion.Value,
    55  		Required:    true,
    56  	})
    57  
    58  	// Optional.
    59  	c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{
    60  		Action: c.AutoClone.Set,
    61  		Dst:    &c.AutoClone.Value,
    62  	})
    63  	c.CmdClause.Flag("access-key", "Your Kinesis account access key").Action(c.AccessKey.Set).StringVar(&c.AccessKey.Value)
    64  	common.Format(c.CmdClause, &c.Format)
    65  	common.FormatVersion(c.CmdClause, &c.FormatVersion)
    66  	c.CmdClause.Flag("iam-role", "The IAM role ARN for logging").Action(c.IAMRole.Set).StringVar(&c.IAMRole.Value)
    67  	c.CmdClause.Flag("new-name", "New name of the Kinesis logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value)
    68  	common.Placement(c.CmdClause, &c.Placement)
    69  	c.CmdClause.Flag("region", "The AWS region where the Kinesis stream exists").Action(c.Region.Set).StringVar(&c.Region.Value)
    70  	common.ResponseCondition(c.CmdClause, &c.ResponseCondition)
    71  	c.CmdClause.Flag("secret-key", "Your Kinesis account secret key").Action(c.SecretKey.Set).StringVar(&c.SecretKey.Value)
    72  	c.RegisterFlag(argparser.StringFlagOpts{
    73  		Name:        argparser.FlagServiceIDName,
    74  		Description: argparser.FlagServiceIDDesc,
    75  		Dst:         &g.Manifest.Flag.ServiceID,
    76  		Short:       's',
    77  	})
    78  	c.RegisterFlag(argparser.StringFlagOpts{
    79  		Action:      c.ServiceName.Set,
    80  		Name:        argparser.FlagServiceName,
    81  		Description: argparser.FlagServiceDesc,
    82  		Dst:         &c.ServiceName.Value,
    83  	})
    84  	c.CmdClause.Flag("stream-name", "Your Kinesis stream name").Action(c.StreamName.Set).StringVar(&c.StreamName.Value)
    85  	return &c
    86  }
    87  
    88  // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library.
    89  func (c *UpdateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.UpdateKinesisInput, error) {
    90  	input := fastly.UpdateKinesisInput{
    91  		ServiceID:      serviceID,
    92  		ServiceVersion: serviceVersion,
    93  		Name:           c.EndpointName,
    94  	}
    95  
    96  	if c.NewName.WasSet {
    97  		input.NewName = &c.NewName.Value
    98  	}
    99  
   100  	if c.StreamName.WasSet {
   101  		input.StreamName = &c.StreamName.Value
   102  	}
   103  
   104  	if c.AccessKey.WasSet {
   105  		input.AccessKey = &c.AccessKey.Value
   106  	}
   107  
   108  	if c.SecretKey.WasSet {
   109  		input.SecretKey = &c.SecretKey.Value
   110  	}
   111  
   112  	if c.IAMRole.WasSet {
   113  		input.IAMRole = &c.IAMRole.Value
   114  	}
   115  
   116  	if c.Region.WasSet {
   117  		input.Region = &c.Region.Value
   118  	}
   119  
   120  	if c.Format.WasSet {
   121  		input.Format = &c.Format.Value
   122  	}
   123  
   124  	if c.FormatVersion.WasSet {
   125  		input.FormatVersion = &c.FormatVersion.Value
   126  	}
   127  
   128  	if c.ResponseCondition.WasSet {
   129  		input.ResponseCondition = &c.ResponseCondition.Value
   130  	}
   131  
   132  	if c.Placement.WasSet {
   133  		input.Placement = &c.Placement.Value
   134  	}
   135  
   136  	return &input, nil
   137  }
   138  
   139  // Exec invokes the application logic for the command.
   140  func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
   141  	serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{
   142  		AutoCloneFlag:      c.AutoClone,
   143  		APIClient:          c.Globals.APIClient,
   144  		Manifest:           *c.Globals.Manifest,
   145  		Out:                out,
   146  		ServiceNameFlag:    c.ServiceName,
   147  		ServiceVersionFlag: c.ServiceVersion,
   148  		VerboseMode:        c.Globals.Flags.Verbose,
   149  	})
   150  	if err != nil {
   151  		c.Globals.ErrLog.AddWithContext(err, map[string]any{
   152  			"Service ID":      serviceID,
   153  			"Service Version": errors.ServiceVersion(serviceVersion),
   154  		})
   155  		return err
   156  	}
   157  
   158  	input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number))
   159  	if err != nil {
   160  		c.Globals.ErrLog.Add(err)
   161  		return err
   162  	}
   163  
   164  	kinesis, err := c.Globals.APIClient.UpdateKinesis(input)
   165  	if err != nil {
   166  		c.Globals.ErrLog.Add(err)
   167  		return err
   168  	}
   169  
   170  	text.Success(out,
   171  		"Updated Kinesis logging endpoint %s (service %s version %d)",
   172  		fastly.ToValue(kinesis.Name),
   173  		fastly.ToValue(kinesis.ServiceID),
   174  		fastly.ToValue(kinesis.ServiceVersion),
   175  	)
   176  	return nil
   177  }