Skip to content

Commit be5f51a

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

File tree

8 files changed

+428
-0
lines changed

8 files changed

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

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)