Skip to content

Commit

Permalink
Merge pull request nanovms#706 from fabiodmferreira-work/656-provider…
Browse files Browse the repository at this point in the history
…s-allow-getting-one-instance

Add GetInstanceByID method into providers nanovms#656
  • Loading branch information
fabioDMFerreira authored Oct 13, 2020
2 parents b31f695 + 083b9d8 commit 64819bb
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 327 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/Azure/azure-storage-blob-go v0.10.0
github.com/Azure/go-autorest/autorest v0.11.2
github.com/Azure/go-autorest/autorest/adal v0.9.0
github.com/Azure/go-autorest/autorest/azure/auth v0.5.0
github.com/Azure/go-autorest/autorest/to v0.4.0
github.com/Azure/go-autorest/autorest/validation v0.3.0 // indirect
github.com/aws/aws-sdk-go v1.23.0
Expand All @@ -27,10 +26,9 @@ require (
github.com/vishvananda/netlink v1.0.0
github.com/vishvananda/netns v0.0.0-20190625233234-7109fa855b0f // indirect
github.com/vmware/govmomi v0.22.2
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd
golang.org/x/tools v0.0.0-20200928172933-50ab9675f5b0 // indirect
google.golang.org/api v0.7.0
gopkg.in/ini.v1 v1.55.0 // indirect
)
170 changes: 3 additions & 167 deletions go.sum

Large diffs are not rendered by default.

94 changes: 55 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,24 @@ 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)
}
}

var privateIps, publicIps []string
for _, ninterface := range instance.NetworkInterfaces {
privateIps = append(privateIps, aws.StringValue(ninterface.PrivateIpAddress))
filters = append(filters, &ec2.Filter{Name: aws.String("tag:Name"), Values: aws.StringSlice([]string{id})})

if ninterface.Association != nil && ninterface.Association.PublicIp != nil {
publicIps = append(publicIps, aws.StringValue(ninterface.Association.PublicIp))
}
}
instances := getAWSInstances(ctx.config.CloudConfig.Zone, filters)

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
func (p *AWS) GetInstances(ctx *Context) ([]CloudInstance, error) {
cinstances := getAWSInstances(ctx.config.CloudConfig.Zone, nil)

return cinstances, nil
}
Expand Down
81 changes: 49 additions & 32 deletions lepton/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,19 @@ 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) {
var cinstances []CloudInstance

Expand All @@ -532,46 +543,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 64819bb

Please sign in to comment.