@@ -2,17 +2,18 @@ package create
22
33import (
44 "context"
5+ "encoding/json"
56 "fmt"
67 "strings"
78
9+ "github.com/goccy/go-yaml"
810 "github.com/spf13/cobra"
911 "github.com/stackitcloud/stackit-cli/internal/pkg/args"
1012 "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
1113 "github.com/stackitcloud/stackit-cli/internal/pkg/examples"
1214 "github.com/stackitcloud/stackit-cli/internal/pkg/flags"
1315 "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1416 "github.com/stackitcloud/stackit-cli/internal/pkg/print"
15- "github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
1617 "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
1718 "github.com/stackitcloud/stackit-sdk-go/services/iaas"
1819)
@@ -69,26 +70,34 @@ func executeCreate(cmd *cobra.Command, p *print.Printer, _ []string) error {
6970 return err
7071 }
7172
72- projectLabel , err := projectname .GetProjectName (ctx , p , cmd )
73- if err != nil {
74- p .Debug (print .ErrorLevel , "get project name: %v" , err )
75- projectLabel = model .ProjectId
73+ if ! model .AssumeYes {
74+ prompt := fmt .Sprintf ("Are you sure you want to create the security group %q?" , model .Name )
75+ err = p .PromptForConfirmation (prompt )
76+ if err != nil {
77+ return err
78+ }
7679 }
7780
7881 // Call API
79- req := buildRequest (ctx , model , apiClient )
80- _ , err = req .Execute ()
82+ request := buildRequest (ctx , model , apiClient )
83+ _ , err = request .Execute ()
8184 if err != nil {
8285 return fmt .Errorf ("create security group: %w" , err )
8386 }
8487
8588 operationState := "Enabled"
8689 if model .Async {
87- operationState = "Triggered enablement of "
90+ operationState = "Triggered label creation "
8891 }
89- p .Info ("%s security group %q for %q\n " , operationState , model .Name , projectLabel )
92+ p .Info ("%s security group %q for %q\n " , operationState , model .Name , model . ProjectId )
9093
91- panic ("todo: invocation not implemented!" )
94+ group , err := request .Execute ()
95+ if err != nil {
96+ return fmt .Errorf ("create security group: %w" , err )
97+ }
98+ if err := outputResult (p , model , group );err != nil {
99+ return err
100+ }
92101
93102 return nil
94103}
@@ -162,3 +171,31 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
162171 return request
163172
164173}
174+
175+ func outputResult (p * print.Printer , model * inputModel , resp * iaas.SecurityGroup ) error {
176+ switch model .OutputFormat {
177+ case print .JSONOutputFormat :
178+ details , err := json .MarshalIndent (resp , "" , " " )
179+ if err != nil {
180+ return fmt .Errorf ("marshal security group: %w" , err )
181+ }
182+ p .Outputln (string (details ))
183+
184+ return nil
185+ case print .YAMLOutputFormat :
186+ details , err := yaml .MarshalWithOptions (resp , yaml .IndentSequence (true ))
187+ if err != nil {
188+ return fmt .Errorf ("marshal security group: %w" , err )
189+ }
190+ p .Outputln (string (details ))
191+
192+ return nil
193+ default :
194+ operationState := "Created"
195+ if model .Async {
196+ operationState = "Triggered creation of"
197+ }
198+ p .Outputf ("%s security group %q\n " , operationState , model .Name )
199+ return nil
200+ }
201+ }
0 commit comments