Skip to content

Commit

Permalink
✨ Add zones, state, isRunning properties to azure compute VMs. (#3073)
Browse files Browse the repository at this point in the history
Signed-off-by: Preslav <[email protected]>
  • Loading branch information
preslavgerchev authored Jan 30, 2024
1 parent afec124 commit 8cb8985
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
6 changes: 6 additions & 0 deletions providers/azure/resources/azure.lr
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ azure.subscription.computeService.vm @defaults("name location properties.hardwar
name string
// VM location
location string
// VM zones
zones []string
// VM state
state() string
// Indicates if the VM is running
isRunning() bool
// VM tags
tags map[string]string
// VM type
Expand Down
40 changes: 40 additions & 0 deletions providers/azure/resources/azure.lr.go

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

3 changes: 3 additions & 0 deletions providers/azure/resources/azure.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,16 @@ resources:
dataDisks: {}
extensions: {}
id: {}
isRunning: {}
location: {}
name: {}
osDisk: {}
properties: {}
publicIpAddresses: {}
state: {}
tags: {}
type: {}
zones: {}
min_mondoo_version: latest
platform:
name:
Expand Down
61 changes: 60 additions & 1 deletion providers/azure/resources/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ func (a *mqlAzureSubscriptionComputeService) id() (string, error) {
return "azure.subscription.compute/" + a.SubscriptionId.Data, nil
}

func getState(vm compute.VirtualMachineInstanceView) string {
if vm.Statuses == nil {
return "unknown"
}
state := "unknown"
for _, s := range vm.Statuses {
if s.Code != nil && *s.Code == "PowerState/running" {
state = "running"
}
if s.Code != nil && *s.Code == "PowerState/deallocated" {
state = "stopped"
}
}
return state
}

func initAzureSubscriptionComputeService(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
if len(args) > 0 {
return args, nil, nil
Expand Down Expand Up @@ -65,6 +81,7 @@ func (a *mqlAzureSubscriptionComputeService) vms() ([]interface{}, error) {
"id": llx.StringData(convert.ToString(vm.ID)),
"name": llx.StringData(convert.ToString(vm.Name)),
"location": llx.StringData(convert.ToString(vm.Location)),
"zones": llx.ArrayData(convert.SliceStrPtrToInterface(vm.Zones), types.String),
"tags": llx.MapData(convert.PtrMapStrToInterface(vm.Tags), types.String),
"type": llx.StringData(convert.ToString(vm.Type)),
"properties": llx.DictData(properties),
Expand All @@ -79,6 +96,48 @@ func (a *mqlAzureSubscriptionComputeService) vms() ([]interface{}, error) {
return res, nil
}

func (a *mqlAzureSubscriptionComputeServiceVm) state() (string, error) {
conn := a.MqlRuntime.Connection.(*connection.AzureConnection)
// id is a Azure resource ID
id := a.Id.Data
resourceID, err := ParseResourceID(id)
if err != nil {
return "", err
}

vm, err := resourceID.Component("virtualMachines")
if err != nil {
return "", err
}

ctx := context.Background()
token := conn.Token()
if err != nil {
return "", err
}

client, err := compute.NewVirtualMachinesClient(resourceID.SubscriptionID, token, &arm.ClientOptions{
ClientOptions: conn.ClientOptions(),
})
if err != nil {
return "", err
}

view, err := client.InstanceView(ctx, resourceID.ResourceGroup, vm, &compute.VirtualMachinesClientInstanceViewOptions{})
if err != nil {
return "", err
}
return getState(view.VirtualMachineInstanceView), nil
}

func (a *mqlAzureSubscriptionComputeServiceVm) isRunning() (bool, error) {
state := a.GetState()
if state.Error != nil {
return false, state.Error
}
return state.Data == "running", nil
}

func (a *mqlAzureSubscriptionComputeServiceVm) extensions() ([]interface{}, error) {
conn := a.MqlRuntime.Connection.(*connection.AzureConnection)
// id is a Azure resource ID
Expand Down Expand Up @@ -274,7 +333,7 @@ func (a *mqlAzureSubscriptionComputeServiceVm) dataDisks() ([]interface{}, error
}

if properties.StorageProfile == nil || properties.StorageProfile.DataDisks == nil {
return nil, errors.New("could not determine os disk from vm storage profile")
return nil, errors.New("could not determine data disks from vm storage profile")
}

dataDisks := properties.StorageProfile.DataDisks
Expand Down

0 comments on commit 8cb8985

Please sign in to comment.