Skip to content

Commit 70757c5

Browse files
committed
feat: Move network and network area examples to own file
1 parent f0d01eb commit 70757c5

File tree

2 files changed

+89
-64
lines changed

2 files changed

+89
-64
lines changed

examples/iaas/network/network.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
projectId := "PROJECT_ID"
17+
18+
// Create a new API client, that uses default authentication and configuration
19+
iaasClient, err := iaas.NewAPIClient(
20+
config.WithRegion("eu01"),
21+
)
22+
if err != nil {
23+
fmt.Fprintf(os.Stderr, "[IaaS API] Creating API client: %v\n", err)
24+
os.Exit(1)
25+
}
26+
27+
// Create a network
28+
createNetworkPayload := iaas.CreateNetworkPayload{
29+
Name: utils.Ptr("example-network"),
30+
AddressFamily: &iaas.CreateNetworkAddressFamily{
31+
Ipv4: &iaas.CreateNetworkIPv4Body{
32+
PrefixLength: utils.Ptr(int64(24)),
33+
Nameservers: &[]string{"1.2.3.4"},
34+
},
35+
},
36+
}
37+
38+
network, err := iaasClient.CreateNetwork(context.Background(), projectId).CreateNetworkPayload(createNetworkPayload).Execute()
39+
if err != nil {
40+
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `CreateNetwork`: %v\n", err)
41+
os.Exit(1)
42+
}
43+
44+
fmt.Printf("[IaaS API] Triggered creation of network with ID %q.\n", *network.NetworkId)
45+
fmt.Printf("[Iaas API] Current state of the network: %q\n", *network.State)
46+
fmt.Println("[Iaas API] Waiting for network to be created...")
47+
48+
network, err = wait.CreateNetworkWaitHandler(context.Background(), iaasClient, projectId, *network.NetworkId).WaitWithContext(context.Background())
49+
if err != nil {
50+
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for creation: %v\n", err)
51+
os.Exit(1)
52+
}
53+
54+
fmt.Printf("[IaaS API] Network has been successfully created.\n")
55+
56+
// Update a network
57+
updateNetworkPayload := iaas.PartialUpdateNetworkPayload{
58+
Name: utils.Ptr("example-network-test-renamed"),
59+
}
60+
61+
err = iaasClient.PartialUpdateNetwork(context.Background(), projectId, *network.NetworkId).PartialUpdateNetworkPayload(updateNetworkPayload).Execute()
62+
if err != nil {
63+
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `PartialUpdateNetwork`: %v\n", err)
64+
os.Exit(1)
65+
}
66+
67+
_, err = wait.UpdateNetworkWaitHandler(context.Background(), iaasClient, projectId, *network.NetworkId).WaitWithContext(context.Background())
68+
if err != nil {
69+
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for update: %v\n", err)
70+
os.Exit(1)
71+
}
72+
73+
fmt.Printf("[IaaS API] Network has been successfully updated.\n")
74+
75+
// Delete a network
76+
err = iaasClient.DeleteNetwork(context.Background(), projectId, *network.NetworkId).Execute()
77+
if err != nil {
78+
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `DeleteNetwork`: %v\n", err)
79+
os.Exit(1)
80+
}
81+
82+
_, err = wait.DeleteNetworkWaitHandler(context.Background(), iaasClient, projectId, *network.NetworkId).WaitWithContext(context.Background())
83+
if err != nil {
84+
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for deletion: %v\n", err)
85+
os.Exit(1)
86+
}
87+
88+
fmt.Printf("[IaaS API] Network has been successfully deleted.\n")
89+
}

examples/iaas/iaas.go renamed to examples/iaas/network_area/network_area.go

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
func main() {
1515
// Specify the organization ID and project ID
1616
organizationId := "ORGANIZATION_ID"
17-
projectId := "PROJECT_ID"
1817

1918
// Create a new API client, that uses default authentication and configuration
2019
iaasClient, err := iaas.NewAPIClient(
@@ -104,67 +103,4 @@ func main() {
104103
}
105104

106105
fmt.Printf("[IaaS API] Network area %q has been successfully deleted.\n", *updatedArea.AreaId)
107-
108-
// Create a network
109-
createNetworkPayload := iaas.CreateNetworkPayload{
110-
Name: utils.Ptr("example-network"),
111-
AddressFamily: &iaas.CreateNetworkAddressFamily{
112-
Ipv4: &iaas.CreateNetworkIPv4Body{
113-
PrefixLength: utils.Ptr(int64(24)),
114-
Nameservers: &[]string{"1.2.3.4"},
115-
},
116-
},
117-
}
118-
119-
network, err := iaasClient.CreateNetwork(context.Background(), projectId).CreateNetworkPayload(createNetworkPayload).Execute()
120-
if err != nil {
121-
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `CreateNetwork`: %v\n", err)
122-
os.Exit(1)
123-
}
124-
125-
fmt.Printf("[IaaS API] Triggered creation of network with ID %q.\n", *network.NetworkId)
126-
fmt.Printf("[Iaas API] Current state of the network: %q\n", *network.State)
127-
fmt.Println("[Iaas API] Waiting for network to be created...")
128-
129-
network, err = wait.CreateNetworkWaitHandler(context.Background(), iaasClient, projectId, *network.NetworkId).WaitWithContext(context.Background())
130-
if err != nil {
131-
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for creation: %v\n", err)
132-
os.Exit(1)
133-
}
134-
135-
fmt.Printf("[IaaS API] Network has been successfully created.\n")
136-
137-
// Update a network
138-
updateNetworkPayload := iaas.PartialUpdateNetworkPayload{
139-
Name: utils.Ptr("example-network-test-renamed"),
140-
}
141-
142-
err = iaasClient.PartialUpdateNetwork(context.Background(), projectId, *network.NetworkId).PartialUpdateNetworkPayload(updateNetworkPayload).Execute()
143-
if err != nil {
144-
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `PartialUpdateNetwork`: %v\n", err)
145-
os.Exit(1)
146-
}
147-
148-
_, err = wait.UpdateNetworkWaitHandler(context.Background(), iaasClient, projectId, *network.NetworkId).WaitWithContext(context.Background())
149-
if err != nil {
150-
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for update: %v\n", err)
151-
os.Exit(1)
152-
}
153-
154-
fmt.Printf("[IaaS API] Network has been successfully updated.\n")
155-
156-
// Delete a network
157-
err = iaasClient.DeleteNetwork(context.Background(), projectId, *network.NetworkId).Execute()
158-
if err != nil {
159-
fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `DeleteNetwork`: %v\n", err)
160-
os.Exit(1)
161-
}
162-
163-
_, err = wait.DeleteNetworkWaitHandler(context.Background(), iaasClient, projectId, *network.NetworkId).WaitWithContext(context.Background())
164-
if err != nil {
165-
fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for deletion: %v\n", err)
166-
os.Exit(1)
167-
}
168-
169-
fmt.Printf("[IaaS API] Network has been successfully deleted.\n")
170106
}

0 commit comments

Comments
 (0)