Skip to content

Commit 7f14d55

Browse files
Merge pull request #1 from stackitcloud/feature/delete-function
feat: implement delete function
2 parents 749fbbc + 0080390 commit 7f14d55

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

pkg/stackit/stackit.go

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"embed"
77
"encoding/base64"
88
"fmt"
9+
"github.com/loft-sh/log"
910
"text/template"
1011

1112
"github.com/stackitcloud/stackit-sdk-go/core/utils"
@@ -110,7 +111,73 @@ func (s *Stackit) Stop(ctx context.Context, projectId, machineName string) error
110111
}
111112

112113
func (s *Stackit) Delete(ctx context.Context, projectId, machineName string) error {
113-
//server, err := s.getServerByName(ctx, projectId, machineName)
114+
server, err := s.getServerByName(ctx, projectId, machineName)
115+
if err != nil {
116+
return fmt.Errorf("delete: %w", err)
117+
}
118+
119+
gotError := false
120+
121+
err = s.client.DeleteServer(ctx, projectId, *server.Id).Execute()
122+
if err != nil {
123+
log.Default.Errorf("failed to delete server")
124+
}
125+
_, err = wait.DeleteServerWaitHandler(ctx, s.client, projectId, *server.Id).WaitWithContext(ctx)
126+
if err != nil {
127+
log.Default.Errorf("error while waiting for server deletion")
128+
}
129+
130+
nics := *server.Nics
131+
if len(nics) == 0 {
132+
log.Default.Errorf("no networks found for server")
133+
gotError = true
134+
} else {
135+
publicIPAddress := nics[0].PublicIp
136+
if publicIPAddress != nil {
137+
publicIP, err := s.getPublicIPByIPAddress(ctx, projectId, *publicIPAddress)
138+
if err != nil {
139+
log.Default.Errorf("failed to get public IP")
140+
gotError = true
141+
}
142+
143+
err = s.client.DeletePublicIP(ctx, projectId, *publicIP.Id).Execute()
144+
if err != nil {
145+
log.Default.Errorf("failed to delete public IP")
146+
gotError = true
147+
}
148+
}
149+
150+
networkId := nics[0].NetworkId
151+
err = s.client.DeleteNetwork(ctx, projectId, *networkId).Execute()
152+
if err != nil {
153+
log.Default.Errorf("failed to delete network")
154+
gotError = true
155+
}
156+
_, err = wait.DeleteNetworkWaitHandler(ctx, s.client, projectId, *networkId).WaitWithContext(ctx)
157+
if err != nil {
158+
log.Default.Errorf("error while waiting for network deletion")
159+
}
160+
161+
for _, id := range *nics[0].SecurityGroups {
162+
name, err := s.getSecurityGroupNameByID(ctx, projectId, id)
163+
if err != nil {
164+
log.Default.Errorf("failed to get name of security group by ID")
165+
gotError = true
166+
}
167+
168+
if name != "default" {
169+
err = s.client.DeleteSecurityGroup(ctx, projectId, id).Execute()
170+
if err != nil {
171+
log.Default.Errorf("failed to delete security group %q", name)
172+
gotError = true
173+
}
174+
}
175+
}
176+
}
177+
178+
if gotError {
179+
return errors.New("failed to delete all components associated to the devpod")
180+
}
114181

115182
return nil
116183
}
@@ -269,3 +336,31 @@ func (s *Stackit) getServerByName(ctx context.Context, projectId, serverName str
269336
}
270337
return nil, errors.New("server not found")
271338
}
339+
340+
func (s *Stackit) getPublicIPByIPAddress(ctx context.Context, projectId, ipAddress string) (*iaas.PublicIp, error) {
341+
publicIPs, err := s.client.ListPublicIPs(ctx, projectId).Execute()
342+
if err != nil {
343+
return nil, err
344+
}
345+
346+
if len(*publicIPs.Items) == 0 {
347+
return nil, errors.New("no public IPs found")
348+
}
349+
350+
for _, publicIP := range *publicIPs.Items {
351+
if *publicIP.Ip == ipAddress {
352+
return &publicIP, nil
353+
}
354+
}
355+
356+
return nil, errors.New("public IP not found")
357+
}
358+
359+
func (s *Stackit) getSecurityGroupNameByID(ctx context.Context, projectId, securityGroupId string) (string, error) {
360+
securityGroup, err := s.client.GetSecurityGroup(ctx, projectId, securityGroupId).Execute()
361+
if err != nil {
362+
return "", errors.New("security group not found")
363+
}
364+
365+
return *securityGroup.Name, nil
366+
}

0 commit comments

Comments
 (0)