Skip to content

Commit

Permalink
Add table azure_backup_jobs Closes #663
Browse files Browse the repository at this point in the history
  • Loading branch information
ParthaI committed Oct 5, 2023
1 parent 982083d commit 4557e29
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions azure/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"azure_application_security_group": tableAzureApplicationSecurityGroup(ctx),
"azure_automation_account": tableAzureApAutomationAccount(ctx),
"azure_automation_variable": tableAzureApAutomationVariable(ctx),
"azure_backup_job": tableAzureBackupJob(ctx),
"azure_bastion_host": tableAzureBastionHost(ctx),
"azure_batch_account": tableAzureBatchAccount(ctx),
"azure_cognitive_account": tableAzureCognitiveAccount(ctx),
Expand Down
166 changes: 166 additions & 0 deletions azure/table_azure_backup_job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package azure

import (
"context"

"github.com/Azure/azure-sdk-for-go/profiles/latest/resources/mgmt/resources"
"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2021-01-01/backup"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)

//// TABLE DEFINITION ////

func tableAzureBackupJob(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "azure_backup_job",
Description: "Azure Backup Job",
List: &plugin.ListConfig{
ParentHydrate: listResourceGroups,
Hydrate: listAzureBackupJobs,
KeyColumns: plugin.KeyColumnSlice{
{
Name: "vault_name",
Require: plugin.Required,
},
{
Name: "resource_group",
Require: plugin.Optional,
},
},
},
Columns: azureColumns([]*plugin.Column{
{
Name: "name",
Description: "Resource name associated with the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "vault_name",
Description: "The recovery vault name.",
Type: proto.ColumnType_STRING,
},
{
Name: "id",
Description: "Resource Id represents the complete path to the resource.",
Type: proto.ColumnType_STRING,
Transform: transform.FromGo(),
},
{
Name: "type",
Description: "Resource type represents the complete path of the form Namespace/ResourceType/ResourceType/...",
Type: proto.ColumnType_STRING,
},
{
Name: "type",
Description: "Resource type represents the complete path of the form Namespace/ResourceType/ResourceType/...",
Type: proto.ColumnType_STRING,
},
{
Name: "etag",
Description: "Optional ETag.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ETag"),
},
{
Name: "time_created",
Description: "The time when the disk was created",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromField("DiskProperties.TimeCreated").Transform(convertDateToTime),
},

// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "tags",
Description: ColumnDescriptionTags,
Type: proto.ColumnType_JSON,
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Transform: transform.FromField("ID").Transform(idToAkas),
},

// Azure standard columns
{
Name: "region",
Description: ColumnDescriptionRegion,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Location").Transform(toLower),
},
{
Name: "resource_group",
Description: ColumnDescriptionResourceGroup,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(extractResourceGroupFromID),
},
}),
}
}

//// LIST FUNCTION ////

func listAzureBackupJobs(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}

resourceGroup := h.Item.(resources.Group)

vaultName := d.EqualsQualString("vault_name")
rgName := d.EqualsQualString("resource_group")

if vaultName == "" {
return nil, nil
}

if rgName != "" {
if rgName != *resourceGroup.Name {
return nil, nil
}
}

subscriptionID := session.SubscriptionID
client := backup.NewJobsClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
client.Authorizer = session.Authorizer
result, err := client.List(ctx, vaultName, *resourceGroup.Name, "", "")
if err != nil {
return nil, err
}

for _, job := range result.Values() {
d.StreamListItem(ctx, job)
// Check if context has been cancelled or if the limit has been hit (if specified)
// if there is a limit, it will return the number of rows required to reach this limit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}

for result.NotDone() {
err = result.NextWithContext(ctx)
if err != nil {
return nil, err
}

for _, job := range result.Values() {
d.StreamListItem(ctx, job)
// Check if context has been cancelled or if the limit has been hit (if specified)
// if there is a limit, it will return the number of rows required to reach this limit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}

return nil, nil
}

0 comments on commit 4557e29

Please sign in to comment.