Skip to content

Commit

Permalink
[datadog_dashboard_list] Migrate to tf framework (#2038)
Browse files Browse the repository at this point in the history
* Create dashboard_list data source for framework

* remove old datasource, migrate test, fix datasource

* Remove datasource from old provider

* Use testAccCheckDatadogDashListDestroyWithFW

* Adds id in Datasource

* Adding Create for Dashboard list resource

* Finish resource methods

* Update desc

* Remove potential segfault

* remove original resource

* remove from old provider

* Update dashboard list json test

* simplify test

* update state and cassettes

* Update cassettes

* update cassettes

* update update_state

* remove manually listed dashboard types

* remove space
  • Loading branch information
HantingZhang2 authored Aug 4, 2023
1 parent 0c17a95 commit c8bfa05
Show file tree
Hide file tree
Showing 17 changed files with 609 additions and 686 deletions.
64 changes: 0 additions & 64 deletions datadog/data_source_datadog_dashboard_list.go

This file was deleted.

95 changes: 95 additions & 0 deletions datadog/fwprovider/data_source_datadog_dashboard_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package fwprovider

import (
"context"
"strconv"

"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
)

var (
_ datasource.DataSource = &datadogDashboardListDatasource{}
)

func NewDatadogDashboardListDataSource() datasource.DataSource {
return &datadogDashboardListDatasource{}
}

type datadogDashboardListDatasourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}

type datadogDashboardListDatasource struct {
Api *datadogV1.DashboardListsApi
Auth context.Context
}

func (d *datadogDashboardListDatasource) Configure(_ context.Context, request datasource.ConfigureRequest, response *datasource.ConfigureResponse) {
providerData, _ := request.ProviderData.(*FrameworkProvider)
d.Api = providerData.DatadogApiInstances.GetDashboardListsApiV1()
d.Auth = providerData.Auth
}

func (d *datadogDashboardListDatasource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "dashboard_list"
}

func (d *datadogDashboardListDatasource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Use this data source to retrieve information about an existing dashboard list, for use in other resources. In particular, it can be used in a dashboard to register it in the list.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "A dashboard list name to limit the search.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
},
"id": utils.ResourceIDAttribute(),
},
}
}

func (d *datadogDashboardListDatasource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state datadogDashboardListDatasourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

listResponse, httpresp, err := d.Api.ListDashboardLists(d.Auth)
if err != nil {
resp.Diagnostics.Append(utils.FrameworkErrorDiag(utils.TranslateClientError(err, httpresp, ""), "error querying dashboard lists"))
return
}
if err := utils.CheckForUnparsed(listResponse); err != nil {
resp.Diagnostics.Append(utils.FrameworkErrorDiag(err, ""))
}

searchedName := state.Name.ValueString()
var foundList *datadogV1.DashboardList

for _, dashList := range listResponse.GetDashboardLists() {
if dashList.GetName() == searchedName {
foundList = &dashList
break
}
}

if foundList == nil {
errString := "Couldn't find a dashboard list named" + searchedName
resp.Diagnostics.AddError(errString, "")
return
}

id := foundList.GetId()
state.ID = types.StringValue(strconv.Itoa(int(id)))
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
2 changes: 2 additions & 0 deletions datadog/fwprovider/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (

var Resources = []func() resource.Resource{
NewAPIKeyResource,
NewDashboardListResource,
NewDowntimeScheduleResource,
NewIntegrationCloudflareAccountResource,
NewIntegrationConfluentAccountResource,
Expand All @@ -52,6 +53,7 @@ var Resources = []func() resource.Resource{

var Datasources = []func() datasource.DataSource{
NewAPIKeyDataSource,
NewDatadogDashboardListDataSource,
NewDatadogIntegrationAWSNamespaceRulesDatasource,
NewDatadogServiceAccountDatasource,
NewDatadogTeamDataSource,
Expand Down
Loading

0 comments on commit c8bfa05

Please sign in to comment.