Skip to content

Commit

Permalink
Added volume snapshot querying
Browse files Browse the repository at this point in the history
  • Loading branch information
jdenoy committed Jan 5, 2024
1 parent 6be1505 commit b052201
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 15 deletions.
32 changes: 32 additions & 0 deletions docs/tables/ovh_cloud_volume_snapshot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Table: ovh_cloud_volume_snapshot

A volume is an independent additional disk.

The `ovh_cloud_volume_snapshot` table can be used to query information about volumes snapshots and **you must specify which cloud project** in the where or join clause (`where project_id=`, `join ovh_cloud_project on id=`).

## Examples

### List volumes snapshots of a cloud project

```sql
select
id,
name
from
ovh_cloud_volume_snapshot
where
project_id='27c5a6d3dfez87893jfd88fdsfmvnqb8'
```

### List available volumes snapshots of a cloud project

```sql
select
id,
name
from
ovh_cloud_volume_snapshot
where
project_id='27c5a6d3dfez87893jfd88fdsfmvnqb8'
and status = 'available'
```
31 changes: 16 additions & 15 deletions ovh/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ func Plugin(ctx context.Context) *plugin.Plugin {
Schema: ConfigSchema,
},
TableMap: map[string]*plugin.Table{
"ovh_bill": tableOvhBill(),
"ovh_bill_detail": tableOvhBillDetails(),
"ovh_cloud_ai_app": tableOvhCloudAIApp(),
"ovh_cloud_ai_job": tableOvhCloudAIJob(),
"ovh_cloud_ai_notebook": tableOvhCloudAINotebook(),
"ovh_cloud_data_job": tableOvhCloudDataJob(),
"ovh_cloud_database": tableOvhCloudDatabase(),
"ovh_cloud_flavor": tableOvhCloudFlavor(),
"ovh_cloud_image": tableOvhCloudImage(),
"ovh_cloud_instance": tableOvhCloudInstance(),
"ovh_cloud_postgres": tableOvhCloudPostgres(),
"ovh_cloud_project": tableOvhCloudProject(),
"ovh_cloud_ssh_key": tableOvhCloudSshKey(),
"ovh_cloud_storage": tableOvhCloudStorage(),
"ovh_cloud_volume": tableOvhCloudVolume(),
"ovh_bill": tableOvhBill(),
"ovh_bill_detail": tableOvhBillDetails(),
"ovh_cloud_ai_app": tableOvhCloudAIApp(),
"ovh_cloud_ai_job": tableOvhCloudAIJob(),
"ovh_cloud_ai_notebook": tableOvhCloudAINotebook(),
"ovh_cloud_data_job": tableOvhCloudDataJob(),
"ovh_cloud_database": tableOvhCloudDatabase(),
"ovh_cloud_flavor": tableOvhCloudFlavor(),
"ovh_cloud_image": tableOvhCloudImage(),
"ovh_cloud_instance": tableOvhCloudInstance(),
"ovh_cloud_postgres": tableOvhCloudPostgres(),
"ovh_cloud_project": tableOvhCloudProject(),
"ovh_cloud_ssh_key": tableOvhCloudSshKey(),
"ovh_cloud_storage": tableOvhCloudStorage(),
"ovh_cloud_volume": tableOvhCloudVolume(),
"ovh_cloud_volume_snapshot": tableOvhCloudVolumeSnapshot(),
},
}
return p
Expand Down
129 changes: 129 additions & 0 deletions ovh/table_ovh_cloud_volume_snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package ovh

import (
"context"
"fmt"
"time"

"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"
)

func tableOvhCloudVolumeSnapshot() *plugin.Table {
return &plugin.Table{
Name: "ovh_cloud_volume_snapshot",
Description: "A volume is an independent additional disk.",
List: &plugin.ListConfig{
KeyColumns: plugin.SingleColumn("project_id"),
Hydrate: listVolumeSnapshot,
},
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"project_id", "id"}),
Hydrate: getVolumeSnapshot,
},
Columns: []*plugin.Column{
{
Name: "project_id",
Type: proto.ColumnType_STRING,
Transform: transform.FromQual("project_id"),
Description: "Project ID.",
},
{
Name: "id",
Type: proto.ColumnType_STRING,
Description: "ID.",
},
{
Name: "creationDate",
Type: proto.ColumnType_TIMESTAMP,
Description: "Volume creation date.",
Transform: transform.FromField("CreationDate"),
},
{
Name: "name",
Type: proto.ColumnType_STRING,
Description: "Volume Snapshot Name.",
},
{
Name: "description",
Type: proto.ColumnType_STRING,
Description: "Volume Snapshot Description",
},
{
Name: "size",
Type: proto.ColumnType_INT,
Description: "Volume Snapshot size (in GB).",
},
{
Name: "volumeId",
Type: proto.ColumnType_STRING,
Description: "Volume Snapshot ID.",
Transform: transform.FromField("VolumeId"),
},
{
Name: "region",
Type: proto.ColumnType_STRING,
Description: "Volume Snapshot Region.",
},
{
Name: "status",
Type: proto.ColumnType_STRING,
Description: "Volume Snapshot Status. (available, creating, deleting, error, error_deleting)",
},
{
Name: "planCode",
Type: proto.ColumnType_STRING,
Description: "Volume Snapshot Plan Code",
},
},
}
}

type VolumeSnapShot struct {
ID string `json:"id"`
CreationDate time.Time `json:"creationDate"`
Name string `json:"name"`
Description string `json:"description"`
Size int `json:"size"`
VolumeId string `json:"volumeId"`
Region string `json:"region"`
Status string `json:"status"`
PlanCode string `json:"planCode"`
}

func listVolumeSnapshot(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
client, err := connect(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("ovh_cloud_volume.listVolumeSnapshot", "connection_error", err)
return nil, err
}
projectId := d.EqualsQuals["project_id"].GetStringValue()
var volumes []VolumeSnapShot
err = client.Get(fmt.Sprintf("/cloud/project/%s/volume/snapshot", projectId), &volumes)
if err != nil {
plugin.Logger(ctx).Error("ovh_cloud_volume.listVolumeSnapshot", err)
return nil, err
}
for _, volume := range volumes {
d.StreamListItem(ctx, volume)
}
return nil, nil
}

func getVolumeSnapshot(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
client, err := connect(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("ovh_cloud_volume.getVolumeSnapshot", "connection_error", err)
return nil, err
}
projectId := d.EqualsQuals["project_id"].GetStringValue()
id := d.EqualsQuals["id"].GetStringValue()
var volume VolumeSnapShot
err = client.Get(fmt.Sprintf("/cloud/project/%s/volume/snapshot/%s", projectId, id), &volume)
if err != nil {
plugin.Logger(ctx).Error("ovh_cloud_volume.getVolumeSnapshot", err)
return nil, err
}
return volume, nil
}

0 comments on commit b052201

Please sign in to comment.