@@ -24,7 +24,8 @@ import (
2424const (
2525 instanceIdArg = "INSTANCE_ID"
2626
27- aclFlag = "acl"
27+ instanceNameFlag = "name"
28+ aclFlag = "acl"
2829
2930 kmsKeyIdFlag = "kms-key-id"
3031 kmsKeyringIdFlag = "kms-keyring-id"
@@ -36,7 +37,8 @@ type inputModel struct {
3637 * globalflags.GlobalFlagModel
3738 InstanceId string
3839
39- Acls * []string
40+ InstanceName * string
41+ Acls * []string
4042
4143 KmsKeyId * string
4244 KmsKeyringId * string
@@ -51,12 +53,18 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
5153 Long : "Updates a Secrets Manager instance." ,
5254 Args : args .SingleArg (instanceIdArg , utils .ValidateUUID ),
5355 Example : examples .Build (
56+ examples .NewExample (
57+ `Update the name of a Secrets Manager instance with ID "xxx"` ,
58+ "$ stackit secrets-manager instance update xxx --name my-new-name" ),
5459 examples .NewExample (
5560 `Update the range of IPs allowed to access a Secrets Manager instance with ID "xxx"` ,
5661 "$ stackit secrets-manager instance update xxx --acl 1.2.3.0/24" ),
62+ examples .NewExample (
63+ `Update the name and ACLs of a Secrets Manager instance with ID "xxx"` ,
64+ "$ stackit secrets-manager instance update xxx --name my-new-name --acl 1.2.3.0/24" ),
5765 examples .NewExample (
5866 `Update the KMS key settings of a Secrets Manager instance with ID "xxx"` ,
59- "$ stackit secrets-manager instance update xxx --kms-key-id key-id --kms-keyring-id keyring-id --kms-key-version 1 --kms-service-account-email my-service-account-1234567@sa.stackit.cloud" ),
67+ "$ stackit secrets-manager instance update xxx --name my-instance -- kms-key-id key-id --kms-keyring-id keyring-id --kms-key-version 1 --kms-service-account-email my-service-account-1234567@sa.stackit.cloud" ),
6068 ),
6169 RunE : func (cmd * cobra.Command , args []string ) error {
6270 ctx := context .Background ()
@@ -71,36 +79,42 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7179 return err
7280 }
7381
74- instanceLabel , err := secretsManagerUtils .GetInstanceName (ctx , apiClient , model .ProjectId , model .InstanceId )
82+ existingInstanceName , err := secretsManagerUtils .GetInstanceName (ctx , apiClient , model .ProjectId , model .InstanceId )
7583 if err != nil {
7684 params .Printer .Debug (print .ErrorLevel , "get instance name: %v" , err )
77- instanceLabel = model .InstanceId
78- if model .KmsKeyId != nil {
79- return fmt .Errorf ("get instance name: %w" , err )
80- }
85+ existingInstanceName = model .InstanceId
8186 }
8287
83- prompt := fmt .Sprintf ("Are you sure you want to update instance %q?" , instanceLabel )
88+ prompt := fmt .Sprintf ("Are you sure you want to update instance %q?" , existingInstanceName )
8489 err = params .Printer .PromptForConfirmation (prompt )
8590 if err != nil {
8691 return err
8792 }
8893
89- // Call API
90- req := buildRequest (ctx , model , instanceLabel , apiClient )
91- switch request := req .(type ) {
92- case secretsmanager.ApiUpdateInstanceRequest :
93- err = request .Execute ()
94- case secretsmanager.ApiUpdateACLsRequest :
95- err = request .Execute ()
96- default :
97- err = fmt .Errorf ("unknown request type" )
94+ // Call API - execute UpdateInstance and/or UpdateACLs based on flags
95+ if model .InstanceName != nil {
96+ req := buildUpdateInstanceRequest (ctx , model , apiClient )
97+ err = req .Execute ()
98+ if err != nil {
99+ return fmt .Errorf ("update Secrets Manager instance: %w" , err )
100+ }
98101 }
99- if err != nil {
100- return fmt .Errorf ("update Secrets Manager instance: %w" , err )
102+
103+ if model .Acls != nil {
104+ req := buildUpdateACLsRequest (ctx , model , apiClient )
105+ err = req .Execute ()
106+ if err != nil {
107+ if model .InstanceName != nil {
108+ return fmt .Errorf (`the Secrets Manager instance was successfully updated, but the configuration of the ACLs failed.
109+
110+ If you want to retry configuring the ACLs, you can do it via:
111+ $ stackit secrets-manager instance update %s --acl %s` , model .InstanceId , * model .Acls )
112+ }
113+ return fmt .Errorf ("update Secrets Manager instance ACLs: %w" , err )
114+ }
101115 }
102116
103- params .Printer .Info ("Updated instance %q\n " , instanceLabel )
117+ params .Printer .Info ("Updated instance %q\n " , existingInstanceName )
104118 return nil
105119 },
106120 }
@@ -109,6 +123,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
109123}
110124
111125func configureFlags (cmd * cobra.Command ) {
126+ cmd .Flags ().StringP (instanceNameFlag , "n" , "" , "Instance name" )
112127 cmd .Flags ().Var (flags .CIDRSliceFlag (), aclFlag , "List of IP networks in CIDR notation which are allowed to access this instance" )
113128
114129 cmd .Flags ().String (kmsKeyIdFlag , "" , "ID of the KMS key to use for encryption" )
@@ -117,8 +132,7 @@ func configureFlags(cmd *cobra.Command) {
117132 cmd .Flags ().String (kmsServiceAccountEmailFlag , "" , "Service account email for KMS access" )
118133
119134 cmd .MarkFlagsRequiredTogether (kmsKeyIdFlag , kmsKeyringIdFlag , kmsKeyVersionFlag , kmsServiceAccountEmailFlag )
120- cmd .MarkFlagsMutuallyExclusive (aclFlag , kmsKeyIdFlag )
121- cmd .MarkFlagsOneRequired (aclFlag , kmsKeyIdFlag )
135+ cmd .MarkFlagsOneRequired (aclFlag , instanceNameFlag )
122136}
123137
124138func parseInput (p * print.Printer , cmd * cobra.Command , inputArgs []string ) (* inputModel , error ) {
@@ -129,41 +143,39 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
129143 return nil , & cliErr.ProjectIdError {}
130144 }
131145
132- acls := flags .FlagToStringSlicePointer (p , cmd , aclFlag )
133-
134146 model := inputModel {
135147 GlobalFlagModel : globalFlags ,
136148 InstanceId : instanceId ,
137- Acls : acls ,
149+ InstanceName : flags .FlagToStringPointer (p , cmd , instanceNameFlag ),
150+ Acls : flags .FlagToStringSlicePointer (p , cmd , aclFlag ),
138151 KmsKeyId : flags .FlagToStringPointer (p , cmd , kmsKeyIdFlag ),
139152 KmsKeyringId : flags .FlagToStringPointer (p , cmd , kmsKeyringIdFlag ),
140153 KmsKeyVersion : flags .FlagToInt64Pointer (p , cmd , kmsKeyVersionFlag ),
141154 KmsServiceAccountEmail : flags .FlagToStringPointer (p , cmd , kmsServiceAccountEmailFlag ),
142155 }
143156
144- p .DebugInputModel (model )
145- return & model , nil
146- }
147-
148- func buildRequest (ctx context.Context , model * inputModel , instanceName string , apiClient * secretsmanager.APIClient ) interface { Execute () error } {
149- if model .KmsKeyId != nil {
150- return buildUpdateInstanceRequest (ctx , model , instanceName , apiClient )
157+ if model .KmsKeyId != nil && model .InstanceName == nil {
158+ return nil , fmt .Errorf ("--name is required when using KMS flags" )
151159 }
152160
153- return buildUpdateACLsRequest (ctx , model , apiClient )
161+ p .DebugInputModel (model )
162+ return & model , nil
154163}
155164
156- func buildUpdateInstanceRequest (ctx context.Context , model * inputModel , instanceName string , apiClient * secretsmanager.APIClient ) secretsmanager.ApiUpdateInstanceRequest {
165+ func buildUpdateInstanceRequest (ctx context.Context , model * inputModel , apiClient * secretsmanager.APIClient ) secretsmanager.ApiUpdateInstanceRequest {
157166 req := apiClient .UpdateInstance (ctx , model .ProjectId , model .InstanceId )
158167
159168 payload := secretsmanager.UpdateInstancePayload {
160- Name : & instanceName ,
161- KmsKey : & secretsmanager.KmsKeyPayload {
169+ Name : model .InstanceName ,
170+ }
171+
172+ if model .KmsKeyId != nil {
173+ payload .KmsKey = & secretsmanager.KmsKeyPayload {
162174 KeyId : model .KmsKeyId ,
163175 KeyRingId : model .KmsKeyringId ,
164176 KeyVersion : model .KmsKeyVersion ,
165177 ServiceAccountEmail : model .KmsServiceAccountEmail ,
166- },
178+ }
167179 }
168180
169181 req = req .UpdateInstancePayload (payload )
0 commit comments