-
Notifications
You must be signed in to change notification settings - Fork 2
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 #101 from Juniper/data-source-routing-zones
Introduce `data.apstra_datacenter_routing_zones`
- Loading branch information
Showing
8 changed files
with
318 additions
and
11 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
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,115 @@ | ||
package tfapstra | ||
|
||
import ( | ||
"context" | ||
"github.com/Juniper/apstra-go-sdk/apstra" | ||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"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/hashicorp/terraform-plugin-framework/types/basetypes" | ||
"terraform-provider-apstra/apstra/blueprint" | ||
) | ||
|
||
var _ datasource.DataSourceWithConfigure = &dataSourceDatacenterRoutingZones{} | ||
|
||
type dataSourceDatacenterRoutingZones struct { | ||
client *apstra.Client | ||
} | ||
|
||
func (o *dataSourceDatacenterRoutingZones) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_datacenter_routing_zones" | ||
} | ||
|
||
func (o *dataSourceDatacenterRoutingZones) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
o.client = DataSourceGetClient(ctx, req, resp) | ||
} | ||
|
||
func (o *dataSourceDatacenterRoutingZones) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
MarkdownDescription: "This data source returns the IDs of Routing Zones within the specified Blueprint. " + | ||
"All of the `filters` attributes are optional.", | ||
Attributes: map[string]schema.Attribute{ | ||
"blueprint_id": schema.StringAttribute{ | ||
MarkdownDescription: "Apstra Blueprint ID.", | ||
Required: true, | ||
Validators: []validator.String{stringvalidator.LengthAtLeast(1)}, | ||
}, | ||
"ids": schema.SetAttribute{ | ||
MarkdownDescription: "Set of Routing Zone IDs", | ||
Computed: true, | ||
ElementType: types.StringType, | ||
}, | ||
"filters": schema.SingleNestedAttribute{ | ||
MarkdownDescription: "Routing Zone attributes used as filters", | ||
Optional: true, | ||
Attributes: blueprint.DatacenterRoutingZone{}.DataSourceFilterAttributes(), | ||
}, | ||
"graph_query": schema.StringAttribute{ | ||
MarkdownDescription: "The graph datastore query used to perform the lookup.", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (o *dataSourceDatacenterRoutingZones) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
if o.client == nil { | ||
resp.Diagnostics.AddError(errDataSourceUnconfiguredSummary, errDatasourceUnconfiguredDetail) | ||
return | ||
} | ||
|
||
type routingZones struct { | ||
BlueprintId types.String `tfsdk:"blueprint_id"` | ||
IDs types.Set `tfsdk:"ids"` | ||
Filters types.Object `tfsdk:"filters"` | ||
Query types.String `tfsdk:"graph_query"` | ||
} | ||
|
||
var config routingZones | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
filters := &blueprint.DatacenterRoutingZone{} | ||
d := config.Filters.As(ctx, &filters, basetypes.ObjectAsOptions{}) | ||
resp.Diagnostics.Append(d...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
query := filters.Query("n_security_zone", "n_policy") | ||
|
||
queryResponse := new(struct { | ||
Items []struct { | ||
SecurityZone struct { | ||
Id string `json:"id"` | ||
} `json:"n_security_zone"` | ||
} `json:"items"` | ||
}) | ||
|
||
query. | ||
SetClient(o.client). | ||
SetBlueprintId(apstra.ObjectId(config.BlueprintId.ValueString())). | ||
SetBlueprintType(apstra.BlueprintTypeStaging) | ||
|
||
err := query.Do(ctx, queryResponse) | ||
if err != nil { | ||
resp.Diagnostics.AddError("error querying graph datastore", err.Error()) | ||
return | ||
} | ||
|
||
ids := make([]attr.Value, len(queryResponse.Items)) | ||
for i, item := range queryResponse.Items { | ||
ids[i] = types.StringValue(item.SecurityZone.Id) | ||
} | ||
|
||
config.IDs = types.SetValueMust(types.StringType, ids) | ||
config.Query = types.StringValue(query.String()) | ||
|
||
// set state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &config)...) | ||
} |
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,67 @@ | ||
--- | ||
page_title: "apstra_datacenter_routing_zones Data Source - terraform-provider-apstra" | ||
subcategory: "" | ||
description: |- | ||
This data source returns the IDs of Routing Zones within the specified Blueprint. All of the filters attributes are optional. | ||
--- | ||
|
||
# apstra_datacenter_routing_zones (Data Source) | ||
|
||
This data source returns the IDs of Routing Zones within the specified Blueprint. All of the `filters` attributes are optional. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
# By specifying no filters, a wide search is performed. All routing | ||
# zones in the blueprint will match. | ||
data "apstra_datacenter_routing_zones" "all" { | ||
blueprint_id = "05f9d3fc-671a-4efc-8e91-5ef87b2937d3" | ||
} | ||
# This example performs a very narrow search. Only one (or zero!) | ||
# routing zones can match the resulting query. | ||
data "apstra_datacenter_routing_zones" "rzs" { | ||
blueprint_id = "05f9d3fc-671a-4efc-8e91-5ef87b2937d3" | ||
filters = { # all filters are optional | ||
"name" = "customer_1" | ||
"vlan_id" = 55 | ||
"vni" = 10055 | ||
dhcp_servers = ["192.168.5.100", "192.168.10.100"] | ||
routing_policy_id = "vqsv3F93MBHUgg5e8ws" | ||
} | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `blueprint_id` (String) Apstra Blueprint ID. | ||
|
||
### Optional | ||
|
||
- `filters` (Attributes) Routing Zone attributes used as filters (see [below for nested schema](#nestedatt--filters)) | ||
|
||
### Read-Only | ||
|
||
- `graph_query` (String) The graph datastore query used to perform the lookup. | ||
- `ids` (Set of String) Set of Routing Zone IDs | ||
|
||
<a id="nestedatt--filters"></a> | ||
### Nested Schema for `filters` | ||
|
||
Optional: | ||
|
||
- `dhcp_servers` (Set of String) Set of DHCP server IPv4 or IPv6 addresses of DHCP servers which must be configured in the Routing Zone. This is a list of *required* servers, not an exact-match list. | ||
- `name` (String) VRF name displayed in thw Apstra web UI. | ||
- `routing_policy_id` (String) Non-EVPN blueprints must use the default policy, so this field must be null. Set this attribute in an EVPN blueprint to use a non-default policy. | ||
- `vlan_id` (Number) Used for VLAN tagged Layer 3 links on external connections. | ||
- `vni` (Number) VxLAN VNI associated with the routing zone. | ||
|
||
Read-Only: | ||
|
||
- `blueprint_id` (String) Not applicable in filter context. Ignore. | ||
- `had_prior_vlan_id_config` (Boolean) Not applicable in filter context. Ignore. | ||
- `had_prior_vni_config` (Boolean) Not applicable in filter context. Ignore. | ||
- `id` (String) Not applicable in filter context. Ignore. |
18 changes: 18 additions & 0 deletions
18
examples/data-sources/apstra_datacenter_routing_zones/example.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,18 @@ | ||
# By specifying no filters, a wide search is performed. All routing | ||
# zones in the blueprint will match. | ||
data "apstra_datacenter_routing_zones" "all" { | ||
blueprint_id = "05f9d3fc-671a-4efc-8e91-5ef87b2937d3" | ||
} | ||
|
||
# This example performs a very narrow search. Only one (or zero!) | ||
# routing zones can match the resulting query. | ||
data "apstra_datacenter_routing_zones" "rzs" { | ||
blueprint_id = "05f9d3fc-671a-4efc-8e91-5ef87b2937d3" | ||
filters = { # all filters are optional | ||
"name" = "customer_1" | ||
"vlan_id" = 55 | ||
"vni" = 10055 | ||
dhcp_servers = ["192.168.5.100", "192.168.10.100"] | ||
routing_policy_id = "vqsv3F93MBHUgg5e8ws" | ||
} | ||
} |
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