Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adding lan to existing natgateway, setting nat ip #109

Merged
merged 7 commits into from
May 29, 2024
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
1 change: 1 addition & 0 deletions internal/utils/client_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ClientService interface {
RemoveLan(datacenterId, lanId string) error

CreateNat(datacenterId, name string, publicIps, flowlogs, natRules []string, lansToGateways map[string][]string, sourceSubnet string, skipDefaultRules bool) (*ionoscloud.NatGateway, error)
PatchNat(datacenterId, natId, name string, publicIps []string, lansToGateways []ionoscloud.NatGatewayLanProperties) (*ionoscloud.NatGateway, error)
GetNat(datacenterId string, natId string) (*ionoscloud.NatGateway, error)
GetNats(datacenterId string) (*ionoscloud.NatGateways, error)
RemoveNat(datacenterId, natId string) error
Expand Down
15 changes: 15 additions & 0 deletions internal/utils/mocks/ClientService.go

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

16 changes: 16 additions & 0 deletions internal/utils/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,22 @@ func keysOfMap[K comparable, V any](m map[K]V) []K {
return keys
}

func (c *Client) PatchNat(datacenterId, natId, name string, publicIps []string, lansToGateways []sdkgo.NatGatewayLanProperties) (*sdkgo.NatGateway, error) {
nat, resp, err := c.NATGatewaysApi.DatacentersNatgatewaysPatch(c.ctx, datacenterId, natId).NatGatewayProperties(
sdkgo.NatGatewayProperties{
Name: &name,
PublicIps: &publicIps,
Lans: &lansToGateways,
},
).Execute()
if err != nil {
return nil, err
}

err = c.waitTillProvisioned(resp.Header.Get("location"))
return &nat, err
}

func (c *Client) CreateNat(datacenterId, name string, publicIps, flowlogs, natRules []string, lansToGateways map[string][]string, sourceSubnet string, skipDefaultRules bool) (*sdkgo.NatGateway, error) {
var lans []sdkgo.NatGatewayLanProperties
publicIp := publicIps[0]
Expand Down
36 changes: 31 additions & 5 deletions ionoscloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ func (d *Driver) PreCreateCheck() error {
return fmt.Errorf("error getting image/alias %s: %w", d.Image, err)
}

if d.NatId != "" && d.DatacenterId != "" {
if d.NatId == "" && d.DatacenterId != "" {
nats, err := d.client().GetNats(d.DatacenterId)
if err != nil {
return err
Expand Down Expand Up @@ -894,7 +894,9 @@ func (d *Driver) Create() (err error) {

nics := server.Entities.GetNics()
for _, nic := range *nics.Items {
log.Infof("%v", *nic.Properties.Name)
if *nic.Properties.Name == d.MachineName {
log.Infof("altceva")
d.NicId = *nic.Id
log.Debugf("Nic ID: %v", d.NicId)
} else {
Expand All @@ -918,10 +920,6 @@ func (d *Driver) Create() (err error) {
if nicProp, ok := nic.GetPropertiesOk(); ok && nicProp != nil {
nicIps = nicProp.GetIps()
}
if len(*nicIps) > 0 {
d.IPAddress = (*nicIps)[0]
log.Infof(d.IPAddress)
}

// --- NAT ---
if d.CreateNat {
Expand All @@ -943,6 +941,34 @@ func (d *Driver) Create() (err error) {
d.NatId = *nat.Id // NatId is used later to retrieve public IP, etc.
d.IPAddress = (*natPublicIps)[0]
log.Infof(d.IPAddress)
} else if d.NatId != "" {
nat, _ := d.client().GetNat(d.DatacenterId, d.NatId)

foundNatLan := false
lanIdInt, _ := strconv.Atoi(d.LanId)
for _, natLan := range *nat.Properties.Lans {
if *natLan.Id == int32(lanIdInt) {
foundNatLan = true
break
}
}
if !foundNatLan {
// connect lan to nat
natLans := append(*nat.Properties.Lans, sdkgo.NatGatewayLanProperties{Id: pointer.From(int32(lanIdInt)), GatewayIps: nil})
_, err = d.client().PatchNat(d.DatacenterId, d.NatId, *nat.Properties.Name, *nat.Properties.PublicIps, natLans)

if err != nil {
return err
}
}

d.IPAddress = (*nat.Properties.PublicIps)[0]
log.Infof(d.IPAddress)
} else {
if len(*nicIps) > 0 {
d.IPAddress = (*nicIps)[0]
log.Infof(d.IPAddress)
}
}

return nil
Expand Down
Loading
Loading