Skip to content
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
10 changes: 9 additions & 1 deletion pkg/stackit/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,13 @@ func (s *Stackit) createNetwork(ctx context.Context, projectId string, networkNa
}

func (s *Stackit) deleteNetwork(ctx context.Context, projectId string, networkId string) error {
return s.client.DeleteNetworkExecute(ctx, projectId, networkId)
err := s.client.DeleteNetworkExecute(ctx, projectId, networkId)
if err != nil {
return err
}
_, err = wait.DeleteNetworkWaitHandler(ctx, s.client, projectId, networkId).WaitWithContext(ctx)
if err != nil {
return err
}
return nil
}
38 changes: 9 additions & 29 deletions pkg/stackit/stackit.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,8 @@ func (s *Stackit) Status(ctx context.Context, projectId, machineName string) (cl
if server == nil {
return client.StatusNotFound, nil
}
if server.GetPowerStatus() == nil {
return client.StatusNotFound, nil
}

return statusFromPowerStateString(*server.GetPowerStatus()), nil
return statusFromPowerStateString(server.GetPowerStatus()), nil
}

func (s *Stackit) Start(ctx context.Context, projectId, machineName string) error {
Expand Down Expand Up @@ -149,15 +146,11 @@ func (s *Stackit) Delete(ctx context.Context, projectId, machineName string) err
}

networkId := nics[0].NetworkId
err = s.client.DeleteNetwork(ctx, projectId, *networkId).Execute()
err = s.deleteNetwork(ctx, projectId, *networkId)
if err != nil {
log.Default.Errorf("failed to delete network")
gotError = true
}
_, err = wait.DeleteNetworkWaitHandler(ctx, s.client, projectId, *networkId).WaitWithContext(ctx)
if err != nil {
log.Default.Errorf("error while waiting for network deletion")
}

for _, id := range *nics[0].SecurityGroups {
name, err := s.getSecurityGroupNameByID(ctx, projectId, id)
Expand Down Expand Up @@ -227,7 +220,7 @@ func (s *Stackit) Create(ctx context.Context, options *options.Options, publicKe
NetworkId: network.NetworkId,
},
},
UserData: &userdata,
UserData: userdata,
}

server, err := s.client.CreateServer(ctx, options.ProjectID).CreateServerPayload(createServerPayload).Execute()
Expand Down Expand Up @@ -286,36 +279,23 @@ func (s *Stackit) Create(ctx context.Context, options *options.Options, publicKe
return nil
}

func generateUserData(publicKey string) (string, error) {
func generateUserData(publicKey string) (*[]byte, error) {
t, err := template.New("cloud-config.yaml").ParseFS(cloudConfigFS, "cloud-config.yaml")
if err != nil {
return "", err
return nil, err
}

output := new(bytes.Buffer)
if err := t.Execute(output, map[string]string{
"PublicKey": publicKey,
"Username": SSHUserName,
}); err != nil {
return "", err
}

return base64.StdEncoding.EncodeToString(output.Bytes()), nil
}

func (s *Stackit) createVolume(ctx context.Context, projectId, volumeName, volumeAvailabilityZone string, volumeSize int64) (string, error) {
createVolumePayload := iaas.CreateVolumePayload{
Name: &volumeName,
AvailabilityZone: &volumeAvailabilityZone,
Size: &volumeSize,
}

volume, err := s.client.CreateVolume(ctx, projectId).CreateVolumePayload(createVolumePayload).Execute()
if err != nil {
return "", err
return nil, err
}

return *volume.Id, nil
encodedUserData := base64.StdEncoding.EncodeToString(output.Bytes())
byteArray := []byte(encodedUserData)
return &byteArray, nil
}

func (s *Stackit) getServerByName(ctx context.Context, projectId, serverName string) (*iaas.Server, error) {
Expand Down