Skip to content

Commit

Permalink
fix address extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
vooon committed Nov 2, 2023
1 parent cb6316e commit 05eabc2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
16 changes: 11 additions & 5 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,27 +223,30 @@ func (g *InstanceGroup) ConnectInfo(ctx context.Context, instanceID string) (pro
return provider.ConnectInfo{}, fmt.Errorf("Server not found %s: %w", node.PhysicalID, os.ErrNotExist)
}

// g.log.Info("srv", "srv", srv)
// g.log.Debug("Server info", "srv", srv)
if srv.Status != "ACTIVE" {
return provider.ConnectInfo{}, fmt.Errorf("instance status is not active: %s", srv.Status)
}

ipAddr := srv.AccessIPv4
if ipAddr == "" {
addrs, err := extractAddresses(srv)
netAddrs, err := extractAddresses(srv)
if err != nil {
return provider.ConnectInfo{}, err
}

// TODO: detect internal (tenant) and external networks
for net, addr := range addrs {
ipAddr = addr.Address
g.log.Debug("Use address", "network", net, "ip_address", ipAddr)
for net, addrs := range netAddrs {
for _, addr := range addrs {
ipAddr = addr.Address
g.log.Debug("Use address", "network", net, "ip_address", ipAddr)
}
}
}

info := provider.ConnectInfo{
ConnectorConfig: g.settings.ConnectorConfig,
ID: instanceID,
InternalAddr: ipAddr,
ExternalAddr: ipAddr,
}
Expand All @@ -252,6 +255,9 @@ func (g *InstanceGroup) ConnectInfo(ctx context.Context, instanceID string) (pro
// TODO: get from image meta
info.OS = "linux"
info.Arch = "amd64"
info.Protocol = provider.ProtocolSSH

// g.log.Debug("Info", "info", info)

return info, nil
}
Expand Down
35 changes: 21 additions & 14 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,31 @@ type Address struct {
Type string `json:"OS-EXT-IPS:type,omitempty"`
}

func extractAddresses(srv *servers.Server) (map[string]Address, error) {
ret := make(map[string]Address, len(srv.Addresses))
func extractAddresses(srv *servers.Server) (map[string][]Address, error) {
ret := make(map[string][]Address, len(srv.Addresses))

for k, iv := range srv.Addresses {
var out Address
for net, isv := range srv.Addresses {
ism := isv.([]interface{})
items := make([]Address, 0, len(ism))

cfg := &mapstructure.DecoderConfig{
Metadata: nil,
Result: &out,
TagName: "json",
}
decoder, _ := mapstructure.NewDecoder(cfg)
err := decoder.Decode(iv)
if err != nil {
return nil, err
for _, iv := range ism {
var out Address

cfg := &mapstructure.DecoderConfig{
Metadata: nil,
Result: &out,
TagName: "json",
}
decoder, _ := mapstructure.NewDecoder(cfg)
err := decoder.Decode(iv)
if err != nil {
return nil, err
}

items = append(items, out)
}

ret[k] = out
ret[net] = items
}

return ret, nil
Expand Down

0 comments on commit 05eabc2

Please sign in to comment.