Skip to content

Commit

Permalink
Merge pull request #35 from vantage-sh/andy/resource_reports
Browse files Browse the repository at this point in the history
feat: Resource reports data source.
  • Loading branch information
whereandy authored Apr 1, 2024
2 parents 7856f60 + 90b1d2f commit e2a8387
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 4 deletions.
35 changes: 35 additions & 0 deletions docs/data-sources/resource_reports.md
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)


18 changes: 18 additions & 0 deletions docs/data-sources/segments.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,22 @@ description: |-
<!-- schema generated by tfplugindocs -->
## Schema

### Read-Only

- `segments` (Attributes List) (see [below for nested schema](#nestedatt--segments))

<a id="nestedatt--segments"></a>
### Nested Schema for `segments`

Read-Only:

- `description` (String)
- `filter` (String)
- `parent_segment_token` (String)
- `priority` (Number)
- `title` (String)
- `token` (String)
- `track_unallocated` (Boolean)
- `workspace_token` (String)


6 changes: 6 additions & 0 deletions examples/data_sources/vantage_resource_reports/data_source.tf
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
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/hashicorp/terraform-plugin-docs v0.14.1
github.com/hashicorp/terraform-plugin-framework v1.5.0
github.com/hashicorp/terraform-plugin-testing v1.6.0
github.com/vantage-sh/vantage-go v0.0.10
github.com/vantage-sh/vantage-go v0.0.11
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/vantage-sh/vantage-go v0.0.10 h1:sfbTMoR0ND7YN7n7C4eXAgGQcxBdAVhNm0KUY+KhSrU=
github.com/vantage-sh/vantage-go v0.0.10/go.mod h1:MsSI4gX/Wvkzo9kqWphZfE6puolmnbHcXSaoGkDCmXg=
github.com/vantage-sh/vantage-go v0.0.11 h1:WZNiwZqbYCfffQ9qfc/rP1gFOC6a+l+Kt0ng5JUP914=
github.com/vantage-sh/vantage-go v0.0.11/go.mod h1:MsSI4gX/Wvkzo9kqWphZfE6puolmnbHcXSaoGkDCmXg=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
1 change: 1 addition & 0 deletions vantage/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (p *vantageProvider) DataSources(_ context.Context) []func() datasource.Dat
NewWorkspacesDataSource,
NewDashboardsDataSource,
NewSegmentsDataSource,
NewResourceReportsDataSource,
}
}

Expand Down
119 changes: 119 additions & 0 deletions vantage/resource_reports_data_source.go
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,
},
},
}
}
51 changes: 51 additions & 0 deletions vantage/resource_reports_data_source_test.go
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" {}
`
34 changes: 33 additions & 1 deletion vantage/segments_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,38 @@ func (d *segmentsDataSource) Read(ctx context.Context, req datasource.ReadReques

func (d *segmentsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{},
Attributes: map[string]schema.Attribute{
"segments": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"token": schema.StringAttribute{
Computed: true,
},
"title": schema.StringAttribute{
Computed: true,
},
"description": schema.StringAttribute{
Computed: true,
},
"parent_segment_token": schema.StringAttribute{
Computed: true,
},
"track_unallocated": schema.BoolAttribute{
Computed: true,
},
"priority": schema.Int64Attribute{
Computed: true,
},
"workspace_token": schema.StringAttribute{
Computed: true,
},
"filter": schema.StringAttribute{
Computed: true,
},
},
},
Computed: true,
},
},
}
}

0 comments on commit e2a8387

Please sign in to comment.