Skip to content

Commit

Permalink
Add GetInstanceByID to azure, aws, vsphere, gcp and openstack provide…
Browse files Browse the repository at this point in the history
…rs (nanovms#656)
  • Loading branch information
fabioDMFerreira committed Oct 13, 2020
1 parent b31f695 commit 453d433
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 155 deletions.
95 changes: 56 additions & 39 deletions lepton/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,58 @@ func getAWSImages(region string) (*ec2.DescribeImagesOutput, error) {
return result, nil
}

func getAWSInstances(region string) *ec2.DescribeInstancesOutput {
func getAWSInstances(region string, filter []*ec2.Filter) []CloudInstance {
svc, err := session.NewSession(&aws.Config{
Region: aws.String(region)},
)
compute := ec2.New(svc)

request := ec2.DescribeInstancesInput{}
request := ec2.DescribeInstancesInput{
Filters: filter,
}
result, err := compute.DescribeInstances(&request)
if err != nil {
fmt.Println(err)
}

return result
var cinstances []CloudInstance

for _, reservation := range result.Reservations {

for i := 0; i < len(reservation.Instances); i++ {
instance := reservation.Instances[i]

instanceName := "unknown"
for x := 0; x < len(instance.Tags); x++ {
if aws.StringValue(instance.Tags[i].Key) == "Name" {
instanceName = aws.StringValue(instance.Tags[i].Value)
}
}

var privateIps, publicIps []string
for _, ninterface := range instance.NetworkInterfaces {
privateIps = append(privateIps, aws.StringValue(ninterface.PrivateIpAddress))

if ninterface.Association != nil && ninterface.Association.PublicIp != nil {
publicIps = append(publicIps, aws.StringValue(ninterface.Association.PublicIp))
}
}

cinstance := CloudInstance{
ID: aws.StringValue(instance.InstanceId),
Name: instanceName,
Status: aws.StringValue(instance.State.Name),
Created: aws.TimeValue(instance.LaunchTime).String(),
PublicIps: publicIps,
PrivateIps: privateIps,
}

cinstances = append(cinstances, cinstance)
}

}

return cinstances
}

// GetImages return all images on AWS
Expand Down Expand Up @@ -557,47 +596,25 @@ func (p *AWS) CreateSG(ctx *Context, svc *ec2.EC2, imgName string) (string, erro
return aws.StringValue(createRes.GroupId), nil
}

// GetInstances return all instances on AWS
// TODO
func (p *AWS) GetInstances(ctx *Context) ([]CloudInstance, error) {
var cinstances []CloudInstance

result := getAWSInstances(ctx.config.CloudConfig.Zone)

for _, reservation := range result.Reservations {
// GetInstanceByID returns the instance with the id passed by argument if it exists
func (p *AWS) GetInstanceByID(ctx *Context, id string) (*CloudInstance, error) {
var filters []*ec2.Filter

for i := 0; i < len(reservation.Instances); i++ {
instance := reservation.Instances[i]

instanceName := "unknown"
for x := 0; x < len(instance.Tags); x++ {
if aws.StringValue(instance.Tags[i].Key) == "Name" {
instanceName = aws.StringValue(instance.Tags[i].Value)
}
}
filters = append(filters, &ec2.Filter{Name: aws.String("tag:Name"), Values: aws.StringSlice([]string{id})})

var privateIps, publicIps []string
for _, ninterface := range instance.NetworkInterfaces {
privateIps = append(privateIps, aws.StringValue(ninterface.PrivateIpAddress))
instances := getAWSInstances(ctx.config.CloudConfig.Zone, filters)

if ninterface.Association != nil && ninterface.Association.PublicIp != nil {
publicIps = append(publicIps, aws.StringValue(ninterface.Association.PublicIp))
}
}

cinstance := CloudInstance{
ID: aws.StringValue(instance.InstanceId),
Name: instanceName,
Status: aws.StringValue(instance.State.Name),
Created: aws.TimeValue(instance.LaunchTime).String(),
PublicIps: publicIps,
PrivateIps: privateIps,
}
if len(instances) == 0 {
return nil, ErrInstanceNotFound(id)
}

cinstances = append(cinstances, cinstance)
}
return &instances[0], nil
}

}
// GetInstances return all instances on AWS
// TODO
func (p *AWS) GetInstances(ctx *Context) ([]CloudInstance, error) {
cinstances := getAWSInstances(ctx.config.CloudConfig.Zone, nil)

return cinstances, nil
}
Expand Down
80 changes: 49 additions & 31 deletions lepton/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,18 @@ func (a *Azure) CreateInstance(ctx *Context) error {
return nil
}

// GetInstanceByID returns the instance with the id passed by argument if it exists
func (a *Azure) GetInstanceByID(ctx *Context, id string) (*CloudInstance, error) {
vmClient := a.getVMClient()

vm, err := vmClient.Get(context.TODO(), a.groupName, id, compute.InstanceView)
if err != nil {
return nil, err
}

return a.convertToCloudInstance(&vm), nil
}

// GetInstances return all instances on Azure
// TODO
func (a *Azure) GetInstances(ctx *Context) ([]CloudInstance, error) {
Expand All @@ -532,46 +544,52 @@ func (a *Azure) GetInstances(ctx *Context) ([]CloudInstance, error) {
instances := vmlist.Values()

for _, instance := range instances {
cinstance := CloudInstance{
Name: *instance.Name,
}
privateIP := ""
publicIP := ""
cinstance := a.convertToCloudInstance(&instance)

if instance.VirtualMachineProperties != nil {
nifs := *((*(*instance.VirtualMachineProperties).NetworkProfile).NetworkInterfaces)
cinstances = append(cinstances, *cinstance)
}

for i := 0; i < len(nifs); i++ {
nicClient := a.getNicClient()
nic, err := nicClient.Get(context.TODO(), a.groupName, cinstance.Name, "")
if err != nil {
fmt.Println(err)
}
return cinstances, nil
}

func (a *Azure) convertToCloudInstance(instance *compute.VirtualMachine) *CloudInstance {
cinstance := CloudInstance{
Name: *instance.Name,
}
privateIP := ""
publicIP := ""

if instance.VirtualMachineProperties != nil {
nifs := *((*(*instance.VirtualMachineProperties).NetworkProfile).NetworkInterfaces)

for i := 0; i < len(nifs); i++ {
nicClient := a.getNicClient()
nic, err := nicClient.Get(context.TODO(), a.groupName, cinstance.Name, "")
if err != nil {
fmt.Println(err)
}

if nic.InterfacePropertiesFormat != nil {
ipconfig := *(*nic.InterfacePropertiesFormat).IPConfigurations
for x := 0; x < len(ipconfig); x++ {
format := *ipconfig[x].InterfaceIPConfigurationPropertiesFormat
privateIP = *format.PrivateIPAddress

ipClient := a.getIPClient()
pubip, err := ipClient.Get(context.TODO(), a.groupName, cinstance.Name, "")
if err != nil {
fmt.Println(err)
}
publicIP = *(*pubip.PublicIPAddressPropertiesFormat).IPAddress
if nic.InterfacePropertiesFormat != nil {
ipconfig := *(*nic.InterfacePropertiesFormat).IPConfigurations
for x := 0; x < len(ipconfig); x++ {
format := *ipconfig[x].InterfaceIPConfigurationPropertiesFormat
privateIP = *format.PrivateIPAddress

ipClient := a.getIPClient()
pubip, err := ipClient.Get(context.TODO(), a.groupName, cinstance.Name, "")
if err != nil {
fmt.Println(err)
}
publicIP = *(*pubip.PublicIPAddressPropertiesFormat).IPAddress
}

}
}
cinstance.PrivateIps = []string{privateIP}
cinstance.PublicIps = []string{publicIP}

cinstances = append(cinstances, cinstance)
}
}
cinstance.PrivateIps = []string{privateIP}
cinstance.PublicIps = []string{publicIP}

return cinstances, nil
return &cinstance
}

// ListInstances lists instances on Azure
Expand Down
5 changes: 5 additions & 0 deletions lepton/digital_ocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func (do *DigitalOcean) CreateInstance(ctx *Context) error {
return nil
}

// GetInstanceByID returns the instance with the id passed by argument if it exists
func (do *DigitalOcean) GetInstanceByID(ctx *Context, id string) (*CloudInstance, error) {
return nil, errors.New("un-implemented")
}

// GetInstances return all instances on DigitalOcean
// TODO
func (do *DigitalOcean) GetInstances(ctx *Context) ([]CloudInstance, error) {
Expand Down
62 changes: 39 additions & 23 deletions lepton/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,18 @@ func (p *GCloud) ListInstances(ctx *Context) error {
return nil
}

// GetInstanceByID returns the instance with the id passed by argument if it exists
func (p *GCloud) GetInstanceByID(ctx *Context, id string) (*CloudInstance, error) {
req := p.Service.Instances.Get(ctx.config.CloudConfig.ProjectID, ctx.config.CloudConfig.Zone, id)

instance, err := req.Do()
if err != nil {
return nil, err
}

return p.convertToCloudInstance(instance), nil
}

// GetInstances return all instances on GCloud
func (p *GCloud) GetInstances(ctx *Context) ([]CloudInstance, error) {
context := context.TODO()
Expand All @@ -465,29 +477,8 @@ func (p *GCloud) GetInstances(ctx *Context) ([]CloudInstance, error) {

if err := req.Pages(context, func(page *compute.InstanceList) error {
for _, instance := range page.Items {
var (
privateIps, publicIps []string
)
for _, ninterface := range instance.NetworkInterfaces {
if ninterface.NetworkIP != "" {
privateIps = append(privateIps, ninterface.NetworkIP)

}
for _, accessConfig := range ninterface.AccessConfigs {
if accessConfig.NatIP != "" {
publicIps = append(publicIps, accessConfig.NatIP)
}
}
}

cinstance := CloudInstance{
Name: instance.Name,
Status: instance.Status,
Created: instance.CreationTimestamp,
PublicIps: publicIps,
PrivateIps: privateIps,
}
cinstances = append(cinstances, cinstance)
cinstance := p.convertToCloudInstance(instance)
cinstances = append(cinstances, *cinstance)
}
return nil
}); err != nil {
Expand All @@ -497,6 +488,31 @@ func (p *GCloud) GetInstances(ctx *Context) ([]CloudInstance, error) {
return cinstances, nil
}

func (p *GCloud) convertToCloudInstance(instance *compute.Instance) *CloudInstance {
var (
privateIps, publicIps []string
)
for _, ninterface := range instance.NetworkInterfaces {
if ninterface.NetworkIP != "" {
privateIps = append(privateIps, ninterface.NetworkIP)

}
for _, accessConfig := range ninterface.AccessConfigs {
if accessConfig.NatIP != "" {
publicIps = append(publicIps, accessConfig.NatIP)
}
}
}

return &CloudInstance{
Name: instance.Name,
Status: instance.Status,
Created: instance.CreationTimestamp,
PublicIps: publicIps,
PrivateIps: privateIps,
}
}

// DeleteInstance deletes instance from Gcloud
func (p *GCloud) DeleteInstance(ctx *Context, instancename string) error {
context := context.TODO()
Expand Down
5 changes: 5 additions & 0 deletions lepton/onprem.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ func (p *OnPrem) CreateInstance(ctx *Context) error {
return nil
}

// GetInstanceByID returns the instance with the id passed by argument if it exists
func (p *OnPrem) GetInstanceByID(ctx *Context, id string) (*CloudInstance, error) {
return nil, errors.New("un-implemented")
}

// GetInstances return all instances on prem
// TODO
func (p *OnPrem) GetInstances(ctx *Context) ([]CloudInstance, error) {
Expand Down
Loading

0 comments on commit 453d433

Please sign in to comment.