|
6 | 6 | "embed" |
7 | 7 | "encoding/base64" |
8 | 8 | "fmt" |
| 9 | + "github.com/loft-sh/log" |
9 | 10 | "text/template" |
10 | 11 |
|
11 | 12 | "github.com/stackitcloud/stackit-sdk-go/core/utils" |
@@ -110,7 +111,58 @@ func (s *Stackit) Stop(ctx context.Context, projectId, machineName string) error |
110 | 111 | } |
111 | 112 |
|
112 | 113 | 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 | + if len(*server.Nics) == 0 { |
| 122 | + log.Default.Errorf("no networks found for server") |
| 123 | + gotError = true |
| 124 | + } else { |
| 125 | + nics := *server.Nics |
| 126 | + networkId := nics[0].NetworkId |
| 127 | + err = s.client.DeleteNetwork(ctx, projectId, *networkId).Execute() |
| 128 | + if err != nil { |
| 129 | + log.Default.Errorf("failed to delete network") |
| 130 | + gotError = true |
| 131 | + } |
| 132 | + |
| 133 | + publicIPAddress := nics[0].PublicIp |
| 134 | + publicIP, err := s.getPublicIPByIPAddress(ctx, projectId, *publicIPAddress) |
| 135 | + if err != nil { |
| 136 | + log.Default.Errorf("failed to get public IP") |
| 137 | + gotError = true |
| 138 | + } |
| 139 | + |
| 140 | + err = s.client.DeletePublicIP(ctx, projectId, *publicIP.Id).Execute() |
| 141 | + if err != nil { |
| 142 | + log.Default.Errorf("failed to delete public IP") |
| 143 | + gotError = true |
| 144 | + } |
| 145 | + |
| 146 | + for _, id := range *nics[0].SecurityGroups { |
| 147 | + name, err := s.getSecurityGroupNameByID(ctx, projectId, id) |
| 148 | + if err != nil { |
| 149 | + log.Default.Errorf("failed to get name of security group by ID") |
| 150 | + gotError = true |
| 151 | + } |
| 152 | + |
| 153 | + if name != "default" { |
| 154 | + err = s.client.DeleteSecurityGroup(ctx, projectId, id).Execute() |
| 155 | + if err != nil { |
| 156 | + log.Default.Errorf("failed to delete security group %q", name) |
| 157 | + gotError = true |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + if gotError { |
| 164 | + return errors.New("failed to delete all components associated to the devpod") |
| 165 | + } |
114 | 166 |
|
115 | 167 | return nil |
116 | 168 | } |
@@ -269,3 +321,31 @@ func (s *Stackit) getServerByName(ctx context.Context, projectId, serverName str |
269 | 321 | } |
270 | 322 | return nil, errors.New("server not found") |
271 | 323 | } |
| 324 | + |
| 325 | +func (s *Stackit) getPublicIPByIPAddress(ctx context.Context, projectId, ipAddress string) (*iaas.PublicIp, error) { |
| 326 | + publicIPs, err := s.client.ListPublicIPs(ctx, projectId).Execute() |
| 327 | + if err != nil { |
| 328 | + return nil, err |
| 329 | + } |
| 330 | + |
| 331 | + if len(*publicIPs.Items) == 0 { |
| 332 | + return nil, errors.New("no public IPs found") |
| 333 | + } |
| 334 | + |
| 335 | + for _, publicIP := range *publicIPs.Items { |
| 336 | + if *publicIP.Ip == ipAddress { |
| 337 | + return &publicIP, nil |
| 338 | + } |
| 339 | + } |
| 340 | + |
| 341 | + return nil, errors.New("public IP not found") |
| 342 | +} |
| 343 | + |
| 344 | +func (s *Stackit) getSecurityGroupNameByID(ctx context.Context, projectId, securityGroupId string) (string, error) { |
| 345 | + securityGroup, err := s.client.GetSecurityGroup(ctx, projectId, securityGroupId).Execute() |
| 346 | + if err != nil { |
| 347 | + return "", errors.New("security group not found") |
| 348 | + } |
| 349 | + |
| 350 | + return *securityGroup.Name, nil |
| 351 | +} |
0 commit comments