Skip to content

Commit

Permalink
refactor: composite requests (#105)
Browse files Browse the repository at this point in the history
* feat: use composite call to create the server and nics

* test: fix tests

* chore: remove unneeded checks

* chore: review proposed changes

* removed useless checks in test
  • Loading branch information
rmocanu-ionos authored Mar 1, 2024
1 parent 667588b commit 7501213
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 223 deletions.
4 changes: 2 additions & 2 deletions internal/utils/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ func (c *Client) CreateServer(datacenterId string, server sdkgo.Server) (*sdkgo.
return &svr, nil
}

func (c *Client) GetServer(datacenterId, serverId string) (*sdkgo.Server, error) {
server, resp, err := c.ServersApi.DatacentersServersFindById(c.ctx, datacenterId, serverId).Depth(1).Execute()
func (c *Client) GetServer(datacenterId, serverId string, depth int32) (*sdkgo.Server, error) {
server, resp, err := c.ServersApi.DatacentersServersFindById(c.ctx, datacenterId, serverId).Depth(depth).Execute()
if err != nil {
return nil, sdk_utils.ShortenOpenApiErr(err)
}
Expand Down
4 changes: 1 addition & 3 deletions internal/utils/client_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ClientService interface {
RemoveNat(datacenterId, natId string) error

CreateServer(datacenterId string, server ionoscloud.Server) (*ionoscloud.Server, error)
GetServer(datacenterId, serverId string) (*ionoscloud.Server, error)
GetServer(datacenterId, serverId string, depth int32) (*ionoscloud.Server, error)
GetLan(datacenterId, LanId string) (*ionoscloud.Lan, error)
GetLans(datacenterId string) (*ionoscloud.Lans, error)
GetNic(datacenterId, ServerId, NicId string) (*ionoscloud.Nic, error)
Expand All @@ -31,9 +31,7 @@ type ClientService interface {
SuspendServer(datacenterId, serverId string) error
RestartServer(datacenterId, serverId string) error
RemoveServer(datacenterId, serverId string) error
CreateAttachVolume(datacenterId, serverId string, properties *ClientVolumeProperties) (*ionoscloud.Volume, error)
RemoveVolume(datacenterId, volumeId string) error
CreateAttachNIC(datacenterId, serverId, name string, dhcp bool, lanId int32, ips *[]string) (*ionoscloud.Nic, error)
RemoveNic(datacenterId, serverId, nicId string) error
GetLocationById(regionId, locationId string) (*ionoscloud.Location, error)
GetImages() (*ionoscloud.Images, error)
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestClientCreateServerErr(t *testing.T) {
}

func TestClientGetServerErr(t *testing.T) {
_, err := getTestClient().GetServer(testName, testName)
_, err := getTestClient().GetServer(testName, testName, 2)
assert.Error(t, err)
}

Expand Down
39 changes: 4 additions & 35 deletions internal/utils/mocks/ClientService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 60 additions & 52 deletions ionoscloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,36 +808,11 @@ func (d *Driver) Create() (err error) {
serverToCreate.Entities = sdkgo.NewServerEntitiesWithDefaults()
serverToCreate.Entities.SetVolumes(*attachedVolumes)

server, err := d.client().CreateServer(d.DatacenterId, serverToCreate)
if err != nil {
// TODO: Export to a func
log.Warn(rollingBackNotice)
if removeErr := d.Remove(); removeErr != nil {
return fmt.Errorf("failed to create server due to error: %w\n Removing created resources: %v", err, removeErr)
}
return err
}
if serverId, ok := server.GetIdOk(); ok && serverId != nil {
d.ServerId = *serverId
log.Debugf("Server ID: %v", d.ServerId)
}

server, err = d.client().GetServer(d.DatacenterId, d.ServerId)
if err != nil {
return fmt.Errorf("error getting server by id: %w", err)
}
volumes, ok := server.Entities.GetVolumesOk()
if ok != true {
return fmt.Errorf("error getting server: d.ServerId is empty")
}
d.VolumeId = *(*volumes.Items)[0].GetId()
log.Debugf("Volume ID: %v", d.VolumeId)

// Reserve IP if needed

// Add nics to server
providedNicIps := len(d.NicIps) != 0
reservedIps := &[]string{}

// Reserve IP if needed
if !isLanPrivate && !providedNicIps ||
d.CreateNat && d.NatPublicIps == nil {
ipBlock, err := d.client().CreateIpBlock(1, d.Location)
Expand All @@ -854,7 +829,6 @@ func (d *Driver) Create() (err error) {
}
}

// Create NIC
var ipsForAttachedNic *[]string

if providedNicIps {
Expand All @@ -865,33 +839,67 @@ func (d *Driver) Create() (err error) {
ipsForAttachedNic = reservedIps // For public NICs we use the generated IPs
}

attachedNics := sdkgo.NewNicsWithDefaults()

lanId, _ := strconv.Atoi(d.LanId)
nic, err := d.client().CreateAttachNIC(d.DatacenterId, d.ServerId, d.MachineName, d.NicDhcp, int32(lanId), ipsForAttachedNic)
nicProperties := &sdkgo.NicProperties{
Name: &d.MachineName,
Lan: sdkgo.PtrInt32(int32(lanId)),
Ips: ipsForAttachedNic,
Dhcp: &d.NicDhcp,
}

attachedNics.Items = &[]sdkgo.Nic{
{
Properties: nicProperties,
},
}

for _, additionalLanId := range d.AdditionalLansIds {
additionalNic := sdkgo.Nic{
Properties: &sdkgo.NicProperties{
Name: sdkgo.PtrString(d.MachineName + " " + fmt.Sprint(additionalLanId)),
Lan: sdkgo.PtrInt32(int32(additionalLanId)),
Ips: nil,
Dhcp: sdkgo.PtrBool(true),
},
}
*attachedNics.Items = append(*attachedNics.Items, additionalNic)
}

serverToCreate.Entities.SetNics(*attachedNics)

server, err := d.client().CreateServer(d.DatacenterId, serverToCreate)
if err != nil {
// TODO: Duplicated
// TODO: Export to a func
log.Warn(rollingBackNotice)
if removeErr := d.Remove(); removeErr != nil {
return fmt.Errorf("failed to create machine due to error: %w\n Removing created resources: %v", fmt.Errorf("error attaching NIC: %w", err), removeErr)
return fmt.Errorf("failed to create server due to error: %w\n Removing created resources: %v", err, removeErr)
}
return err
}
for _, additionalLanId := range d.AdditionalLansIds {
additionalNic, err := d.client().CreateAttachNIC(d.DatacenterId, d.ServerId, d.MachineName, true, int32(additionalLanId), nil)
if err != nil {
// TODO: Duplicated
log.Warn(rollingBackNotice)
if removeErr := d.Remove(); removeErr != nil {
return fmt.Errorf("failed to create machine due to error: %w\n Removing created resources: %v", fmt.Errorf("error attaching additional NIC: %w", err), removeErr)
}
return err
}
if nicId, ok := additionalNic.GetIdOk(); ok && nicId != nil {
d.AdditionalNicsIds = append(d.AdditionalNicsIds, *nicId)
}
if serverId, ok := server.GetIdOk(); ok && serverId != nil {
d.ServerId = *serverId
log.Debugf("Server ID: %v", d.ServerId)
} else {
return fmt.Errorf("error getting server: d.ServerId is empty")
}
if nicId, ok := nic.GetIdOk(); ok && nicId != nil {
d.NicId = *nic.Id
log.Debugf("Nic ID: %v", d.NicId)

server, err = d.client().GetServer(d.DatacenterId, d.ServerId, 2)
if err != nil {
return fmt.Errorf("error getting server by id: %w", err)
}
d.VolumeId = *(*server.Entities.GetVolumes().Items)[0].GetId()
log.Debugf("Volume ID: %v", d.VolumeId)

nics := server.Entities.GetNics()
for _, nic := range *nics.Items {
if *nic.Properties.Name == d.MachineName {
d.NicId = *nic.Id
log.Debugf("Nic ID: %v", d.NicId)
} else {
d.AdditionalNicsIds = append(d.AdditionalNicsIds, *nic.Id)
}
}

if d.WaitForIpChange {
Expand All @@ -901,7 +909,7 @@ func (d *Driver) Create() (err error) {
}
}

nic, err = d.client().GetNic(d.DatacenterId, d.ServerId, d.NicId)
nic, err := d.client().GetNic(d.DatacenterId, d.ServerId, d.NicId)
if err != nil {
return fmt.Errorf("error getting NIC: %w", err)
}
Expand All @@ -927,7 +935,7 @@ func (d *Driver) Create() (err error) {
natLansToGateways = &d.NatLansToGateways
}
sourceSubnet := net.ParseIP((*nicIps)[0]).Mask(net.CIDRMask(24, 32)).String() + "/24"
nat, err := d.client().CreateNat(d.DatacenterId, d.NatName, *natPublicIps, *&d.NatFlowlogs, *&d.NatRules, *natLansToGateways, sourceSubnet, d.SkipDefaultNatRules)
nat, err := d.client().CreateNat(d.DatacenterId, d.NatName, *natPublicIps, d.NatFlowlogs, d.NatRules, *natLansToGateways, sourceSubnet, d.SkipDefaultNatRules)
if err != nil {
return err
}
Expand Down Expand Up @@ -1051,7 +1059,7 @@ func (d *Driver) Start() error {
} else if d.ServerType == "CUBE" {
err = d.client().ResumeServer(d.DatacenterId, d.ServerId)
} else {
err = fmt.Errorf("Wrong server type: %s", d.ServerType)
err = fmt.Errorf("wrong server type: %s", d.ServerType)
}

if err != nil {
Expand All @@ -1078,7 +1086,7 @@ func (d *Driver) Stop() error {
} else if d.ServerType == "CUBE" {
err = d.client().SuspendServer(d.DatacenterId, d.ServerId)
} else {
err = fmt.Errorf("Wrong server type: %s", d.ServerType)
err = fmt.Errorf("wrong server type: %s", d.ServerType)
}
if err != nil {
return fmt.Errorf("error stoping server: %w", err)
Expand Down Expand Up @@ -1138,7 +1146,7 @@ func (d *Driver) GetState() (state.State, error) {
if d.ServerId == "" {
return state.None, fmt.Errorf("error getting server: d.ServerID is empty")
}
server, err := d.client().GetServer(d.DatacenterId, d.ServerId)
server, err := d.client().GetServer(d.DatacenterId, d.ServerId, 1)
if err != nil {
return state.None, fmt.Errorf("error getting server: %w", err)
}
Expand Down
Loading

0 comments on commit 7501213

Please sign in to comment.