66
77 "github.com/spf13/cobra"
88 iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2alpha1api"
9+ "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2alpha1api/wait"
910
1011 "github.com/stackitcloud/stackit-cli/internal/pkg/args"
1112 "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -15,6 +16,7 @@ import (
1516 "github.com/stackitcloud/stackit-cli/internal/pkg/print"
1617 "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaasalpha/client"
1718 iaasUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaasalpha/utils"
19+ "github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
1820 "github.com/stackitcloud/stackit-cli/internal/pkg/types"
1921)
2022
@@ -92,7 +94,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
9294 }
9395
9496 // Call API
95- req , err := buildRequest (ctx , model , apiClient )
97+ req , err := buildRequest (ctx , model , apiClient . DefaultAPI )
9698 if err != nil {
9799 return err
98100 }
@@ -101,7 +103,28 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
101103 return fmt .Errorf ("create network range: %w" , err )
102104 }
103105
104- return outputResult (params .Printer , model .OutputFormat , vpcLabel , resp )
106+ if ! model .Async {
107+ var networkRangeId string
108+ if ipv4 := resp .VPCNetworkRangeIPv4 ; ipv4 != nil && ipv4 .Id != nil {
109+ networkRangeId = * ipv4 .Id
110+ } else if ipv6 := resp .VPCNetworkRangeIPv6 ; ipv6 != nil && ipv6 .Id != nil {
111+ networkRangeId = * ipv6 .Id
112+ } else {
113+ return fmt .Errorf ("network range ID is missing in API response" )
114+ }
115+
116+ err = spinner .Run (params .Printer , "Creating VPC Network range" , func () error {
117+ resp , err = wait .CreateVPCNetworkRangeWaitHandler (ctx , apiClient .DefaultAPI , model .ProjectId , model .VpcId , model .Region , networkRangeId ).WaitWithContext (ctx )
118+ return err
119+ })
120+ if err != nil {
121+ if err != nil {
122+ return fmt .Errorf ("wait for network range %q creation: %w" , networkRangeId , err )
123+ }
124+ }
125+ }
126+
127+ return outputResult (params .Printer , model .OutputFormat , model .Async , vpcLabel , resp )
105128 },
106129 }
107130 configureFlags (cmd )
@@ -146,7 +169,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
146169 return & model , nil
147170}
148171
149- func buildRequest (ctx context.Context , model * inputModel , apiClient * iaas.APIClient ) (iaas.ApiCreateVPCNetworkRangeRequest , error ) {
172+ func buildRequest (ctx context.Context , model * inputModel , apiClient iaas.DefaultAPI ) (iaas.ApiCreateVPCNetworkRangeRequest , error ) {
150173 if model .IpVersion != "ipv4" {
151174 return iaas.ApiCreateVPCNetworkRangeRequest {}, fmt .Errorf ("unsupported IP version: %q" , model .IpVersion )
152175 }
@@ -163,10 +186,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
163186 Labels : model .Labels ,
164187 },
165188 }
166- return apiClient .DefaultAPI . CreateVPCNetworkRange (ctx , model .ProjectId , model .VpcId , model .Region ).CreateVPCNetworkRangePayload (payload ), nil
189+ return apiClient .CreateVPCNetworkRange (ctx , model .ProjectId , model .VpcId , model .Region ).CreateVPCNetworkRangePayload (payload ), nil
167190}
168191
169- func outputResult (p * print.Printer , outputFormat , vpcLabel string , networkRange * iaas.VPCNetworkRange ) error {
192+ func outputResult (p * print.Printer , outputFormat string , async bool , vpcLabel string , networkRange * iaas.VPCNetworkRange ) error {
170193 if networkRange == nil {
171194 return fmt .Errorf ("create vpc network range response is nil" )
172195 }
@@ -180,7 +203,12 @@ func outputResult(p *print.Printer, outputFormat, vpcLabel string, networkRange
180203 return fmt .Errorf ("network range ID is missing in API response" )
181204 }
182205
183- p .Outputf ("Created network range for VPC %q.\n Network range ID: %s\n " , vpcLabel , networkRangeId )
206+ operationState := "Created"
207+ if async {
208+ operationState = "Triggered creation of"
209+ }
210+
211+ p .Outputf ("%s network range for VPC %q.\n Network range ID: %s\n " , operationState , vpcLabel , networkRangeId )
184212 return nil
185213 })
186214}
0 commit comments