Skip to content

Commit eb261a6

Browse files
add catalystcenter_sites data source (#242)
1 parent fb0492b commit eb261a6

File tree

14 files changed

+570
-4
lines changed

14 files changed

+570
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.2.11 (unreleased)
2+
3+
- Add `catalystcenter_sites` data source to retrieve list of all sites
4+
15
## 0.2.10
26

37
- Add enhancement to `catalystcenter_pnp_device` resource, to skip error during DELETE operation, if pnp device was provisioned

docs/data-sources/site.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
page_title: "catalystcenter_site Data Source - terraform-provider-catalystcenter"
44
subcategory: "Sites"
55
description: |-
6-
This data source fetches sites
6+
This data source fetches site based on name hierarchy
77
---
88

99
# catalystcenter_site (Data Source)
1010

11-
This data source fetches sites
11+
This data source fetches site based on name hierarchy
1212

1313
## Example Usage
1414

docs/data-sources/sites.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "catalystcenter_sites Data Source - terraform-provider-catalystcenter"
4+
subcategory: "Sites"
5+
description: |-
6+
This data source fetches all sites defined on the Catalyst Center.
7+
Sites represent the logical and physical hierarchy of your network environment, including areas, buildings, and floors.
8+
You can optionally specify the type attribute to filter the results and retrieve only sites of a specific type—such as area, building or floor.
9+
To retrieve detailed information about a specific site, use the data source data.catalystcenter_site.
10+
---
11+
12+
# catalystcenter_sites (Data Source)
13+
14+
This data source fetches all sites defined on the Catalyst Center.
15+
16+
Sites represent the logical and physical hierarchy of your network environment, including areas, buildings, and floors.
17+
You can optionally specify the `type` attribute to filter the results and retrieve only sites of a specific type—such as `area`, `building` or `floor`.
18+
To retrieve detailed information about a specific site, use the data source `data.catalystcenter_site`.
19+
20+
## Example Usage
21+
22+
```terraform
23+
data "catalystcenter_sites" "example" {
24+
}
25+
```
26+
27+
<!-- schema generated by tfplugindocs -->
28+
## Schema
29+
30+
### Optional
31+
32+
- `type` (String) Site type (eg. area, building, floor)
33+
34+
### Read-Only
35+
36+
- `sites` (Attributes List) (see [below for nested schema](#nestedatt--sites))
37+
38+
<a id="nestedatt--sites"></a>
39+
### Nested Schema for `sites`
40+
41+
Read-Only:
42+
43+
- `address` (String) Building address
44+
- `country` (String) Country name for the building
45+
- `floor_number` (Number) Floor number
46+
- `height` (Number) Floor height
47+
- `id` (String)
48+
- `latitude` (String) Building latitude
49+
- `length` (Number) Floor length
50+
- `longitude` (String) Building longitude
51+
- `name` (String)
52+
- `name_hierarchy` (String)
53+
- `parent_id` (String) Parent Id
54+
- `rf_model` (String) Floor RF model
55+
- `type` (String)
56+
- `units_of_measure` (String) Floor unit of measure
57+
- `width` (Number) Floor width

docs/guides/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ description: |-
77

88
# Changelog
99

10+
## 0.2.11 (unreleased)
11+
12+
- Add `catalystcenter_sites` data source to retrieve list of all sites
13+
1014
## 0.2.10
1115

1216
- Add enhancement to `catalystcenter_pnp_device` resource, to skip error during DELETE operation, if pnp device was provisioned
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
data "catalystcenter_sites" "example" {
2+
}

gen/definitions/site.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
name: Site
33
rest_endpoint: /dna/intent/api/v1/sites
4-
ds_description: This data source fetches sites
4+
ds_description: This data source fetches site based on name hierarchy
55
data_source_no_id: true
66
id_from_query_path: response.0
77
no_resource: true

gen/definitions/sites.yaml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
name: Sites
3+
rest_endpoint: /dna/intent/api/v1/sites
4+
# manually managed data source
5+
ds_description:
6+
"This data source fetches all sites defined on the Catalyst Center.
7+
Sites represent the logical and physical hierarchy of your network environment, including areas, buildings, and floors. You can optionally specify the `type` attribute to filter the results and retrieve only sites of a specific type—such as `area`, `building` or `floor`.
8+
To retrieve detailed information about a specific site, use the data source `data.catalystcenter_site`."
9+
get_no_id: true
10+
no_resource: true
11+
no_data_source: true
12+
doc_category: Sites
13+
attributes:
14+
- model_name: type
15+
tf_name: type
16+
type: String
17+
description: Site type
18+
enum_values: [area, building, floor]
19+
computed: true
20+
- model_name: response
21+
tf_name: sites
22+
type: List
23+
attributes:
24+
- model_name: id
25+
type: String
26+
example: e0ba1a00-b69b-45aa-8c13-4cdfb59afe65
27+
- model_name: name
28+
type: String
29+
example: Poland
30+
- model_name: nameHierarchy
31+
type: String
32+
example: Global/Poland
33+
- model_name: type
34+
type: String
35+
example: area
36+
- model_name: parentId
37+
type: String
38+
description: Parent Id
39+
exclude_example: true
40+
- model_name: latitude
41+
type: String
42+
description: Building latitude
43+
exclude_example: true
44+
- model_name: longitude
45+
type: String
46+
description: Building longitude
47+
exclude_example: true
48+
- model_name: address
49+
type: String
50+
description: Building address
51+
exclude_example: true
52+
- model_name: country
53+
type: String
54+
description: Country name for the building
55+
exclude_example: true
56+
- model_name: floorNumber
57+
type: Int64
58+
description: Floor number
59+
exclude_example: true
60+
- model_name: rfModel
61+
type: String
62+
description: Floor RF model
63+
exclude_example: true
64+
- model_name: width
65+
type: Float64
66+
description: Floor width
67+
exclude_example: true
68+
- model_name: length
69+
type: Float64
70+
description: Floor length
71+
exclude_example: true
72+
- model_name: height
73+
type: Float64
74+
description: Floor height
75+
exclude_example: true
76+
- model_name: unitsOfMeasure
77+
type: String
78+
description: Floor unit of measure
79+
exclude_example: true

gen/templates/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ func (p *CcProvider) Resources(ctx context.Context) []func() resource.Resource {
280280
func (p *CcProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
281281
return []func() datasource.DataSource{
282282
NewNetworkDevicesDataSource, // manually maintained
283+
NewSitesDataSource, // manually maintained
283284
{{- range .}}
284285
{{- if not .NoDataSource}}
285286
New{{camelCase .Name}}DataSource,

internal/provider/data_source_catalystcenter_site.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (d *SiteDataSource) Metadata(_ context.Context, req datasource.MetadataRequ
5454
func (d *SiteDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
5555
resp.Schema = schema.Schema{
5656
// This description is used by the documentation generator and the language server.
57-
MarkdownDescription: "This data source fetches sites",
57+
MarkdownDescription: "This data source fetches site based on name hierarchy",
5858

5959
Attributes: map[string]schema.Attribute{
6060
"id": schema.StringAttribute{
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
2+
// All rights reserved.
3+
//
4+
// Licensed under the Mozilla Public License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://mozilla.org/MPL/2.0/
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
// SPDX-License-Identifier: MPL-2.0
17+
18+
package provider
19+
20+
// Section below is generated&owned by "gen/generator.go". //template:begin imports
21+
import (
22+
"context"
23+
"fmt"
24+
25+
"github.com/hashicorp/terraform-plugin-framework/datasource"
26+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
27+
"github.com/hashicorp/terraform-plugin-log/tflog"
28+
cc "github.com/netascode/go-catalystcenter"
29+
)
30+
31+
// End of section. //template:end imports
32+
33+
// Section below is generated&owned by "gen/generator.go". //template:begin model
34+
35+
// Ensure the implementation satisfies the expected interfaces.
36+
var (
37+
_ datasource.DataSource = &SitesDataSource{}
38+
_ datasource.DataSourceWithConfigure = &SitesDataSource{}
39+
)
40+
41+
func NewSitesDataSource() datasource.DataSource {
42+
return &SitesDataSource{}
43+
}
44+
45+
type SitesDataSource struct {
46+
client *cc.Client
47+
}
48+
49+
func (d *SitesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
50+
resp.TypeName = req.ProviderTypeName + "_sites"
51+
}
52+
53+
func (d *SitesDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
54+
resp.Schema = schema.Schema{
55+
// This description is used by the documentation generator and the language server.
56+
MarkdownDescription: "This data source fetches all sites defined on the Catalyst Center.\n\n" +
57+
"Sites represent the logical and physical hierarchy of your network environment, including areas, buildings, and floors.\n" +
58+
"You can optionally specify the `type` attribute to filter the results and retrieve only sites of a specific type—such as `area`, `building` or `floor`.\n" +
59+
"To retrieve detailed information about a specific site, use the data source `data.catalystcenter_site`.",
60+
61+
Attributes: map[string]schema.Attribute{
62+
"type": schema.StringAttribute{
63+
MarkdownDescription: "Site type (eg. area, building, floor)",
64+
Optional: true,
65+
},
66+
"sites": schema.ListNestedAttribute{
67+
MarkdownDescription: "",
68+
Computed: true,
69+
NestedObject: schema.NestedAttributeObject{
70+
Attributes: map[string]schema.Attribute{
71+
"id": schema.StringAttribute{
72+
MarkdownDescription: "",
73+
Computed: true,
74+
},
75+
"name": schema.StringAttribute{
76+
MarkdownDescription: "",
77+
Computed: true,
78+
},
79+
"name_hierarchy": schema.StringAttribute{
80+
MarkdownDescription: "",
81+
Computed: true,
82+
},
83+
"type": schema.StringAttribute{
84+
MarkdownDescription: "",
85+
Computed: true,
86+
},
87+
"parent_id": schema.StringAttribute{
88+
MarkdownDescription: "Parent Id",
89+
Computed: true,
90+
},
91+
"latitude": schema.StringAttribute{
92+
MarkdownDescription: "Building latitude",
93+
Computed: true,
94+
},
95+
"longitude": schema.StringAttribute{
96+
MarkdownDescription: "Building longitude",
97+
Computed: true,
98+
},
99+
"address": schema.StringAttribute{
100+
MarkdownDescription: "Building address",
101+
Computed: true,
102+
},
103+
"country": schema.StringAttribute{
104+
MarkdownDescription: "Country name for the building",
105+
Computed: true,
106+
},
107+
"floor_number": schema.Int64Attribute{
108+
MarkdownDescription: "Floor number",
109+
Computed: true,
110+
},
111+
"rf_model": schema.StringAttribute{
112+
MarkdownDescription: "Floor RF model",
113+
Computed: true,
114+
},
115+
"width": schema.Float64Attribute{
116+
MarkdownDescription: "Floor width",
117+
Computed: true,
118+
},
119+
"length": schema.Float64Attribute{
120+
MarkdownDescription: "Floor length",
121+
Computed: true,
122+
},
123+
"height": schema.Float64Attribute{
124+
MarkdownDescription: "Floor height",
125+
Computed: true,
126+
},
127+
"units_of_measure": schema.StringAttribute{
128+
MarkdownDescription: "Floor unit of measure",
129+
Computed: true,
130+
},
131+
},
132+
},
133+
},
134+
},
135+
}
136+
}
137+
138+
func (d *SitesDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
139+
if req.ProviderData == nil {
140+
return
141+
}
142+
143+
d.client = req.ProviderData.(*CcProviderData).Client
144+
}
145+
146+
// End of section. //template:end model
147+
148+
func (d *SitesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
149+
var config Sites
150+
151+
// Read config
152+
diags := req.Config.Get(ctx, &config)
153+
resp.Diagnostics.Append(diags...)
154+
if resp.Diagnostics.HasError() {
155+
return
156+
}
157+
158+
tflog.Debug(ctx, "singleton: Beginning Read")
159+
params := ""
160+
if !config.Type.IsNull() {
161+
params = "?type=" + config.Type.ValueString()
162+
} else {
163+
params = ""
164+
}
165+
res, err := d.client.Get(config.getPath() + params)
166+
if err != nil {
167+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object, got error: %s", err))
168+
return
169+
}
170+
171+
config.fromBody(ctx, res)
172+
173+
tflog.Debug(ctx, "singleton: Read finished successfully")
174+
175+
diags = resp.State.Set(ctx, &config)
176+
resp.Diagnostics.Append(diags...)
177+
}

0 commit comments

Comments
 (0)