Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples for the IaaS service #505

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
- **Feature:** New fields for `MachineType`: `Architecture`, `Gpu`
- `iaas`: [v0.1.0](services/iaas/CHANGELOG.md#v010-2024-05-10)
- **New BETA module**: manage Infrastructure as a Service (IaaS) resources `Network` and `NetworkArea`
- `iaas`: [v0.2.0](services/iaas/CHANGELOG.md#v010-2024-05-17)
- `iaas`: [v0.2.0](services/iaas/CHANGELOG.md#v020-2024-05-17)
- **Feature**: New methods to manage networks:
- `CreateNetwork`
- `PartialUpdateNetwork`
- `DeleteNetwork`
- **Breaking change**: Rename methods for better correspondence with endpoint behaviour (see service [release notes](services/iaas/CHANGELOG.md#v010-2024-05-17) for detailed changes)
- **Breaking change**: Rename types (see service [release notes](services/iaas/CHANGELOG.md#v010-2024-05-17) for detailed changes)
- **Breaking change**: Rename methods for better correspondence with endpoint behaviour (see service [release notes](services/iaas/CHANGELOG.md#v020-2024-05-17) for detailed changes)
- **Breaking change**: Rename types (see service [release notes](services/iaas/CHANGELOG.md#v020-2024-05-17) for detailed changes)
- Add `Response` suffix to types only used in method responses
- Remove `V1` prefix from all types
- `iaas`: [v0.3.0](services/iaas/CHANGELOG.md#v030-2024-05-17)
- **Feature**: Add waiters for async operations: `CreateNetworkAreaWaitHandler`, `UpdateNetworkAreaWaitHandler`, `DeleteNetworkAreaWaitHandler`, `CreateNetworkWaitHandler`, `UpdateNetworkWaitHandler`, `DeleteNetworkWaitHandler`
- `logme`: [v0.14.0](services/logme/CHANGELOG.md#v0140-2024-05-13)
- **Feature**: New method `GetMetrics` to get the latest metrics for cpu load, memory and disk usage for an instance
- **Feature**: New method `ListBackups` to list the backups for an instance
Expand Down
13 changes: 13 additions & 0 deletions examples/iaas/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/stackitcloud/stackit-sdk-go/examples/iaas

go 1.18

require (
github.com/stackitcloud/stackit-sdk-go/core v0.12.0
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.3.0
)

require (
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/uuid v1.6.0 // indirect
)
9 changes: 9 additions & 0 deletions examples/iaas/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/stackitcloud/stackit-sdk-go/core v0.12.0 h1:auIzUUNRuydKOScvpICP4MifGgvOajiDQd+ncGmBL0U=
github.com/stackitcloud/stackit-sdk-go/core v0.12.0/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw=
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.3.0 h1:0Nb7CruTyM/HxhZQjntQUHJqwCoKjFUC9KZcBBj+c5c=
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.3.0/go.mod h1:XtJA9FMK/yJ0dj4HtRAogmZPRUsZiFcuwUSfHYNASjo=
170 changes: 170 additions & 0 deletions examples/iaas/iaas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package main

import (
"context"
"fmt"
"net/http"
"os"

"github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/core/runtime"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
"github.com/stackitcloud/stackit-sdk-go/services/iaas/wait"
)

func main() {
// Specify the organization ID and project ID
organizationId := "ORGANIZATION_ID"
projectId := "PROJECT_ID"

// Create a new API client, that uses default authentication and configuration
iaasClient, err := iaas.NewAPIClient(
config.WithRegion("eu01"),
)
if err != nil {
fmt.Fprintf(os.Stderr, "[IaaS API] Creating API client: %v\n", err)
os.Exit(1)
}

// List the network areas for your organization
var areas *iaas.NetworkAreaListResponse //nolint:golint // transparency on data model naming
areas, err = iaasClient.ListNetworkAreas(context.Background(), organizationId).Execute()

if err != nil {
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `ListNetworkAreas`: %v\n", err)
} else {
fmt.Printf("[IaaS API] Number of network areas: %v\n", len(*areas.Items))
}

// Create a network area
createNetworkAreaPayload := iaas.CreateNetworkAreaPayload{
Name: utils.Ptr("example-network-area"),
AddressFamily: &iaas.CreateAreaAddressFamily{
Ipv4: &iaas.CreateAreaIPv4{
DefaultPrefixLen: utils.Ptr(int64(25)),
MaxPrefixLen: utils.Ptr(int64(29)),
MinPrefixLen: utils.Ptr(int64(24)),
NetworkRanges: &[]iaas.NetworkRange{
{
Prefix: utils.Ptr("1.2.3.4/24"),
},
},
TransferNetwork: utils.Ptr("1.2.4.5/24"),
},
},
}
area, err := iaasClient.CreateNetworkArea(context.Background(), organizationId).CreateNetworkAreaPayload(createNetworkAreaPayload).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `CreateNetworkAreas`: %v\n", err)
} else {
fmt.Printf("[IaaS API] Triggered creation of network area with ID %q.\n", *area.AreaId)
}

// Wait for creation of the network area
_, err = wait.CreateNetworkAreaWaitHandler(context.Background(), iaasClient, organizationId, *area.AreaId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for creation: %v\n", err)
os.Exit(1)
}

fmt.Printf("[IaaS API] Network area %q has been successfully created.\n", *area.AreaId)

// Update a network area
updateNetworkAreaPayload := iaas.PartialUpdateNetworkAreaPayload{
Name: utils.Ptr(*area.Name + "-renamed"),
}
updatedArea, err := iaasClient.PartialUpdateNetworkArea(context.Background(), organizationId, *area.AreaId).PartialUpdateNetworkAreaPayload(updateNetworkAreaPayload).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `PartialUpdateNetworkArea`: %v\n", err)
} else {
fmt.Printf("[IaaS API] Triggered update of network area with ID %q.\n", *updatedArea.AreaId)
}

// Wait for update of the network area
_, err = wait.UpdateNetworkAreaWaitHandler(context.Background(), iaasClient, organizationId, *updatedArea.AreaId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for update: %v\n", err)
os.Exit(1)
}

fmt.Printf("[IaaS API] Network area %q has been successfully updated.\n", *updatedArea.AreaId)

// Delete a network area
err = iaasClient.DeleteNetworkArea(context.Background(), organizationId, *updatedArea.AreaId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `DeleteNetworkArea`: %v\n", err)
} else {
fmt.Printf("[IaaS API] Triggered deletion of network area with ID %q.\n", *updatedArea.AreaId)
}

// Wait for deletion of the network area
_, err = wait.DeleteNetworkAreaWaitHandler(context.Background(), iaasClient, organizationId, *updatedArea.AreaId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for deletion: %v\n", err)
os.Exit(1)
}

fmt.Printf("[IaaS API] Network area %q has been successfully deleted.\n", *updatedArea.AreaId)

// Create a network
createNetworkPayload := iaas.CreateNetworkPayload{
Name: utils.Ptr("example-network"),
AddressFamily: &iaas.CreateNetworkAddressFamily{
Ipv4: &iaas.CreateNetworkIPv4{
PrefixLength: utils.Ptr(int64(24)),
Nameservers: &[]string{"1.2.3.4"},
},
},
}

var httpResp *http.Response
ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(context.Background(), &httpResp)
err = iaasClient.CreateNetwork(ctxWithHTTPResp, projectId).CreateNetworkPayload(createNetworkPayload).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `CreateNetwork`: %v\n", err)
os.Exit(1)
}

network, err := wait.CreateNetworkWaitHandler(context.Background(), iaasClient, projectId, httpResp.Header.Get("x-request-id")).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for creation: %v\n", err)
os.Exit(1)
}

fmt.Printf("[IaaS API] Network has been successfully created.\n")

// Update a network
updateNetworkPayload := iaas.PartialUpdateNetworkPayload{
Name: utils.Ptr("example-network-test-renamed"),
}

err = iaasClient.PartialUpdateNetwork(context.Background(), projectId, *network.NetworkId).PartialUpdateNetworkPayload(updateNetworkPayload).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `PartialUpdateNetwork`: %v\n", err)
os.Exit(1)
}

_, err = wait.UpdateNetworkWaitHandler(context.Background(), iaasClient, projectId, *network.NetworkId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for update: %v\n", err)
os.Exit(1)
}

fmt.Printf("[IaaS API] Network has been successfully updated.\n")

// Delete a network
err = iaasClient.DeleteNetwork(context.Background(), projectId, *network.NetworkId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `DeleteNetwork`: %v\n", err)
os.Exit(1)
}

_, err = wait.DeleteNetworkWaitHandler(context.Background(), iaasClient, projectId, *network.NetworkId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for deletion: %v\n", err)
os.Exit(1)
}

fmt.Printf("[IaaS API] Network has been successfully deleted.\n")
}
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use (
./examples/configuration
./examples/dns
./examples/errorhandling
./examples/iaas
./examples/loadbalancer
./examples/logme
./examples/mariadb
Expand Down
4 changes: 4 additions & 0 deletions services/iaas/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v0.3.0 (2024-05-17)

- **Feature**: Add waiters for async operations: `CreateNetworkAreaWaitHandler`, `UpdateNetworkAreaWaitHandler`, `DeleteNetworkAreaWaitHandler`, `CreateNetworkWaitHandler`, `UpdateNetworkWaitHandler`, `DeleteNetworkWaitHandler`

## v0.2.0 (2024-05-17)

- **Feature**: New methods to manage networks:
Expand Down
Loading