From 0658eef5d8aa8ed8fdd919b42bccd396dfe9a9e8 Mon Sep 17 00:00:00 2001 From: danischm Date: Mon, 8 Jan 2024 17:34:26 +0100 Subject: [PATCH] Add name query option to network profile data source --- CHANGELOG.md | 1 + docs/data-sources/network_profile.md | 4 +- docs/guides/changelog.md | 1 + gen/definitions/network_profile.yaml | 1 + gen/templates/data_source.go | 3 +- ...a_source_catalystcenter_network_profile.go | 38 ++++++++++++++++++- templates/guides/changelog.md.tmpl | 1 + 7 files changed, 45 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50b1b577..68cf66ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Add `name` query option to `catalystcenter_project` data source - Add `name` query option to `catalystcenter_template` data source - Add `name` query option to `catalystcenter_area` and `catalystcenter_building` data source +- Add `name` query option to `catalystcenter_network_profile` data source ## 0.1.1 diff --git a/docs/data-sources/network_profile.md b/docs/data-sources/network_profile.md index 5e7ff574..15efd2f6 100644 --- a/docs/data-sources/network_profile.md +++ b/docs/data-sources/network_profile.md @@ -21,13 +21,13 @@ data "catalystcenter_network_profile" "example" { ## Schema -### Required +### Optional - `id` (String) The id of the object +- `name` (String) The name of the network profile ### Read-Only -- `name` (String) The name of the network profile - `templates` (Attributes List) (see [below for nested schema](#nestedatt--templates)) - `type` (String) Profile type diff --git a/docs/guides/changelog.md b/docs/guides/changelog.md index bf7a054b..12ee7018 100644 --- a/docs/guides/changelog.md +++ b/docs/guides/changelog.md @@ -17,6 +17,7 @@ description: |- - Add `name` query option to `catalystcenter_project` data source - Add `name` query option to `catalystcenter_template` data source - Add `name` query option to `catalystcenter_area` and `catalystcenter_building` data source +- Add `name` query option to `catalystcenter_network_profile` data source ## 0.1.1 diff --git a/gen/definitions/network_profile.yaml b/gen/definitions/network_profile.yaml index b2ce53a3..e283db13 100644 --- a/gen/definitions/network_profile.yaml +++ b/gen/definitions/network_profile.yaml @@ -10,6 +10,7 @@ attributes: response_data_path: response.name type: String id: true + data_source_query: true description: The name of the network profile example: Profile1 - model_name: namespace diff --git a/gen/templates/data_source.go b/gen/templates/data_source.go index 629cb701..0c2a32c0 100644 --- a/gen/templates/data_source.go +++ b/gen/templates/data_source.go @@ -185,6 +185,7 @@ func (d *{{camelCase .Name}}DataSource) Read(ctx context.Context, req datasource {{- if $dataSourceQuery}} {{- $getFromAllPath := getFromAllPath .}} + {{- $idFromQueryPathAttribute := .IdFromQueryPathAttribute}} {{- range .Attributes}} {{- if .DataSourceQuery}} if config.Id.IsNull() && !config.{{toGoName .TfName}}.IsNull() { @@ -196,7 +197,7 @@ func (d *{{camelCase .Name}}DataSource) Read(ctx context.Context, req datasource if value := res{{if .ResponseDataPath}}.Get("{{firstPathElement .ResponseDataPath $getFromAllPath}}"){{end}}; len(value.Array()) > 0 { value.ForEach(func(k, v gjson.Result) bool { if config.{{toGoName .TfName}}.ValueString() == v.Get("{{if .ResponseDataPath}}{{remainingPathElements .ResponseDataPath $getFromAllPath}}{{else}}{{if .DataPath}}{{.DataPath}}.{{end}}{{.ModelName}}{{end}}").String() { - config.Id = types.StringValue(v.Get("id").String()) + config.Id = types.StringValue(v.Get("{{if $idFromQueryPathAttribute}}{{$idFromQueryPathAttribute}}{{else}}id{{end}}").String()) tflog.Debug(ctx, fmt.Sprintf("%s: Found object with {{.ModelName}} '%v', id: %v", config.Id.String(), config.{{toGoName .TfName}}.ValueString(), config.Id.String())) return false } diff --git a/internal/provider/data_source_catalystcenter_network_profile.go b/internal/provider/data_source_catalystcenter_network_profile.go index 1b613122..eee72bb2 100644 --- a/internal/provider/data_source_catalystcenter_network_profile.go +++ b/internal/provider/data_source_catalystcenter_network_profile.go @@ -24,10 +24,14 @@ import ( "context" "fmt" + "github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/path" + "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" cc "github.com/netascode/go-catalystcenter" + "github.com/tidwall/gjson" ) //template:end imports @@ -60,10 +64,12 @@ func (d *NetworkProfileDataSource) Schema(ctx context.Context, req datasource.Sc Attributes: map[string]schema.Attribute{ "id": schema.StringAttribute{ MarkdownDescription: "The id of the object", - Required: true, + Optional: true, + Computed: true, }, "name": schema.StringAttribute{ MarkdownDescription: "The name of the network profile", + Optional: true, Computed: true, }, "type": schema.StringAttribute{ @@ -89,6 +95,14 @@ func (d *NetworkProfileDataSource) Schema(ctx context.Context, req datasource.Sc }, } } +func (d *NetworkProfileDataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator { + return []datasource.ConfigValidator{ + datasourcevalidator.ExactlyOneOf( + path.MatchRoot("id"), + path.MatchRoot("name"), + ), + } +} func (d *NetworkProfileDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { if req.ProviderData == nil { @@ -112,6 +126,28 @@ func (d *NetworkProfileDataSource) Read(ctx context.Context, req datasource.Read } tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", config.Id.String())) + if config.Id.IsNull() && !config.Name.IsNull() { + res, err := d.client.Get(config.getPath()) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve objects, got error: %s", err)) + return + } + if value := res.Get("response"); len(value.Array()) > 0 { + value.ForEach(func(k, v gjson.Result) bool { + if config.Name.ValueString() == v.Get("name").String() { + config.Id = types.StringValue(v.Get("siteProfileUuid").String()) + tflog.Debug(ctx, fmt.Sprintf("%s: Found object with name '%v', id: %v", config.Id.String(), config.Name.ValueString(), config.Id.String())) + return false + } + return true + }) + } + + if config.Id.IsNull() { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to find object with name: %s", config.Name.ValueString())) + return + } + } params := "" params += "/" + config.Id.ValueString() diff --git a/templates/guides/changelog.md.tmpl b/templates/guides/changelog.md.tmpl index bf7a054b..12ee7018 100644 --- a/templates/guides/changelog.md.tmpl +++ b/templates/guides/changelog.md.tmpl @@ -17,6 +17,7 @@ description: |- - Add `name` query option to `catalystcenter_project` data source - Add `name` query option to `catalystcenter_template` data source - Add `name` query option to `catalystcenter_area` and `catalystcenter_building` data source +- Add `name` query option to `catalystcenter_network_profile` data source ## 0.1.1