-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from vantage-sh/andy/resource_reports
feat: Resource reports data source.
- Loading branch information
Showing
9 changed files
with
266 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "vantage_resource_reports Data Source - terraform-provider-vantage" | ||
subcategory: "" | ||
description: |- | ||
--- | ||
|
||
# vantage_resource_reports (Data Source) | ||
|
||
|
||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Read-Only | ||
|
||
- `resource_reports` (Attributes List) (see [below for nested schema](#nestedatt--resource_reports)) | ||
|
||
<a id="nestedatt--resource_reports"></a> | ||
### Nested Schema for `resource_reports` | ||
|
||
Read-Only: | ||
|
||
- `created_at` (String) | ||
- `default` (Boolean) | ||
- `segment_token` (String) | ||
- `title` (String) | ||
- `token` (String) | ||
- `user_token` (String) | ||
- `workspace_token` (String) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
examples/data_sources/vantage_resource_reports/data_source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
data "vantage_resource_reports" "reports" { | ||
} | ||
|
||
output "reports" { | ||
value = data.vantage_resource_reports.reports | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package vantage | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
resourcereportsv2 "github.com/vantage-sh/vantage-go/vantagev2/vantage/resource_reports" | ||
) | ||
|
||
var ( | ||
_ datasource.DataSource = &resourceReportsDataSource{} | ||
_ datasource.DataSourceWithConfigure = &resourceReportsDataSource{} | ||
) | ||
|
||
func NewResourceReportsDataSource() datasource.DataSource { | ||
return &resourceReportsDataSource{} | ||
} | ||
|
||
type resourceReportDataSourceModel struct { | ||
Token types.String `tfsdk:"token"` | ||
Title types.String `tfsdk:"title"` | ||
UserToken types.String `tfsdk:"user_token"` | ||
WorkspaceToken types.String `tfsdk:"workspace_token"` | ||
Default types.Bool `tfsdk:"default"` | ||
CreatedAt types.String `tfsdk:"created_at"` | ||
SegmentToken types.String `tfsdk:"segment_token"` | ||
} | ||
|
||
type resourceReportsDataSourceModel struct { | ||
ResourceReports []resourceReportDataSourceModel `tfsdk:"resource_reports"` | ||
} | ||
|
||
type resourceReportsDataSource struct { | ||
client *Client | ||
} | ||
|
||
// Configure implements datasource.DataSourceWithConfigure. | ||
func (r *resourceReportsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
r.client = req.ProviderData.(*Client) | ||
} | ||
|
||
// Metadata implements datasource.DataSource. | ||
func (r *resourceReportsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_resource_reports" | ||
} | ||
|
||
// Read implements datasource.DataSource. | ||
func (r *resourceReportsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var state resourceReportsDataSourceModel | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...) | ||
params := resourcereportsv2.NewGetResourceReportsParams() | ||
out, err := r.client.V2.ResourceReports.GetResourceReports(params, r.client.Auth) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Unable to Get Vantage Resource Reports", | ||
err.Error(), | ||
) | ||
return | ||
} | ||
|
||
for _, report := range out.Payload.ResourceReports { | ||
state.ResourceReports = append(state.ResourceReports, resourceReportDataSourceModel{ | ||
Token: types.StringValue(report.Token), | ||
Title: types.StringValue(report.Title), | ||
UserToken: types.StringValue(report.UserToken), | ||
WorkspaceToken: types.StringValue(report.WorkspaceToken), | ||
Default: types.BoolValue(report.Default), | ||
CreatedAt: types.StringValue(report.CreatedAt), | ||
SegmentToken: types.StringValue(report.SegmentToken), | ||
}) | ||
} | ||
|
||
diags := resp.State.Set(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
// Schema implements datasource.DataSource. | ||
func (r *resourceReportsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"resource_reports": schema.ListNestedAttribute{ | ||
NestedObject: schema.NestedAttributeObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"token": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"title": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"user_token": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"workspace_token": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"segment_token": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"created_at": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"default": schema.BoolAttribute{ | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package vantage | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/terraform" | ||
"github.com/vantage-sh/terraform-provider-vantage/vantage/acctest" | ||
) | ||
|
||
func TestAccVantageResourceReportsDataSource_basic(t *testing.T) { | ||
resourceName := "data.vantage_resource_reports.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acctest.PreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccExampleReportsDataSourceConfig, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
testAccVantageCheckReportsExist(resourceName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccVantageCheckReportsExist(resourceName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
reports, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", resourceName) | ||
} | ||
numReports, err := strconv.Atoi(reports.Primary.Attributes["resource_reports.#"]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if numReports > 0 { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Reports not found") | ||
} | ||
} | ||
|
||
const testAccExampleReportsDataSourceConfig = ` | ||
data "vantage_resource_reports" "test" {} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters