-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[datadog_dashboard_list] Migrate to tf framework (#2038)
* 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
1 parent
0c17a95
commit c8bfa05
Showing
17 changed files
with
609 additions
and
686 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,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)...) | ||
} |
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
Oops, something went wrong.