Skip to content

Commit 494a239

Browse files
committed
Add network area waiters and examples
1 parent d28ef6a commit 494a239

File tree

8 files changed

+429
-0
lines changed

8 files changed

+429
-0
lines changed

examples/iaas/go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/stackitcloud/stackit-sdk-go/examples/iaas
2+
3+
go 1.18
4+
5+
require (
6+
github.com/stackitcloud/stackit-sdk-go/core v0.12.0
7+
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.1.0
8+
)
9+
10+
require (
11+
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
12+
github.com/google/uuid v1.6.0 // indirect
13+
)

examples/iaas/go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
2+
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
3+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
4+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
5+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
6+
github.com/stackitcloud/stackit-sdk-go/core v0.12.0 h1:auIzUUNRuydKOScvpICP4MifGgvOajiDQd+ncGmBL0U=
7+
github.com/stackitcloud/stackit-sdk-go/core v0.12.0/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw=
8+
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.1.0 h1:AHOTa8N0ubwiLMneqGAM16oSus19ZEDr+yneThGgKtI=
9+
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.1.0/go.mod h1:4HJj/6h0SeuM+28L4bGhcAo+Q3aWqrWwnkOgoDF+7so=

examples/iaas/iaas.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/stackitcloud/stackit-sdk-go/core/config"
9+
"github.com/stackitcloud/stackit-sdk-go/core/utils"
10+
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
11+
"github.com/stackitcloud/stackit-sdk-go/services/iaas/wait"
12+
)
13+
14+
func main() {
15+
// Specify the organization ID and project ID
16+
organizationId := "d83100e9-9be4-45ad-b874-28166786ec0c"
17+
// organizationId := "ORGANIZATION_ID"
18+
// projectId := "PROJECT_ID"
19+
20+
// Create a new API client, that uses default authentication and configuration
21+
iaasClient, err := iaas.NewAPIClient(
22+
config.WithRegion("eu01"),
23+
)
24+
if err != nil {
25+
fmt.Fprintf(os.Stderr, "[IaaS API] Creating API client: %v\n", err)
26+
os.Exit(1)
27+
}
28+
29+
// List the network areas for your organization
30+
var areas *iaas.NetworkAreaList //nolint:golint // transparency on data model naming
31+
areas, err = iaasClient.ListNetworkAreas(context.Background(), organizationId).Execute()
32+
33+
if err != nil {
34+
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `ListNetworkAreas`: %v\n", err)
35+
} else {
36+
fmt.Printf("[IaaS API] Number of network areas: %v\n", len(*areas.Items))
37+
}
38+
39+
// Create a network area
40+
createNetworkAreaPayload := iaas.CreateNetworkAreasPayload{
41+
Name: utils.Ptr("my-network-area"),
42+
AddressFamily: &iaas.V1CreateAreaAddressFamily{
43+
Ipv4: &iaas.NetworkArea{
44+
DefaultPrefixLen: utils.Ptr(int64(25)),
45+
MaxPrefixLen: utils.Ptr(int64(29)),
46+
MinPrefixLen: utils.Ptr(int64(24)),
47+
NetworkRanges: &[]iaas.NetworkRange{
48+
{
49+
Prefix: utils.Ptr("192.147.0.0/24"),
50+
},
51+
},
52+
TransferNetwork: utils.Ptr("192.148.0.0/24"),
53+
},
54+
},
55+
}
56+
area, err := iaasClient.CreateNetworkAreas(context.Background(), organizationId).CreateNetworkAreasPayload(createNetworkAreaPayload).Execute()
57+
if err != nil {
58+
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `CreateNetworkAreas`: %v\n", err)
59+
} else {
60+
fmt.Printf("[IaaS API] Triggered creation of network area with ID %q.\n", *area.AreaId)
61+
}
62+
63+
// Wait for creation of the network area
64+
_, err = wait.CreateNetworkAreaWaitHandler(context.Background(), iaasClient, organizationId, *area.AreaId).WaitWithContext(context.Background())
65+
if err != nil {
66+
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for creation: %v\n", err)
67+
os.Exit(1)
68+
}
69+
70+
fmt.Printf("[IaaS API] Network area %q has been successfully created.\n", *area.AreaId)
71+
72+
// Update a network area
73+
updateNetworkAreaPayload := iaas.UpdateNetworkAreaPayload{
74+
Name: utils.Ptr(*area.Name + "-renamed"),
75+
}
76+
updatedArea, err := iaasClient.UpdateNetworkArea(context.Background(), organizationId, *area.AreaId).UpdateNetworkAreaPayload(updateNetworkAreaPayload).Execute()
77+
if err != nil {
78+
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `UpdateNetworkArea`: %v\n", err)
79+
} else {
80+
fmt.Printf("[IaaS API] Triggered update of network area with ID %q.\n", *updatedArea.AreaId)
81+
}
82+
83+
// Wait for update of the network area
84+
_, err = wait.UpdateNetworkAreaWaitHandler(context.Background(), iaasClient, organizationId, *updatedArea.AreaId).WaitWithContext(context.Background())
85+
if err != nil {
86+
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for update: %v\n", err)
87+
os.Exit(1)
88+
}
89+
90+
fmt.Printf("[IaaS API] Network area %q has been successfully updated.\n", *updatedArea.AreaId)
91+
92+
// Delete a network area
93+
err = iaasClient.DeleteNetworkAreas(context.Background(), organizationId, *updatedArea.AreaId).Execute()
94+
if err != nil {
95+
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `DeleteNetworkArea`: %v\n", err)
96+
} else {
97+
fmt.Printf("[IaaS API] Triggered deletion of network area with ID %q.\n", *updatedArea.AreaId)
98+
}
99+
100+
// Wait for deletion of the network area
101+
_, err = wait.DeleteNetworkAreaWaitHandler(context.Background(), iaasClient, organizationId, *updatedArea.AreaId).WaitWithContext(context.Background())
102+
if err != nil {
103+
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for deletion: %v\n", err)
104+
os.Exit(1)
105+
}
106+
107+
fmt.Printf("[IaaS API] Network area %q has been successfully deleted.\n", *updatedArea.AreaId)
108+
}

go.work

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use (
99
./examples/configuration
1010
./examples/dns
1111
./examples/errorhandling
12+
./examples/iaas
1213
./examples/loadbalancer
1314
./examples/logme
1415
./examples/mariadb

services/iaas/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ require github.com/stackitcloud/stackit-sdk-go/core v0.12.0
66

77
require (
88
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
9+
github.com/google/go-cmp v0.6.0
910
github.com/google/uuid v1.6.0 // indirect
1011
)

services/iaas/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
22
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
33
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
4+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
45
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
56
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
67
github.com/stackitcloud/stackit-sdk-go/core v0.12.0 h1:auIzUUNRuydKOScvpICP4MifGgvOajiDQd+ncGmBL0U=

services/iaas/wait/wait.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package wait
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"time"
8+
9+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
10+
"github.com/stackitcloud/stackit-sdk-go/core/wait"
11+
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
12+
)
13+
14+
const (
15+
CreateSuccess = "CREATED"
16+
// The UPDATED state also seems to not exist, and updates seem to be syncronous
17+
// TODO: Confirm with the team
18+
UpdateSuccess = "UPDATED"
19+
)
20+
21+
// Interfaces needed for tests
22+
type APIClientInterface interface {
23+
GetNetworkAreaExecute(ctx context.Context, organizationId, areaId string) (*iaas.V1NetworkArea, error)
24+
// GetNetworkExecute(ctx context.Context, projectId, networkId string) (*iaas.Network, error)
25+
}
26+
27+
// CreateNetworkAreaWaitHandler will wait for network area creation
28+
func CreateNetworkAreaWaitHandler(ctx context.Context, a APIClientInterface, organizationId, areaId string) *wait.AsyncActionHandler[iaas.V1NetworkArea] {
29+
handler := wait.New(func() (waitFinished bool, response *iaas.V1NetworkArea, err error) {
30+
area, err := a.GetNetworkAreaExecute(ctx, organizationId, areaId)
31+
if err != nil {
32+
return false, area, err
33+
}
34+
if area.AreaId == nil || area.State == nil {
35+
return false, area, fmt.Errorf("create failed for netwrok area with id %s, the response is not valid: the id or the state are missing", areaId)
36+
}
37+
if *area.AreaId == areaId && *area.State == CreateSuccess {
38+
return true, area, nil
39+
}
40+
return false, area, nil
41+
})
42+
handler.SetTimeout(10 * time.Minute)
43+
return handler
44+
}
45+
46+
// UpdateNetworkAreaWaitHandler will wait for network area update
47+
func UpdateNetworkAreaWaitHandler(ctx context.Context, a APIClientInterface, organizationId, areaId string) *wait.AsyncActionHandler[iaas.V1NetworkArea] {
48+
handler := wait.New(func() (waitFinished bool, response *iaas.V1NetworkArea, err error) {
49+
area, err := a.GetNetworkAreaExecute(ctx, organizationId, areaId)
50+
if err != nil {
51+
return false, area, err
52+
}
53+
if area.AreaId == nil || area.State == nil {
54+
return false, nil, fmt.Errorf("update failed for network area with id %s, the response is not valid: the id or the state are missing", areaId)
55+
}
56+
if *area.AreaId == areaId && *area.State == UpdateSuccess {
57+
return true, area, nil
58+
}
59+
return false, area, nil
60+
})
61+
handler.SetTimeout(10 * time.Minute)
62+
return handler
63+
}
64+
65+
// DeleteNetworkAreaWaitHandler will wait for network area deletion
66+
func DeleteNetworkAreaWaitHandler(ctx context.Context, a APIClientInterface, organizationId, areaId string) *wait.AsyncActionHandler[iaas.V1NetworkArea] {
67+
handler := wait.New(func() (waitFinished bool, response *iaas.V1NetworkArea, err error) {
68+
area, err := a.GetNetworkAreaExecute(ctx, organizationId, areaId)
69+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
70+
if !ok {
71+
return false, area, fmt.Errorf("could not convert error to oapierror.GenericOpenAPIError")
72+
}
73+
if oapiErr.StatusCode != http.StatusNotFound {
74+
return false, area, err
75+
}
76+
return true, nil, nil
77+
})
78+
handler.SetTimeout(10 * time.Minute)
79+
return handler
80+
}

0 commit comments

Comments
 (0)